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