]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/class.c
d19172ac98b5ab0c404b7672bed9310d0e100eee
[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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23
24 /* High-level class interface. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "lex.h"
36
37 #include "obstack.h"
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
40
41 /* This is how we tell when two virtual member functions are really the
42 same. */
43 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44
45 extern void set_class_shadows PARAMS ((tree));
46
47 /* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
49
50 int current_class_depth;
51
52 /* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
55
56 typedef struct class_stack_node {
57 /* The name of the class. */
58 tree name;
59
60 /* The _TYPE node for the class. */
61 tree type;
62
63 /* The access specifier pending for new declarations in the scope of
64 this class. */
65 tree access;
66
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used;
69 }* class_stack_node_t;
70
71 /* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73 static int current_class_stack_size;
74 static class_stack_node_t current_class_stack;
75
76 static tree get_vfield_name PARAMS ((tree));
77 static void finish_struct_anon PARAMS ((tree));
78 static tree build_vbase_pointer PARAMS ((tree, tree));
79 static tree build_vtable_entry PARAMS ((tree, tree, tree));
80 static tree get_vtable_name PARAMS ((tree));
81 static tree get_derived_offset PARAMS ((tree, tree));
82 static tree get_basefndecls PARAMS ((tree, tree));
83 static void set_rtti_entry PARAMS ((tree, tree, tree));
84 static int build_primary_vtable PARAMS ((tree, tree));
85 static int build_secondary_vtable PARAMS ((tree, tree));
86 static tree dfs_finish_vtbls PARAMS ((tree, void *));
87 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
88 static void finish_vtbls PARAMS ((tree));
89 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
90 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
91 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
92 static void delete_duplicate_fields PARAMS ((tree));
93 static void finish_struct_bits PARAMS ((tree));
94 static int alter_access PARAMS ((tree, tree, tree));
95 static void handle_using_decl PARAMS ((tree, tree));
96 static int overrides PARAMS ((tree, tree));
97 static int strictly_overrides PARAMS ((tree, tree));
98 static void mark_overriders PARAMS ((tree, tree));
99 static void check_for_override PARAMS ((tree, tree));
100 static tree dfs_modify_vtables PARAMS ((tree, void *));
101 static tree modify_all_vtables PARAMS ((tree, int *, tree));
102 static void determine_primary_base PARAMS ((tree, int *));
103 static void finish_struct_methods PARAMS ((tree));
104 static void maybe_warn_about_overly_private_class PARAMS ((tree));
105 static int field_decl_cmp PARAMS ((const tree *, const tree *));
106 static int method_name_cmp PARAMS ((const tree *, const tree *));
107 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
108 static tree fixed_type_or_null PARAMS ((tree, int *));
109 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
110 int, tree));
111 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
112 static tree build_vtbl_initializer PARAMS ((tree, tree));
113 static int count_fields PARAMS ((tree));
114 static int add_fields_to_vec PARAMS ((tree, tree, int));
115 static void check_bitfield_decl PARAMS ((tree));
116 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
117 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
118 int *));
119 static int avoid_overlap PARAMS ((tree, tree, int *));
120 static tree build_base_field PARAMS ((tree, tree, int *, int *, unsigned int *));
121 static tree build_base_fields PARAMS ((tree, int *));
122 static tree build_vbase_pointer_fields PARAMS ((tree, int *));
123 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
124 int *));
125 static void check_methods PARAMS ((tree));
126 static void remove_zero_width_bit_fields PARAMS ((tree));
127 static void check_bases PARAMS ((tree, int *, int *, int *));
128 static void check_bases_and_members PARAMS ((tree, int *));
129 static void create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
130 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
131 static void fixup_pending_inline PARAMS ((struct pending_inline *));
132 static void fixup_inline_methods PARAMS ((tree));
133 static void set_primary_base PARAMS ((tree, int, int *));
134 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
135 static void propagate_binfo_offsets PARAMS ((tree, tree));
136 static void layout_basetypes PARAMS ((tree));
137 static void layout_virtual_bases PARAMS ((tree));
138 static void remove_base_field PARAMS ((tree, tree, tree *));
139 static void remove_base_fields PARAMS ((tree));
140 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
141 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
142 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
143 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
144 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
145 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
146 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
147 static tree dfs_count_virtuals PARAMS ((tree, void *));
148 static void start_vtable PARAMS ((tree, int *));
149 static void layout_vtable_decl PARAMS ((tree, int));
150 static int num_vfun_entries PARAMS ((tree));
151 static tree dfs_find_final_overrider PARAMS ((tree, void *));
152 static tree find_final_overrider PARAMS ((tree, tree, tree));
153 static tree dfs_find_base PARAMS ((tree, void *));
154 static int make_new_vtable PARAMS ((tree, tree));
155 extern void dump_class_hierarchy PARAMS ((tree, int));
156 static tree build_vtable PARAMS ((tree, tree, tree));
157 static void initialize_vtable PARAMS ((tree, tree));
158
159 /* Variables shared between class.c and call.c. */
160
161 #ifdef GATHER_STATISTICS
162 int n_vtables = 0;
163 int n_vtable_entries = 0;
164 int n_vtable_searches = 0;
165 int n_vtable_elems = 0;
166 int n_convert_harshness = 0;
167 int n_compute_conversion_costs = 0;
168 int n_build_method_call = 0;
169 int n_inner_fields_searched = 0;
170 #endif
171
172 /* Virtual base class layout. */
173
174 /* Returns a list of virtual base class pointers as a chain of
175 FIELD_DECLS. */
176
177 static tree
178 build_vbase_pointer_fields (rec, empty_p)
179 tree rec;
180 int *empty_p;
181 {
182 /* Chain to hold all the new FIELD_DECLs which point at virtual
183 base classes. */
184 tree vbase_decls = NULL_TREE;
185 tree binfos = TYPE_BINFO_BASETYPES (rec);
186 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
187 tree decl;
188 int i;
189
190 /* Under the new ABI, there are no vbase pointers in the object.
191 Instead, the offsets are stored in the vtable. */
192 if (vbase_offsets_in_vtable_p ())
193 return NULL_TREE;
194
195 /* Loop over the baseclasses, adding vbase pointers as needed. */
196 for (i = 0; i < n_baseclasses; i++)
197 {
198 register tree base_binfo = TREE_VEC_ELT (binfos, i);
199 register tree basetype = BINFO_TYPE (base_binfo);
200
201 if (TYPE_SIZE (basetype) == 0)
202 /* This error is now reported in xref_tag, thus giving better
203 location information. */
204 continue;
205
206 /* All basetypes are recorded in the association list of the
207 derived type. */
208
209 if (TREE_VIA_VIRTUAL (base_binfo))
210 {
211 int j;
212 const char *name;
213
214 /* The offset for a virtual base class is only used in computing
215 virtual function tables and for initializing virtual base
216 pointers. It is built once `get_vbase_types' is called. */
217
218 /* If this basetype can come from another vbase pointer
219 without an additional indirection, we will share
220 that pointer. If an indirection is involved, we
221 make our own pointer. */
222 for (j = 0; j < n_baseclasses; j++)
223 {
224 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
225 if (! TREE_VIA_VIRTUAL (other_base_binfo)
226 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
227 goto got_it;
228 }
229 FORMAT_VBASE_NAME (name, basetype);
230 decl = build_vtbl_or_vbase_field (get_identifier (name),
231 get_identifier (VTABLE_BASE),
232 build_pointer_type (basetype),
233 rec,
234 basetype,
235 empty_p);
236 BINFO_VPTR_FIELD (base_binfo) = decl;
237 TREE_CHAIN (decl) = vbase_decls;
238 vbase_decls = decl;
239 *empty_p = 0;
240
241 got_it:
242 /* The space this decl occupies has already been accounted for. */
243 ;
244 }
245 }
246
247 return vbase_decls;
248 }
249
250 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
251
252 static tree
253 dfs_build_vbase_offset_vtbl_entries (binfo, data)
254 tree binfo;
255 void *data;
256 {
257 tree list = (tree) data;
258
259 if (TREE_TYPE (list) == binfo)
260 /* The TREE_TYPE of LIST is the base class from which we started
261 walking. If that BINFO is virtual it's not a virtual baseclass
262 of itself. */
263 ;
264 else if (TREE_VIA_VIRTUAL (binfo))
265 {
266 tree init;
267 tree vbase;
268
269 /* Remember the index to the vbase offset for this virtual
270 base. */
271 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
272 if (!TREE_VALUE (list))
273 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
274 else
275 {
276 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
277 BINFO_VPTR_FIELD (vbase)
278 = fold (build (MINUS_EXPR, integer_type_node,
279 BINFO_VPTR_FIELD (vbase), integer_one_node));
280 }
281
282 /* And record the offset at which this virtual base lies in the
283 vtable. */
284 init = BINFO_OFFSET (binfo);
285 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
286 init, TREE_VALUE (list));
287 }
288
289 SET_BINFO_VTABLE_PATH_MARKED (binfo);
290
291 return NULL_TREE;
292 }
293
294 /* Returns the initializers for the vbase offset entries in the vtable
295 for BINFO (which is part of the class hierarchy dominated by T), in
296 reverse order. */
297
298 static tree
299 build_vbase_offset_vtbl_entries (binfo, t)
300 tree binfo;
301 tree t;
302 {
303 tree inits;
304 tree init;
305 tree list;
306
307 /* Under the old ABI, pointers to virtual bases are stored in each
308 object. */
309 if (!vbase_offsets_in_vtable_p ())
310 return NULL_TREE;
311
312 /* If there are no virtual baseclasses, then there is nothing to
313 do. */
314 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
315 return NULL_TREE;
316
317 inits = NULL_TREE;
318
319 /* The offsets are allocated in the reverse order of a
320 depth-first left-to-right traversal of the hierarchy. We use
321 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
322 dfs_walk, and so BINFO_MARKED is already in use. */
323 list = build_tree_list (t, NULL_TREE);
324 TREE_TYPE (list) = binfo;
325 dfs_walk (binfo,
326 dfs_build_vbase_offset_vtbl_entries,
327 dfs_vtable_path_unmarked_real_bases_queue_p,
328 list);
329 dfs_walk (binfo,
330 dfs_vtable_path_unmark,
331 dfs_vtable_path_marked_real_bases_queue_p,
332 list);
333 inits = nreverse (TREE_VALUE (list));
334
335 /* We've now got offsets in the right order. However, the offsets
336 we've stored are offsets from the beginning of the complete
337 object, and we need offsets from this BINFO. */
338 for (init = inits; init; init = TREE_CHAIN (init))
339 {
340 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
341 TREE_PURPOSE to scribble in. But, we need to clear it now so
342 that the values are not perceived as labeled initializers. */
343 TREE_PURPOSE (init) = NULL_TREE;
344 TREE_VALUE (init)
345 = fold (build1 (NOP_EXPR, vtable_entry_type,
346 size_diffop (TREE_VALUE (init),
347 BINFO_OFFSET (binfo))));
348 }
349
350 return inits;
351 }
352
353 typedef struct vcall_offset_data_s
354 {
355 /* The binfo for the most-derived type. */
356 tree derived;
357 /* The binfo for the virtual base for which we're building
358 initializers. */
359 tree vbase;
360 /* The vcall offset initializers built up so far. */
361 tree inits;
362 /* The number of vcall offsets accumulated. */
363 int offsets;
364 } vcall_offset_data;
365
366 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
367
368 static tree
369 dfs_vcall_offset_queue_p (binfo, data)
370 tree binfo;
371 void *data;
372 {
373 vcall_offset_data* vod = (vcall_offset_data *) data;
374
375 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
376 }
377
378 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
379
380 static tree
381 dfs_build_vcall_offset_vtbl_entries (binfo, data)
382 tree binfo;
383 void *data;
384 {
385 vcall_offset_data* vod;
386 tree virtuals;
387 tree binfo_inits;
388
389 /* Primary bases are not interesting; all of the virtual
390 function table entries have been overridden. */
391 if (BINFO_PRIMARY_MARKED_P (binfo))
392 return NULL_TREE;
393
394 vod = (vcall_offset_data *) data;
395 binfo_inits = NULL_TREE;
396
397 /* We chain the offsets on in reverse order. That's correct --
398 build_vtbl_initializer will straighten them out. */
399 for (virtuals = skip_rtti_stuff (binfo,
400 BINFO_TYPE (binfo),
401 NULL);
402 virtuals;
403 virtuals = TREE_CHAIN (virtuals))
404 {
405 /* Figure out what function we're looking at. */
406 tree fn = TREE_VALUE (virtuals);
407 tree base = DECL_CONTEXT (fn);
408 /* The FN comes from BASE. So, we must caculate the adjustment
409 from the virtual base that derived from BINFO to BASE. */
410 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
411
412 binfo_inits
413 = tree_cons (NULL_TREE,
414 fold (build1 (NOP_EXPR, vtable_entry_type,
415 size_diffop (BINFO_OFFSET (base_binfo),
416 BINFO_OFFSET (vod->vbase)))),
417 binfo_inits);
418 }
419
420 /* Now add the initializers we've just created to the list that will
421 be returned to our caller. */
422 vod->inits = chainon (vod->inits, binfo_inits);
423
424 return NULL_TREE;
425 }
426
427 /* Returns the initializers for the vcall offset entries in the vtable
428 for BINFO (which is part of the class hierarchy dominated by T), in
429 reverse order. */
430
431 static tree
432 build_vcall_offset_vtbl_entries (binfo, t)
433 tree binfo;
434 tree t;
435 {
436 vcall_offset_data vod;
437
438 /* Under the old ABI, the adjustments to the `this' pointer were made
439 elsewhere. */
440 if (!vcall_offsets_in_vtable_p ())
441 return NULL_TREE;
442
443 /* We only need these entries if this base is a virtual base. */
444 if (!TREE_VIA_VIRTUAL (binfo))
445 return NULL_TREE;
446
447 /* We need a vcall offset for each of the virtual functions in this
448 vtable. For example:
449
450 class A { virtual void f (); };
451 class B : virtual public A { };
452 class C: virtual public A, public B {};
453
454 Now imagine:
455
456 B* b = new C;
457 b->f();
458
459 The location of `A' is not at a fixed offset relative to `B'; the
460 offset depends on the complete object derived from `B'. So,
461 `B' vtable contains an entry for `f' that indicates by what
462 amount the `this' pointer for `B' needs to be adjusted to arrive
463 at `A'.
464
465 We need entries for all the functions in our primary vtable and
466 in our non-virtual bases vtables. For each base, the entries
467 appear in the same order as in the base; but the bases themselves
468 appear in reverse depth-first, left-to-right order. */
469 vod.derived = t;
470 vod.vbase = binfo;
471 vod.inits = NULL_TREE;
472 dfs_walk (binfo,
473 dfs_build_vcall_offset_vtbl_entries,
474 dfs_vcall_offset_queue_p,
475 &vod);
476
477 return vod.inits;
478 }
479
480 /* Returns a pointer to the virtual base class of EXP that has the
481 indicated TYPE. EXP is of class type, not a pointer type. */
482
483 static tree
484 build_vbase_pointer (exp, type)
485 tree exp, type;
486 {
487 if (vbase_offsets_in_vtable_p ())
488 {
489 tree vbase;
490 tree vbase_ptr;
491
492 /* Find the shared copy of TYPE; that's where the vtable offset
493 is recorded. */
494 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
495 /* Find the virtual function table pointer. */
496 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
497 /* Compute the location where the offset will lie. */
498 vbase_ptr = build_binary_op (PLUS_EXPR,
499 vbase_ptr,
500 BINFO_VPTR_FIELD (vbase));
501 vbase_ptr = build1 (NOP_EXPR,
502 build_pointer_type (ptrdiff_type_node),
503 vbase_ptr);
504 /* Add the contents of this location to EXP. */
505 return build (PLUS_EXPR,
506 build_pointer_type (type),
507 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
508 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
509 }
510 else
511 {
512 char *name;
513 FORMAT_VBASE_NAME (name, type);
514 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
515 }
516 }
517
518 /* Build multi-level access to EXPR using hierarchy path PATH.
519 CODE is PLUS_EXPR if we are going with the grain,
520 and MINUS_EXPR if we are not (in which case, we cannot traverse
521 virtual baseclass links).
522
523 TYPE is the type we want this path to have on exit.
524
525 NONNULL is non-zero if we know (for any reason) that EXPR is
526 not, in fact, zero. */
527
528 tree
529 build_vbase_path (code, type, expr, path, nonnull)
530 enum tree_code code;
531 tree type, expr, path;
532 int nonnull;
533 {
534 register int changed = 0;
535 tree last = NULL_TREE, last_virtual = NULL_TREE;
536 int fixed_type_p;
537 tree null_expr = 0, nonnull_expr;
538 tree basetype;
539 tree offset = integer_zero_node;
540
541 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
542 return build1 (NOP_EXPR, type, expr);
543
544 /* We could do better if we had additional logic to convert back to the
545 unconverted type (the static type of the complete object), and then
546 convert back to the type we want. Until that is done, we only optimize
547 if the complete type is the same type as expr has. */
548 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
549
550 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
551 expr = save_expr (expr);
552 nonnull_expr = expr;
553
554 path = reverse_path (path);
555
556 basetype = BINFO_TYPE (path);
557
558 while (path)
559 {
560 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
561 {
562 last_virtual = BINFO_TYPE (TREE_VALUE (path));
563 if (code == PLUS_EXPR)
564 {
565 changed = ! fixed_type_p;
566
567 if (changed)
568 {
569 tree ind;
570
571 /* We already check for ambiguous things in the caller, just
572 find a path. */
573 if (last)
574 {
575 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
576 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
577 }
578 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
579 nonnull_expr = build_vbase_pointer (ind, last_virtual);
580 if (nonnull == 0
581 && TREE_CODE (type) == POINTER_TYPE
582 && null_expr == NULL_TREE)
583 {
584 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
585 expr = build (COND_EXPR, build_pointer_type (last_virtual),
586 build (EQ_EXPR, boolean_type_node, expr,
587 integer_zero_node),
588 null_expr, nonnull_expr);
589 }
590 }
591 /* else we'll figure out the offset below. */
592
593 /* Happens in the case of parse errors. */
594 if (nonnull_expr == error_mark_node)
595 return error_mark_node;
596 }
597 else
598 {
599 cp_error ("cannot cast up from virtual baseclass `%T'",
600 last_virtual);
601 return error_mark_node;
602 }
603 }
604 last = TREE_VALUE (path);
605 path = TREE_CHAIN (path);
606 }
607 /* LAST is now the last basetype assoc on the path. */
608
609 /* A pointer to a virtual base member of a non-null object
610 is non-null. Therefore, we only need to test for zeroness once.
611 Make EXPR the canonical expression to deal with here. */
612 if (null_expr)
613 {
614 TREE_OPERAND (expr, 2) = nonnull_expr;
615 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
616 = TREE_TYPE (nonnull_expr);
617 }
618 else
619 expr = nonnull_expr;
620
621 /* If we go through any virtual base pointers, make sure that
622 casts to BASETYPE from the last virtual base class use
623 the right value for BASETYPE. */
624 if (changed)
625 {
626 tree intype = TREE_TYPE (TREE_TYPE (expr));
627
628 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
629 offset
630 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
631 }
632 else
633 offset = BINFO_OFFSET (last);
634
635 if (TREE_INT_CST_LOW (offset))
636 {
637 /* Bash types to make the backend happy. */
638 offset = cp_convert (type, offset);
639
640 /* If expr might be 0, we need to preserve that zeroness. */
641 if (nonnull == 0)
642 {
643 if (null_expr)
644 TREE_TYPE (null_expr) = type;
645 else
646 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
647 if (TREE_SIDE_EFFECTS (expr))
648 expr = save_expr (expr);
649
650 return build (COND_EXPR, type,
651 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
652 null_expr,
653 build (code, type, expr, offset));
654 }
655 else return build (code, type, expr, offset);
656 }
657
658 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
659 be used multiple times in initialization of multiple inheritance. */
660 if (null_expr)
661 {
662 TREE_TYPE (expr) = type;
663 return expr;
664 }
665 else
666 return build1 (NOP_EXPR, type, expr);
667 }
668
669 \f
670 /* Virtual function things. */
671
672 /* Build an entry in the virtual function table. DELTA is the offset
673 for the `this' pointer. VCALL_INDEX is the vtable index containing
674 the vcall offset; zero if none. ENTRY is the virtual function
675 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
676 but it may not actually be a virtual function table pointer. (For
677 example, it might be the address of the RTTI object, under the new
678 ABI.) */
679
680 static tree
681 build_vtable_entry (delta, vcall_index, entry)
682 tree delta;
683 tree vcall_index;
684 tree entry;
685 {
686 if (flag_vtable_thunks)
687 {
688 HOST_WIDE_INT idelta;
689 HOST_WIDE_INT ivindex;
690
691 idelta = TREE_INT_CST_LOW (delta);
692 ivindex = TREE_INT_CST_LOW (vcall_index);
693 if ((idelta || ivindex)
694 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
695 {
696 entry = make_thunk (entry, idelta, ivindex);
697 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
698 TREE_READONLY (entry) = 1;
699 TREE_CONSTANT (entry) = 1;
700 }
701 #ifdef GATHER_STATISTICS
702 n_vtable_entries += 1;
703 #endif
704 return entry;
705 }
706 else
707 {
708 extern int flag_huge_objects;
709 tree elems = tree_cons (NULL_TREE, delta,
710 tree_cons (NULL_TREE, integer_zero_node,
711 build_tree_list (NULL_TREE, entry)));
712 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
713
714 /* We don't use vcall offsets when not using vtable thunks. */
715 my_friendly_assert (integer_zerop (vcall_index), 20000125);
716
717 /* DELTA used to be constructed by `size_int' and/or size_binop,
718 which caused overflow problems when it was negative. That should
719 be fixed now. */
720
721 if (! int_fits_type_p (delta, delta_type_node))
722 {
723 if (flag_huge_objects)
724 sorry ("object size exceeds built-in limit for virtual function table implementation");
725 else
726 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
727 }
728
729 TREE_CONSTANT (entry) = 1;
730 TREE_STATIC (entry) = 1;
731 TREE_READONLY (entry) = 1;
732
733 #ifdef GATHER_STATISTICS
734 n_vtable_entries += 1;
735 #endif
736
737 return entry;
738 }
739 }
740
741 /* We want to give the assembler the vtable identifier as well as
742 the offset to the function pointer. So we generate
743
744 __asm__ __volatile__ (".vtable_entry %c0, %c1"
745 : : "s"(&class_vtable),
746 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
747
748 static void
749 build_vtable_entry_ref (basetype, vtbl, idx)
750 tree basetype, vtbl, idx;
751 {
752 static char asm_stmt[] = ".vtable_entry %c0, %c1";
753 tree s, i, i2;
754
755 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
756 s = build_tree_list (build_string (1, "s"), s);
757
758 i = build_array_ref (vtbl, idx);
759 if (!flag_vtable_thunks)
760 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
761 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
762 i2 = build_array_ref (vtbl, build_int_2(0,0));
763 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
764 i = build_binary_op (MINUS_EXPR, i, i2);
765 i = build_tree_list (build_string (1, "i"), i);
766
767 finish_asm_stmt (ridpointers[RID_VOLATILE],
768 build_string (sizeof(asm_stmt)-1, asm_stmt),
769 NULL_TREE, chainon (s, i), NULL_TREE);
770 }
771
772 /* Given an object INSTANCE, return an expression which yields the
773 virtual function vtable element corresponding to INDEX. There are
774 many special cases for INSTANCE which we take care of here, mainly
775 to avoid creating extra tree nodes when we don't have to. */
776
777 tree
778 build_vtbl_ref (instance, idx)
779 tree instance, idx;
780 {
781 tree vtbl, aref;
782 tree basetype = TREE_TYPE (instance);
783
784 if (TREE_CODE (basetype) == REFERENCE_TYPE)
785 basetype = TREE_TYPE (basetype);
786
787 if (instance == current_class_ref)
788 vtbl = build_vfield_ref (instance, basetype);
789 else
790 {
791 if (optimize)
792 {
793 /* Try to figure out what a reference refers to, and
794 access its virtual function table directly. */
795 tree ref = NULL_TREE;
796
797 if (TREE_CODE (instance) == INDIRECT_REF
798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
799 ref = TREE_OPERAND (instance, 0);
800 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
801 ref = instance;
802
803 if (ref && TREE_CODE (ref) == VAR_DECL
804 && DECL_INITIAL (ref))
805 {
806 tree init = DECL_INITIAL (ref);
807
808 while (TREE_CODE (init) == NOP_EXPR
809 || TREE_CODE (init) == NON_LVALUE_EXPR)
810 init = TREE_OPERAND (init, 0);
811 if (TREE_CODE (init) == ADDR_EXPR)
812 {
813 init = TREE_OPERAND (init, 0);
814 if (IS_AGGR_TYPE (TREE_TYPE (init))
815 && (TREE_CODE (init) == PARM_DECL
816 || TREE_CODE (init) == VAR_DECL))
817 instance = init;
818 }
819 }
820 }
821
822 if (IS_AGGR_TYPE (TREE_TYPE (instance))
823 && (TREE_CODE (instance) == RESULT_DECL
824 || TREE_CODE (instance) == PARM_DECL
825 || TREE_CODE (instance) == VAR_DECL))
826 vtbl = TYPE_BINFO_VTABLE (basetype);
827 else
828 vtbl = build_vfield_ref (instance, basetype);
829 }
830
831 assemble_external (vtbl);
832
833 if (flag_vtable_gc)
834 build_vtable_entry_ref (basetype, vtbl, idx);
835
836 aref = build_array_ref (vtbl, idx);
837
838 return aref;
839 }
840
841 /* Given an object INSTANCE, return an expression which yields the
842 virtual function corresponding to INDEX. There are many special
843 cases for INSTANCE which we take care of here, mainly to avoid
844 creating extra tree nodes when we don't have to. */
845
846 tree
847 build_vfn_ref (ptr_to_instptr, instance, idx)
848 tree *ptr_to_instptr, instance;
849 tree idx;
850 {
851 tree aref = build_vtbl_ref (instance, idx);
852
853 /* When using thunks, there is no extra delta, and we get the pfn
854 directly. */
855 if (flag_vtable_thunks)
856 return aref;
857
858 if (ptr_to_instptr)
859 {
860 /* Save the intermediate result in a SAVE_EXPR so we don't have to
861 compute each component of the virtual function pointer twice. */
862 if (TREE_CODE (aref) == INDIRECT_REF)
863 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
864
865 *ptr_to_instptr
866 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
867 *ptr_to_instptr,
868 cp_convert (ptrdiff_type_node,
869 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
870 }
871
872 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
873 }
874
875 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
876 for the given TYPE. */
877
878 static tree
879 get_vtable_name (type)
880 tree type;
881 {
882 tree type_id = build_typename_overload (type);
883 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
884 + IDENTIFIER_LENGTH (type_id) + 2);
885 const char *ptr = IDENTIFIER_POINTER (type_id);
886 int i;
887 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
888 #if 0
889 /* We don't take off the numbers; build_secondary_vtable uses the
890 DECL_ASSEMBLER_NAME for the type, which includes the number
891 in `3foo'. If we were to pull them off here, we'd end up with
892 something like `_vt.foo.3bar', instead of a uniform definition. */
893 while (ptr[i] >= '0' && ptr[i] <= '9')
894 i += 1;
895 #endif
896 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
897 return get_identifier (buf);
898 }
899
900 /* Return the offset to the main vtable for a given base BINFO. */
901
902 tree
903 get_vfield_offset (binfo)
904 tree binfo;
905 {
906 tree tmp
907 = size_binop (FLOOR_DIV_EXPR,
908 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
909 bitsize_int (BITS_PER_UNIT));
910
911 return size_binop (PLUS_EXPR, convert (sizetype, tmp),
912 BINFO_OFFSET (binfo));
913 }
914
915 /* Get the offset to the start of the original binfo that we derived
916 this binfo from. If we find TYPE first, return the offset only
917 that far. The shortened search is useful because the this pointer
918 on method calling is expected to point to a DECL_CONTEXT (fndecl)
919 object, and not a baseclass of it. */
920
921
922 static tree
923 get_derived_offset (binfo, type)
924 tree binfo, type;
925 {
926 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
927 tree offset2;
928 int i;
929
930 while (BINFO_BASETYPES (binfo)
931 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
932 {
933 tree binfos = BINFO_BASETYPES (binfo);
934 if (BINFO_TYPE (binfo) == type)
935 break;
936 binfo = TREE_VEC_ELT (binfos, i);
937 }
938
939 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
940 return size_binop (MINUS_EXPR, offset1, offset2);
941 }
942
943 /* Update the rtti info for this class. */
944
945 static void
946 set_rtti_entry (virtuals, offset, type)
947 tree virtuals, offset, type;
948 {
949 tree decl;
950
951 if (CLASSTYPE_COM_INTERFACE (type))
952 return;
953
954 if (flag_rtti)
955 decl = get_tinfo_decl (type);
956 else if (!new_abi_rtti_p ())
957 /* If someone tries to get RTTI information for a type compiled
958 without RTTI, they're out of luck. By calling __pure_virtual
959 in this case, we give a small clue as to what went wrong. We
960 could consider having a __no_typeinfo function as well, for a
961 more specific hint. */
962 decl = abort_fndecl;
963 else
964 /* For the new-abi, we just point to the type_info object. */
965 decl = NULL_TREE;
966
967 if (flag_vtable_thunks)
968 {
969 /* The first slot holds the offset. */
970 BV_DELTA (virtuals) = offset;
971 BV_VCALL_INDEX (virtuals) = integer_zero_node;
972
973 /* The next node holds the decl. */
974 virtuals = TREE_CHAIN (virtuals);
975 offset = integer_zero_node;
976 }
977
978 /* This slot holds the function to call. */
979 BV_DELTA (virtuals) = offset;
980 BV_VCALL_INDEX (virtuals) = integer_zero_node;
981 BV_FN (virtuals) = decl;
982 }
983
984 /* Create a VAR_DECL for a primary or secondary vtable for
985 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
986 for its type. */
987
988 static tree
989 build_vtable (class_type, name, vtable_type)
990 tree class_type;
991 tree name;
992 tree vtable_type;
993 {
994 tree decl;
995
996 decl = build_lang_decl (VAR_DECL, name, vtable_type);
997 DECL_CONTEXT (decl) = class_type;
998 DECL_ARTIFICIAL (decl) = 1;
999 TREE_STATIC (decl) = 1;
1000 #ifndef WRITABLE_VTABLES
1001 /* Make them READONLY by default. (mrs) */
1002 TREE_READONLY (decl) = 1;
1003 #endif
1004 DECL_VIRTUAL_P (decl) = 1;
1005 import_export_vtable (decl, class_type, 0);
1006
1007 return decl;
1008 }
1009
1010 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1011 or even complete. If this does not exist, create it. If COMPLETE is
1012 non-zero, then complete the definition of it -- that will render it
1013 impossible to actually build the vtable, but is useful to get at those
1014 which are known to exist in the runtime. */
1015
1016 tree
1017 get_vtable_decl (type, complete)
1018 tree type;
1019 int complete;
1020 {
1021 tree name = get_vtable_name (type);
1022 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1023
1024 if (decl)
1025 {
1026 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1027 && DECL_VIRTUAL_P (decl), 20000118);
1028 return decl;
1029 }
1030
1031 decl = build_vtable (type, name, void_type_node);
1032 decl = pushdecl_top_level (decl);
1033 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1034
1035 /* At one time the vtable info was grabbed 2 words at a time. This
1036 fails on sparc unless you have 8-byte alignment. (tiemann) */
1037 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1038 DECL_ALIGN (decl));
1039
1040 if (complete)
1041 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1042
1043 return decl;
1044 }
1045
1046 /* Build the primary virtual function table for TYPE. If BINFO is
1047 non-NULL, build the vtable starting with the initial approximation
1048 that it is the same as the one which is the head of the association
1049 list. Returns a non-zero value if a new vtable is actually
1050 created. */
1051
1052 static int
1053 build_primary_vtable (binfo, type)
1054 tree binfo, type;
1055 {
1056 tree virtuals, decl;
1057
1058 decl = get_vtable_decl (type, /*complete=*/0);
1059
1060 if (binfo)
1061 {
1062 tree offset;
1063
1064 if (BINFO_NEW_VTABLE_MARKED (binfo))
1065 /* We have already created a vtable for this base, so there's
1066 no need to do it again. */
1067 return 0;
1068
1069 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1070 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1071 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1072 DECL_SIZE_UNIT (decl)
1073 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo)));
1074
1075 /* Now do rtti stuff. */
1076 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
1077 offset = size_diffop (size_zero_node, offset);
1078 set_rtti_entry (virtuals, offset, type);
1079 }
1080 else
1081 {
1082 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1083 20000118);
1084 virtuals = NULL_TREE;
1085 }
1086
1087 #ifdef GATHER_STATISTICS
1088 n_vtables += 1;
1089 n_vtable_elems += list_length (virtuals);
1090 #endif
1091
1092 /* Initialize the association list for this type, based
1093 on our first approximation. */
1094 TYPE_BINFO_VTABLE (type) = decl;
1095 TYPE_BINFO_VIRTUALS (type) = virtuals;
1096
1097 binfo = TYPE_BINFO (type);
1098 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1099 return 1;
1100 }
1101
1102 /* Give TYPE a new virtual function table which is initialized
1103 with a skeleton-copy of its original initialization. The only
1104 entry that changes is the `delta' entry, so we can really
1105 share a lot of structure.
1106
1107 FOR_TYPE is the derived type which caused this table to
1108 be needed.
1109
1110 BINFO is the type association which provided TYPE for FOR_TYPE.
1111
1112 The order in which vtables are built (by calling this function) for
1113 an object must remain the same, otherwise a binary incompatibility
1114 can result. */
1115
1116 static int
1117 build_secondary_vtable (binfo, for_type)
1118 tree binfo, for_type;
1119 {
1120 tree basetype;
1121 tree orig_decl = BINFO_VTABLE (binfo);
1122 tree name;
1123 tree new_decl;
1124 tree offset;
1125 tree path = binfo;
1126 char *buf, *buf2;
1127 char joiner = '_';
1128 int i;
1129
1130 #ifdef JOINER
1131 joiner = JOINER;
1132 #endif
1133
1134 if (TREE_VIA_VIRTUAL (binfo))
1135 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1136 current_class_type),
1137 170);
1138
1139 if (BINFO_NEW_VTABLE_MARKED (binfo))
1140 /* We already created a vtable for this base. There's no need to
1141 do it again. */
1142 return 0;
1143
1144 /* Remember that we've created a vtable for this BINFO, so that we
1145 don't try to do so again. */
1146 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1147
1148 /* Make fresh virtual list, so we can smash it later. */
1149 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1150
1151 if (TREE_VIA_VIRTUAL (binfo))
1152 {
1153 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1154
1155 /* XXX - This should never happen, if it does, the caller should
1156 ensure that the binfo is from for_type's binfos, not from any
1157 base type's. We can remove all this code after a while. */
1158 if (binfo1 != binfo)
1159 warning ("internal inconsistency: binfo offset error for rtti");
1160
1161 offset = BINFO_OFFSET (binfo1);
1162 }
1163 else
1164 offset = BINFO_OFFSET (binfo);
1165
1166 set_rtti_entry (BINFO_VIRTUALS (binfo),
1167 size_diffop (size_zero_node, offset),
1168 for_type);
1169
1170 /* In the new ABI, secondary vtables are laid out as part of the
1171 same structure as the primary vtable. */
1172 if (merge_primary_and_secondary_vtables_p ())
1173 {
1174 BINFO_VTABLE (binfo) = NULL_TREE;
1175 return 1;
1176 }
1177
1178 /* Create the declaration for the secondary vtable. */
1179 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1180 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1181 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1182
1183 /* We know that the vtable that we are going to create doesn't exist
1184 yet in the global namespace, and when we finish, it will be
1185 pushed into the global namespace. In complex MI hierarchies, we
1186 have to loop while the name we are thinking of adding is globally
1187 defined, adding more name components to the vtable name as we
1188 loop, until the name is unique. This is because in complex MI
1189 cases, we might have the same base more than once. This means
1190 that the order in which this function is called for vtables must
1191 remain the same, otherwise binary compatibility can be
1192 compromised. */
1193
1194 while (1)
1195 {
1196 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1197 + 1 + i);
1198 char *new_buf2;
1199
1200 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1201 buf2);
1202 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1203 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1204 name = get_identifier (buf);
1205
1206 /* If this name doesn't clash, then we can use it, otherwise
1207 we add more to the name until it is unique. */
1208
1209 if (! IDENTIFIER_GLOBAL_VALUE (name))
1210 break;
1211
1212 /* Set values for next loop through, if the name isn't unique. */
1213
1214 path = BINFO_INHERITANCE_CHAIN (path);
1215
1216 /* We better not run out of stuff to make it unique. */
1217 my_friendly_assert (path != NULL_TREE, 368);
1218
1219 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1220
1221 if (for_type == basetype)
1222 {
1223 /* If we run out of basetypes in the path, we have already
1224 found created a vtable with that name before, we now
1225 resort to tacking on _%d to distinguish them. */
1226 int j = 2;
1227 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1228 buf1 = (char *) alloca (i);
1229 do {
1230 sprintf (buf1, "%s%c%s%c%d",
1231 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1232 buf2, joiner, j);
1233 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1234 + strlen (buf1) + 1);
1235 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1236 name = get_identifier (buf);
1237
1238 /* If this name doesn't clash, then we can use it,
1239 otherwise we add something different to the name until
1240 it is unique. */
1241 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1242
1243 /* Hey, they really like MI don't they? Increase the 3
1244 above to 6, and the 999 to 999999. :-) */
1245 my_friendly_assert (j <= 999, 369);
1246
1247 break;
1248 }
1249
1250 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1251 new_buf2 = (char *) alloca (i);
1252 sprintf (new_buf2, "%s%c%s",
1253 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1254 buf2 = new_buf2;
1255 }
1256
1257 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
1258 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1259 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1260
1261 #ifdef GATHER_STATISTICS
1262 n_vtables += 1;
1263 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1264 #endif
1265
1266 return 1;
1267 }
1268
1269 /* Create a new vtable for BINFO which is the hierarchy dominated by
1270 T. */
1271
1272 static int
1273 make_new_vtable (t, binfo)
1274 tree t;
1275 tree binfo;
1276 {
1277 if (binfo == TYPE_BINFO (t))
1278 /* In this case, it is *type*'s vtable we are modifying. We start
1279 with the approximation that it's vtable is that of the
1280 immediate base class. */
1281 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1282 t);
1283 else
1284 /* This is our very own copy of `basetype' to play with. Later,
1285 we will fill in all the virtual functions that override the
1286 virtual functions in these base classes which are not defined
1287 by the current type. */
1288 return build_secondary_vtable (binfo, t);
1289 }
1290
1291 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1292 (which is in the hierarchy dominated by T) list FNDECL as its
1293 BV_FN. DELTA is the required adjustment from the `this' pointer
1294 where the vtable entry appears to the `this' required when the
1295 function is actually called. */
1296
1297 static void
1298 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1299 tree t;
1300 tree binfo;
1301 tree fndecl;
1302 tree delta;
1303 tree *virtuals;
1304 {
1305 tree vcall_index;
1306 tree v;
1307
1308 v = *virtuals;
1309 vcall_index = integer_zero_node;
1310
1311 if (fndecl != BV_FN (v)
1312 || !tree_int_cst_equal (delta, BV_DELTA (v))
1313 || !tree_int_cst_equal (vcall_index, BV_VCALL_INDEX (v)))
1314 {
1315 tree base_fndecl;
1316
1317 /* We need a new vtable for BINFO. */
1318 if (make_new_vtable (t, binfo))
1319 {
1320 /* If we really did make a new vtable, we also made a copy
1321 of the BINFO_VIRTUALS list. Now, we have to find the
1322 corresponding entry in that list. */
1323 *virtuals = BINFO_VIRTUALS (binfo);
1324 while (BV_FN (*virtuals) != BV_FN (v))
1325 *virtuals = TREE_CHAIN (*virtuals);
1326 v = *virtuals;
1327 }
1328
1329 base_fndecl = BV_FN (v);
1330 BV_DELTA (v) = delta;
1331 BV_VCALL_INDEX (v) = vcall_index;
1332 BV_FN (v) = fndecl;
1333
1334 /* Now assign virtual dispatch information, if unset. We can
1335 dispatch this, through any overridden base function. */
1336 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1337 {
1338 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1339 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1340 }
1341 }
1342 }
1343
1344 /* Call this function whenever its known that a vtable for T is going
1345 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1346 is initialized to the number of slots that are reserved at the
1347 beginning of the vtable for RTTI information. */
1348
1349 static void
1350 start_vtable (t, has_virtual_p)
1351 tree t;
1352 int *has_virtual_p;
1353 {
1354 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1355 {
1356 /* If we are using thunks, use two slots at the front, one
1357 for the offset pointer, one for the tdesc pointer.
1358 For ARM-style vtables, use the same slot for both. */
1359 if (flag_vtable_thunks)
1360 *has_virtual_p = 2;
1361 else
1362 *has_virtual_p = 1;
1363 }
1364 }
1365
1366 /* Add a virtual function to all the appropriate vtables for the class
1367 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1368 allocate a new slot in our table. If it is error_mark_node, we
1369 know that no other function from another vtable is overridden by X.
1370 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1371 vtable for the type, and we build upon the NEW_VIRTUALS list
1372 and return it. */
1373
1374 static void
1375 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1376 has_virtual, fndecl, t)
1377 tree *new_virtuals_p;
1378 tree *overridden_virtuals_p;
1379 int *has_virtual;
1380 tree fndecl;
1381 tree t; /* Structure type. */
1382 {
1383 tree new_virtual;
1384
1385 /* If this function doesn't override anything from a base class, we
1386 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1387 override something, we keep it around and assign its DECL_VINDEX
1388 later, in modify_all_vtables. */
1389 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1390 /* We've already dealt with this function. */
1391 return;
1392
1393 new_virtual = build_tree_list (integer_zero_node, fndecl);
1394 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1395
1396 if (DECL_VINDEX (fndecl) == error_mark_node)
1397 {
1398 /* FNDECL is a new virtual function; it doesn't override any
1399 virtual function in a base class. */
1400
1401 /* We remember that this was the base sub-object for rtti. */
1402 CLASSTYPE_RTTI (t) = t;
1403
1404 start_vtable (t, has_virtual);
1405
1406 /* Now assign virtual dispatch information. */
1407 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1408 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1409
1410 /* Save the state we've computed on the NEW_VIRTUALS list. */
1411 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1412 *new_virtuals_p = new_virtual;
1413 }
1414 else
1415 {
1416 /* FNDECL overrides a function from a base class. */
1417 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1418 *overridden_virtuals_p = new_virtual;
1419 }
1420 }
1421 \f
1422 extern struct obstack *current_obstack;
1423
1424 /* Add method METHOD to class TYPE.
1425
1426 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1427 the class type where the method should be added. */
1428
1429 void
1430 add_method (type, fields, method)
1431 tree type, *fields, method;
1432 {
1433 int using = (DECL_CONTEXT (method) != type);
1434
1435 if (fields && *fields)
1436 *fields = build_overload (method, *fields);
1437 else
1438 {
1439 int len;
1440 int slot;
1441 tree method_vec;
1442
1443 if (!CLASSTYPE_METHOD_VEC (type))
1444 /* Make a new method vector. We start with 8 entries. We must
1445 allocate at least two (for constructors and destructors), and
1446 we're going to end up with an assignment operator at some
1447 point as well.
1448
1449 We could use a TREE_LIST for now, and convert it to a
1450 TREE_VEC in finish_struct, but we would probably waste more
1451 memory making the links in the list than we would by
1452 over-allocating the size of the vector here. Furthermore,
1453 we would complicate all the code that expects this to be a
1454 vector. */
1455 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1456
1457 method_vec = CLASSTYPE_METHOD_VEC (type);
1458 len = TREE_VEC_LENGTH (method_vec);
1459
1460 if (DECL_NAME (method) == constructor_name (type))
1461 /* A new constructor or destructor. Constructors go in
1462 slot 0; destructors go in slot 1. */
1463 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1464 else
1465 {
1466 /* See if we already have an entry with this name. */
1467 for (slot = 2; slot < len; ++slot)
1468 if (!TREE_VEC_ELT (method_vec, slot)
1469 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1470 slot)))
1471 == DECL_NAME (method)))
1472 break;
1473
1474 if (slot == len)
1475 {
1476 /* We need a bigger method vector. */
1477 tree new_vec = make_tree_vec (2 * len);
1478 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1479 (PTR) &TREE_VEC_ELT (new_vec, 0),
1480 len * sizeof (tree));
1481 len = 2 * len;
1482 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1483 }
1484
1485 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1486 {
1487 /* Type conversion operators have to come before
1488 ordinary methods; add_conversions depends on this to
1489 speed up looking for conversion operators. So, if
1490 necessary, we slide some of the vector elements up.
1491 In theory, this makes this algorithm O(N^2) but we
1492 don't expect many conversion operators. */
1493 for (slot = 2; slot < len; ++slot)
1494 {
1495 tree fn = TREE_VEC_ELT (method_vec, slot);
1496
1497 if (!fn)
1498 /* There are no more entries in the vector, so we
1499 can insert the new conversion operator here. */
1500 break;
1501
1502 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1503 /* We can insert the new function right at the
1504 SLOTth position. */
1505 break;
1506 }
1507
1508 if (!TREE_VEC_ELT (method_vec, slot))
1509 /* There is nothing in the Ith slot, so we can avoid
1510 moving anything. */
1511 ;
1512 else
1513 {
1514 /* We know the last slot in the vector is empty
1515 because we know that at this point there's room
1516 for a new function. */
1517 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1518 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1519 (len - slot - 1) * sizeof (tree));
1520 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1521 }
1522 }
1523 }
1524
1525 if (template_class_depth (type))
1526 /* TYPE is a template class. Don't issue any errors now; wait
1527 until instantiation time to complain. */
1528 ;
1529 else
1530 {
1531 tree fns;
1532
1533 /* Check to see if we've already got this method. */
1534 for (fns = TREE_VEC_ELT (method_vec, slot);
1535 fns;
1536 fns = OVL_NEXT (fns))
1537 {
1538 tree fn = OVL_CURRENT (fns);
1539
1540 if (TREE_CODE (fn) != TREE_CODE (method))
1541 continue;
1542
1543 if (TREE_CODE (method) != TEMPLATE_DECL)
1544 {
1545 /* [over.load] Member function declarations with the
1546 same name and the same parameter types cannot be
1547 overloaded if any of them is a static member
1548 function declaration. */
1549 if ((DECL_STATIC_FUNCTION_P (fn)
1550 != DECL_STATIC_FUNCTION_P (method))
1551 || using)
1552 {
1553 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1554 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1555
1556 if (! DECL_STATIC_FUNCTION_P (fn))
1557 parms1 = TREE_CHAIN (parms1);
1558 if (! DECL_STATIC_FUNCTION_P (method))
1559 parms2 = TREE_CHAIN (parms2);
1560
1561 if (compparms (parms1, parms2))
1562 {
1563 if (using)
1564 /* Defer to the local function. */
1565 return;
1566 else
1567 cp_error ("`%#D' and `%#D' cannot be overloaded",
1568 fn, method);
1569 }
1570 }
1571
1572 /* Since this is an ordinary function in a
1573 non-template class, it's mangled name can be used
1574 as a unique identifier. This technique is only
1575 an optimization; we would get the same results if
1576 we just used decls_match here. */
1577 if (DECL_ASSEMBLER_NAME (fn)
1578 != DECL_ASSEMBLER_NAME (method))
1579 continue;
1580 }
1581 else if (!decls_match (fn, method))
1582 continue;
1583
1584 /* There has already been a declaration of this method
1585 or member template. */
1586 cp_error_at ("`%D' has already been declared in `%T'",
1587 method, type);
1588
1589 /* We don't call duplicate_decls here to merge the
1590 declarations because that will confuse things if the
1591 methods have inline definitions. In particular, we
1592 will crash while processing the definitions. */
1593 return;
1594 }
1595 }
1596
1597 /* Actually insert the new method. */
1598 TREE_VEC_ELT (method_vec, slot)
1599 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1600
1601 /* Add the new binding. */
1602 if (!DECL_CONSTRUCTOR_P (method)
1603 && !DECL_DESTRUCTOR_P (method))
1604 push_class_level_binding (DECL_NAME (method),
1605 TREE_VEC_ELT (method_vec, slot));
1606 }
1607 }
1608
1609 /* Subroutines of finish_struct. */
1610
1611 /* Look through the list of fields for this struct, deleting
1612 duplicates as we go. This must be recursive to handle
1613 anonymous unions.
1614
1615 FIELD is the field which may not appear anywhere in FIELDS.
1616 FIELD_PTR, if non-null, is the starting point at which
1617 chained deletions may take place.
1618 The value returned is the first acceptable entry found
1619 in FIELDS.
1620
1621 Note that anonymous fields which are not of UNION_TYPE are
1622 not duplicates, they are just anonymous fields. This happens
1623 when we have unnamed bitfields, for example. */
1624
1625 static tree
1626 delete_duplicate_fields_1 (field, fields)
1627 tree field, fields;
1628 {
1629 tree x;
1630 tree prev = 0;
1631 if (DECL_NAME (field) == 0)
1632 {
1633 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1634 return fields;
1635
1636 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1637 fields = delete_duplicate_fields_1 (x, fields);
1638 return fields;
1639 }
1640 else
1641 {
1642 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1643 {
1644 if (DECL_NAME (x) == 0)
1645 {
1646 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1647 continue;
1648 TYPE_FIELDS (TREE_TYPE (x))
1649 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1650 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1651 {
1652 if (prev == 0)
1653 fields = TREE_CHAIN (fields);
1654 else
1655 TREE_CHAIN (prev) = TREE_CHAIN (x);
1656 }
1657 }
1658 else if (TREE_CODE (field) == USING_DECL)
1659 /* A using declaration may is allowed to appear more than
1660 once. We'll prune these from the field list later, and
1661 handle_using_decl will complain about invalid multiple
1662 uses. */
1663 ;
1664 else if (DECL_NAME (field) == DECL_NAME (x))
1665 {
1666 if (TREE_CODE (field) == CONST_DECL
1667 && TREE_CODE (x) == CONST_DECL)
1668 cp_error_at ("duplicate enum value `%D'", x);
1669 else if (TREE_CODE (field) == CONST_DECL
1670 || TREE_CODE (x) == CONST_DECL)
1671 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1672 x);
1673 else if (DECL_DECLARES_TYPE_P (field)
1674 && DECL_DECLARES_TYPE_P (x))
1675 {
1676 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1677 continue;
1678 cp_error_at ("duplicate nested type `%D'", x);
1679 }
1680 else if (DECL_DECLARES_TYPE_P (field)
1681 || DECL_DECLARES_TYPE_P (x))
1682 {
1683 /* Hide tag decls. */
1684 if ((TREE_CODE (field) == TYPE_DECL
1685 && DECL_ARTIFICIAL (field))
1686 || (TREE_CODE (x) == TYPE_DECL
1687 && DECL_ARTIFICIAL (x)))
1688 continue;
1689 cp_error_at ("duplicate field `%D' (as type and non-type)",
1690 x);
1691 }
1692 else
1693 cp_error_at ("duplicate member `%D'", x);
1694 if (prev == 0)
1695 fields = TREE_CHAIN (fields);
1696 else
1697 TREE_CHAIN (prev) = TREE_CHAIN (x);
1698 }
1699 }
1700 }
1701 return fields;
1702 }
1703
1704 static void
1705 delete_duplicate_fields (fields)
1706 tree fields;
1707 {
1708 tree x;
1709 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1710 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1711 }
1712
1713 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1714 legit, otherwise return 0. */
1715
1716 static int
1717 alter_access (t, fdecl, access)
1718 tree t;
1719 tree fdecl;
1720 tree access;
1721 {
1722 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1723 if (elem)
1724 {
1725 if (TREE_VALUE (elem) != access)
1726 {
1727 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1728 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1729 else
1730 error ("conflicting access specifications for field `%s', ignored",
1731 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1732 }
1733 else
1734 {
1735 /* They're changing the access to the same thing they changed
1736 it to before. That's OK. */
1737 ;
1738 }
1739 }
1740 else
1741 {
1742 enforce_access (t, fdecl);
1743 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1744 return 1;
1745 }
1746 return 0;
1747 }
1748
1749 /* Process the USING_DECL, which is a member of T. */
1750
1751 static void
1752 handle_using_decl (using_decl, t)
1753 tree using_decl;
1754 tree t;
1755 {
1756 tree ctype = DECL_INITIAL (using_decl);
1757 tree name = DECL_NAME (using_decl);
1758 tree access
1759 = TREE_PRIVATE (using_decl) ? access_private_node
1760 : TREE_PROTECTED (using_decl) ? access_protected_node
1761 : access_public_node;
1762 tree fdecl, binfo;
1763 tree flist = NULL_TREE;
1764 tree old_value;
1765
1766 binfo = binfo_or_else (ctype, t);
1767 if (! binfo)
1768 return;
1769
1770 if (name == constructor_name (ctype)
1771 || name == constructor_name_full (ctype))
1772 {
1773 cp_error_at ("using-declaration for constructor", using_decl);
1774 return;
1775 }
1776
1777 fdecl = lookup_member (binfo, name, 0, 0);
1778
1779 if (!fdecl)
1780 {
1781 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1782 return;
1783 }
1784
1785 if (BASELINK_P (fdecl))
1786 /* Ignore base type this came from. */
1787 fdecl = TREE_VALUE (fdecl);
1788
1789 old_value = IDENTIFIER_CLASS_VALUE (name);
1790 if (old_value)
1791 {
1792 if (is_overloaded_fn (old_value))
1793 old_value = OVL_CURRENT (old_value);
1794
1795 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1796 /* OK */;
1797 else
1798 old_value = NULL_TREE;
1799 }
1800
1801 if (is_overloaded_fn (fdecl))
1802 flist = fdecl;
1803 else if (! DECL_LANG_SPECIFIC (fdecl))
1804 my_friendly_abort (20000221);
1805
1806 if (! old_value)
1807 ;
1808 else if (is_overloaded_fn (old_value))
1809 {
1810 if (flist)
1811 /* It's OK to use functions from a base when there are functions with
1812 the same name already present in the current class. */;
1813 else
1814 {
1815 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1816 cp_error_at (" because of local method `%#D' with same name",
1817 OVL_CURRENT (old_value));
1818 return;
1819 }
1820 }
1821 else
1822 {
1823 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1824 cp_error_at (" because of local field `%#D' with same name", old_value);
1825 return;
1826 }
1827
1828 /* Make type T see field decl FDECL with access ACCESS.*/
1829 if (flist)
1830 for (; flist; flist = OVL_NEXT (flist))
1831 {
1832 add_method (t, 0, OVL_CURRENT (flist));
1833 alter_access (t, OVL_CURRENT (flist), access);
1834 }
1835 else
1836 alter_access (t, fdecl, access);
1837 }
1838 \f
1839 /* Run through the base clases of T, updating
1840 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1841 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1842 the bases. */
1843
1844 static void
1845 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1846 no_const_asn_ref_p)
1847 tree t;
1848 int *cant_have_default_ctor_p;
1849 int *cant_have_const_ctor_p;
1850 int *no_const_asn_ref_p;
1851 {
1852 int n_baseclasses;
1853 int i;
1854 int seen_nearly_empty_base_p;
1855 tree binfos;
1856
1857 binfos = TYPE_BINFO_BASETYPES (t);
1858 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1859 seen_nearly_empty_base_p = 0;
1860
1861 /* An aggregate cannot have baseclasses. */
1862 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1863
1864 for (i = 0; i < n_baseclasses; ++i)
1865 {
1866 tree base_binfo;
1867 tree basetype;
1868
1869 /* Figure out what base we're looking at. */
1870 base_binfo = TREE_VEC_ELT (binfos, i);
1871 basetype = TREE_TYPE (base_binfo);
1872
1873 /* If the type of basetype is incomplete, then we already
1874 complained about that fact (and we should have fixed it up as
1875 well). */
1876 if (TYPE_SIZE (basetype) == 0)
1877 {
1878 int j;
1879 /* The base type is of incomplete type. It is
1880 probably best to pretend that it does not
1881 exist. */
1882 if (i == n_baseclasses-1)
1883 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1884 TREE_VEC_LENGTH (binfos) -= 1;
1885 n_baseclasses -= 1;
1886 for (j = i; j+1 < n_baseclasses; j++)
1887 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1888 continue;
1889 }
1890
1891 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1892 here because the case of virtual functions but non-virtual
1893 dtor is handled in finish_struct_1. */
1894 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1895 && TYPE_HAS_DESTRUCTOR (basetype))
1896 cp_warning ("base class `%#T' has a non-virtual destructor",
1897 basetype);
1898
1899 /* If the base class doesn't have copy constructors or
1900 assignment operators that take const references, then the
1901 derived class cannot have such a member automatically
1902 generated. */
1903 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1904 *cant_have_const_ctor_p = 1;
1905 if (TYPE_HAS_ASSIGN_REF (basetype)
1906 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1907 *no_const_asn_ref_p = 1;
1908 /* Similarly, if the base class doesn't have a default
1909 constructor, then the derived class won't have an
1910 automatically generated default constructor. */
1911 if (TYPE_HAS_CONSTRUCTOR (basetype)
1912 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1913 {
1914 *cant_have_default_ctor_p = 1;
1915 if (! TYPE_HAS_CONSTRUCTOR (t))
1916 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1917 basetype);
1918 }
1919
1920 /* If the base class is not empty or nearly empty, then this
1921 class cannot be nearly empty. */
1922 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1923 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1924 /* And if there is more than one nearly empty base, then the
1925 derived class is not nearly empty either. */
1926 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1927 && seen_nearly_empty_base_p)
1928 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1929 /* If this is the first nearly empty base class, then remember
1930 that we saw it. */
1931 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1932 seen_nearly_empty_base_p = 1;
1933
1934 /* A lot of properties from the bases also apply to the derived
1935 class. */
1936 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1937 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1938 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1939 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1940 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1941 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1942 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1943 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1944 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1945 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1946
1947 /* Derived classes can implicitly become COMified if their bases
1948 are COM. */
1949 if (CLASSTYPE_COM_INTERFACE (basetype))
1950 CLASSTYPE_COM_INTERFACE (t) = 1;
1951 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1952 {
1953 cp_error
1954 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1955 t, basetype);
1956 CLASSTYPE_COM_INTERFACE (t) = 0;
1957 }
1958 }
1959 }
1960
1961 /* Make the Ith baseclass of T its primary base. */
1962
1963 static void
1964 set_primary_base (t, i, has_virtual_p)
1965 tree t;
1966 int i;
1967 int *has_virtual_p;
1968 {
1969 tree basetype;
1970
1971 CLASSTYPE_VFIELD_PARENT (t) = i;
1972 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1973 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1974 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1975 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1976 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1977 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1978 }
1979
1980 /* Determine the primary class for T. */
1981
1982 static void
1983 determine_primary_base (t, has_virtual_p)
1984 tree t;
1985 int *has_virtual_p;
1986 {
1987 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1988
1989 /* If there are no baseclasses, there is certainly no primary base. */
1990 if (n_baseclasses == 0)
1991 return;
1992
1993 *has_virtual_p = 0;
1994
1995 for (i = 0; i < n_baseclasses; i++)
1996 {
1997 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1998 tree basetype = BINFO_TYPE (base_binfo);
1999
2000 if (TYPE_CONTAINS_VPTR_P (basetype))
2001 {
2002 /* Even a virtual baseclass can contain our RTTI
2003 information. But, we prefer a non-virtual polymorphic
2004 baseclass. */
2005 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2006 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
2007
2008 /* A virtual baseclass can't be the primary base under the
2009 old ABI. And under the new ABI we still prefer a
2010 non-virtual base. */
2011 if (TREE_VIA_VIRTUAL (base_binfo))
2012 continue;
2013
2014 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2015 {
2016 set_primary_base (t, i, has_virtual_p);
2017 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2018 }
2019 else
2020 {
2021 tree vfields;
2022
2023 /* Only add unique vfields, and flatten them out as we go. */
2024 for (vfields = CLASSTYPE_VFIELDS (basetype);
2025 vfields;
2026 vfields = TREE_CHAIN (vfields))
2027 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2028 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2029 CLASSTYPE_VFIELDS (t)
2030 = tree_cons (base_binfo,
2031 VF_BASETYPE_VALUE (vfields),
2032 CLASSTYPE_VFIELDS (t));
2033
2034 if (*has_virtual_p == 0)
2035 set_primary_base (t, i, has_virtual_p);
2036 }
2037 }
2038 }
2039
2040 if (!TYPE_VFIELD (t))
2041 CLASSTYPE_VFIELD_PARENT (t) = -1;
2042
2043 /* The new ABI allows for the use of a "nearly-empty" virtual base
2044 class as the primary base class if no non-virtual polymorphic
2045 base can be found. */
2046 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2047 for (i = 0; i < n_baseclasses; ++i)
2048 {
2049 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2050 tree basetype = BINFO_TYPE (base_binfo);
2051
2052 if (TREE_VIA_VIRTUAL (base_binfo)
2053 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2054 {
2055 set_primary_base (t, i, has_virtual_p);
2056 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2057 break;
2058 }
2059 }
2060
2061 /* Mark the primary base classes at this point. */
2062 mark_primary_bases (t);
2063 }
2064 \f
2065 /* Set memoizing fields and bits of T (and its variants) for later
2066 use. */
2067
2068 static void
2069 finish_struct_bits (t)
2070 tree t;
2071 {
2072 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2073
2074 /* Fix up variants (if any). */
2075 tree variants = TYPE_NEXT_VARIANT (t);
2076 while (variants)
2077 {
2078 /* These fields are in the _TYPE part of the node, not in
2079 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2080 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2081 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2082 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2083 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2084 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2085
2086 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2087 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2088 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2089 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2090 /* Copy whatever these are holding today. */
2091 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2092 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2093 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2094 TYPE_SIZE (variants) = TYPE_SIZE (t);
2095 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2096 variants = TYPE_NEXT_VARIANT (variants);
2097 }
2098
2099 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2100 /* For a class w/o baseclasses, `finish_struct' has set
2101 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2102 definition). Similarly for a class whose base classes do not
2103 have vtables. When neither of these is true, we might have
2104 removed abstract virtuals (by providing a definition), added
2105 some (by declaring new ones), or redeclared ones from a base
2106 class. We need to recalculate what's really an abstract virtual
2107 at this point (by looking in the vtables). */
2108 get_pure_virtuals (t);
2109
2110 if (n_baseclasses)
2111 {
2112 /* Notice whether this class has type conversion functions defined. */
2113 tree binfo = TYPE_BINFO (t);
2114 tree binfos = BINFO_BASETYPES (binfo);
2115 tree basetype;
2116
2117 for (i = n_baseclasses-1; i >= 0; i--)
2118 {
2119 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2120
2121 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2122 }
2123 }
2124
2125 /* If this type has a copy constructor, force its mode to be BLKmode, and
2126 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2127 be passed by invisible reference and prevent it from being returned in
2128 a register.
2129
2130 Also do this if the class has BLKmode but can still be returned in
2131 registers, since function_cannot_inline_p won't let us inline
2132 functions returning such a type. This affects the HP-PA. */
2133 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2134 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2135 && CLASSTYPE_NON_AGGREGATE (t)))
2136 {
2137 tree variants;
2138 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2139 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2140 {
2141 TYPE_MODE (variants) = BLKmode;
2142 TREE_ADDRESSABLE (variants) = 1;
2143 }
2144 }
2145 }
2146
2147 /* Issue warnings about T having private constructors, but no friends,
2148 and so forth.
2149
2150 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2151 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2152 non-private static member functions. */
2153
2154 static void
2155 maybe_warn_about_overly_private_class (t)
2156 tree t;
2157 {
2158 int has_member_fn = 0;
2159 int has_nonprivate_method = 0;
2160 tree fn;
2161
2162 if (!warn_ctor_dtor_privacy
2163 /* If the class has friends, those entities might create and
2164 access instances, so we should not warn. */
2165 || (CLASSTYPE_FRIEND_CLASSES (t)
2166 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2167 /* We will have warned when the template was declared; there's
2168 no need to warn on every instantiation. */
2169 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2170 /* There's no reason to even consider warning about this
2171 class. */
2172 return;
2173
2174 /* We only issue one warning, if more than one applies, because
2175 otherwise, on code like:
2176
2177 class A {
2178 // Oops - forgot `public:'
2179 A();
2180 A(const A&);
2181 ~A();
2182 };
2183
2184 we warn several times about essentially the same problem. */
2185
2186 /* Check to see if all (non-constructor, non-destructor) member
2187 functions are private. (Since there are no friends or
2188 non-private statics, we can't ever call any of the private member
2189 functions.) */
2190 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2191 /* We're not interested in compiler-generated methods; they don't
2192 provide any way to call private members. */
2193 if (!DECL_ARTIFICIAL (fn))
2194 {
2195 if (!TREE_PRIVATE (fn))
2196 {
2197 if (DECL_STATIC_FUNCTION_P (fn))
2198 /* A non-private static member function is just like a
2199 friend; it can create and invoke private member
2200 functions, and be accessed without a class
2201 instance. */
2202 return;
2203
2204 has_nonprivate_method = 1;
2205 break;
2206 }
2207 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2208 has_member_fn = 1;
2209 }
2210
2211 if (!has_nonprivate_method && has_member_fn)
2212 {
2213 /* There are no non-private methods, and there's at least one
2214 private member function that isn't a constructor or
2215 destructor. (If all the private members are
2216 constructors/destructors we want to use the code below that
2217 issues error messages specifically referring to
2218 constructors/destructors.) */
2219 int i;
2220 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2221 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2222 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2223 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2224 {
2225 has_nonprivate_method = 1;
2226 break;
2227 }
2228 if (!has_nonprivate_method)
2229 {
2230 cp_warning ("all member functions in class `%T' are private", t);
2231 return;
2232 }
2233 }
2234
2235 /* Even if some of the member functions are non-private, the class
2236 won't be useful for much if all the constructors or destructors
2237 are private: such an object can never be created or destroyed. */
2238 if (TYPE_HAS_DESTRUCTOR (t))
2239 {
2240 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2241
2242 if (TREE_PRIVATE (dtor))
2243 {
2244 cp_warning ("`%#T' only defines a private destructor and has no friends",
2245 t);
2246 return;
2247 }
2248 }
2249
2250 if (TYPE_HAS_CONSTRUCTOR (t))
2251 {
2252 int nonprivate_ctor = 0;
2253
2254 /* If a non-template class does not define a copy
2255 constructor, one is defined for it, enabling it to avoid
2256 this warning. For a template class, this does not
2257 happen, and so we would normally get a warning on:
2258
2259 template <class T> class C { private: C(); };
2260
2261 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2262 complete non-template or fully instantiated classes have this
2263 flag set. */
2264 if (!TYPE_HAS_INIT_REF (t))
2265 nonprivate_ctor = 1;
2266 else
2267 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2268 fn;
2269 fn = OVL_NEXT (fn))
2270 {
2271 tree ctor = OVL_CURRENT (fn);
2272 /* Ideally, we wouldn't count copy constructors (or, in
2273 fact, any constructor that takes an argument of the
2274 class type as a parameter) because such things cannot
2275 be used to construct an instance of the class unless
2276 you already have one. But, for now at least, we're
2277 more generous. */
2278 if (! TREE_PRIVATE (ctor))
2279 {
2280 nonprivate_ctor = 1;
2281 break;
2282 }
2283 }
2284
2285 if (nonprivate_ctor == 0)
2286 {
2287 cp_warning ("`%#T' only defines private constructors and has no friends",
2288 t);
2289 return;
2290 }
2291 }
2292 }
2293
2294 /* Function to help qsort sort FIELD_DECLs by name order. */
2295
2296 static int
2297 field_decl_cmp (x, y)
2298 const tree *x, *y;
2299 {
2300 if (DECL_NAME (*x) == DECL_NAME (*y))
2301 /* A nontype is "greater" than a type. */
2302 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2303 if (DECL_NAME (*x) == NULL_TREE)
2304 return -1;
2305 if (DECL_NAME (*y) == NULL_TREE)
2306 return 1;
2307 if (DECL_NAME (*x) < DECL_NAME (*y))
2308 return -1;
2309 return 1;
2310 }
2311
2312 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2313
2314 static int
2315 method_name_cmp (m1, m2)
2316 const tree *m1, *m2;
2317 {
2318 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2319 return 0;
2320 if (*m1 == NULL_TREE)
2321 return -1;
2322 if (*m2 == NULL_TREE)
2323 return 1;
2324 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2325 return -1;
2326 return 1;
2327 }
2328
2329 /* Warn about duplicate methods in fn_fields. Also compact method
2330 lists so that lookup can be made faster.
2331
2332 Data Structure: List of method lists. The outer list is a
2333 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2334 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2335 links the entire list of methods for TYPE_METHODS. Friends are
2336 chained in the same way as member functions (? TREE_CHAIN or
2337 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2338 list. That allows them to be quickly deleted, and requires no
2339 extra storage.
2340
2341 If there are any constructors/destructors, they are moved to the
2342 front of the list. This makes pushclass more efficient.
2343
2344 @@ The above comment is obsolete. It mostly describes what add_method
2345 @@ and add_implicitly_declared_members do.
2346
2347 Sort methods that are not special (i.e., constructors, destructors, and
2348 type conversion operators) so that we can find them faster in search. */
2349
2350 static void
2351 finish_struct_methods (t)
2352 tree t;
2353 {
2354 tree fn_fields;
2355 tree method_vec;
2356 tree ctor_name = constructor_name (t);
2357 int slot, len;
2358
2359 if (!TYPE_METHODS (t))
2360 {
2361 /* Clear these for safety; perhaps some parsing error could set
2362 these incorrectly. */
2363 TYPE_HAS_CONSTRUCTOR (t) = 0;
2364 TYPE_HAS_DESTRUCTOR (t) = 0;
2365 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2366 return;
2367 }
2368
2369 method_vec = CLASSTYPE_METHOD_VEC (t);
2370 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2371 len = TREE_VEC_LENGTH (method_vec);
2372
2373 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2374 and the next few with type conversion operators (if any). */
2375 for (fn_fields = TYPE_METHODS (t); fn_fields;
2376 fn_fields = TREE_CHAIN (fn_fields))
2377 {
2378 tree fn_name = DECL_NAME (fn_fields);
2379
2380 /* Clear out this flag.
2381
2382 @@ Doug may figure out how to break
2383 @@ this with nested classes and friends. */
2384 DECL_IN_AGGR_P (fn_fields) = 0;
2385
2386 /* Note here that a copy ctor is private, so we don't dare generate
2387 a default copy constructor for a class that has a member
2388 of this type without making sure they have access to it. */
2389 if (fn_name == ctor_name)
2390 {
2391 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2392 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2393
2394 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2395 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2396 {
2397 if (TREE_CHAIN (parmtypes) == NULL_TREE
2398 || TREE_CHAIN (parmtypes) == void_list_node
2399 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2400 {
2401 if (TREE_PROTECTED (fn_fields))
2402 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2403 else if (TREE_PRIVATE (fn_fields))
2404 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2405 }
2406 }
2407 }
2408 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2409 {
2410 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2411
2412 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2413 {
2414 if (TREE_PROTECTED (fn_fields))
2415 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2416 else if (TREE_PRIVATE (fn_fields))
2417 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2418 }
2419 }
2420 }
2421
2422 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2423 /* We thought there was a destructor, but there wasn't. Some
2424 parse errors cause this anomalous situation. */
2425 TYPE_HAS_DESTRUCTOR (t) = 0;
2426
2427 /* Issue warnings about private constructors and such. If there are
2428 no methods, then some public defaults are generated. */
2429 maybe_warn_about_overly_private_class (t);
2430
2431 /* Now sort the methods. */
2432 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2433 len--;
2434 TREE_VEC_LENGTH (method_vec) = len;
2435
2436 /* The type conversion ops have to live at the front of the vec, so we
2437 can't sort them. */
2438 for (slot = 2; slot < len; ++slot)
2439 {
2440 tree fn = TREE_VEC_ELT (method_vec, slot);
2441
2442 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2443 break;
2444 }
2445 if (len - slot > 1)
2446 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2447 (int (*)(const void *, const void *))method_name_cmp);
2448 }
2449
2450 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2451
2452 void
2453 duplicate_tag_error (t)
2454 tree t;
2455 {
2456 cp_error ("redefinition of `%#T'", t);
2457 cp_error_at ("previous definition here", t);
2458
2459 /* Pretend we haven't defined this type. */
2460
2461 /* All of the component_decl's were TREE_CHAINed together in the parser.
2462 finish_struct_methods walks these chains and assembles all methods with
2463 the same base name into DECL_CHAINs. Now we don't need the parser chains
2464 anymore, so we unravel them. */
2465
2466 /* This used to be in finish_struct, but it turns out that the
2467 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2468 things... */
2469 if (CLASSTYPE_METHOD_VEC (t))
2470 {
2471 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2472 int i, len = TREE_VEC_LENGTH (method_vec);
2473 for (i = 0; i < len; i++)
2474 {
2475 tree unchain = TREE_VEC_ELT (method_vec, i);
2476 while (unchain != NULL_TREE)
2477 {
2478 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2479 unchain = OVL_NEXT (unchain);
2480 }
2481 }
2482 }
2483
2484 if (TYPE_LANG_SPECIFIC (t))
2485 {
2486 tree binfo = TYPE_BINFO (t);
2487 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2488 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2489 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2490 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2491
2492 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2493 BINFO_BASETYPES(binfo) = NULL_TREE;
2494
2495 TYPE_BINFO (t) = binfo;
2496 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2497 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2498 TYPE_REDEFINED (t) = 1;
2499 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2500 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2501 }
2502 TYPE_SIZE (t) = NULL_TREE;
2503 TYPE_MODE (t) = VOIDmode;
2504 TYPE_FIELDS (t) = NULL_TREE;
2505 TYPE_METHODS (t) = NULL_TREE;
2506 TYPE_VFIELD (t) = NULL_TREE;
2507 TYPE_CONTEXT (t) = NULL_TREE;
2508 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2509 }
2510
2511 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2512 vbase and vcall offsets, etc. Set its type and call the backend
2513 to lay it out. */
2514
2515 static void
2516 layout_vtable_decl (binfo, n)
2517 tree binfo;
2518 int n;
2519 {
2520 tree itype;
2521 tree atype;
2522
2523 itype = size_int (n);
2524 atype = build_cplus_array_type (vtable_entry_type,
2525 build_index_type (itype));
2526 layout_type (atype);
2527
2528 /* We may have to grow the vtable. */
2529 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2530 {
2531 tree vtable = BINFO_VTABLE (binfo);
2532
2533 TREE_TYPE (vtable) = atype;
2534 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = 0;
2535 layout_decl (vtable, 0);
2536
2537 /* At one time the vtable info was grabbed 2 words at a time. This
2538 fails on Sparc unless you have 8-byte alignment. */
2539 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2540 DECL_ALIGN (vtable));
2541 }
2542 }
2543
2544 /* Returns the number of virtual function table entries (excluding
2545 RTTI information, vbase and vcall offests, etc.) in the vtable for
2546 BINFO. */
2547
2548 static int
2549 num_vfun_entries (binfo)
2550 tree binfo;
2551 {
2552 return list_length (skip_rtti_stuff (binfo,
2553 BINFO_TYPE (binfo),
2554 NULL));
2555 }
2556
2557 /* Called from num_extra_vtbl_entries via dfs_walk. */
2558
2559 static tree
2560 dfs_count_virtuals (binfo, data)
2561 tree binfo;
2562 void *data;
2563 {
2564 /* Non-primary bases are not interesting; all of the virtual
2565 function table entries have been overridden. */
2566 if (!BINFO_PRIMARY_MARKED_P (binfo))
2567 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2568
2569 return NULL_TREE;
2570 }
2571
2572 /* Returns the number of extra entries (at negative indices) required
2573 for BINFO's vtable. */
2574
2575 tree
2576 num_extra_vtbl_entries (binfo)
2577 tree binfo;
2578 {
2579 tree type;
2580 int entries;
2581
2582 type = BINFO_TYPE (binfo);
2583 entries = 0;
2584
2585 /* There is an entry for the offset to each virtual base. */
2586 if (vbase_offsets_in_vtable_p ())
2587 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2588
2589 /* If this is a virtual base, there are entries for each virtual
2590 function defined in this class or its bases. */
2591 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2592 {
2593 vcall_offset_data vod;
2594
2595 vod.vbase = binfo;
2596 vod.offsets = 0;
2597 dfs_walk (binfo,
2598 dfs_count_virtuals,
2599 dfs_vcall_offset_queue_p,
2600 &vod);
2601 entries += vod.offsets;
2602 }
2603
2604 return entries ? size_int (entries) : size_zero_node;
2605 }
2606
2607 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2608 where the vptr should actually point. */
2609
2610 tree
2611 size_extra_vtbl_entries (binfo)
2612 tree binfo;
2613 {
2614 tree offset = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (vtable_entry_type),
2615 num_extra_vtbl_entries (binfo));
2616 return fold (offset);
2617 }
2618
2619 /* Construct the initializer for BINFOs virtual function table. BINFO
2620 is part of the hierarchy dominated by T. The value returned is a
2621 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2622 DECL_INITIAL for a vtable. */
2623
2624 static tree
2625 build_vtbl_initializer (binfo, t)
2626 tree binfo;
2627 tree t;
2628 {
2629 tree v = BINFO_VIRTUALS (binfo);
2630 tree inits = NULL_TREE;
2631 tree type = BINFO_TYPE (binfo);
2632
2633 /* Add entries to the vtable that indicate how to adjust the this
2634 pointer when calling a virtual function in this class. */
2635 inits = build_vcall_offset_vtbl_entries (binfo, t);
2636
2637 /* Add entries to the vtable for offsets to our virtual bases. */
2638 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2639 inits);
2640
2641 /* Process the RTTI stuff at the head of the list. If we're not
2642 using vtable thunks, then the RTTI entry is just an ordinary
2643 function, and we can process it just like the other virtual
2644 function entries. */
2645 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2646 {
2647 tree offset;
2648 tree init;
2649
2650 /* The first entry is an offset. */
2651 offset = TREE_PURPOSE (v);
2652 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2653 19990727);
2654
2655 /* Convert the offset to look like a function pointer, so that
2656 we can put it in the vtable. */
2657 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2658 TREE_CONSTANT (init) = 1;
2659 inits = tree_cons (NULL_TREE, init, inits);
2660
2661 v = TREE_CHAIN (v);
2662
2663 if (new_abi_rtti_p ())
2664 {
2665 tree decl = TREE_VALUE (v);
2666
2667 if (decl)
2668 decl = build_unary_op (ADDR_EXPR, decl, 0);
2669 else
2670 decl = integer_zero_node;
2671 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2672 TREE_CONSTANT (decl) = 1;
2673 decl = build_vtable_entry (integer_zero_node, integer_zero_node,
2674 decl);
2675 inits = tree_cons (NULL_TREE, decl, inits);
2676
2677 v = TREE_CHAIN (v);
2678 }
2679 /* In the old abi the second entry (the tdesc pointer) is
2680 just an ordinary function, so it can be dealt with like the
2681 virtual functions. */
2682 }
2683
2684 /* Go through all the ordinary virtual functions, building up
2685 initializers. */
2686 while (v)
2687 {
2688 tree delta;
2689 tree vcall_index;
2690 tree fn;
2691 tree pfn;
2692 tree init;
2693
2694 /* Pull the offset for `this', and the function to call, out of
2695 the list. */
2696 delta = BV_DELTA (v);
2697 vcall_index = BV_VCALL_INDEX (v);
2698 fn = BV_FN (v);
2699 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2700 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2701
2702 /* You can't call an abstract virtual function; it's abstract.
2703 So, we replace these functions with __pure_virtual. */
2704 if (DECL_PURE_VIRTUAL_P (fn))
2705 fn = abort_fndecl;
2706
2707 /* Take the address of the function, considering it to be of an
2708 appropriate generic type. */
2709 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
2710 /* The address of a function can't change. */
2711 TREE_CONSTANT (pfn) = 1;
2712 /* Enter it in the vtable. */
2713 init = build_vtable_entry (delta, vcall_index, pfn);
2714 /* And add it to the chain of initializers. */
2715 inits = tree_cons (NULL_TREE, init, inits);
2716
2717 /* Keep going. */
2718 v = TREE_CHAIN (v);
2719 }
2720
2721 /* The initializers were built up in reverse order; straighten them
2722 out now. */
2723 return nreverse (inits);
2724 }
2725
2726 /* Initialize the vtable for BINFO with the INITS. */
2727
2728 static void
2729 initialize_vtable (binfo, inits)
2730 tree binfo;
2731 tree inits;
2732 {
2733 tree context;
2734 tree decl;
2735
2736 layout_vtable_decl (binfo, list_length (inits));
2737 decl = BINFO_VTABLE (binfo);
2738 context = DECL_CONTEXT (decl);
2739 DECL_CONTEXT (decl) = 0;
2740 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
2741 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2742 DECL_CONTEXT (decl) = context;
2743 }
2744
2745 /* Called from finish_vtbls via dfs_walk. */
2746
2747 static tree
2748 dfs_finish_vtbls (binfo, data)
2749 tree binfo;
2750 void *data;
2751 {
2752 if (!BINFO_PRIMARY_MARKED_P (binfo)
2753 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2754 && BINFO_NEW_VTABLE_MARKED (binfo))
2755 initialize_vtable (binfo,
2756 build_vtbl_initializer (binfo, (tree) data));
2757
2758 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2759 SET_BINFO_MARKED (binfo);
2760
2761 return NULL_TREE;
2762 }
2763
2764 /* Called from finish_vtbls via dfs_walk when using the new ABI.
2765 Accumulates the vtable initializers for all of the vtables into
2766 TREE_VALUE (DATA). */
2767
2768 static tree
2769 dfs_accumulate_vtbl_inits (binfo, data)
2770 tree binfo;
2771 void *data;
2772 {
2773 if (!BINFO_PRIMARY_MARKED_P (binfo)
2774 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2775 && BINFO_NEW_VTABLE_MARKED (binfo))
2776 {
2777 tree l;
2778 tree t;
2779
2780 l = (tree) data;
2781 t = TREE_PURPOSE (l);
2782
2783 /* If this is a secondary vtable, record its location. */
2784 if (binfo != TYPE_BINFO (t))
2785 {
2786 tree vtbl;
2787
2788 vtbl = TYPE_BINFO_VTABLE (t);
2789 vtbl = build1 (ADDR_EXPR,
2790 build_pointer_type (TREE_TYPE (vtbl)),
2791 vtbl);
2792 BINFO_VTABLE (binfo)
2793 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2794 size_binop (MULT_EXPR,
2795 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
2796 size_int (list_length (TREE_VALUE (l)))));
2797 }
2798
2799 /* Add the initializers for this vtable to the initailizers for
2800 the other vtables we've already got. */
2801 TREE_VALUE (l)
2802 = chainon (TREE_VALUE (l),
2803 build_vtbl_initializer (binfo, t));
2804 }
2805
2806 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2807 SET_BINFO_MARKED (binfo);
2808
2809 return NULL_TREE;
2810 }
2811
2812 /* Create all the necessary vtables for T and its base classes. */
2813
2814 static void
2815 finish_vtbls (t)
2816 tree t;
2817 {
2818 if (merge_primary_and_secondary_vtables_p ())
2819 {
2820 tree list;
2821
2822 /* Under the new ABI, we lay out the primary and secondary
2823 vtables in one contiguous vtable. The primary vtable is
2824 first, followed by the secondary vtables as encountered in a
2825 pre-order depth-first left-to-right traversal. */
2826 list = build_tree_list (t, NULL_TREE);
2827 dfs_walk_real (TYPE_BINFO (t),
2828 dfs_accumulate_vtbl_inits,
2829 NULL,
2830 dfs_unmarked_real_bases_queue_p,
2831 list);
2832 if (TYPE_BINFO_VTABLE (t))
2833 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
2834 }
2835 else
2836 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2837 dfs_unmarked_real_bases_queue_p, t);
2838
2839 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2840 dfs_marked_real_bases_queue_p, t);
2841 }
2842
2843 /* True if we should override the given BASE_FNDECL with the given
2844 FNDECL. */
2845
2846 static int
2847 overrides (fndecl, base_fndecl)
2848 tree fndecl, base_fndecl;
2849 {
2850 /* Destructors have special names. */
2851 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2852 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2853 return 1;
2854 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2855 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2856 return 0;
2857 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2858 {
2859 tree types, base_types;
2860 #if 0
2861 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2862 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2863 #endif
2864 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2865 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2866 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2867 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2868 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2869 return 1;
2870 }
2871 return 0;
2872 }
2873
2874 typedef struct find_final_overrider_data_s {
2875 /* The function for which we are trying to find a final overrider. */
2876 tree fn;
2877 /* The base class in which the function was declared. */
2878 tree declaring_base;
2879 /* The most derived class in the hierarchy. */
2880 tree most_derived_type;
2881 /* The final overriding function. */
2882 tree overriding_fn;
2883 /* The BINFO for the class in which the final overriding function
2884 appears. */
2885 tree overriding_base;
2886 } find_final_overrider_data;
2887
2888 /* Called from find_final_overrider via dfs_walk. */
2889
2890 static tree
2891 dfs_find_final_overrider (binfo, data)
2892 tree binfo;
2893 void *data;
2894 {
2895 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2896
2897 if (same_type_p (BINFO_TYPE (binfo),
2898 BINFO_TYPE (ffod->declaring_base))
2899 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2900 BINFO_OFFSET (ffod->declaring_base)))
2901 {
2902 tree path;
2903 tree method;
2904
2905 /* We've found a path to the declaring base. Walk down the path
2906 looking for an overrider for FN. */
2907 for (path = reverse_path (binfo);
2908 path;
2909 path = TREE_CHAIN (path))
2910 {
2911 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2912 method;
2913 method = TREE_CHAIN (method))
2914 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2915 break;
2916
2917 if (method)
2918 break;
2919 }
2920
2921 /* If we found an overrider, record the overriding function, and
2922 the base from which it came. */
2923 if (path)
2924 {
2925 if (ffod->overriding_fn && ffod->overriding_fn != method)
2926 {
2927 /* We've found a different overrider along a different
2928 path. That can be OK if the new one overrides the
2929 old one. Consider:
2930
2931 struct S { virtual void f(); };
2932 struct T : public virtual S { virtual void f(); };
2933 struct U : public virtual S, public virtual T {};
2934
2935 Here `T::f' is the final overrider for `S::f'. */
2936 if (strictly_overrides (method, ffod->overriding_fn))
2937 {
2938 ffod->overriding_fn = method;
2939 ffod->overriding_base = TREE_VALUE (path);
2940 }
2941 else if (!strictly_overrides (ffod->overriding_fn, method))
2942 {
2943 cp_error ("no unique final overrider for `%D' in `%T'",
2944 ffod->most_derived_type,
2945 ffod->fn);
2946 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2947 cp_error (" `%#D'", method);
2948 return error_mark_node;
2949 }
2950 }
2951 else if (ffod->overriding_base
2952 && (!tree_int_cst_equal
2953 (BINFO_OFFSET (TREE_VALUE (path)),
2954 BINFO_OFFSET (ffod->overriding_base))))
2955 {
2956 /* We've found two instances of the same base that
2957 provide overriders. */
2958 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2959 ffod->fn,
2960 BINFO_TYPE (ffod->overriding_base),
2961 ffod->most_derived_type);
2962 return error_mark_node;
2963 }
2964 else
2965 {
2966 ffod->overriding_fn = method;
2967 ffod->overriding_base = TREE_VALUE (path);
2968 }
2969 }
2970 }
2971
2972 return NULL_TREE;
2973 }
2974
2975 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2976 FN and whose TREE_VALUE is the binfo for the base where the
2977 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2978 base object in which FN is declared. */
2979
2980 static tree
2981 find_final_overrider (t, binfo, fn)
2982 tree t;
2983 tree binfo;
2984 tree fn;
2985 {
2986 find_final_overrider_data ffod;
2987
2988 /* Getting this right is a little tricky. This is legal:
2989
2990 struct S { virtual void f (); };
2991 struct T { virtual void f (); };
2992 struct U : public S, public T { };
2993
2994 even though calling `f' in `U' is ambiguous. But,
2995
2996 struct R { virtual void f(); };
2997 struct S : virtual public R { virtual void f (); };
2998 struct T : virtual public R { virtual void f (); };
2999 struct U : public S, public T { };
3000
3001 is not -- there's no way to decide whether to put `S::f' or
3002 `T::f' in the vtable for `R'.
3003
3004 The solution is to look at all paths to BINFO. If we find
3005 different overriders along any two, then there is a problem. */
3006 ffod.fn = fn;
3007 ffod.declaring_base = binfo;
3008 ffod.most_derived_type = t;
3009 ffod.overriding_fn = NULL_TREE;
3010 ffod.overriding_base = NULL_TREE;
3011
3012 if (dfs_walk (TYPE_BINFO (t),
3013 dfs_find_final_overrider,
3014 NULL,
3015 &ffod))
3016 return error_mark_node;
3017
3018 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
3019 }
3020
3021 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
3022 the front. If non-NULL, N is set to the number of entries
3023 skipped. */
3024
3025 tree
3026 skip_rtti_stuff (binfo, t, n)
3027 tree binfo;
3028 tree t;
3029 HOST_WIDE_INT *n;
3030 {
3031 tree virtuals;
3032
3033 if (CLASSTYPE_COM_INTERFACE (t))
3034 return 0;
3035
3036 if (n)
3037 *n = 0;
3038 virtuals = BINFO_VIRTUALS (binfo);
3039 if (virtuals)
3040 {
3041 /* We always reserve a slot for the offset/tdesc entry. */
3042 if (n)
3043 ++*n;
3044 virtuals = TREE_CHAIN (virtuals);
3045 }
3046 if (flag_vtable_thunks && virtuals)
3047 {
3048 /* The second slot is reserved for the tdesc pointer when thunks
3049 are used. */
3050 if (n)
3051 ++*n;
3052 virtuals = TREE_CHAIN (virtuals);
3053 }
3054
3055 return virtuals;
3056 }
3057
3058 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3059 DATA (which is really an _TYPE node). */
3060
3061 static tree
3062 dfs_find_base (binfo, data)
3063 tree binfo;
3064 void *data;
3065 {
3066 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
3067 ? binfo : NULL_TREE);
3068 }
3069
3070 /* Called from modify_all_vtables via dfs_walk. */
3071
3072 static tree
3073 dfs_modify_vtables (binfo, data)
3074 tree binfo;
3075 void *data;
3076 {
3077 if (/* There's no need to modify the vtable for a primary base;
3078 we're not going to use that vtable anyhow. */
3079 !BINFO_PRIMARY_MARKED_P (binfo)
3080 /* Similarly, a base without a vtable needs no modification. */
3081 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3082 {
3083 tree t;
3084 tree virtuals;
3085 tree old_virtuals;
3086
3087 t = (tree) data;
3088
3089 /* If we're supporting RTTI then we always need a new vtable to
3090 point to the RTTI information. Under the new ABI we may need
3091 a new vtable to contain vcall and vbase offsets. */
3092 if (flag_rtti || flag_new_abi)
3093 make_new_vtable (t, binfo);
3094
3095 /* Now, go through each of the virtual functions in the virtual
3096 function table for BINFO. Find the final overrider, and
3097 update the BINFO_VIRTUALS list appropriately. */
3098 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL),
3099 old_virtuals = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (binfo)),
3100 BINFO_TYPE (binfo),
3101 NULL);
3102 virtuals;
3103 virtuals = TREE_CHAIN (virtuals),
3104 old_virtuals = TREE_CHAIN (old_virtuals))
3105 {
3106 tree b;
3107 tree fn;
3108 tree overrider;
3109 tree vindex;
3110 tree delta;
3111 unsigned HOST_WIDE_INT i;
3112
3113 /* Find the function which originally caused this vtable
3114 entry to be present. */
3115 fn = BV_FN (old_virtuals);
3116 vindex = DECL_VINDEX (fn);
3117 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
3118 fn = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (b)),
3119 BINFO_TYPE (b),
3120 &i);
3121 while (i < TREE_INT_CST_LOW (vindex))
3122 {
3123 fn = TREE_CHAIN (fn);
3124 ++i;
3125 }
3126 fn = BV_FN (fn);
3127
3128 /* Handle the case of a virtual function defined in BINFO
3129 itself. */
3130 overrider = find_final_overrider (t, b, fn);
3131 if (overrider == error_mark_node)
3132 continue;
3133
3134 /* The `this' pointer needs to be adjusted from pointing to
3135 BINFO to pointing at the base where the final overrider
3136 appears. */
3137 delta = size_binop (PLUS_EXPR,
3138 get_derived_offset (binfo,
3139 DECL_VIRTUAL_CONTEXT (fn)),
3140 BINFO_OFFSET (binfo));
3141 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
3142
3143 modify_vtable_entry (t,
3144 binfo,
3145 TREE_PURPOSE (overrider),
3146 delta,
3147 &virtuals);
3148 }
3149 }
3150
3151 SET_BINFO_MARKED (binfo);
3152
3153 return NULL_TREE;
3154 }
3155
3156 /* Update all of the primary and secondary vtables for T. Create new
3157 vtables as required, and initialize their RTTI information. Each
3158 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3159 function from a base class; find and modify the appropriate entries
3160 to point to the overriding functions. Returns a list, in
3161 declaration order, of the functions that are overridden in this
3162 class, but do not appear in the primary base class vtable, and
3163 which should therefore be appended to the end of the vtable for T. */
3164
3165 static tree
3166 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
3167 tree t;
3168 int *has_virtual_p;
3169 tree overridden_virtuals;
3170 {
3171 tree binfo;
3172
3173 binfo = TYPE_BINFO (t);
3174
3175 /* Update all of the vtables. */
3176 dfs_walk (binfo,
3177 dfs_modify_vtables,
3178 dfs_unmarked_real_bases_queue_p,
3179 t);
3180 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3181
3182 /* If we should include overriding functions for secondary vtables
3183 in our primary vtable, add them now. */
3184 if (all_overridden_vfuns_in_vtables_p ())
3185 {
3186 tree *fnsp = &overridden_virtuals;
3187
3188 while (*fnsp)
3189 {
3190 tree fn = TREE_VALUE (*fnsp);
3191
3192 if (BINFO_VIRTUALS (binfo)
3193 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3194 {
3195 /* We know we need a vtable for this class now. */
3196 start_vtable (t, has_virtual_p);
3197 /* Set the vtable index. */
3198 DECL_VINDEX (fn)
3199 = build_shared_int_cst ((*has_virtual_p)++);
3200 /* We don't need to convert to a base class when calling
3201 this function. */
3202 DECL_VIRTUAL_CONTEXT (fn) = t;
3203
3204 /* We don't need to adjust the `this' pointer when
3205 calling this function. */
3206 BV_DELTA (*fnsp) = integer_zero_node;
3207 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
3208
3209 /* This is an overridden function not already in our
3210 vtable. Keep it. */
3211 fnsp = &TREE_CHAIN (*fnsp);
3212 }
3213 else
3214 /* We've already got an entry for this function. Skip
3215 it. */
3216 *fnsp = TREE_CHAIN (*fnsp);
3217 }
3218 }
3219 else
3220 overridden_virtuals = NULL_TREE;
3221
3222 return overridden_virtuals;
3223 }
3224
3225 /* Here, we already know that they match in every respect.
3226 All we have to check is where they had their declarations. */
3227
3228 static int
3229 strictly_overrides (fndecl1, fndecl2)
3230 tree fndecl1, fndecl2;
3231 {
3232 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
3233 DECL_CONTEXT (fndecl1),
3234 0, (tree *)0);
3235 if (distance == -2 || distance > 0)
3236 return 1;
3237 return 0;
3238 }
3239
3240 /* Get the base virtual function declarations in T that are either
3241 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3242 the overrider/hider. */
3243
3244 static tree
3245 get_basefndecls (fndecl, t)
3246 tree fndecl, t;
3247 {
3248 tree methods = TYPE_METHODS (t);
3249 tree base_fndecls = NULL_TREE;
3250 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3251 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3252
3253 while (methods)
3254 {
3255 if (TREE_CODE (methods) == FUNCTION_DECL
3256 && DECL_VINDEX (methods) != NULL_TREE
3257 && DECL_NAME (fndecl) == DECL_NAME (methods))
3258 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3259
3260 methods = TREE_CHAIN (methods);
3261 }
3262
3263 if (base_fndecls)
3264 return base_fndecls;
3265
3266 for (i = 0; i < n_baseclasses; i++)
3267 {
3268 tree base_binfo = TREE_VEC_ELT (binfos, i);
3269 tree basetype = BINFO_TYPE (base_binfo);
3270
3271 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3272 base_fndecls);
3273 }
3274
3275 return base_fndecls;
3276 }
3277
3278 /* Mark the functions that have been hidden with their overriders.
3279 Since we start out with all functions already marked with a hider,
3280 no need to mark functions that are just hidden.
3281
3282 Subroutine of warn_hidden. */
3283
3284 static void
3285 mark_overriders (fndecl, base_fndecls)
3286 tree fndecl, base_fndecls;
3287 {
3288 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3289 {
3290 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3291 TREE_PURPOSE (base_fndecls) = fndecl;
3292 }
3293 }
3294
3295 /* If this declaration supersedes the declaration of
3296 a method declared virtual in the base class, then
3297 mark this field as being virtual as well. */
3298
3299 static void
3300 check_for_override (decl, ctype)
3301 tree decl, ctype;
3302 {
3303 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3304 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3305 int virtualp = DECL_VIRTUAL_P (decl);
3306 int found_overriden_fn = 0;
3307
3308 for (i = 0; i < n_baselinks; i++)
3309 {
3310 tree base_binfo = TREE_VEC_ELT (binfos, i);
3311 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3312 {
3313 tree tmp = get_matching_virtual
3314 (base_binfo, decl,
3315 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3316
3317 if (tmp && !found_overriden_fn)
3318 {
3319 /* If this function overrides some virtual in some base
3320 class, then the function itself is also necessarily
3321 virtual, even if the user didn't explicitly say so. */
3322 DECL_VIRTUAL_P (decl) = 1;
3323
3324 /* The TMP we really want is the one from the deepest
3325 baseclass on this path, taking care not to
3326 duplicate if we have already found it (via another
3327 path to its virtual baseclass. */
3328 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3329 {
3330 cp_error_at ("`static %#D' cannot be declared", decl);
3331 cp_error_at (" since `virtual %#D' declared in base class",
3332 tmp);
3333 break;
3334 }
3335 virtualp = 1;
3336
3337 /* Set DECL_VINDEX to a value that is neither an
3338 INTEGER_CST nor the error_mark_node so that
3339 add_virtual_function will realize this is an
3340 overridden function. */
3341 DECL_VINDEX (decl)
3342 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3343
3344 /* We now know that DECL overrides something,
3345 which is all that is important. But, we must
3346 continue to iterate through all the base-classes
3347 in order to allow get_matching_virtual to check for
3348 various illegal overrides. */
3349 found_overriden_fn = 1;
3350 }
3351 }
3352 }
3353 if (virtualp)
3354 {
3355 if (DECL_VINDEX (decl) == NULL_TREE)
3356 DECL_VINDEX (decl) = error_mark_node;
3357 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3358 }
3359 }
3360
3361 /* Warn about hidden virtual functions that are not overridden in t.
3362 We know that constructors and destructors don't apply. */
3363
3364 void
3365 warn_hidden (t)
3366 tree t;
3367 {
3368 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3369 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3370 int i;
3371
3372 /* We go through each separately named virtual function. */
3373 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3374 {
3375 tree fns = TREE_VEC_ELT (method_vec, i);
3376 tree fndecl = NULL_TREE;
3377
3378 tree base_fndecls = NULL_TREE;
3379 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3380 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3381
3382 /* First see if we have any virtual functions in this batch. */
3383 for (; fns; fns = OVL_NEXT (fns))
3384 {
3385 fndecl = OVL_CURRENT (fns);
3386 if (DECL_VINDEX (fndecl))
3387 break;
3388 }
3389
3390 if (fns == NULL_TREE)
3391 continue;
3392
3393 /* First we get a list of all possible functions that might be
3394 hidden from each base class. */
3395 for (i = 0; i < n_baseclasses; i++)
3396 {
3397 tree base_binfo = TREE_VEC_ELT (binfos, i);
3398 tree basetype = BINFO_TYPE (base_binfo);
3399
3400 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3401 base_fndecls);
3402 }
3403
3404 fns = OVL_NEXT (fns);
3405
3406 /* ...then mark up all the base functions with overriders, preferring
3407 overriders to hiders. */
3408 if (base_fndecls)
3409 for (; fns; fns = OVL_NEXT (fns))
3410 {
3411 fndecl = OVL_CURRENT (fns);
3412 if (DECL_VINDEX (fndecl))
3413 mark_overriders (fndecl, base_fndecls);
3414 }
3415
3416 /* Now give a warning for all base functions without overriders,
3417 as they are hidden. */
3418 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3419 {
3420 if (! overrides (TREE_PURPOSE (base_fndecls),
3421 TREE_VALUE (base_fndecls)))
3422 {
3423 /* Here we know it is a hider, and no overrider exists. */
3424 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3425 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3426 }
3427 }
3428 }
3429 }
3430
3431 /* Check for things that are invalid. There are probably plenty of other
3432 things we should check for also. */
3433
3434 static void
3435 finish_struct_anon (t)
3436 tree t;
3437 {
3438 tree field;
3439
3440 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3441 {
3442 if (TREE_STATIC (field))
3443 continue;
3444 if (TREE_CODE (field) != FIELD_DECL)
3445 continue;
3446
3447 if (DECL_NAME (field) == NULL_TREE
3448 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3449 {
3450 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3451 for (; elt; elt = TREE_CHAIN (elt))
3452 {
3453 if (DECL_ARTIFICIAL (elt))
3454 continue;
3455
3456 if (DECL_NAME (elt) == constructor_name (t))
3457 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3458 elt);
3459
3460 if (TREE_CODE (elt) != FIELD_DECL)
3461 {
3462 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3463 elt);
3464 continue;
3465 }
3466
3467 if (TREE_PRIVATE (elt))
3468 cp_pedwarn_at ("private member `%#D' in anonymous union",
3469 elt);
3470 else if (TREE_PROTECTED (elt))
3471 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3472 elt);
3473
3474 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3475 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3476 }
3477 }
3478 }
3479 }
3480
3481 extern int interface_only, interface_unknown;
3482
3483 /* Create default constructors, assignment operators, and so forth for
3484 the type indicated by T, if they are needed.
3485 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3486 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3487 cannot have a default constructor, copy constructor taking a const
3488 reference argument, or an assignment operator, respectively. If a
3489 virtual destructor is created, its DECL is returned; otherwise the
3490 return value is NULL_TREE. */
3491
3492 static tree
3493 add_implicitly_declared_members (t, cant_have_default_ctor,
3494 cant_have_const_cctor,
3495 cant_have_assignment)
3496 tree t;
3497 int cant_have_default_ctor;
3498 int cant_have_const_cctor;
3499 int cant_have_assignment;
3500 {
3501 tree default_fn;
3502 tree implicit_fns = NULL_TREE;
3503 tree name = TYPE_IDENTIFIER (t);
3504 tree virtual_dtor = NULL_TREE;
3505 tree *f;
3506
3507 /* Destructor. */
3508 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3509 {
3510 default_fn = cons_up_default_function (t, name, 0);
3511 check_for_override (default_fn, t);
3512
3513 /* If we couldn't make it work, then pretend we didn't need it. */
3514 if (default_fn == void_type_node)
3515 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3516 else
3517 {
3518 TREE_CHAIN (default_fn) = implicit_fns;
3519 implicit_fns = default_fn;
3520
3521 if (DECL_VINDEX (default_fn))
3522 virtual_dtor = default_fn;
3523 }
3524 }
3525 else
3526 /* Any non-implicit destructor is non-trivial. */
3527 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3528
3529 /* Default constructor. */
3530 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3531 {
3532 default_fn = cons_up_default_function (t, name, 2);
3533 TREE_CHAIN (default_fn) = implicit_fns;
3534 implicit_fns = default_fn;
3535 }
3536
3537 /* Copy constructor. */
3538 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3539 {
3540 /* ARM 12.18: You get either X(X&) or X(const X&), but
3541 not both. --Chip */
3542 default_fn = cons_up_default_function (t, name,
3543 3 + cant_have_const_cctor);
3544 TREE_CHAIN (default_fn) = implicit_fns;
3545 implicit_fns = default_fn;
3546 }
3547
3548 /* Assignment operator. */
3549 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3550 {
3551 default_fn = cons_up_default_function (t, name,
3552 5 + cant_have_assignment);
3553 TREE_CHAIN (default_fn) = implicit_fns;
3554 implicit_fns = default_fn;
3555 }
3556
3557 /* Now, hook all of the new functions on to TYPE_METHODS,
3558 and add them to the CLASSTYPE_METHOD_VEC. */
3559 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3560 add_method (t, 0, *f);
3561 *f = TYPE_METHODS (t);
3562 TYPE_METHODS (t) = implicit_fns;
3563
3564 return virtual_dtor;
3565 }
3566
3567 /* Subroutine of finish_struct_1. Recursively count the number of fields
3568 in TYPE, including anonymous union members. */
3569
3570 static int
3571 count_fields (fields)
3572 tree fields;
3573 {
3574 tree x;
3575 int n_fields = 0;
3576 for (x = fields; x; x = TREE_CHAIN (x))
3577 {
3578 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3579 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3580 else
3581 n_fields += 1;
3582 }
3583 return n_fields;
3584 }
3585
3586 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3587 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3588
3589 static int
3590 add_fields_to_vec (fields, field_vec, idx)
3591 tree fields, field_vec;
3592 int idx;
3593 {
3594 tree x;
3595 for (x = fields; x; x = TREE_CHAIN (x))
3596 {
3597 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3598 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3599 else
3600 TREE_VEC_ELT (field_vec, idx++) = x;
3601 }
3602 return idx;
3603 }
3604
3605 /* FIELD is a bit-field. We are finishing the processing for its
3606 enclosing type. Issue any appropriate messages and set appropriate
3607 flags. */
3608
3609 static void
3610 check_bitfield_decl (field)
3611 tree field;
3612 {
3613 tree type = TREE_TYPE (field);
3614
3615 /* Invalid bit-field size done by grokfield. */
3616 /* Detect invalid bit-field type. Simply checking if TYPE is
3617 integral is insufficient, as that is the array core of the field
3618 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3619 if (DECL_INITIAL (field)
3620 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3621 {
3622 cp_error_at ("bit-field `%#D' with non-integral type", field);
3623 DECL_INITIAL (field) = NULL;
3624 }
3625
3626 /* Detect and ignore out of range field width. */
3627 if (DECL_INITIAL (field))
3628 {
3629 tree w = DECL_INITIAL (field);
3630
3631 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3632 STRIP_NOPS (w);
3633
3634 /* detect invalid field size. */
3635 if (TREE_CODE (w) == CONST_DECL)
3636 w = DECL_INITIAL (w);
3637 else if (TREE_READONLY_DECL_P (w))
3638 w = decl_constant_value (w);
3639
3640 if (TREE_CODE (w) != INTEGER_CST)
3641 {
3642 cp_error_at ("bit-field `%D' width not an integer constant",
3643 field);
3644 DECL_INITIAL (field) = NULL_TREE;
3645 }
3646 else if (tree_int_cst_sgn (w) < 0)
3647 {
3648 DECL_INITIAL (field) = NULL;
3649 cp_error_at ("negative width in bit-field `%D'", field);
3650 }
3651 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3652 {
3653 DECL_INITIAL (field) = NULL;
3654 cp_error_at ("zero width for bit-field `%D'", field);
3655 }
3656 else if (0 < compare_tree_int (w,
3657 TYPE_PRECISION
3658 (long_long_unsigned_type_node)))
3659 {
3660 /* The backend will dump if you try to use something too
3661 big; avoid that. */
3662 DECL_INITIAL (field) = NULL;
3663 sorry ("bit-fields larger than %d bits",
3664 TYPE_PRECISION (long_long_unsigned_type_node));
3665 cp_error_at (" in declaration of `%D'", field);
3666 }
3667 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3668 && TREE_CODE (type) != ENUMERAL_TYPE
3669 && TREE_CODE (type) != BOOLEAN_TYPE)
3670 cp_warning_at ("width of `%D' exceeds its type", field);
3671 else if (TREE_CODE (type) == ENUMERAL_TYPE
3672 && (0 > compare_tree_int (w,
3673 min_precision (TYPE_MIN_VALUE (type),
3674 TREE_UNSIGNED (type)))
3675 || 0 > compare_tree_int (w,
3676 min_precision
3677 (TYPE_MAX_VALUE (type),
3678 TREE_UNSIGNED (type)))))
3679 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3680 field, type);
3681
3682 if (DECL_INITIAL (field))
3683 {
3684 DECL_INITIAL (field) = NULL_TREE;
3685 DECL_SIZE (field) = bitsize_int (TREE_INT_CST_LOW (w));
3686 DECL_BIT_FIELD (field) = 1;
3687
3688 if (integer_zerop (w))
3689 {
3690 #ifdef EMPTY_FIELD_BOUNDARY
3691 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3692 EMPTY_FIELD_BOUNDARY);
3693 #endif
3694 #ifdef PCC_BITFIELD_TYPE_MATTERS
3695 if (PCC_BITFIELD_TYPE_MATTERS)
3696 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3697 TYPE_ALIGN (type));
3698 #endif
3699 }
3700 }
3701 }
3702 else
3703 /* Non-bit-fields are aligned for their type. */
3704 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3705 }
3706
3707 /* FIELD is a non bit-field. We are finishing the processing for its
3708 enclosing type T. Issue any appropriate messages and set appropriate
3709 flags. */
3710
3711 static void
3712 check_field_decl (field, t, cant_have_const_ctor,
3713 cant_have_default_ctor, no_const_asn_ref,
3714 any_default_members)
3715 tree field;
3716 tree t;
3717 int *cant_have_const_ctor;
3718 int *cant_have_default_ctor;
3719 int *no_const_asn_ref;
3720 int *any_default_members;
3721 {
3722 tree type = strip_array_types (TREE_TYPE (field));
3723
3724 /* An anonymous union cannot contain any fields which would change
3725 the settings of CANT_HAVE_CONST_CTOR and friends. */
3726 if (ANON_UNION_TYPE_P (type))
3727 ;
3728 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3729 structs. So, we recurse through their fields here. */
3730 else if (ANON_AGGR_TYPE_P (type))
3731 {
3732 tree fields;
3733
3734 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3735 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3736 check_field_decl (fields, t, cant_have_const_ctor,
3737 cant_have_default_ctor, no_const_asn_ref,
3738 any_default_members);
3739 }
3740 /* Check members with class type for constructors, destructors,
3741 etc. */
3742 else if (CLASS_TYPE_P (type))
3743 {
3744 /* Never let anything with uninheritable virtuals
3745 make it through without complaint. */
3746 abstract_virtuals_error (field, type);
3747
3748 if (TREE_CODE (t) == UNION_TYPE)
3749 {
3750 if (TYPE_NEEDS_CONSTRUCTING (type))
3751 cp_error_at ("member `%#D' with constructor not allowed in union",
3752 field);
3753 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3754 cp_error_at ("member `%#D' with destructor not allowed in union",
3755 field);
3756 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3757 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3758 field);
3759 }
3760 else
3761 {
3762 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3763 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3764 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3765 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3766 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3767 }
3768
3769 if (!TYPE_HAS_CONST_INIT_REF (type))
3770 *cant_have_const_ctor = 1;
3771
3772 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3773 *no_const_asn_ref = 1;
3774
3775 if (TYPE_HAS_CONSTRUCTOR (type)
3776 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3777 *cant_have_default_ctor = 1;
3778 }
3779 if (DECL_INITIAL (field) != NULL_TREE)
3780 {
3781 /* `build_class_init_list' does not recognize
3782 non-FIELD_DECLs. */
3783 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3784 cp_error_at ("multiple fields in union `%T' initialized");
3785 *any_default_members = 1;
3786 }
3787
3788 /* Non-bit-fields are aligned for their type, except packed fields
3789 which require only BITS_PER_UNIT alignment. */
3790 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3791 (DECL_PACKED (field)
3792 ? BITS_PER_UNIT
3793 : TYPE_ALIGN (TREE_TYPE (field))));
3794 }
3795
3796 /* Check the data members (both static and non-static), class-scoped
3797 typedefs, etc., appearing in the declaration of T. Issue
3798 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3799 declaration order) of access declarations; each TREE_VALUE in this
3800 list is a USING_DECL.
3801
3802 In addition, set the following flags:
3803
3804 EMPTY_P
3805 The class is empty, i.e., contains no non-static data members.
3806
3807 CANT_HAVE_DEFAULT_CTOR_P
3808 This class cannot have an implicitly generated default
3809 constructor.
3810
3811 CANT_HAVE_CONST_CTOR_P
3812 This class cannot have an implicitly generated copy constructor
3813 taking a const reference.
3814
3815 CANT_HAVE_CONST_ASN_REF
3816 This class cannot have an implicitly generated assignment
3817 operator taking a const reference.
3818
3819 All of these flags should be initialized before calling this
3820 function.
3821
3822 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3823 fields can be added by adding to this chain. */
3824
3825 static void
3826 check_field_decls (t, access_decls, empty_p,
3827 cant_have_default_ctor_p, cant_have_const_ctor_p,
3828 no_const_asn_ref_p)
3829 tree t;
3830 tree *access_decls;
3831 int *empty_p;
3832 int *cant_have_default_ctor_p;
3833 int *cant_have_const_ctor_p;
3834 int *no_const_asn_ref_p;
3835 {
3836 tree *field;
3837 tree *next;
3838 int has_pointers;
3839 int any_default_members;
3840
3841 /* First, delete any duplicate fields. */
3842 delete_duplicate_fields (TYPE_FIELDS (t));
3843
3844 /* Assume there are no access declarations. */
3845 *access_decls = NULL_TREE;
3846 /* Assume this class has no pointer members. */
3847 has_pointers = 0;
3848 /* Assume none of the members of this class have default
3849 initializations. */
3850 any_default_members = 0;
3851
3852 for (field = &TYPE_FIELDS (t); *field; field = next)
3853 {
3854 tree x = *field;
3855 tree type = TREE_TYPE (x);
3856
3857 GNU_xref_member (current_class_name, x);
3858
3859 next = &TREE_CHAIN (x);
3860
3861 if (TREE_CODE (x) == FIELD_DECL)
3862 {
3863 DECL_PACKED (x) |= TYPE_PACKED (t);
3864
3865 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3866 /* We don't treat zero-width bitfields as making a class
3867 non-empty. */
3868 ;
3869 else
3870 {
3871 /* The class is non-empty. */
3872 *empty_p = 0;
3873 /* The class is not even nearly empty. */
3874 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3875 }
3876 }
3877
3878 if (TREE_CODE (x) == USING_DECL)
3879 {
3880 /* Prune the access declaration from the list of fields. */
3881 *field = TREE_CHAIN (x);
3882
3883 /* Save the access declarations for our caller. */
3884 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3885
3886 /* Since we've reset *FIELD there's no reason to skip to the
3887 next field. */
3888 next = field;
3889 continue;
3890 }
3891
3892 if (TREE_CODE (x) == TYPE_DECL
3893 || TREE_CODE (x) == TEMPLATE_DECL)
3894 continue;
3895
3896 /* If we've gotten this far, it's a data member, possibly static,
3897 or an enumerator. */
3898
3899 DECL_FIELD_CONTEXT (x) = t;
3900
3901 /* ``A local class cannot have static data members.'' ARM 9.4 */
3902 if (current_function_decl && TREE_STATIC (x))
3903 cp_error_at ("field `%D' in local class cannot be static", x);
3904
3905 /* Perform error checking that did not get done in
3906 grokdeclarator. */
3907 if (TREE_CODE (type) == FUNCTION_TYPE)
3908 {
3909 cp_error_at ("field `%D' invalidly declared function type",
3910 x);
3911 type = build_pointer_type (type);
3912 TREE_TYPE (x) = type;
3913 }
3914 else if (TREE_CODE (type) == METHOD_TYPE)
3915 {
3916 cp_error_at ("field `%D' invalidly declared method type", x);
3917 type = build_pointer_type (type);
3918 TREE_TYPE (x) = type;
3919 }
3920 else if (TREE_CODE (type) == OFFSET_TYPE)
3921 {
3922 cp_error_at ("field `%D' invalidly declared offset type", x);
3923 type = build_pointer_type (type);
3924 TREE_TYPE (x) = type;
3925 }
3926
3927 if (type == error_mark_node)
3928 continue;
3929
3930 DECL_SAVED_INSNS (x) = 0;
3931
3932 /* When this goes into scope, it will be a non-local reference. */
3933 DECL_NONLOCAL (x) = 1;
3934
3935 if (TREE_CODE (x) == CONST_DECL)
3936 continue;
3937
3938 if (TREE_CODE (x) == VAR_DECL)
3939 {
3940 if (TREE_CODE (t) == UNION_TYPE)
3941 /* Unions cannot have static members. */
3942 cp_error_at ("field `%D' declared static in union", x);
3943
3944 continue;
3945 }
3946
3947 /* Now it can only be a FIELD_DECL. */
3948
3949 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3950 CLASSTYPE_NON_AGGREGATE (t) = 1;
3951
3952 /* If this is of reference type, check if it needs an init.
3953 Also do a little ANSI jig if necessary. */
3954 if (TREE_CODE (type) == REFERENCE_TYPE)
3955 {
3956 CLASSTYPE_NON_POD_P (t) = 1;
3957 if (DECL_INITIAL (x) == NULL_TREE)
3958 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3959
3960 /* ARM $12.6.2: [A member initializer list] (or, for an
3961 aggregate, initialization by a brace-enclosed list) is the
3962 only way to initialize nonstatic const and reference
3963 members. */
3964 *cant_have_default_ctor_p = 1;
3965 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3966
3967 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3968 {
3969 if (DECL_NAME (x))
3970 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3971 else
3972 cp_warning_at ("non-static reference in class without a constructor", x);
3973 }
3974 }
3975
3976 type = strip_array_types (type);
3977
3978 if (TREE_CODE (type) == POINTER_TYPE)
3979 has_pointers = 1;
3980
3981 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3982 CLASSTYPE_HAS_MUTABLE (t) = 1;
3983
3984 if (! pod_type_p (type)
3985 /* For some reason, pointers to members are POD types themselves,
3986 but are not allowed in POD structs. Silly. */
3987 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3988 CLASSTYPE_NON_POD_P (t) = 1;
3989
3990 /* If any field is const, the structure type is pseudo-const. */
3991 if (CP_TYPE_CONST_P (type))
3992 {
3993 C_TYPE_FIELDS_READONLY (t) = 1;
3994 if (DECL_INITIAL (x) == NULL_TREE)
3995 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3996
3997 /* ARM $12.6.2: [A member initializer list] (or, for an
3998 aggregate, initialization by a brace-enclosed list) is the
3999 only way to initialize nonstatic const and reference
4000 members. */
4001 *cant_have_default_ctor_p = 1;
4002 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
4003
4004 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4005 {
4006 if (DECL_NAME (x))
4007 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
4008 else
4009 cp_warning_at ("non-static const member in class without a constructor", x);
4010 }
4011 }
4012 /* A field that is pseudo-const makes the structure likewise. */
4013 else if (IS_AGGR_TYPE (type))
4014 {
4015 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4016 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4017 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4018 }
4019
4020 /* We set DECL_C_BIT_FIELD in grokbitfield.
4021 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4022 if (DECL_C_BIT_FIELD (x))
4023 check_bitfield_decl (x);
4024 else
4025 check_field_decl (x, t,
4026 cant_have_const_ctor_p,
4027 cant_have_default_ctor_p,
4028 no_const_asn_ref_p,
4029 &any_default_members);
4030 }
4031
4032 /* Effective C++ rule 11. */
4033 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4034 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4035 {
4036 cp_warning ("`%#T' has pointer data members", t);
4037
4038 if (! TYPE_HAS_INIT_REF (t))
4039 {
4040 cp_warning (" but does not override `%T(const %T&)'", t, t);
4041 if (! TYPE_HAS_ASSIGN_REF (t))
4042 cp_warning (" or `operator=(const %T&)'", t);
4043 }
4044 else if (! TYPE_HAS_ASSIGN_REF (t))
4045 cp_warning (" but does not override `operator=(const %T&)'", t);
4046 }
4047
4048
4049 /* Check anonymous struct/anonymous union fields. */
4050 finish_struct_anon (t);
4051
4052 /* We've built up the list of access declarations in reverse order.
4053 Fix that now. */
4054 *access_decls = nreverse (*access_decls);
4055 }
4056
4057 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4058 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4059 field are as indicated. The CLASS_TYPE in which this field occurs
4060 is also indicated. FCONTEXT is the type that is needed for the debug
4061 info output routines. *EMPTY_P is set to a non-zero value by this
4062 function to indicate that a class containing this field is
4063 non-empty. */
4064
4065 static tree
4066 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
4067 empty_p)
4068 tree name;
4069 tree assembler_name;
4070 tree type;
4071 tree class_type;
4072 tree fcontext;
4073 int *empty_p;
4074 {
4075 tree field;
4076
4077 /* This class is non-empty. */
4078 *empty_p = 0;
4079
4080 /* Build the FIELD_DECL. */
4081 field = build_lang_decl (FIELD_DECL, name, type);
4082 DECL_ASSEMBLER_NAME (field) = assembler_name;
4083 DECL_VIRTUAL_P (field) = 1;
4084 DECL_ARTIFICIAL (field) = 1;
4085 DECL_FIELD_CONTEXT (field) = class_type;
4086 DECL_FCONTEXT (field) = fcontext;
4087 DECL_SAVED_INSNS (field) = 0;
4088 DECL_ALIGN (field) = TYPE_ALIGN (type);
4089
4090 /* Return it. */
4091 return field;
4092 }
4093
4094 /* If the empty base field in DECL overlaps with a base of the same type in
4095 NEWDECL, which is either another base field or the first data field of
4096 the class, pad the base just before NEWDECL and return 1. Otherwise,
4097 return 0. */
4098
4099 static int
4100 avoid_overlap (decl, newdecl, empty_p)
4101 tree decl, newdecl;
4102 int *empty_p;
4103 {
4104 tree field;
4105
4106 if (newdecl == NULL_TREE
4107 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
4108 return 0;
4109
4110 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
4111 field = TREE_CHAIN (field))
4112 ;
4113
4114 DECL_SIZE (field) = bitsize_int (1);
4115 DECL_SIZE_UNIT (field) = 0;
4116 /* The containing class cannot be empty; this field takes up space. */
4117 *empty_p = 0;
4118
4119 return 1;
4120 }
4121
4122 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
4123 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
4124 *BASE_ALIGN is a running maximum of the alignments of any base
4125 class. */
4126
4127 static tree
4128 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
4129 tree t;
4130 tree binfo;
4131 int *empty_p;
4132 int *saw_empty_p;
4133 unsigned int *base_align;
4134 {
4135 tree basetype = BINFO_TYPE (binfo);
4136 tree decl;
4137
4138 if (TYPE_SIZE (basetype) == 0)
4139 /* This error is now reported in xref_tag, thus giving better
4140 location information. */
4141 return NULL_TREE;
4142
4143 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4144 DECL_ARTIFICIAL (decl) = 1;
4145 DECL_FIELD_CONTEXT (decl) = t;
4146 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4147 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4148 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4149
4150 if (flag_new_abi && integer_zerop (DECL_SIZE (decl)))
4151 {
4152 *saw_empty_p = 1;
4153 return decl;
4154 }
4155
4156 /* The containing class is non-empty because it has a non-empty base
4157 class. */
4158 *empty_p = 0;
4159
4160 if (! flag_new_abi)
4161 {
4162 /* Brain damage for backwards compatibility. For no good
4163 reason, the old layout_basetypes made every base at least
4164 as large as the alignment for the bases up to that point,
4165 gratuitously wasting space. So we do the same thing
4166 here. */
4167 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4168 DECL_SIZE (decl)
4169 = size_int (MAX ((HOST_WIDE_INT) TREE_INT_CST_LOW (DECL_SIZE (decl)),
4170 (int) (*base_align)));
4171 DECL_SIZE_UNIT (decl)
4172 = size_int (MAX (((HOST_WIDE_INT) TREE_INT_CST_LOW
4173 (DECL_SIZE_UNIT (decl))),
4174 (int) *base_align / BITS_PER_UNIT));
4175 }
4176
4177 return decl;
4178 }
4179
4180 /* Returns a list of fields to stand in for the base class subobjects
4181 of REC. These fields are later removed by layout_basetypes. */
4182
4183 static tree
4184 build_base_fields (rec, empty_p)
4185 tree rec;
4186 int *empty_p;
4187 {
4188 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4189 subobjects. */
4190 tree base_decls = NULL_TREE;
4191 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4192 tree decl, nextdecl;
4193 int i, saw_empty = 0;
4194 unsigned int base_align = 0;
4195
4196 /* Under the new ABI, the primary base class is always allocated
4197 first. */
4198 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4199 {
4200 tree primary_base;
4201
4202 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
4203 base_decls = chainon (build_base_field (rec,
4204 primary_base,
4205 empty_p,
4206 &saw_empty,
4207 &base_align),
4208 base_decls);
4209 }
4210
4211 /* Now allocate the rest of the bases. */
4212 for (i = 0; i < n_baseclasses; ++i)
4213 {
4214 tree base_binfo;
4215
4216 /* Under the new ABI, the primary base was already allocated
4217 above, so we don't need to allocate it again here. */
4218 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4219 continue;
4220
4221 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4222
4223 /* A primary virtual base class is allocated just like any other
4224 base class, but a non-primary virtual base is allocated
4225 later, in layout_basetypes. */
4226 if (TREE_VIA_VIRTUAL (base_binfo)
4227 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4228 continue;
4229
4230 base_decls = chainon (build_base_field (rec, base_binfo,
4231 empty_p,
4232 &saw_empty,
4233 &base_align),
4234 base_decls);
4235 }
4236
4237 /* Reverse the list of fields so we allocate the bases in the proper
4238 order. */
4239 base_decls = nreverse (base_decls);
4240
4241 /* In the presence of empty base classes, we run the risk of allocating
4242 two objects of the same class on top of one another. Avoid that. */
4243 if (flag_new_abi && saw_empty)
4244 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4245 {
4246 if (integer_zerop (DECL_SIZE (decl)))
4247 {
4248 /* First step through the following bases until we find
4249 an overlap or a non-empty base. */
4250 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4251 nextdecl = TREE_CHAIN (nextdecl))
4252 if (avoid_overlap (decl, nextdecl, empty_p)
4253 || ! integer_zerop (DECL_SIZE (nextdecl)))
4254 goto nextbase;
4255
4256 /* If we're still looking, also check against the first
4257 field. */
4258 for (nextdecl = TYPE_FIELDS (rec);
4259 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4260 nextdecl = TREE_CHAIN (nextdecl))
4261 /* keep looking */;
4262 avoid_overlap (decl, nextdecl, empty_p);
4263 }
4264 nextbase:;
4265 }
4266
4267 return base_decls;
4268 }
4269
4270 /* Go through the TYPE_METHODS of T issuing any appropriate
4271 diagnostics, figuring out which methods override which other
4272 methods, and so forth. */
4273
4274 static void
4275 check_methods (t)
4276 tree t;
4277 {
4278 tree x;
4279 int seen_one_arg_array_delete_p = 0;
4280
4281 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4282 {
4283 GNU_xref_member (current_class_name, x);
4284
4285 /* If this was an evil function, don't keep it in class. */
4286 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4287 continue;
4288
4289 DECL_SAVED_INSNS (x) = 0;
4290 check_for_override (x, t);
4291 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4292 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4293
4294 /* The name of the field is the original field name
4295 Save this in auxiliary field for later overloading. */
4296 if (DECL_VINDEX (x))
4297 {
4298 TYPE_POLYMORPHIC_P (t) = 1;
4299 if (DECL_PURE_VIRTUAL_P (x))
4300 CLASSTYPE_PURE_VIRTUALS (t)
4301 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4302 }
4303
4304 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4305 {
4306 tree second_parm;
4307
4308 /* When dynamically allocating an array of this type, we
4309 need a "cookie" to record how many elements we allocated,
4310 even if the array elements have no non-trivial
4311 destructor, if the usual array deallocation function
4312 takes a second argument of type size_t. The standard (in
4313 [class.free]) requires that the second argument be set
4314 correctly. */
4315 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4316 /* This is overly conservative, but we must maintain this
4317 behavior for backwards compatibility. */
4318 if (!flag_new_abi && second_parm != void_list_node)
4319 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4320 /* Under the new ABI, we choose only those function that are
4321 explicitly declared as `operator delete[] (void *,
4322 size_t)'. */
4323 else if (flag_new_abi
4324 && !seen_one_arg_array_delete_p
4325 && second_parm
4326 && TREE_CHAIN (second_parm) == void_list_node
4327 && same_type_p (TREE_VALUE (second_parm), sizetype))
4328 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4329 /* If there's no second parameter, then this is the usual
4330 deallocation function. */
4331 else if (second_parm == void_list_node)
4332 seen_one_arg_array_delete_p = 1;
4333 }
4334 }
4335 }
4336
4337 /* Remove all zero-width bit-fields from T. */
4338
4339 static void
4340 remove_zero_width_bit_fields (t)
4341 tree t;
4342 {
4343 tree *fieldsp;
4344
4345 fieldsp = &TYPE_FIELDS (t);
4346 while (*fieldsp)
4347 {
4348 if (TREE_CODE (*fieldsp) == FIELD_DECL
4349 && DECL_C_BIT_FIELD (*fieldsp)
4350 && DECL_INITIAL (*fieldsp))
4351 *fieldsp = TREE_CHAIN (*fieldsp);
4352 else
4353 fieldsp = &TREE_CHAIN (*fieldsp);
4354 }
4355 }
4356
4357 /* Check the validity of the bases and members declared in T. Add any
4358 implicitly-generated functions (like copy-constructors and
4359 assignment operators). Compute various flag bits (like
4360 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4361 level: i.e., independently of the ABI in use. */
4362
4363 static void
4364 check_bases_and_members (t, empty_p)
4365 tree t;
4366 int *empty_p;
4367 {
4368 /* Nonzero if we are not allowed to generate a default constructor
4369 for this case. */
4370 int cant_have_default_ctor;
4371 /* Nonzero if the implicitly generated copy constructor should take
4372 a non-const reference argument. */
4373 int cant_have_const_ctor;
4374 /* Nonzero if the the implicitly generated assignment operator
4375 should take a non-const reference argument. */
4376 int no_const_asn_ref;
4377 tree access_decls;
4378
4379 /* By default, we use const reference arguments and generate default
4380 constructors. */
4381 cant_have_default_ctor = 0;
4382 cant_have_const_ctor = 0;
4383 no_const_asn_ref = 0;
4384
4385 /* Assume that the class is nearly empty; we'll clear this flag if
4386 it turns out not to be nearly empty. */
4387 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4388
4389 /* Check all the base-classes. */
4390 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4391 &no_const_asn_ref);
4392
4393 /* Check all the data member declarations. */
4394 check_field_decls (t, &access_decls, empty_p,
4395 &cant_have_default_ctor,
4396 &cant_have_const_ctor,
4397 &no_const_asn_ref);
4398
4399 /* Check all the method declarations. */
4400 check_methods (t);
4401
4402 /* A nearly-empty class has to be vptr-containing; a nearly empty
4403 class contains just a vptr. */
4404 if (!TYPE_CONTAINS_VPTR_P (t))
4405 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4406
4407 /* Do some bookkeeping that will guide the generation of implicitly
4408 declared member functions. */
4409 TYPE_HAS_COMPLEX_INIT_REF (t)
4410 |= (TYPE_HAS_INIT_REF (t)
4411 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4412 || TYPE_POLYMORPHIC_P (t));
4413 TYPE_NEEDS_CONSTRUCTING (t)
4414 |= (TYPE_HAS_CONSTRUCTOR (t)
4415 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4416 || TYPE_POLYMORPHIC_P (t));
4417 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4418 || TYPE_POLYMORPHIC_P (t));
4419 CLASSTYPE_NON_POD_P (t)
4420 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4421 || TYPE_HAS_ASSIGN_REF (t));
4422 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4423 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4424 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4425
4426 /* Synthesize any needed methods. Note that methods will be synthesized
4427 for anonymous unions; grok_x_components undoes that. */
4428 add_implicitly_declared_members (t, cant_have_default_ctor,
4429 cant_have_const_ctor,
4430 no_const_asn_ref);
4431
4432 /* Process the using-declarations. */
4433 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4434 handle_using_decl (TREE_VALUE (access_decls), t);
4435
4436 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4437 finish_struct_methods (t);
4438 }
4439
4440 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4441 accordingly, and, if necessary, add the TYPE_VFIELD to the
4442 TYPE_FIELDS list. */
4443
4444 static void
4445 create_vtable_ptr (t, empty_p, has_virtual_p,
4446 new_virtuals_p, overridden_virtuals_p)
4447 tree t;
4448 int *empty_p;
4449 int *has_virtual_p;
4450 tree *new_virtuals_p;
4451 tree *overridden_virtuals_p;
4452 {
4453 tree fn;
4454
4455 /* Loop over the virtual functions, adding them to our various
4456 vtables. */
4457 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4458 if (DECL_VINDEX (fn))
4459 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4460 has_virtual_p, fn, t);
4461
4462 /* Even if there weren't any new virtual functions, we might need a
4463 new virtual function table if we're supposed to include vptrs in
4464 all classes that need them. */
4465 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4466 start_vtable (t, has_virtual_p);
4467
4468 /* If we couldn't find an appropriate base class, create a new field
4469 here. */
4470 if (*has_virtual_p && !TYPE_VFIELD (t))
4471 {
4472 /* We build this decl with vtbl_ptr_type_node, which is a
4473 `vtable_entry_type*'. It might seem more precise to use
4474 `vtable_entry_type (*)[N]' where N is the number of firtual
4475 functions. However, that would require the vtable pointer in
4476 base classes to have a different type than the vtable pointer
4477 in derived classes. We could make that happen, but that
4478 still wouldn't solve all the problems. In particular, the
4479 type-based alias analysis code would decide that assignments
4480 to the base class vtable pointer can't alias assignments to
4481 the derived class vtable pointer, since they have different
4482 types. Thus, in an derived class destructor, where the base
4483 class constructor was inlined, we could generate bad code for
4484 setting up the vtable pointer.
4485
4486 Therefore, we use one type for all vtable pointers. We still
4487 use a type-correct type; it's just doesn't indicate the array
4488 bounds. That's better than using `void*' or some such; it's
4489 cleaner, and it let's the alias analysis code know that these
4490 stores cannot alias stores to void*! */
4491 TYPE_VFIELD (t)
4492 = build_vtbl_or_vbase_field (get_vfield_name (t),
4493 get_identifier (VFIELD_BASE),
4494 vtbl_ptr_type_node,
4495 t,
4496 t,
4497 empty_p);
4498
4499 /* Add the new field to the list of fields in this class. */
4500 if (!flag_new_abi)
4501 /* In the old ABI, the vtable pointer goes at the end of the
4502 class. */
4503 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4504 else
4505 {
4506 /* But in the new ABI, the vtable pointer is the first thing
4507 in the class. */
4508 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4509 /* If there were any baseclasses, they can't possibly be at
4510 offset zero any more, because that's where the vtable
4511 pointer is. So, converting to a base class is going to
4512 take work. */
4513 if (CLASSTYPE_N_BASECLASSES (t))
4514 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4515 }
4516
4517 /* We can't yet add this new field to the list of all virtual
4518 function table pointers in this class. The
4519 modify_all_vtables function depends on this not being done.
4520 So, it is done later, in finish_struct_1. */
4521 }
4522 }
4523
4524 /* Fixup the inline function given by INFO now that the class is
4525 complete. */
4526
4527 static void
4528 fixup_pending_inline (info)
4529 struct pending_inline *info;
4530 {
4531 if (info)
4532 {
4533 tree args;
4534 tree fn = info->fndecl;
4535
4536 args = DECL_ARGUMENTS (fn);
4537 while (args)
4538 {
4539 DECL_CONTEXT (args) = fn;
4540 args = TREE_CHAIN (args);
4541 }
4542 }
4543 }
4544
4545 /* Fixup the inline methods and friends in TYPE now that TYPE is
4546 complete. */
4547
4548 static void
4549 fixup_inline_methods (type)
4550 tree type;
4551 {
4552 tree method = TYPE_METHODS (type);
4553
4554 if (method && TREE_CODE (method) == TREE_VEC)
4555 {
4556 if (TREE_VEC_ELT (method, 1))
4557 method = TREE_VEC_ELT (method, 1);
4558 else if (TREE_VEC_ELT (method, 0))
4559 method = TREE_VEC_ELT (method, 0);
4560 else
4561 method = TREE_VEC_ELT (method, 2);
4562 }
4563
4564 /* Do inline member functions. */
4565 for (; method; method = TREE_CHAIN (method))
4566 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4567
4568 /* Do friends. */
4569 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4570 method;
4571 method = TREE_CHAIN (method))
4572 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4573 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4574 }
4575
4576 /* Called from propagate_binfo_offsets via dfs_walk. */
4577
4578 static tree
4579 dfs_propagate_binfo_offsets (binfo, data)
4580 tree binfo;
4581 void *data;
4582 {
4583 tree offset = (tree) data;
4584
4585 /* Update the BINFO_OFFSET for this base. */
4586 BINFO_OFFSET (binfo) = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4587
4588 SET_BINFO_MARKED (binfo);
4589
4590 return NULL_TREE;
4591 }
4592
4593 /* Add OFFSET to all base types of BINFO which is a base in the
4594 hierarchy dominated by T.
4595
4596 OFFSET, which is a type offset, is number of bytes.
4597
4598 Note that we don't have to worry about having two paths to the
4599 same base type, since this type owns its association list. */
4600
4601 static void
4602 propagate_binfo_offsets (binfo, offset)
4603 tree binfo;
4604 tree offset;
4605 {
4606 dfs_walk (binfo,
4607 dfs_propagate_binfo_offsets,
4608 dfs_skip_nonprimary_vbases_unmarkedp,
4609 offset);
4610 dfs_walk (binfo,
4611 dfs_unmark,
4612 dfs_skip_nonprimary_vbases_markedp,
4613 NULL);
4614 }
4615
4616 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4617 the field list for T. */
4618
4619 static void
4620 remove_base_field (t, binfo, field)
4621 tree t;
4622 tree binfo;
4623 tree *field;
4624 {
4625 tree basetype = BINFO_TYPE (binfo);
4626 tree offset;
4627
4628 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4629
4630 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4631 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4632 basetype, t);
4633
4634 offset
4635 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4636 BITS_PER_UNIT));
4637 propagate_binfo_offsets (binfo, offset);
4638
4639 /* Remove this field. */
4640 *field = TREE_CHAIN (*field);
4641 }
4642
4643 /* Remove the FIELD_DECLs created for T's base classes in
4644 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4645 bases, except for non-primary virtual baseclasses. */
4646
4647 static void
4648 remove_base_fields (t)
4649 tree t;
4650 {
4651 int i;
4652 tree *field;
4653
4654 /* Now propagate offset information throughout the lattice.
4655 Simultaneously, remove the temporary FIELD_DECLS we created in
4656 build_base_fields to refer to base types. */
4657 field = &TYPE_FIELDS (t);
4658 if (TYPE_VFIELD (t) == *field)
4659 {
4660 /* If this class did not have a primary base, we create a
4661 virtual function table pointer. It will be the first thing
4662 in the class, under the new ABI. Skip it; the base fields
4663 will follow it. */
4664 my_friendly_assert (flag_new_abi
4665 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4666 19991218);
4667 field = &TREE_CHAIN (*field);
4668 }
4669
4670 /* Under the new ABI, the primary base is always allocated first. */
4671 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4672 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4673
4674 /* Now remove the rest of the bases. */
4675 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4676 {
4677 tree binfo;
4678
4679 /* Under the new ABI, we've already removed the primary base
4680 above. */
4681 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4682 continue;
4683
4684 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4685
4686 /* We treat a primary virtual base class just like an ordinary base
4687 class. But, non-primary virtual bases are laid out later. */
4688 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4689 continue;
4690
4691 remove_base_field (t, binfo, field);
4692 }
4693 }
4694
4695 /* Called via dfs_walk from layout_virtual bases. */
4696
4697 static tree
4698 dfs_set_offset_for_shared_vbases (binfo, data)
4699 tree binfo;
4700 void *data;
4701 {
4702 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4703 {
4704 /* Update the shared copy. */
4705 tree shared_binfo;
4706
4707 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4708 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4709 }
4710
4711 return NULL_TREE;
4712 }
4713
4714 /* Called via dfs_walk from layout_virtual bases. */
4715
4716 static tree
4717 dfs_set_offset_for_unshared_vbases (binfo, data)
4718 tree binfo;
4719 void *data;
4720 {
4721 /* If this is a virtual base, make sure it has the same offset as
4722 the shared copy. If it's a primary base, then we know it's
4723 correct. */
4724 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4725 {
4726 tree t = (tree) data;
4727 tree vbase;
4728 tree offset;
4729
4730 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4731 offset = size_binop (MINUS_EXPR,
4732 BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4733 propagate_binfo_offsets (binfo, offset);
4734 }
4735
4736 return NULL_TREE;
4737 }
4738
4739 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4740 TYPE_ALIGN and TYPE_SIZE for T. */
4741
4742 static void
4743 layout_virtual_bases (t)
4744 tree t;
4745 {
4746 tree vbase;
4747 int dsize;
4748
4749 /* DSIZE is the size of the class without the virtual bases. */
4750 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4751 /* Make every class have alignment of at least one. */
4752 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4753
4754 /* Go through the virtual bases, allocating space for each virtual
4755 base that is not already a primary base class. */
4756 for (vbase = CLASSTYPE_VBASECLASSES (t);
4757 vbase;
4758 vbase = TREE_CHAIN (vbase))
4759 if (!BINFO_VBASE_PRIMARY_P (vbase))
4760 {
4761 /* This virtual base is not a primary base of any class in the
4762 hierarchy, so we have to add space for it. */
4763 tree basetype;
4764 unsigned int desired_align;
4765
4766 basetype = BINFO_TYPE (vbase);
4767 desired_align = TYPE_ALIGN (basetype);
4768 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4769
4770 /* Add padding so that we can put the virtual base class at an
4771 appropriately aligned offset. */
4772 dsize = CEIL (dsize, desired_align) * desired_align;
4773 /* And compute the offset of the virtual base. */
4774 propagate_binfo_offsets (vbase,
4775 size_int (CEIL (dsize, BITS_PER_UNIT)));
4776 /* Every virtual baseclass takes a least a UNIT, so that we can
4777 take it's address and get something different for each base. */
4778 dsize += MAX (BITS_PER_UNIT,
4779 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4780 }
4781
4782 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4783 BINFO_OFFSET set correctly. Those we just allocated certainly
4784 will. The others are primary baseclasses; we walk the hierarchy
4785 to find the primary copies and update the shared copy. */
4786 dfs_walk (TYPE_BINFO (t),
4787 dfs_set_offset_for_shared_vbases,
4788 dfs_unmarked_real_bases_queue_p,
4789 t);
4790
4791 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4792 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4793 bases and their direct and indirect bases. The ambiguity checks
4794 in get_base_distance depend on the BINFO_OFFSETs being set
4795 correctly. */
4796 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4797 for (vbase = CLASSTYPE_VBASECLASSES (t);
4798 vbase;
4799 vbase = TREE_CHAIN (vbase))
4800 dfs_walk (vbase, dfs_set_offset_for_unshared_vbases, NULL, t);
4801
4802 /* Now, make sure that the total size of the type is a multiple of
4803 its alignment. */
4804 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4805 TYPE_SIZE (t) = bitsize_int (dsize);
4806 TYPE_SIZE_UNIT (t) = convert (sizetype,
4807 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4808 bitsize_int (BITS_PER_UNIT)));
4809 }
4810
4811 /* Finish the work of layout_record, now taking virtual bases into account.
4812 Also compute the actual offsets that our base classes will have.
4813 This must be performed after the fields are laid out, since virtual
4814 baseclasses must lay down at the end of the record. */
4815
4816 static void
4817 layout_basetypes (rec)
4818 tree rec;
4819 {
4820 tree vbase_types;
4821
4822 #ifdef STRUCTURE_SIZE_BOUNDARY
4823 /* Packed structures don't need to have minimum size. */
4824 if (! TYPE_PACKED (rec))
4825 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4826 #endif
4827
4828 /* Remove the FIELD_DECLs we created for baseclasses in
4829 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4830 everything in the hierarcy except non-primary virtual bases. */
4831 remove_base_fields (rec);
4832
4833 /* Allocate the virtual base classes. */
4834 layout_virtual_bases (rec);
4835
4836 /* Get all the virtual base types that this type uses. The
4837 TREE_VALUE slot holds the virtual baseclass type. Note that
4838 get_vbase_types makes copies of the virtual base BINFOs, so that
4839 the vbase_types are unshared. */
4840 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4841 vbase_types = TREE_CHAIN (vbase_types))
4842 if (extra_warnings)
4843 {
4844 tree basetype = BINFO_TYPE (vbase_types);
4845 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4846 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4847 basetype, rec);
4848 }
4849 }
4850
4851 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4852 BINFO_OFFSETs for all of the base-classes. Position the vtable
4853 pointer. */
4854
4855 static void
4856 layout_class_type (t, empty_p, has_virtual_p,
4857 new_virtuals_p, overridden_virtuals_p)
4858 tree t;
4859 int *empty_p;
4860 int *has_virtual_p;
4861 tree *new_virtuals_p;
4862 tree *overridden_virtuals_p;
4863 {
4864 tree padding = NULL_TREE;
4865
4866 /* If possible, we reuse the virtual function table pointer from one
4867 of our base classes. */
4868 determine_primary_base (t, has_virtual_p);
4869
4870 /* Add pointers to all of our virtual base-classes. */
4871 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4872 TYPE_FIELDS (t));
4873 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4874 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4875 TYPE_FIELDS (t));
4876
4877 /* Create a pointer to our virtual function table. */
4878 create_vtable_ptr (t, empty_p, has_virtual_p,
4879 new_virtuals_p, overridden_virtuals_p);
4880
4881 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4882 we have to save this before we start modifying
4883 TYPE_NONCOPIED_PARTS. */
4884 fixup_inline_methods (t);
4885
4886 /* We make all structures have at least one element, so that they
4887 have non-zero size. The field that we add here is fake, in the
4888 sense that, for example, we don't want people to be able to
4889 initialize it later. So, we add it just long enough to let the
4890 back-end lay out the type, and then remove it. In the new ABI,
4891 the class may be empty even if it has basetypes. Therefore, we
4892 add the fake field at the end of the fields list; if there are
4893 already FIELD_DECLs on the list, their offsets will not be
4894 disturbed. */
4895 if (*empty_p)
4896 {
4897 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4898 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), padding);
4899 TYPE_NONCOPIED_PARTS (t)
4900 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4901 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4902 }
4903
4904 /* Let the back-end lay out the type. Note that at this point we
4905 have only included non-virtual base-classes; we will lay out the
4906 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4907 this call are not necessarily correct; they are just the size and
4908 alignment when no virtual base clases are used. */
4909 layout_type (t);
4910
4911 /* If we added an extra field to make this class non-empty, remove
4912 it now. */
4913 if (*empty_p)
4914 {
4915 tree *declp;
4916
4917 declp = &TYPE_FIELDS (t);
4918 while (*declp != padding)
4919 declp = &TREE_CHAIN (*declp);
4920 *declp = TREE_CHAIN (*declp);
4921 }
4922
4923 /* Delete all zero-width bit-fields from the list of fields. Now
4924 that the type is laid out they are no longer important. */
4925 remove_zero_width_bit_fields (t);
4926
4927 /* Remember the size and alignment of the class before adding
4928 the virtual bases. */
4929 if (*empty_p && flag_new_abi)
4930 {
4931 CLASSTYPE_SIZE (t) = bitsize_int (0);
4932 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4933 }
4934 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4935 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4936 {
4937 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4938 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4939 }
4940 else
4941 {
4942 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4943 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4944 }
4945
4946 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4947
4948 /* Set the TYPE_DECL for this type to contain the right
4949 value for DECL_OFFSET, so that we can use it as part
4950 of a COMPONENT_REF for multiple inheritance. */
4951 layout_decl (TYPE_MAIN_DECL (t), 0);
4952
4953 /* Now fix up any virtual base class types that we left lying
4954 around. We must get these done before we try to lay out the
4955 virtual function table. */
4956 if (CLASSTYPE_N_BASECLASSES (t))
4957 /* layout_basetypes will remove the base subobject fields. */
4958 layout_basetypes (t);
4959 }
4960
4961 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4962 (or C++ class declaration).
4963
4964 For C++, we must handle the building of derived classes.
4965 Also, C++ allows static class members. The way that this is
4966 handled is to keep the field name where it is (as the DECL_NAME
4967 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4968 of the field. layout_record and layout_union will know about this.
4969
4970 More C++ hair: inline functions have text in their
4971 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4972 meaningful tree structure. After the struct has been laid out, set
4973 things up so that this can happen.
4974
4975 And still more: virtual functions. In the case of single inheritance,
4976 when a new virtual function is seen which redefines a virtual function
4977 from the base class, the new virtual function is placed into
4978 the virtual function table at exactly the same address that
4979 it had in the base class. When this is extended to multiple
4980 inheritance, the same thing happens, except that multiple virtual
4981 function tables must be maintained. The first virtual function
4982 table is treated in exactly the same way as in the case of single
4983 inheritance. Additional virtual function tables have different
4984 DELTAs, which tell how to adjust `this' to point to the right thing.
4985
4986 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4987
4988 void
4989 finish_struct_1 (t)
4990 tree t;
4991 {
4992 tree x;
4993 int has_virtual;
4994 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4995 a FUNCTION_DECL. Each of these functions is a virtual function
4996 declared in T that does not override any virtual function from a
4997 base class. */
4998 tree new_virtuals = NULL_TREE;
4999 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5000 except that each declaration here overrides the declaration from
5001 a base class. */
5002 tree overridden_virtuals = NULL_TREE;
5003 int n_fields = 0;
5004 tree vfield;
5005 int empty = 1;
5006
5007 if (TYPE_SIZE (t))
5008 {
5009 if (IS_AGGR_TYPE (t))
5010 cp_error ("redefinition of `%#T'", t);
5011 else
5012 my_friendly_abort (172);
5013 popclass ();
5014 return;
5015 }
5016
5017 GNU_xref_decl (current_function_decl, t);
5018
5019 /* If this type was previously laid out as a forward reference,
5020 make sure we lay it out again. */
5021 TYPE_SIZE (t) = NULL_TREE;
5022 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5023 CLASSTYPE_VFIELD_PARENT (t) = -1;
5024 has_virtual = 0;
5025 CLASSTYPE_RTTI (t) = NULL_TREE;
5026
5027 /* Do end-of-class semantic processing: checking the validity of the
5028 bases and members and add implicitly generated methods. */
5029 check_bases_and_members (t, &empty);
5030
5031 /* Layout the class itself. */
5032 layout_class_type (t, &empty, &has_virtual,
5033 &new_virtuals, &overridden_virtuals);
5034
5035 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5036 might need to know it for setting up the offsets in the vtable
5037 (or in thunks) below. */
5038 vfield = TYPE_VFIELD (t);
5039 if (vfield != NULL_TREE
5040 && DECL_FIELD_CONTEXT (vfield) != t)
5041 {
5042 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5043 tree offset = convert (bitsizetype, BINFO_OFFSET (binfo));
5044
5045 vfield = copy_node (vfield);
5046 copy_lang_decl (vfield);
5047
5048 if (! integer_zerop (offset))
5049 offset = size_binop (MULT_EXPR, offset, bitsize_int (BITS_PER_UNIT));
5050
5051 DECL_FIELD_CONTEXT (vfield) = t;
5052 DECL_FIELD_BITPOS (vfield)
5053 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
5054 TYPE_VFIELD (t) = vfield;
5055 }
5056
5057 overridden_virtuals
5058 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
5059
5060 /* If necessary, create the primary vtable for this class. */
5061 if (new_virtuals
5062 || overridden_virtuals
5063 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5064 {
5065 new_virtuals = nreverse (new_virtuals);
5066 /* We must enter these virtuals into the table. */
5067 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5068 {
5069 if (! CLASSTYPE_COM_INTERFACE (t))
5070 {
5071 /* The second slot is for the tdesc pointer when thunks
5072 are used. */
5073 if (flag_vtable_thunks)
5074 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5075
5076 /* The first slot is for the rtti offset. */
5077 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5078
5079 set_rtti_entry (new_virtuals,
5080 convert (ssizetype, integer_zero_node), t);
5081 }
5082 build_primary_vtable (NULL_TREE, t);
5083 }
5084 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5085 /* Here we know enough to change the type of our virtual
5086 function table, but we will wait until later this function. */
5087 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5088
5089 /* If this type has basetypes with constructors, then those
5090 constructors might clobber the virtual function table. But
5091 they don't if the derived class shares the exact vtable of the base
5092 class. */
5093
5094 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5095 }
5096 /* If we didn't need a new vtable, see if we should copy one from
5097 the base. */
5098 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5099 {
5100 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5101
5102 /* This class contributes nothing new to the virtual function
5103 table. However, it may have declared functions which
5104 went into the virtual function table "inherited" from the
5105 base class. If so, we grab a copy of those updated functions,
5106 and pretend they are ours. */
5107
5108 /* See if we should steal the virtual info from base class. */
5109 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5110 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5111 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5112 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5113 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5114 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5115 }
5116
5117 if (TYPE_CONTAINS_VPTR_P (t))
5118 {
5119 if (TYPE_BINFO_VTABLE (t))
5120 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5121 20000116);
5122 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5123 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5124 20000116);
5125
5126 CLASSTYPE_VSIZE (t) = has_virtual;
5127 /* Entries for virtual functions defined in the primary base are
5128 followed by entries for new functions unique to this class. */
5129 TYPE_BINFO_VIRTUALS (t)
5130 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5131 /* Finally, add entries for functions that override virtuals
5132 from non-primary bases. */
5133 TYPE_BINFO_VIRTUALS (t)
5134 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5135 }
5136
5137 /* If we created a new vtbl pointer for this class, add it to the
5138 list. */
5139 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5140 CLASSTYPE_VFIELDS (t)
5141 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5142
5143 finish_struct_bits (t);
5144
5145 /* Complete the rtl for any static member objects of the type we're
5146 working on. */
5147 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5148 {
5149 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5150 && TREE_TYPE (x) == t)
5151 {
5152 DECL_MODE (x) = TYPE_MODE (t);
5153 make_decl_rtl (x, NULL, 0);
5154 }
5155 }
5156
5157 /* Done with FIELDS...now decide whether to sort these for
5158 faster lookups later.
5159
5160 The C front-end only does this when n_fields > 15. We use
5161 a smaller number because most searches fail (succeeding
5162 ultimately as the search bores through the inheritance
5163 hierarchy), and we want this failure to occur quickly. */
5164
5165 n_fields = count_fields (TYPE_FIELDS (t));
5166 if (n_fields > 7)
5167 {
5168 tree field_vec = make_tree_vec (n_fields);
5169 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5170 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5171 (int (*)(const void *, const void *))field_decl_cmp);
5172 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5173 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5174 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5175 }
5176
5177 if (TYPE_HAS_CONSTRUCTOR (t))
5178 {
5179 tree vfields = CLASSTYPE_VFIELDS (t);
5180
5181 while (vfields)
5182 {
5183 /* Mark the fact that constructor for T
5184 could affect anybody inheriting from T
5185 who wants to initialize vtables for VFIELDS's type. */
5186 if (VF_DERIVED_VALUE (vfields))
5187 TREE_ADDRESSABLE (vfields) = 1;
5188 vfields = TREE_CHAIN (vfields);
5189 }
5190 }
5191
5192 /* Make the rtl for any new vtables we have created, and unmark
5193 the base types we marked. */
5194 finish_vtbls (t);
5195
5196 if (CLASSTYPE_VSIZE (t) != 0)
5197 {
5198 /* In addition to this one, all the other vfields should be listed. */
5199 /* Before that can be done, we have to have FIELD_DECLs for them, and
5200 a place to find them. */
5201 TYPE_NONCOPIED_PARTS (t)
5202 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5203 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5204
5205 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5206 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5207 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5208 t);
5209 }
5210
5211 hack_incomplete_structures (t);
5212
5213 if (warn_overloaded_virtual)
5214 warn_hidden (t);
5215
5216 maybe_suppress_debug_info (t);
5217
5218 /* Finish debugging output for this type. */
5219 rest_of_type_compilation (t, toplevel_bindings_p ());
5220 }
5221
5222 /* When T was built up, the member declarations were added in reverse
5223 order. Rearrange them to declaration order. */
5224
5225 void
5226 unreverse_member_declarations (t)
5227 tree t;
5228 {
5229 tree next;
5230 tree prev;
5231 tree x;
5232
5233 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5234 reverse order. Put them in declaration order now. */
5235 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5236 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5237
5238 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5239 reverse order, so we can't just use nreverse. */
5240 prev = NULL_TREE;
5241 for (x = TYPE_FIELDS (t);
5242 x && TREE_CODE (x) != TYPE_DECL;
5243 x = next)
5244 {
5245 next = TREE_CHAIN (x);
5246 TREE_CHAIN (x) = prev;
5247 prev = x;
5248 }
5249 if (prev)
5250 {
5251 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5252 if (prev)
5253 TYPE_FIELDS (t) = prev;
5254 }
5255 }
5256
5257 tree
5258 finish_struct (t, attributes)
5259 tree t, attributes;
5260 {
5261 /* Now that we've got all the field declarations, reverse everything
5262 as necessary. */
5263 unreverse_member_declarations (t);
5264
5265 cplus_decl_attributes (t, attributes, NULL_TREE);
5266
5267 if (processing_template_decl)
5268 {
5269 finish_struct_methods (t);
5270 TYPE_SIZE (t) = integer_zero_node;
5271 }
5272 else
5273 finish_struct_1 (t);
5274
5275 TYPE_BEING_DEFINED (t) = 0;
5276
5277 if (current_class_type)
5278 popclass ();
5279 else
5280 error ("trying to finish struct, but kicked out due to previous parse errors.");
5281
5282 if (processing_template_decl)
5283 {
5284 tree scope = current_scope ();
5285 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5286 add_tree (build_min (TAG_DEFN, t));
5287 }
5288
5289 return t;
5290 }
5291 \f
5292 /* Return the dynamic type of INSTANCE, if known.
5293 Used to determine whether the virtual function table is needed
5294 or not.
5295
5296 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5297 of our knowledge of its type. *NONNULL should be initialized
5298 before this function is called. */
5299
5300 static tree
5301 fixed_type_or_null (instance, nonnull)
5302 tree instance;
5303 int *nonnull;
5304 {
5305 switch (TREE_CODE (instance))
5306 {
5307 case INDIRECT_REF:
5308 /* Check that we are not going through a cast of some sort. */
5309 if (TREE_TYPE (instance)
5310 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5311 instance = TREE_OPERAND (instance, 0);
5312 /* fall through... */
5313 case CALL_EXPR:
5314 /* This is a call to a constructor, hence it's never zero. */
5315 if (TREE_HAS_CONSTRUCTOR (instance))
5316 {
5317 if (nonnull)
5318 *nonnull = 1;
5319 return TREE_TYPE (instance);
5320 }
5321 return NULL_TREE;
5322
5323 case SAVE_EXPR:
5324 /* This is a call to a constructor, hence it's never zero. */
5325 if (TREE_HAS_CONSTRUCTOR (instance))
5326 {
5327 if (nonnull)
5328 *nonnull = 1;
5329 return TREE_TYPE (instance);
5330 }
5331 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5332
5333 case RTL_EXPR:
5334 return NULL_TREE;
5335
5336 case PLUS_EXPR:
5337 case MINUS_EXPR:
5338 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5339 /* Propagate nonnull. */
5340 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5341 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5342 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5343 return NULL_TREE;
5344
5345 case NOP_EXPR:
5346 case CONVERT_EXPR:
5347 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5348
5349 case ADDR_EXPR:
5350 if (nonnull)
5351 *nonnull = 1;
5352 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5353
5354 case COMPONENT_REF:
5355 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5356
5357 case VAR_DECL:
5358 case FIELD_DECL:
5359 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5360 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5361 {
5362 if (nonnull)
5363 *nonnull = 1;
5364 return TREE_TYPE (TREE_TYPE (instance));
5365 }
5366 /* fall through... */
5367 case TARGET_EXPR:
5368 case PARM_DECL:
5369 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5370 {
5371 if (nonnull)
5372 *nonnull = 1;
5373 return TREE_TYPE (instance);
5374 }
5375 else if (nonnull)
5376 {
5377 if (instance == current_class_ptr
5378 && flag_this_is_variable <= 0)
5379 {
5380 /* Normally, 'this' must be non-null. */
5381 if (flag_this_is_variable == 0)
5382 *nonnull = 1;
5383
5384 /* <0 means we're in a constructor and we know our type. */
5385 if (flag_this_is_variable < 0)
5386 return TREE_TYPE (TREE_TYPE (instance));
5387 }
5388 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5389 /* Reference variables should be references to objects. */
5390 *nonnull = 1;
5391 }
5392 return NULL_TREE;
5393
5394 default:
5395 return NULL_TREE;
5396 }
5397 }
5398
5399 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5400 to the static type. We also handle the case where INSTANCE is really
5401 a pointer.
5402
5403 Used to determine whether the virtual function table is needed
5404 or not.
5405
5406 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5407 of our knowledge of its type. *NONNULL should be initialized
5408 before this function is called. */
5409
5410 int
5411 resolves_to_fixed_type_p (instance, nonnull)
5412 tree instance;
5413 int *nonnull;
5414 {
5415 tree t = TREE_TYPE (instance);
5416 tree fixed = fixed_type_or_null (instance, nonnull);
5417 if (fixed == NULL_TREE)
5418 return 0;
5419 if (POINTER_TYPE_P (t))
5420 t = TREE_TYPE (t);
5421 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5422 }
5423
5424 \f
5425 void
5426 init_class_processing ()
5427 {
5428 current_class_depth = 0;
5429 current_class_stack_size = 10;
5430 current_class_stack
5431 = (class_stack_node_t) xmalloc (current_class_stack_size
5432 * sizeof (struct class_stack_node));
5433
5434 access_default_node = build_int_2 (0, 0);
5435 access_public_node = build_int_2 (1, 0);
5436 access_protected_node = build_int_2 (2, 0);
5437 access_private_node = build_int_2 (3, 0);
5438 access_default_virtual_node = build_int_2 (4, 0);
5439 access_public_virtual_node = build_int_2 (5, 0);
5440 access_protected_virtual_node = build_int_2 (6, 0);
5441 access_private_virtual_node = build_int_2 (7, 0);
5442 }
5443
5444 /* Set current scope to NAME. CODE tells us if this is a
5445 STRUCT, UNION, or ENUM environment.
5446
5447 NAME may end up being NULL_TREE if this is an anonymous or
5448 late-bound struct (as in "struct { ... } foo;") */
5449
5450 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5451 appropriate values, found by looking up the type definition of
5452 NAME (as a CODE).
5453
5454 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5455 which can be seen locally to the class. They are shadowed by
5456 any subsequent local declaration (including parameter names).
5457
5458 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5459 which have static meaning (i.e., static members, static
5460 member functions, enum declarations, etc).
5461
5462 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5463 which can be seen locally to the class (as in 1), but
5464 know that we are doing this for declaration purposes
5465 (i.e. friend foo::bar (int)).
5466
5467 So that we may avoid calls to lookup_name, we cache the _TYPE
5468 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5469
5470 For multiple inheritance, we perform a two-pass depth-first search
5471 of the type lattice. The first pass performs a pre-order search,
5472 marking types after the type has had its fields installed in
5473 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5474 unmarks the marked types. If a field or member function name
5475 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5476 that name becomes `error_mark_node'. */
5477
5478 void
5479 pushclass (type, modify)
5480 tree type;
5481 int modify;
5482 {
5483 type = TYPE_MAIN_VARIANT (type);
5484
5485 /* Make sure there is enough room for the new entry on the stack. */
5486 if (current_class_depth + 1 >= current_class_stack_size)
5487 {
5488 current_class_stack_size *= 2;
5489 current_class_stack
5490 = (class_stack_node_t) xrealloc (current_class_stack,
5491 current_class_stack_size
5492 * sizeof (struct class_stack_node));
5493 }
5494
5495 /* Insert a new entry on the class stack. */
5496 current_class_stack[current_class_depth].name = current_class_name;
5497 current_class_stack[current_class_depth].type = current_class_type;
5498 current_class_stack[current_class_depth].access = current_access_specifier;
5499 current_class_stack[current_class_depth].names_used = 0;
5500 current_class_depth++;
5501
5502 /* Now set up the new type. */
5503 current_class_name = TYPE_NAME (type);
5504 if (TREE_CODE (current_class_name) == TYPE_DECL)
5505 current_class_name = DECL_NAME (current_class_name);
5506 current_class_type = type;
5507
5508 /* By default, things in classes are private, while things in
5509 structures or unions are public. */
5510 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5511 ? access_private_node
5512 : access_public_node);
5513
5514 if (previous_class_type != NULL_TREE
5515 && (type != previous_class_type
5516 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5517 && current_class_depth == 1)
5518 {
5519 /* Forcibly remove any old class remnants. */
5520 invalidate_class_lookup_cache ();
5521 }
5522
5523 /* If we're about to enter a nested class, clear
5524 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5525 if (modify && current_class_depth > 1)
5526 clear_identifier_class_values ();
5527
5528 pushlevel_class ();
5529
5530 #if 0
5531 if (CLASSTYPE_TEMPLATE_INFO (type))
5532 overload_template_name (type);
5533 #endif
5534
5535 if (modify)
5536 {
5537 if (type != previous_class_type || current_class_depth > 1)
5538 push_class_decls (type);
5539 else
5540 {
5541 tree item;
5542
5543 /* We are re-entering the same class we just left, so we
5544 don't have to search the whole inheritance matrix to find
5545 all the decls to bind again. Instead, we install the
5546 cached class_shadowed list, and walk through it binding
5547 names and setting up IDENTIFIER_TYPE_VALUEs. */
5548 set_class_shadows (previous_class_values);
5549 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5550 {
5551 tree id = TREE_PURPOSE (item);
5552 tree decl = TREE_TYPE (item);
5553
5554 push_class_binding (id, decl);
5555 if (TREE_CODE (decl) == TYPE_DECL)
5556 set_identifier_type_value (id, TREE_TYPE (decl));
5557 }
5558 unuse_fields (type);
5559 }
5560
5561 storetags (CLASSTYPE_TAGS (type));
5562 }
5563 }
5564
5565 /* When we exit a toplevel class scope, we save the
5566 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5567 reenter the class. Here, we've entered some other class, so we
5568 must invalidate our cache. */
5569
5570 void
5571 invalidate_class_lookup_cache ()
5572 {
5573 tree t;
5574
5575 /* This code can be seen as a cache miss. When we've cached a
5576 class' scope's bindings and we can't use them, we need to reset
5577 them. This is it! */
5578 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5579 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5580
5581 previous_class_type = NULL_TREE;
5582 }
5583
5584 /* Get out of the current class scope. If we were in a class scope
5585 previously, that is the one popped to. */
5586
5587 void
5588 popclass ()
5589 {
5590 poplevel_class ();
5591 /* Since poplevel_class does the popping of class decls nowadays,
5592 this really only frees the obstack used for these decls. */
5593 pop_class_decls ();
5594
5595 current_class_depth--;
5596 current_class_name = current_class_stack[current_class_depth].name;
5597 current_class_type = current_class_stack[current_class_depth].type;
5598 current_access_specifier = current_class_stack[current_class_depth].access;
5599 if (current_class_stack[current_class_depth].names_used)
5600 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5601 }
5602
5603 /* Returns 1 if current_class_type is either T or a nested type of T.
5604 We start looking from 1 because entry 0 is from global scope, and has
5605 no type. */
5606
5607 int
5608 currently_open_class (t)
5609 tree t;
5610 {
5611 int i;
5612 if (t == current_class_type)
5613 return 1;
5614 for (i = 1; i < current_class_depth; ++i)
5615 if (current_class_stack [i].type == t)
5616 return 1;
5617 return 0;
5618 }
5619
5620 /* If either current_class_type or one of its enclosing classes are derived
5621 from T, return the appropriate type. Used to determine how we found
5622 something via unqualified lookup. */
5623
5624 tree
5625 currently_open_derived_class (t)
5626 tree t;
5627 {
5628 int i;
5629
5630 if (DERIVED_FROM_P (t, current_class_type))
5631 return current_class_type;
5632
5633 for (i = current_class_depth - 1; i > 0; --i)
5634 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5635 return current_class_stack[i].type;
5636
5637 return NULL_TREE;
5638 }
5639
5640 /* When entering a class scope, all enclosing class scopes' names with
5641 static meaning (static variables, static functions, types and enumerators)
5642 have to be visible. This recursive function calls pushclass for all
5643 enclosing class contexts until global or a local scope is reached.
5644 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5645 formal of the same name. */
5646
5647 void
5648 push_nested_class (type, modify)
5649 tree type;
5650 int modify;
5651 {
5652 tree context;
5653
5654 /* A namespace might be passed in error cases, like A::B:C. */
5655 if (type == NULL_TREE
5656 || type == error_mark_node
5657 || TREE_CODE (type) == NAMESPACE_DECL
5658 || ! IS_AGGR_TYPE (type)
5659 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5660 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5661 return;
5662
5663 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5664
5665 if (context && CLASS_TYPE_P (context))
5666 push_nested_class (context, 2);
5667 pushclass (type, modify);
5668 }
5669
5670 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5671
5672 void
5673 pop_nested_class ()
5674 {
5675 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5676
5677 popclass ();
5678 if (context && CLASS_TYPE_P (context))
5679 pop_nested_class ();
5680 }
5681
5682 /* Set global variables CURRENT_LANG_NAME to appropriate value
5683 so that behavior of name-mangling machinery is correct. */
5684
5685 void
5686 push_lang_context (name)
5687 tree name;
5688 {
5689 *current_lang_stack++ = current_lang_name;
5690 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5691 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5692 {
5693 size_t old_size = VARRAY_SIZE (current_lang_base);
5694
5695 VARRAY_GROW (current_lang_base, old_size + 10);
5696 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5697 }
5698
5699 if (name == lang_name_cplusplus)
5700 {
5701 strict_prototype = strict_prototypes_lang_cplusplus;
5702 current_lang_name = name;
5703 }
5704 else if (name == lang_name_java)
5705 {
5706 strict_prototype = strict_prototypes_lang_cplusplus;
5707 current_lang_name = name;
5708 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5709 (See record_builtin_java_type in decl.c.) However, that causes
5710 incorrect debug entries if these types are actually used.
5711 So we re-enable debug output after extern "Java". */
5712 DECL_IGNORED_P (java_byte_type_node) = 0;
5713 DECL_IGNORED_P (java_short_type_node) = 0;
5714 DECL_IGNORED_P (java_int_type_node) = 0;
5715 DECL_IGNORED_P (java_long_type_node) = 0;
5716 DECL_IGNORED_P (java_float_type_node) = 0;
5717 DECL_IGNORED_P (java_double_type_node) = 0;
5718 DECL_IGNORED_P (java_char_type_node) = 0;
5719 DECL_IGNORED_P (java_boolean_type_node) = 0;
5720 }
5721 else if (name == lang_name_c)
5722 {
5723 strict_prototype = strict_prototypes_lang_c;
5724 current_lang_name = name;
5725 }
5726 else
5727 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5728 }
5729
5730 /* Get out of the current language scope. */
5731
5732 void
5733 pop_lang_context ()
5734 {
5735 /* Clear the current entry so that garbage collector won't hold on
5736 to it. */
5737 *current_lang_stack = NULL_TREE;
5738 current_lang_name = *--current_lang_stack;
5739 if (current_lang_name == lang_name_cplusplus
5740 || current_lang_name == lang_name_java)
5741 strict_prototype = strict_prototypes_lang_cplusplus;
5742 else if (current_lang_name == lang_name_c)
5743 strict_prototype = strict_prototypes_lang_c;
5744 }
5745 \f
5746 /* Type instantiation routines. */
5747
5748 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5749 matches the TARGET_TYPE. If there is no satisfactory match, return
5750 error_mark_node, and issue an error message if COMPLAIN is
5751 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5752 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5753 template arguments. */
5754
5755 static tree
5756 resolve_address_of_overloaded_function (target_type,
5757 overload,
5758 complain,
5759 template_only,
5760 explicit_targs)
5761 tree target_type;
5762 tree overload;
5763 int complain;
5764 int template_only;
5765 tree explicit_targs;
5766 {
5767 /* Here's what the standard says:
5768
5769 [over.over]
5770
5771 If the name is a function template, template argument deduction
5772 is done, and if the argument deduction succeeds, the deduced
5773 arguments are used to generate a single template function, which
5774 is added to the set of overloaded functions considered.
5775
5776 Non-member functions and static member functions match targets of
5777 type "pointer-to-function" or "reference-to-function." Nonstatic
5778 member functions match targets of type "pointer-to-member
5779 function;" the function type of the pointer to member is used to
5780 select the member function from the set of overloaded member
5781 functions. If a nonstatic member function is selected, the
5782 reference to the overloaded function name is required to have the
5783 form of a pointer to member as described in 5.3.1.
5784
5785 If more than one function is selected, any template functions in
5786 the set are eliminated if the set also contains a non-template
5787 function, and any given template function is eliminated if the
5788 set contains a second template function that is more specialized
5789 than the first according to the partial ordering rules 14.5.5.2.
5790 After such eliminations, if any, there shall remain exactly one
5791 selected function. */
5792
5793 int is_ptrmem = 0;
5794 int is_reference = 0;
5795 /* We store the matches in a TREE_LIST rooted here. The functions
5796 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5797 interoperability with most_specialized_instantiation. */
5798 tree matches = NULL_TREE;
5799 tree fn;
5800
5801 /* By the time we get here, we should be seeing only real
5802 pointer-to-member types, not the internal POINTER_TYPE to
5803 METHOD_TYPE representation. */
5804 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5805 && (TREE_CODE (TREE_TYPE (target_type))
5806 == METHOD_TYPE)), 0);
5807
5808 /* Check that the TARGET_TYPE is reasonable. */
5809 if (TYPE_PTRFN_P (target_type))
5810 /* This is OK. */
5811 ;
5812 else if (TYPE_PTRMEMFUNC_P (target_type))
5813 /* This is OK, too. */
5814 is_ptrmem = 1;
5815 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5816 {
5817 /* This is OK, too. This comes from a conversion to reference
5818 type. */
5819 target_type = build_reference_type (target_type);
5820 is_reference = 1;
5821 }
5822 else
5823 {
5824 if (complain)
5825 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5826 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5827 return error_mark_node;
5828 }
5829
5830 /* If we can find a non-template function that matches, we can just
5831 use it. There's no point in generating template instantiations
5832 if we're just going to throw them out anyhow. But, of course, we
5833 can only do this when we don't *need* a template function. */
5834 if (!template_only)
5835 {
5836 tree fns;
5837
5838 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5839 {
5840 tree fn = OVL_FUNCTION (fns);
5841 tree fntype;
5842
5843 if (TREE_CODE (fn) == TEMPLATE_DECL)
5844 /* We're not looking for templates just yet. */
5845 continue;
5846
5847 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5848 != is_ptrmem)
5849 /* We're looking for a non-static member, and this isn't
5850 one, or vice versa. */
5851 continue;
5852
5853 /* See if there's a match. */
5854 fntype = TREE_TYPE (fn);
5855 if (is_ptrmem)
5856 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5857 else if (!is_reference)
5858 fntype = build_pointer_type (fntype);
5859
5860 if (can_convert_arg (target_type, fntype, fn))
5861 matches = tree_cons (fn, NULL_TREE, matches);
5862 }
5863 }
5864
5865 /* Now, if we've already got a match (or matches), there's no need
5866 to proceed to the template functions. But, if we don't have a
5867 match we need to look at them, too. */
5868 if (!matches)
5869 {
5870 tree target_fn_type;
5871 tree target_arg_types;
5872 tree target_ret_type;
5873 tree fns;
5874
5875 if (is_ptrmem)
5876 target_fn_type
5877 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5878 else
5879 target_fn_type = TREE_TYPE (target_type);
5880 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5881 target_ret_type = TREE_TYPE (target_fn_type);
5882
5883 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5884 {
5885 tree fn = OVL_FUNCTION (fns);
5886 tree instantiation;
5887 tree instantiation_type;
5888 tree targs;
5889
5890 if (TREE_CODE (fn) != TEMPLATE_DECL)
5891 /* We're only looking for templates. */
5892 continue;
5893
5894 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5895 != is_ptrmem)
5896 /* We're not looking for a non-static member, and this is
5897 one, or vice versa. */
5898 continue;
5899
5900 /* Try to do argument deduction. */
5901 targs = make_tree_vec (DECL_NTPARMS (fn));
5902 if (fn_type_unification (fn, explicit_targs, targs,
5903 target_arg_types, target_ret_type,
5904 DEDUCE_EXACT) != 0)
5905 /* Argument deduction failed. */
5906 continue;
5907
5908 /* Instantiate the template. */
5909 instantiation = instantiate_template (fn, targs);
5910 if (instantiation == error_mark_node)
5911 /* Instantiation failed. */
5912 continue;
5913
5914 /* See if there's a match. */
5915 instantiation_type = TREE_TYPE (instantiation);
5916 if (is_ptrmem)
5917 instantiation_type =
5918 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5919 else if (!is_reference)
5920 instantiation_type = build_pointer_type (instantiation_type);
5921 if (can_convert_arg (target_type, instantiation_type, instantiation))
5922 matches = tree_cons (instantiation, fn, matches);
5923 }
5924
5925 /* Now, remove all but the most specialized of the matches. */
5926 if (matches)
5927 {
5928 tree match = most_specialized_instantiation (matches,
5929 explicit_targs);
5930
5931 if (match != error_mark_node)
5932 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5933 }
5934 }
5935
5936 /* Now we should have exactly one function in MATCHES. */
5937 if (matches == NULL_TREE)
5938 {
5939 /* There were *no* matches. */
5940 if (complain)
5941 {
5942 cp_error ("no matches converting function `%D' to type `%#T'",
5943 DECL_NAME (OVL_FUNCTION (overload)),
5944 target_type);
5945
5946 /* print_candidates expects a chain with the functions in
5947 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5948 so why be clever?). */
5949 for (; overload; overload = OVL_NEXT (overload))
5950 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5951 matches);
5952
5953 print_candidates (matches);
5954 }
5955 return error_mark_node;
5956 }
5957 else if (TREE_CHAIN (matches))
5958 {
5959 /* There were too many matches. */
5960
5961 if (complain)
5962 {
5963 tree match;
5964
5965 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5966 DECL_NAME (OVL_FUNCTION (overload)),
5967 target_type);
5968
5969 /* Since print_candidates expects the functions in the
5970 TREE_VALUE slot, we flip them here. */
5971 for (match = matches; match; match = TREE_CHAIN (match))
5972 TREE_VALUE (match) = TREE_PURPOSE (match);
5973
5974 print_candidates (matches);
5975 }
5976
5977 return error_mark_node;
5978 }
5979
5980 /* Good, exactly one match. Now, convert it to the correct type. */
5981 fn = TREE_PURPOSE (matches);
5982
5983 mark_used (fn);
5984
5985 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5986 return build_unary_op (ADDR_EXPR, fn, 0);
5987 else
5988 {
5989 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5990 will mark the function as addressed, but here we must do it
5991 explicitly. */
5992 mark_addressable (fn);
5993
5994 return fn;
5995 }
5996 }
5997
5998 /* This function will instantiate the type of the expression given in
5999 RHS to match the type of LHSTYPE. If errors exist, then return
6000 error_mark_node. We only complain is COMPLAIN is set. If we are
6001 not complaining, never modify rhs, as overload resolution wants to
6002 try many possible instantiations, in hopes that at least one will
6003 work.
6004
6005 FLAGS is a bitmask, as we see at the top of the function.
6006
6007 For non-recursive calls, LHSTYPE should be a function, pointer to
6008 function, or a pointer to member function. */
6009
6010 tree
6011 instantiate_type (lhstype, rhs, flags)
6012 tree lhstype, rhs;
6013 int flags;
6014 {
6015 int complain = (flags & 1);
6016 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6017
6018 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6019 {
6020 if (complain)
6021 error ("not enough type information");
6022 return error_mark_node;
6023 }
6024
6025 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6026 {
6027 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6028 return rhs;
6029 if (complain)
6030 cp_error ("argument of type `%T' does not match `%T'",
6031 TREE_TYPE (rhs), lhstype);
6032 return error_mark_node;
6033 }
6034
6035 /* We don't overwrite rhs if it is an overloaded function.
6036 Copying it would destroy the tree link. */
6037 if (TREE_CODE (rhs) != OVERLOAD)
6038 rhs = copy_node (rhs);
6039
6040 /* This should really only be used when attempting to distinguish
6041 what sort of a pointer to function we have. For now, any
6042 arithmetic operation which is not supported on pointers
6043 is rejected as an error. */
6044
6045 switch (TREE_CODE (rhs))
6046 {
6047 case TYPE_EXPR:
6048 case CONVERT_EXPR:
6049 case SAVE_EXPR:
6050 case CONSTRUCTOR:
6051 case BUFFER_REF:
6052 my_friendly_abort (177);
6053 return error_mark_node;
6054
6055 case INDIRECT_REF:
6056 case ARRAY_REF:
6057 {
6058 tree new_rhs;
6059
6060 new_rhs = instantiate_type (build_pointer_type (lhstype),
6061 TREE_OPERAND (rhs, 0), flags);
6062 if (new_rhs == error_mark_node)
6063 return error_mark_node;
6064
6065 TREE_TYPE (rhs) = lhstype;
6066 TREE_OPERAND (rhs, 0) = new_rhs;
6067 return rhs;
6068 }
6069
6070 case NOP_EXPR:
6071 rhs = copy_node (TREE_OPERAND (rhs, 0));
6072 TREE_TYPE (rhs) = unknown_type_node;
6073 return instantiate_type (lhstype, rhs, flags);
6074
6075 case COMPONENT_REF:
6076 {
6077 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6078
6079 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6080 && complain && !flag_ms_extensions)
6081 {
6082 /* Note: we check this after the recursive call to avoid
6083 complaining about cases where overload resolution fails. */
6084
6085 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6086 tree fn = PTRMEM_CST_MEMBER (r);
6087
6088 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6089
6090 cp_pedwarn
6091 ("object-dependent reference to `%E' can only be used in a call",
6092 DECL_NAME (fn));
6093 cp_pedwarn
6094 (" to form a pointer to member function, say `&%T::%E'",
6095 t, DECL_NAME (fn));
6096 }
6097
6098 return r;
6099 }
6100
6101 case OFFSET_REF:
6102 rhs = TREE_OPERAND (rhs, 1);
6103 if (BASELINK_P (rhs))
6104 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6105
6106 /* This can happen if we are forming a pointer-to-member for a
6107 member template. */
6108 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6109
6110 /* Fall through. */
6111
6112 case TEMPLATE_ID_EXPR:
6113 return
6114 resolve_address_of_overloaded_function (lhstype,
6115 TREE_OPERAND (rhs, 0),
6116 complain,
6117 /*template_only=*/1,
6118 TREE_OPERAND (rhs, 1));
6119
6120 case OVERLOAD:
6121 return
6122 resolve_address_of_overloaded_function (lhstype,
6123 rhs,
6124 complain,
6125 /*template_only=*/0,
6126 /*explicit_targs=*/NULL_TREE);
6127
6128 case TREE_LIST:
6129 /* Now we should have a baselink. */
6130 my_friendly_assert (BASELINK_P (rhs), 990412);
6131
6132 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6133
6134 case CALL_EXPR:
6135 /* This is too hard for now. */
6136 my_friendly_abort (183);
6137 return error_mark_node;
6138
6139 case PLUS_EXPR:
6140 case MINUS_EXPR:
6141 case COMPOUND_EXPR:
6142 TREE_OPERAND (rhs, 0)
6143 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6144 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6145 return error_mark_node;
6146 TREE_OPERAND (rhs, 1)
6147 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6148 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6149 return error_mark_node;
6150
6151 TREE_TYPE (rhs) = lhstype;
6152 return rhs;
6153
6154 case MULT_EXPR:
6155 case TRUNC_DIV_EXPR:
6156 case FLOOR_DIV_EXPR:
6157 case CEIL_DIV_EXPR:
6158 case ROUND_DIV_EXPR:
6159 case RDIV_EXPR:
6160 case TRUNC_MOD_EXPR:
6161 case FLOOR_MOD_EXPR:
6162 case CEIL_MOD_EXPR:
6163 case ROUND_MOD_EXPR:
6164 case FIX_ROUND_EXPR:
6165 case FIX_FLOOR_EXPR:
6166 case FIX_CEIL_EXPR:
6167 case FIX_TRUNC_EXPR:
6168 case FLOAT_EXPR:
6169 case NEGATE_EXPR:
6170 case ABS_EXPR:
6171 case MAX_EXPR:
6172 case MIN_EXPR:
6173 case FFS_EXPR:
6174
6175 case BIT_AND_EXPR:
6176 case BIT_IOR_EXPR:
6177 case BIT_XOR_EXPR:
6178 case LSHIFT_EXPR:
6179 case RSHIFT_EXPR:
6180 case LROTATE_EXPR:
6181 case RROTATE_EXPR:
6182
6183 case PREINCREMENT_EXPR:
6184 case PREDECREMENT_EXPR:
6185 case POSTINCREMENT_EXPR:
6186 case POSTDECREMENT_EXPR:
6187 if (complain)
6188 error ("invalid operation on uninstantiated type");
6189 return error_mark_node;
6190
6191 case TRUTH_AND_EXPR:
6192 case TRUTH_OR_EXPR:
6193 case TRUTH_XOR_EXPR:
6194 case LT_EXPR:
6195 case LE_EXPR:
6196 case GT_EXPR:
6197 case GE_EXPR:
6198 case EQ_EXPR:
6199 case NE_EXPR:
6200 case TRUTH_ANDIF_EXPR:
6201 case TRUTH_ORIF_EXPR:
6202 case TRUTH_NOT_EXPR:
6203 if (complain)
6204 error ("not enough type information");
6205 return error_mark_node;
6206
6207 case COND_EXPR:
6208 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6209 {
6210 if (complain)
6211 error ("not enough type information");
6212 return error_mark_node;
6213 }
6214 TREE_OPERAND (rhs, 1)
6215 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6216 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6217 return error_mark_node;
6218 TREE_OPERAND (rhs, 2)
6219 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6220 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6221 return error_mark_node;
6222
6223 TREE_TYPE (rhs) = lhstype;
6224 return rhs;
6225
6226 case MODIFY_EXPR:
6227 TREE_OPERAND (rhs, 1)
6228 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6229 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6230 return error_mark_node;
6231
6232 TREE_TYPE (rhs) = lhstype;
6233 return rhs;
6234
6235 case ADDR_EXPR:
6236 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6237
6238 case ENTRY_VALUE_EXPR:
6239 my_friendly_abort (184);
6240 return error_mark_node;
6241
6242 case ERROR_MARK:
6243 return error_mark_node;
6244
6245 default:
6246 my_friendly_abort (185);
6247 return error_mark_node;
6248 }
6249 }
6250 \f
6251 /* Return the name of the virtual function pointer field
6252 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6253 this may have to look back through base types to find the
6254 ultimate field name. (For single inheritance, these could
6255 all be the same name. Who knows for multiple inheritance). */
6256
6257 static tree
6258 get_vfield_name (type)
6259 tree type;
6260 {
6261 tree binfo = TYPE_BINFO (type);
6262 char *buf;
6263
6264 while (BINFO_BASETYPES (binfo)
6265 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6266 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6267 binfo = BINFO_BASETYPE (binfo, 0);
6268
6269 type = BINFO_TYPE (binfo);
6270 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6271 + TYPE_NAME_LENGTH (type) + 2);
6272 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6273 return get_identifier (buf);
6274 }
6275
6276 void
6277 print_class_statistics ()
6278 {
6279 #ifdef GATHER_STATISTICS
6280 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6281 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6282 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6283 n_build_method_call, n_inner_fields_searched);
6284 if (n_vtables)
6285 {
6286 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6287 n_vtables, n_vtable_searches);
6288 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6289 n_vtable_entries, n_vtable_elems);
6290 }
6291 #endif
6292 }
6293
6294 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6295 according to [class]:
6296 The class-name is also inserted
6297 into the scope of the class itself. For purposes of access checking,
6298 the inserted class name is treated as if it were a public member name. */
6299
6300 void
6301 build_self_reference ()
6302 {
6303 tree name = constructor_name (current_class_type);
6304 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6305 tree saved_cas;
6306
6307 DECL_NONLOCAL (value) = 1;
6308 DECL_CONTEXT (value) = current_class_type;
6309 DECL_ARTIFICIAL (value) = 1;
6310
6311 if (processing_template_decl)
6312 value = push_template_decl (value);
6313
6314 saved_cas = current_access_specifier;
6315 current_access_specifier = access_public_node;
6316 finish_member_declaration (value);
6317 current_access_specifier = saved_cas;
6318 }
6319
6320 /* Returns 1 if TYPE contains only padding bytes. */
6321
6322 int
6323 is_empty_class (type)
6324 tree type;
6325 {
6326 tree t;
6327
6328 if (type == error_mark_node)
6329 return 0;
6330
6331 if (! IS_AGGR_TYPE (type))
6332 return 0;
6333
6334 if (flag_new_abi)
6335 return integer_zerop (CLASSTYPE_SIZE (type));
6336
6337 if (TYPE_BINFO_BASETYPES (type))
6338 return 0;
6339 t = TYPE_FIELDS (type);
6340 while (t && TREE_CODE (t) != FIELD_DECL)
6341 t = TREE_CHAIN (t);
6342 return (t == NULL_TREE);
6343 }
6344
6345 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6346 a *_TYPE node. NODE can also be a local class. */
6347
6348 tree
6349 get_enclosing_class (type)
6350 tree type;
6351 {
6352 tree node = type;
6353
6354 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6355 {
6356 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6357 {
6358 case 'd':
6359 node = DECL_CONTEXT (node);
6360 break;
6361
6362 case 't':
6363 if (node != type)
6364 return node;
6365 node = TYPE_CONTEXT (node);
6366 break;
6367
6368 default:
6369 my_friendly_abort (0);
6370 }
6371 }
6372 return NULL_TREE;
6373 }
6374
6375 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6376
6377 int
6378 is_base_of_enclosing_class (base, type)
6379 tree base, type;
6380 {
6381 while (type)
6382 {
6383 if (get_binfo (base, type, 0))
6384 return 1;
6385
6386 type = get_enclosing_class (type);
6387 }
6388 return 0;
6389 }
6390
6391 /* Note that NAME was looked up while the current class was being
6392 defined and that the result of that lookup was DECL. */
6393
6394 void
6395 maybe_note_name_used_in_class (name, decl)
6396 tree name;
6397 tree decl;
6398 {
6399 splay_tree names_used;
6400
6401 /* If we're not defining a class, there's nothing to do. */
6402 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6403 return;
6404
6405 /* If there's already a binding for this NAME, then we don't have
6406 anything to worry about. */
6407 if (IDENTIFIER_CLASS_VALUE (name))
6408 return;
6409
6410 if (!current_class_stack[current_class_depth - 1].names_used)
6411 current_class_stack[current_class_depth - 1].names_used
6412 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6413 names_used = current_class_stack[current_class_depth - 1].names_used;
6414
6415 splay_tree_insert (names_used,
6416 (splay_tree_key) name,
6417 (splay_tree_value) decl);
6418 }
6419
6420 /* Note that NAME was declared (as DECL) in the current class. Check
6421 to see that the declaration is legal. */
6422
6423 void
6424 note_name_declared_in_class (name, decl)
6425 tree name;
6426 tree decl;
6427 {
6428 splay_tree names_used;
6429 splay_tree_node n;
6430
6431 /* Look to see if we ever used this name. */
6432 names_used
6433 = current_class_stack[current_class_depth - 1].names_used;
6434 if (!names_used)
6435 return;
6436
6437 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6438 if (n)
6439 {
6440 /* [basic.scope.class]
6441
6442 A name N used in a class S shall refer to the same declaration
6443 in its context and when re-evaluated in the completed scope of
6444 S. */
6445 cp_error ("declaration of `%#D'", decl);
6446 cp_error_at ("changes meaning of `%s' from `%+#D'",
6447 IDENTIFIER_POINTER (DECL_NAME (decl)),
6448 (tree) n->value);
6449 }
6450 }
6451
6452 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6453 INDENT should be zero when called from the top level; it is
6454 incremented recursively. */
6455
6456 void
6457 dump_class_hierarchy (binfo, indent)
6458 tree binfo;
6459 int indent;
6460 {
6461 int i;
6462
6463 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6464 (unsigned long) binfo,
6465 type_as_string (binfo, TS_PLAIN));
6466 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6467 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6468 fprintf (stderr, " %s\n",
6469 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6470
6471 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6472 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);
6473 }
This page took 0.362042 seconds and 4 git commands to generate.