]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Functions related to building classes and their related objects. |
956d6950 | 2 | Copyright (C) 1987, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. |
8d08fdba MS |
3 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
21 | |
22 | ||
e92cc029 | 23 | /* High-level class interface. */ |
8d08fdba MS |
24 | |
25 | #include "config.h" | |
26 | #include "tree.h" | |
27 | #include <stdio.h> | |
28 | #include "cp-tree.h" | |
29 | #include "flags.h" | |
28cbf42c | 30 | #include "rtl.h" |
e8abc66f | 31 | #include "output.h" |
8d08fdba MS |
32 | |
33 | #include "obstack.h" | |
34 | #define obstack_chunk_alloc xmalloc | |
35 | #define obstack_chunk_free free | |
36 | ||
37 | extern struct obstack permanent_obstack; | |
38 | ||
39 | /* This is how we tell when two virtual member functions are really the | |
e92cc029 | 40 | same. */ |
8d08fdba MS |
41 | #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL)) |
42 | ||
43 | extern void set_class_shadows PROTO ((tree)); | |
44 | ||
45 | /* Way of stacking class types. */ | |
46 | static tree *current_class_base, *current_class_stack; | |
47 | static int current_class_stacksize; | |
48 | int current_class_depth; | |
49 | ||
50 | struct class_level | |
51 | { | |
52 | /* The previous class level. */ | |
53 | struct class_level *level_chain; | |
54 | ||
55 | /* The class instance variable, as a PARM_DECL. */ | |
56 | tree decl; | |
57 | /* The class instance variable, as an object. */ | |
58 | tree object; | |
59 | /* The virtual function table pointer | |
60 | for the class instance variable. */ | |
61 | tree vtable_decl; | |
62 | ||
63 | /* Name of the current class. */ | |
64 | tree name; | |
65 | /* Type of the current class. */ | |
66 | tree type; | |
67 | ||
68 | /* Flags for this class level. */ | |
69 | int this_is_variable; | |
70 | int memoized_lookups; | |
71 | int save_memoized; | |
72 | int unused; | |
73 | }; | |
74 | ||
956d6950 | 75 | /* The current_class_ptr is the pointer to the current class. |
4ac14744 MS |
76 | current_class_ref is the actual current class. */ |
77 | tree current_class_ptr, current_class_ref; | |
8d08fdba MS |
78 | |
79 | /* The following two can be derived from the previous one */ | |
80 | tree current_class_name; /* IDENTIFIER_NODE: name of current class */ | |
81 | tree current_class_type; /* _TYPE: the type of the current class */ | |
82 | tree previous_class_type; /* _TYPE: the previous type that was a class */ | |
83 | tree previous_class_values; /* TREE_LIST: copy of the class_shadowed list | |
84 | when leaving an outermost class scope. */ | |
49c249e1 JM |
85 | |
86 | struct base_info; | |
87 | ||
8d08fdba | 88 | static tree get_vfield_name PROTO((tree)); |
49c249e1 JM |
89 | static void finish_struct_anon PROTO((tree)); |
90 | static tree build_vbase_pointer PROTO((tree, tree)); | |
91 | static int complete_type_p PROTO((tree)); | |
92 | static int typecode_p PROTO((tree, enum tree_code)); | |
93 | static tree build_vtable_entry PROTO((tree, tree)); | |
94 | static tree get_vtable_name PROTO((tree)); | |
95 | static tree get_derived_offset PROTO((tree, tree)); | |
96 | static tree get_basefndecls PROTO((tree, tree)); | |
97 | static void set_rtti_entry PROTO((tree, tree, tree)); | |
98 | static tree build_vtable PROTO((tree, tree)); | |
49c249e1 JM |
99 | static void prepare_fresh_vtable PROTO((tree, tree)); |
100 | static void fixup_vtable_deltas1 PROTO((tree, tree)); | |
101 | static void fixup_vtable_deltas PROTO((tree, int, tree)); | |
102 | static void grow_method PROTO((tree, tree *)); | |
103 | static void finish_vtbls PROTO((tree, int, tree)); | |
104 | static void modify_vtable_entry PROTO((tree, tree, tree)); | |
105 | static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT)); | |
aa598818 | 106 | static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree)); |
49c249e1 JM |
107 | static tree delete_duplicate_fields_1 PROTO((tree, tree)); |
108 | static void delete_duplicate_fields PROTO((tree)); | |
109 | static void finish_struct_bits PROTO((tree, int)); | |
110 | static int alter_access PROTO((tree, tree, tree)); | |
111 | static int overrides PROTO((tree, tree)); | |
112 | static int strictly_overrides PROTO((tree, tree)); | |
113 | static void merge_overrides PROTO((tree, tree, int, tree)); | |
114 | static void override_one_vtable PROTO((tree, tree, tree)); | |
115 | static void mark_overriders PROTO((tree, tree)); | |
116 | static void check_for_override PROTO((tree, tree)); | |
117 | static tree maybe_fixup_vptrs PROTO((tree, tree, tree)); | |
118 | static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree)); | |
119 | static tree get_class_offset PROTO((tree, tree, tree, tree)); | |
120 | static void modify_one_vtable PROTO((tree, tree, tree, tree)); | |
121 | static void modify_all_vtables PROTO((tree, tree, tree)); | |
122 | static void modify_all_direct_vtables PROTO((tree, int, tree, tree, | |
123 | tree)); | |
124 | static void modify_all_indirect_vtables PROTO((tree, int, int, tree, | |
125 | tree, tree)); | |
126 | static void build_class_init_list PROTO((tree)); | |
127 | static int finish_base_struct PROTO((tree, struct base_info *, tree)); | |
8d08fdba MS |
128 | |
129 | /* Way of stacking language names. */ | |
130 | tree *current_lang_base, *current_lang_stack; | |
51c184be | 131 | int current_lang_stacksize; |
8d08fdba MS |
132 | |
133 | /* Names of languages we recognize. */ | |
134 | tree lang_name_c, lang_name_cplusplus; | |
135 | tree current_lang_name; | |
136 | ||
137 | /* When layout out an aggregate type, the size of the | |
138 | basetypes (virtual and non-virtual) is passed to layout_record | |
139 | via this node. */ | |
140 | static tree base_layout_decl; | |
141 | ||
be99da77 MS |
142 | /* Constants used for access control. */ |
143 | tree access_default_node; /* 0 */ | |
144 | tree access_public_node; /* 1 */ | |
145 | tree access_protected_node; /* 2 */ | |
146 | tree access_private_node; /* 3 */ | |
147 | tree access_default_virtual_node; /* 4 */ | |
148 | tree access_public_virtual_node; /* 5 */ | |
d8b55a76 JM |
149 | tree access_protected_virtual_node; /* 6 */ |
150 | tree access_private_virtual_node; /* 7 */ | |
be99da77 | 151 | |
51c184be | 152 | /* Variables shared between class.c and call.c. */ |
8d08fdba | 153 | |
5566b478 | 154 | #ifdef GATHER_STATISTICS |
8d08fdba MS |
155 | int n_vtables = 0; |
156 | int n_vtable_entries = 0; | |
157 | int n_vtable_searches = 0; | |
158 | int n_vtable_elems = 0; | |
159 | int n_convert_harshness = 0; | |
160 | int n_compute_conversion_costs = 0; | |
161 | int n_build_method_call = 0; | |
162 | int n_inner_fields_searched = 0; | |
5566b478 | 163 | #endif |
8d08fdba MS |
164 | |
165 | /* Virtual baseclass things. */ | |
e92cc029 | 166 | |
bd6dd845 | 167 | static tree |
8d08fdba MS |
168 | build_vbase_pointer (exp, type) |
169 | tree exp, type; | |
170 | { | |
171 | char *name; | |
172 | ||
173 | name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1); | |
174 | sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type)); | |
4dabb379 | 175 | return build_component_ref (exp, get_identifier (name), NULL_TREE, 0); |
8d08fdba MS |
176 | } |
177 | ||
178 | /* Is the type of the EXPR, the complete type of the object? | |
e92cc029 MS |
179 | If we are going to be wrong, we must be conservative, and return 0. */ |
180 | ||
bd6dd845 | 181 | static int |
8d08fdba MS |
182 | complete_type_p (expr) |
183 | tree expr; | |
184 | { | |
185 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); | |
186 | while (1) | |
187 | { | |
188 | switch (TREE_CODE (expr)) | |
189 | { | |
190 | case SAVE_EXPR: | |
191 | case INDIRECT_REF: | |
192 | case ADDR_EXPR: | |
193 | case NOP_EXPR: | |
194 | case CONVERT_EXPR: | |
195 | expr = TREE_OPERAND (expr, 0); | |
196 | continue; | |
197 | ||
198 | case CALL_EXPR: | |
199 | if (! TREE_HAS_CONSTRUCTOR (expr)) | |
200 | break; | |
e92cc029 | 201 | /* fall through... */ |
8d08fdba MS |
202 | case VAR_DECL: |
203 | case FIELD_DECL: | |
204 | if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE | |
205 | && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr))) | |
206 | && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type) | |
207 | return 1; | |
e92cc029 | 208 | /* fall through... */ |
8d08fdba MS |
209 | case TARGET_EXPR: |
210 | case PARM_DECL: | |
211 | if (IS_AGGR_TYPE (TREE_TYPE (expr)) | |
212 | && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type) | |
213 | return 1; | |
e92cc029 | 214 | /* fall through... */ |
8d08fdba MS |
215 | case PLUS_EXPR: |
216 | default: | |
217 | break; | |
218 | } | |
219 | break; | |
220 | } | |
221 | return 0; | |
222 | } | |
223 | ||
224 | /* Build multi-level access to EXPR using hierarchy path PATH. | |
225 | CODE is PLUS_EXPR if we are going with the grain, | |
226 | and MINUS_EXPR if we are not (in which case, we cannot traverse | |
227 | virtual baseclass links). | |
228 | ||
229 | TYPE is the type we want this path to have on exit. | |
230 | ||
231 | ALIAS_THIS is non-zero if EXPR in an expression involving `this'. */ | |
e92cc029 | 232 | |
8d08fdba MS |
233 | tree |
234 | build_vbase_path (code, type, expr, path, alias_this) | |
235 | enum tree_code code; | |
236 | tree type, expr, path; | |
237 | int alias_this; | |
238 | { | |
239 | register int changed = 0; | |
240 | tree last = NULL_TREE, last_virtual = NULL_TREE; | |
241 | int nonnull = 0; | |
6633d636 | 242 | int fixed_type_p; |
8d08fdba MS |
243 | tree null_expr = 0, nonnull_expr; |
244 | tree basetype; | |
245 | tree offset = integer_zero_node; | |
246 | ||
6633d636 MS |
247 | if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE) |
248 | return build1 (NOP_EXPR, type, expr); | |
249 | ||
f30432d7 MS |
250 | if (nonnull == 0 && (alias_this && flag_this_is_variable <= 0)) |
251 | nonnull = 1; | |
252 | ||
6633d636 | 253 | #if 0 |
8d08fdba MS |
254 | /* We need additional logic to convert back to the unconverted type |
255 | (the static type of the complete object), and then convert back | |
256 | to the type we want. Until that is done, or until we can | |
257 | recognize when that is, we cannot do the short cut logic. (mrs) */ | |
6633d636 MS |
258 | fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull); |
259 | #else | |
ddd5a7c1 | 260 | /* Do this, until we can undo any previous conversions. See net35.C |
e92cc029 | 261 | for a testcase. */ |
8d08fdba | 262 | fixed_type_p = complete_type_p (expr); |
6633d636 | 263 | #endif |
8d08fdba MS |
264 | |
265 | if (!fixed_type_p && TREE_SIDE_EFFECTS (expr)) | |
266 | expr = save_expr (expr); | |
267 | nonnull_expr = expr; | |
268 | ||
269 | if (BINFO_INHERITANCE_CHAIN (path)) | |
270 | { | |
271 | tree reverse_path = NULL_TREE; | |
272 | ||
e66d884e | 273 | push_expression_obstack (); |
8d08fdba MS |
274 | while (path) |
275 | { | |
276 | tree r = copy_node (path); | |
277 | BINFO_INHERITANCE_CHAIN (r) = reverse_path; | |
278 | reverse_path = r; | |
279 | path = BINFO_INHERITANCE_CHAIN (path); | |
280 | } | |
281 | path = reverse_path; | |
e66d884e | 282 | pop_obstacks (); |
8d08fdba MS |
283 | } |
284 | ||
285 | basetype = BINFO_TYPE (path); | |
286 | ||
287 | while (path) | |
288 | { | |
289 | if (TREE_VIA_VIRTUAL (path)) | |
290 | { | |
291 | last_virtual = BINFO_TYPE (path); | |
292 | if (code == PLUS_EXPR) | |
293 | { | |
294 | changed = ! fixed_type_p; | |
295 | ||
296 | if (changed) | |
297 | { | |
8d08fdba MS |
298 | tree ind; |
299 | ||
300 | /* We already check for ambiguous things in the caller, just | |
e92cc029 | 301 | find a path. */ |
8d08fdba MS |
302 | if (last) |
303 | { | |
304 | tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0); | |
305 | nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr); | |
306 | } | |
307 | ind = build_indirect_ref (nonnull_expr, NULL_PTR); | |
308 | nonnull_expr = build_vbase_pointer (ind, last_virtual); | |
a9aedbc2 MS |
309 | if (nonnull == 0 |
310 | && (TREE_CODE (type) == POINTER_TYPE | |
311 | || !flag_assume_nonnull_objects) | |
8d08fdba MS |
312 | && null_expr == NULL_TREE) |
313 | { | |
f30432d7 MS |
314 | null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node); |
315 | expr = build (COND_EXPR, build_pointer_type (last_virtual), | |
b7484fbe | 316 | build (EQ_EXPR, boolean_type_node, expr, |
8d08fdba MS |
317 | integer_zero_node), |
318 | null_expr, nonnull_expr); | |
319 | } | |
320 | } | |
321 | /* else we'll figure out the offset below. */ | |
322 | ||
323 | /* Happens in the case of parse errors. */ | |
324 | if (nonnull_expr == error_mark_node) | |
325 | return error_mark_node; | |
326 | } | |
327 | else | |
328 | { | |
329 | cp_error ("cannot cast up from virtual baseclass `%T'", | |
330 | last_virtual); | |
331 | return error_mark_node; | |
332 | } | |
333 | } | |
334 | last = path; | |
335 | path = BINFO_INHERITANCE_CHAIN (path); | |
336 | } | |
337 | /* LAST is now the last basetype assoc on the path. */ | |
338 | ||
339 | /* A pointer to a virtual base member of a non-null object | |
340 | is non-null. Therefore, we only need to test for zeroness once. | |
341 | Make EXPR the canonical expression to deal with here. */ | |
342 | if (null_expr) | |
343 | { | |
344 | TREE_OPERAND (expr, 2) = nonnull_expr; | |
b9ddcfac JM |
345 | TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1)) |
346 | = TREE_TYPE (nonnull_expr); | |
8d08fdba MS |
347 | } |
348 | else | |
349 | expr = nonnull_expr; | |
350 | ||
351 | /* If we go through any virtual base pointers, make sure that | |
352 | casts to BASETYPE from the last virtual base class use | |
353 | the right value for BASETYPE. */ | |
354 | if (changed) | |
355 | { | |
356 | tree intype = TREE_TYPE (TREE_TYPE (expr)); | |
f30432d7 | 357 | if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last)) |
8d08fdba MS |
358 | { |
359 | tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0); | |
8d08fdba MS |
360 | offset = BINFO_OFFSET (binfo); |
361 | } | |
362 | } | |
363 | else | |
364 | { | |
365 | if (last_virtual) | |
366 | { | |
367 | offset = BINFO_OFFSET (binfo_member (last_virtual, | |
368 | CLASSTYPE_VBASECLASSES (basetype))); | |
369 | offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last)); | |
370 | } | |
371 | else | |
372 | offset = BINFO_OFFSET (last); | |
373 | } | |
374 | ||
375 | if (TREE_INT_CST_LOW (offset)) | |
376 | { | |
59be85d7 | 377 | /* Bash types to make the backend happy. */ |
37c46b43 MS |
378 | offset = cp_convert (type, offset); |
379 | #if 0 | |
380 | /* This shouldn't be necessary. (mrs) */ | |
59be85d7 | 381 | expr = build1 (NOP_EXPR, type, expr); |
37c46b43 | 382 | #endif |
59be85d7 | 383 | |
8d08fdba MS |
384 | /* For multiple inheritance: if `this' can be set by any |
385 | function, then it could be 0 on entry to any function. | |
386 | Preserve such zeroness here. Otherwise, only in the | |
387 | case of constructors need we worry, and in those cases, | |
db6f8fbe | 388 | it will be zero, or initialized to some valid value to |
8d08fdba | 389 | which we may add. */ |
f30432d7 | 390 | if (nonnull == 0) |
8d08fdba MS |
391 | { |
392 | if (null_expr) | |
393 | TREE_TYPE (null_expr) = type; | |
394 | else | |
395 | null_expr = build1 (NOP_EXPR, type, integer_zero_node); | |
396 | if (TREE_SIDE_EFFECTS (expr)) | |
397 | expr = save_expr (expr); | |
398 | ||
399 | return build (COND_EXPR, type, | |
b7484fbe | 400 | build (EQ_EXPR, boolean_type_node, expr, integer_zero_node), |
8d08fdba MS |
401 | null_expr, |
402 | build (code, type, expr, offset)); | |
403 | } | |
404 | else return build (code, type, expr, offset); | |
405 | } | |
406 | ||
407 | /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may | |
408 | be used multiple times in initialization of multiple inheritance. */ | |
409 | if (null_expr) | |
410 | { | |
411 | TREE_TYPE (expr) = type; | |
412 | return expr; | |
413 | } | |
414 | else | |
415 | return build1 (NOP_EXPR, type, expr); | |
416 | } | |
417 | ||
418 | /* Virtual function things. */ | |
419 | ||
8d08fdba MS |
420 | /* Build an entry in the virtual function table. |
421 | DELTA is the offset for the `this' pointer. | |
422 | PFN is an ADDR_EXPR containing a pointer to the virtual function. | |
423 | Note that the index (DELTA2) in the virtual function table | |
424 | is always 0. */ | |
e92cc029 | 425 | |
bd6dd845 | 426 | static tree |
8d08fdba MS |
427 | build_vtable_entry (delta, pfn) |
428 | tree delta, pfn; | |
429 | { | |
8926095f MS |
430 | if (flag_vtable_thunks) |
431 | { | |
432 | HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta); | |
46b02c6d | 433 | if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0))) |
8926095f | 434 | { |
700f8a87 | 435 | pfn = build1 (ADDR_EXPR, vtable_entry_type, |
8926095f MS |
436 | make_thunk (pfn, idelta)); |
437 | TREE_READONLY (pfn) = 1; | |
438 | TREE_CONSTANT (pfn) = 1; | |
439 | } | |
440 | #ifdef GATHER_STATISTICS | |
441 | n_vtable_entries += 1; | |
442 | #endif | |
443 | return pfn; | |
444 | } | |
445 | else | |
446 | { | |
447 | extern int flag_huge_objects; | |
e66d884e JM |
448 | tree elems = expr_tree_cons (NULL_TREE, delta, |
449 | expr_tree_cons (NULL_TREE, integer_zero_node, | |
450 | build_expr_list (NULL_TREE, pfn))); | |
8926095f MS |
451 | tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems); |
452 | ||
453 | /* DELTA is constructed by `size_int', which means it may be an | |
454 | unsigned quantity on some platforms. Therefore, we cannot use | |
455 | `int_fits_type_p', because when DELTA is really negative, | |
456 | `force_fit_type' will make it look like a very large number. */ | |
457 | ||
458 | if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node)) | |
459 | < TREE_INT_CST_LOW (delta)) | |
460 | || (TREE_INT_CST_LOW (delta) | |
461 | < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node)))) | |
a703fb38 KG |
462 | { |
463 | if (flag_huge_objects) | |
464 | sorry ("object size exceeds built-in limit for virtual function table implementation"); | |
465 | else | |
466 | sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects"); | |
467 | } | |
468 | ||
8926095f MS |
469 | TREE_CONSTANT (entry) = 1; |
470 | TREE_STATIC (entry) = 1; | |
471 | TREE_READONLY (entry) = 1; | |
8d08fdba MS |
472 | |
473 | #ifdef GATHER_STATISTICS | |
8926095f | 474 | n_vtable_entries += 1; |
8d08fdba MS |
475 | #endif |
476 | ||
8926095f MS |
477 | return entry; |
478 | } | |
8d08fdba MS |
479 | } |
480 | ||
481 | /* Given an object INSTANCE, return an expression which yields the | |
6b5fbb55 MS |
482 | virtual function vtable element corresponding to INDEX. There are |
483 | many special cases for INSTANCE which we take care of here, mainly | |
484 | to avoid creating extra tree nodes when we don't have to. */ | |
e92cc029 | 485 | |
8d08fdba | 486 | tree |
6b5fbb55 MS |
487 | build_vtbl_ref (instance, idx) |
488 | tree instance, idx; | |
8d08fdba | 489 | { |
8d08fdba MS |
490 | tree vtbl, aref; |
491 | tree basetype = TREE_TYPE (instance); | |
492 | ||
493 | if (TREE_CODE (basetype) == REFERENCE_TYPE) | |
494 | basetype = TREE_TYPE (basetype); | |
495 | ||
4ac14744 | 496 | if (instance == current_class_ref) |
8bf41929 RK |
497 | vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), |
498 | NULL_PTR); | |
8d08fdba MS |
499 | else |
500 | { | |
501 | if (optimize) | |
502 | { | |
503 | /* Try to figure out what a reference refers to, and | |
504 | access its virtual function table directly. */ | |
505 | tree ref = NULL_TREE; | |
506 | ||
507 | if (TREE_CODE (instance) == INDIRECT_REF | |
508 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE) | |
509 | ref = TREE_OPERAND (instance, 0); | |
510 | else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) | |
511 | ref = instance; | |
512 | ||
513 | if (ref && TREE_CODE (ref) == VAR_DECL | |
514 | && DECL_INITIAL (ref)) | |
515 | { | |
516 | tree init = DECL_INITIAL (ref); | |
517 | ||
518 | while (TREE_CODE (init) == NOP_EXPR | |
519 | || TREE_CODE (init) == NON_LVALUE_EXPR) | |
520 | init = TREE_OPERAND (init, 0); | |
521 | if (TREE_CODE (init) == ADDR_EXPR) | |
522 | { | |
523 | init = TREE_OPERAND (init, 0); | |
524 | if (IS_AGGR_TYPE (TREE_TYPE (init)) | |
525 | && (TREE_CODE (init) == PARM_DECL | |
526 | || TREE_CODE (init) == VAR_DECL)) | |
527 | instance = init; | |
528 | } | |
529 | } | |
530 | } | |
531 | ||
532 | if (IS_AGGR_TYPE (TREE_TYPE (instance)) | |
8d08fdba MS |
533 | && (TREE_CODE (instance) == RESULT_DECL |
534 | || TREE_CODE (instance) == PARM_DECL | |
535 | || TREE_CODE (instance) == VAR_DECL)) | |
536 | vtbl = TYPE_BINFO_VTABLE (basetype); | |
537 | else | |
538 | vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), | |
539 | NULL_PTR); | |
540 | } | |
e3417fcd | 541 | assemble_external (vtbl); |
8d08fdba MS |
542 | aref = build_array_ref (vtbl, idx); |
543 | ||
6b5fbb55 MS |
544 | return aref; |
545 | } | |
546 | ||
547 | /* Given an object INSTANCE, return an expression which yields the | |
548 | virtual function corresponding to INDEX. There are many special | |
549 | cases for INSTANCE which we take care of here, mainly to avoid | |
550 | creating extra tree nodes when we don't have to. */ | |
e92cc029 | 551 | |
6b5fbb55 MS |
552 | tree |
553 | build_vfn_ref (ptr_to_instptr, instance, idx) | |
554 | tree *ptr_to_instptr, instance; | |
555 | tree idx; | |
556 | { | |
557 | tree aref = build_vtbl_ref (instance, idx); | |
8d08fdba | 558 | |
6b5fbb55 MS |
559 | /* When using thunks, there is no extra delta, and we get the pfn |
560 | directly. */ | |
8926095f MS |
561 | if (flag_vtable_thunks) |
562 | return aref; | |
6b5fbb55 MS |
563 | |
564 | if (ptr_to_instptr) | |
8926095f | 565 | { |
6b5fbb55 MS |
566 | /* Save the intermediate result in a SAVE_EXPR so we don't have to |
567 | compute each component of the virtual function pointer twice. */ | |
568 | if (TREE_CODE (aref) == INDIRECT_REF) | |
569 | TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0)); | |
570 | ||
571 | *ptr_to_instptr | |
572 | = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr), | |
573 | *ptr_to_instptr, | |
37c46b43 MS |
574 | cp_convert (ptrdiff_type_node, |
575 | build_component_ref (aref, delta_identifier, NULL_TREE, 0))); | |
8926095f | 576 | } |
6b5fbb55 MS |
577 | |
578 | return build_component_ref (aref, pfn_identifier, NULL_TREE, 0); | |
8d08fdba MS |
579 | } |
580 | ||
8d08fdba MS |
581 | /* Return the name of the virtual function table (as an IDENTIFIER_NODE) |
582 | for the given TYPE. */ | |
e92cc029 | 583 | |
8d08fdba MS |
584 | static tree |
585 | get_vtable_name (type) | |
586 | tree type; | |
587 | { | |
588 | tree type_id = build_typename_overload (type); | |
2636fde4 JM |
589 | char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) |
590 | + IDENTIFIER_LENGTH (type_id) + 2); | |
8d08fdba MS |
591 | char *ptr = IDENTIFIER_POINTER (type_id); |
592 | int i; | |
593 | for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ; | |
594 | #if 0 | |
595 | /* We don't take off the numbers; prepare_fresh_vtable uses the | |
596 | DECL_ASSEMBLER_NAME for the type, which includes the number | |
597 | in `3foo'. If we were to pull them off here, we'd end up with | |
598 | something like `_vt.foo.3bar', instead of a uniform definition. */ | |
599 | while (ptr[i] >= '0' && ptr[i] <= '9') | |
600 | i += 1; | |
601 | #endif | |
602 | sprintf (buf, VTABLE_NAME_FORMAT, ptr+i); | |
603 | return get_identifier (buf); | |
604 | } | |
605 | ||
6b5fbb55 | 606 | /* Return the offset to the main vtable for a given base BINFO. */ |
e92cc029 | 607 | |
6b5fbb55 MS |
608 | tree |
609 | get_vfield_offset (binfo) | |
610 | tree binfo; | |
611 | { | |
f5426d1e R |
612 | tree tmp |
613 | = size_binop (FLOOR_DIV_EXPR, | |
614 | DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))), | |
615 | size_int (BITS_PER_UNIT)); | |
616 | tmp = convert (sizetype, tmp); | |
617 | return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo)); | |
6b5fbb55 MS |
618 | } |
619 | ||
620 | /* Get the offset to the start of the original binfo that we derived | |
621 | this binfo from. If we find TYPE first, return the offset only | |
622 | that far. The shortened search is useful because the this pointer | |
623 | on method calling is expected to point to a DECL_CONTEXT (fndecl) | |
624 | object, and not a baseclass of it. */ | |
e92cc029 | 625 | |
6b5fbb55 MS |
626 | static tree |
627 | get_derived_offset (binfo, type) | |
628 | tree binfo, type; | |
629 | { | |
630 | tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo))); | |
631 | tree offset2; | |
632 | int i; | |
633 | while (BINFO_BASETYPES (binfo) | |
634 | && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1) | |
635 | { | |
636 | tree binfos = BINFO_BASETYPES (binfo); | |
637 | if (BINFO_TYPE (binfo) == type) | |
638 | break; | |
639 | binfo = TREE_VEC_ELT (binfos, i); | |
640 | } | |
641 | offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo))); | |
642 | return size_binop (MINUS_EXPR, offset1, offset2); | |
643 | } | |
644 | ||
645 | /* Update the rtti info for this class. */ | |
e92cc029 | 646 | |
6b5fbb55 MS |
647 | static void |
648 | set_rtti_entry (virtuals, offset, type) | |
649 | tree virtuals, offset, type; | |
650 | { | |
651 | tree vfn; | |
652 | ||
653 | if (flag_rtti) | |
654 | vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type)); | |
655 | else | |
46b02c6d | 656 | vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node); |
6b5fbb55 MS |
657 | TREE_CONSTANT (vfn) = 1; |
658 | ||
659 | if (! flag_vtable_thunks) | |
660 | TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn); | |
661 | else | |
662 | { | |
663 | tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset); | |
664 | TREE_CONSTANT (voff) = 1; | |
665 | ||
46b02c6d | 666 | TREE_VALUE (virtuals) = build_vtable_entry (size_zero_node, voff); |
6b5fbb55 MS |
667 | |
668 | /* The second slot is for the tdesc pointer when thunks are used. */ | |
669 | TREE_VALUE (TREE_CHAIN (virtuals)) | |
46b02c6d | 670 | = build_vtable_entry (size_zero_node, vfn); |
6b5fbb55 MS |
671 | } |
672 | } | |
673 | ||
8d08fdba MS |
674 | /* Build a virtual function for type TYPE. |
675 | If BINFO is non-NULL, build the vtable starting with the initial | |
676 | approximation that it is the same as the one which is the head of | |
677 | the association list. */ | |
e92cc029 | 678 | |
8d08fdba MS |
679 | static tree |
680 | build_vtable (binfo, type) | |
681 | tree binfo, type; | |
682 | { | |
683 | tree name = get_vtable_name (type); | |
684 | tree virtuals, decl; | |
685 | ||
686 | if (binfo) | |
687 | { | |
6b5fbb55 MS |
688 | tree offset; |
689 | ||
8d08fdba MS |
690 | virtuals = copy_list (BINFO_VIRTUALS (binfo)); |
691 | decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo))); | |
6b5fbb55 MS |
692 | |
693 | /* Now do rtti stuff. */ | |
694 | offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE); | |
46b02c6d | 695 | offset = size_binop (MINUS_EXPR, size_zero_node, offset); |
6b5fbb55 | 696 | set_rtti_entry (virtuals, offset, type); |
8d08fdba MS |
697 | } |
698 | else | |
699 | { | |
700 | virtuals = NULL_TREE; | |
701 | decl = build_decl (VAR_DECL, name, void_type_node); | |
702 | } | |
703 | ||
704 | #ifdef GATHER_STATISTICS | |
705 | n_vtables += 1; | |
706 | n_vtable_elems += list_length (virtuals); | |
707 | #endif | |
708 | ||
709 | /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */ | |
e3417fcd | 710 | import_export_vtable (decl, type, 0); |
8d08fdba MS |
711 | |
712 | IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl); | |
713 | /* Initialize the association list for this type, based | |
714 | on our first approximation. */ | |
715 | TYPE_BINFO_VTABLE (type) = decl; | |
716 | TYPE_BINFO_VIRTUALS (type) = virtuals; | |
717 | ||
da20811c | 718 | DECL_ARTIFICIAL (decl) = 1; |
8d08fdba MS |
719 | TREE_STATIC (decl) = 1; |
720 | #ifndef WRITABLE_VTABLES | |
721 | /* Make them READONLY by default. (mrs) */ | |
722 | TREE_READONLY (decl) = 1; | |
723 | #endif | |
724 | /* At one time the vtable info was grabbed 2 words at a time. This | |
725 | fails on sparc unless you have 8-byte alignment. (tiemann) */ | |
726 | DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node), | |
727 | DECL_ALIGN (decl)); | |
728 | ||
729 | /* Why is this conditional? (mrs) */ | |
730 | if (binfo && write_virtuals >= 0) | |
731 | DECL_VIRTUAL_P (decl) = 1; | |
8d08fdba MS |
732 | DECL_CONTEXT (decl) = type; |
733 | ||
734 | binfo = TYPE_BINFO (type); | |
8d08fdba MS |
735 | SET_BINFO_NEW_VTABLE_MARKED (binfo); |
736 | return decl; | |
737 | } | |
738 | ||
e92cc029 MS |
739 | extern tree signed_size_zero_node; |
740 | ||
8d08fdba MS |
741 | /* Give TYPE a new virtual function table which is initialized |
742 | with a skeleton-copy of its original initialization. The only | |
743 | entry that changes is the `delta' entry, so we can really | |
744 | share a lot of structure. | |
745 | ||
746 | FOR_TYPE is the derived type which caused this table to | |
747 | be needed. | |
748 | ||
2636fde4 JM |
749 | BINFO is the type association which provided TYPE for FOR_TYPE. |
750 | ||
751 | The order in which vtables are built (by calling this function) for | |
752 | an object must remain the same, otherwise a binary incompatibility | |
753 | can result. */ | |
e92cc029 | 754 | |
8d08fdba | 755 | static void |
7177d104 MS |
756 | prepare_fresh_vtable (binfo, for_type) |
757 | tree binfo, for_type; | |
8d08fdba | 758 | { |
2636fde4 | 759 | tree basetype; |
8d08fdba | 760 | tree orig_decl = BINFO_VTABLE (binfo); |
2636fde4 JM |
761 | tree name; |
762 | tree new_decl; | |
5566b478 | 763 | tree offset; |
2636fde4 JM |
764 | tree path = binfo; |
765 | char *buf, *buf2; | |
766 | char joiner = '_'; | |
767 | int i; | |
768 | ||
769 | #ifdef JOINER | |
770 | joiner = JOINER; | |
771 | #endif | |
772 | ||
773 | basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo)); | |
774 | ||
775 | buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype); | |
776 | i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1; | |
777 | ||
778 | /* We know that the vtable that we are going to create doesn't exist | |
779 | yet in the global namespace, and when we finish, it will be | |
780 | pushed into the global namespace. In complex MI hierarchies, we | |
781 | have to loop while the name we are thinking of adding is globally | |
782 | defined, adding more name components to the vtable name as we | |
783 | loop, until the name is unique. This is because in complex MI | |
784 | cases, we might have the same base more than once. This means | |
785 | that the order in which this function is called for vtables must | |
786 | remain the same, otherwise binary compatibility can be | |
787 | compromised. */ | |
788 | ||
789 | while (1) | |
790 | { | |
de35891e JM |
791 | char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type) |
792 | + 1 + i); | |
2636fde4 JM |
793 | char *new_buf2; |
794 | ||
795 | sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner, | |
796 | buf2); | |
797 | buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1); | |
798 | sprintf (buf, VTABLE_NAME_FORMAT, buf1); | |
799 | name = get_identifier (buf); | |
800 | ||
801 | /* If this name doesn't clash, then we can use it, otherwise | |
802 | we add more to the name until it is unique. */ | |
803 | ||
804 | if (! IDENTIFIER_GLOBAL_VALUE (name)) | |
805 | break; | |
806 | ||
807 | /* Set values for next loop through, if the name isn't unique. */ | |
808 | ||
809 | path = BINFO_INHERITANCE_CHAIN (path); | |
810 | ||
811 | /* We better not run out of stuff to make it unique. */ | |
812 | my_friendly_assert (path != NULL_TREE, 368); | |
813 | ||
814 | basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path)); | |
815 | ||
de35891e JM |
816 | if (for_type == basetype) |
817 | { | |
818 | /* If we run out of basetypes in the path, we have already | |
819 | found created a vtable with that name before, we now | |
820 | resort to tacking on _%d to distinguish them. */ | |
821 | int j = 2; | |
822 | i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3; | |
823 | buf1 = (char *) alloca (i); | |
824 | do { | |
825 | sprintf (buf1, "%s%c%s%c%d", | |
826 | TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, | |
827 | buf2, joiner, j); | |
828 | buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) | |
829 | + strlen (buf1) + 1); | |
830 | sprintf (buf, VTABLE_NAME_FORMAT, buf1); | |
831 | name = get_identifier (buf); | |
832 | ||
833 | /* If this name doesn't clash, then we can use it, | |
834 | otherwise we add something different to the name until | |
835 | it is unique. */ | |
836 | } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name)); | |
837 | ||
838 | /* Hey, they really like MI don't they? Increase the 3 | |
839 | above to 6, and the 999 to 999999. :-) */ | |
840 | my_friendly_assert (j <= 999, 369); | |
841 | ||
842 | break; | |
843 | } | |
2636fde4 JM |
844 | |
845 | i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i; | |
846 | new_buf2 = (char *) alloca (i); | |
847 | sprintf (new_buf2, "%s%c%s", | |
848 | TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2); | |
849 | buf2 = new_buf2; | |
850 | } | |
8d08fdba | 851 | |
2636fde4 | 852 | new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl)); |
8d08fdba | 853 | /* Remember which class this vtable is really for. */ |
8d08fdba MS |
854 | DECL_CONTEXT (new_decl) = for_type; |
855 | ||
da20811c | 856 | DECL_ARTIFICIAL (new_decl) = 1; |
8d08fdba MS |
857 | TREE_STATIC (new_decl) = 1; |
858 | BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl); | |
859 | DECL_VIRTUAL_P (new_decl) = 1; | |
860 | #ifndef WRITABLE_VTABLES | |
861 | /* Make them READONLY by default. (mrs) */ | |
862 | TREE_READONLY (new_decl) = 1; | |
863 | #endif | |
864 | DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl); | |
865 | ||
866 | /* Make fresh virtual list, so we can smash it later. */ | |
867 | BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo)); | |
db5ae43f MS |
868 | |
869 | if (TREE_VIA_VIRTUAL (binfo)) | |
6b5fbb55 MS |
870 | { |
871 | tree binfo1 = binfo_member (BINFO_TYPE (binfo), | |
872 | CLASSTYPE_VBASECLASSES (for_type)); | |
873 | ||
874 | /* XXX - This should never happen, if it does, the caller should | |
875 | ensure that the binfo is from for_type's binfos, not from any | |
876 | base type's. We can remove all this code after a while. */ | |
877 | if (binfo1 != binfo) | |
878 | warning ("internal inconsistency: binfo offset error for rtti"); | |
879 | ||
880 | offset = BINFO_OFFSET (binfo1); | |
881 | } | |
db5ae43f MS |
882 | else |
883 | offset = BINFO_OFFSET (binfo); | |
884 | ||
f30432d7 | 885 | set_rtti_entry (BINFO_VIRTUALS (binfo), |
e92cc029 | 886 | size_binop (MINUS_EXPR, signed_size_zero_node, offset), |
f30432d7 | 887 | for_type); |
8d08fdba MS |
888 | |
889 | #ifdef GATHER_STATISTICS | |
890 | n_vtables += 1; | |
891 | n_vtable_elems += list_length (BINFO_VIRTUALS (binfo)); | |
892 | #endif | |
893 | ||
894 | /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */ | |
e3417fcd | 895 | import_export_vtable (new_decl, for_type, 0); |
8d08fdba MS |
896 | |
897 | if (TREE_VIA_VIRTUAL (binfo)) | |
898 | my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo), | |
899 | CLASSTYPE_VBASECLASSES (current_class_type)), | |
900 | 170); | |
901 | SET_BINFO_NEW_VTABLE_MARKED (binfo); | |
8d08fdba MS |
902 | } |
903 | ||
5566b478 | 904 | #if 0 |
8d08fdba MS |
905 | /* Access the virtual function table entry that logically |
906 | contains BASE_FNDECL. VIRTUALS is the virtual function table's | |
51c184be MS |
907 | initializer. We can run off the end, when dealing with virtual |
908 | destructors in MI situations, return NULL_TREE in that case. */ | |
e92cc029 | 909 | |
8d08fdba MS |
910 | static tree |
911 | get_vtable_entry (virtuals, base_fndecl) | |
912 | tree virtuals, base_fndecl; | |
913 | { | |
f30432d7 | 914 | unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD |
8d08fdba MS |
915 | ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)) |
916 | & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1)) | |
917 | : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))); | |
918 | ||
919 | #ifdef GATHER_STATISTICS | |
f30432d7 | 920 | n_vtable_searches += n; |
8d08fdba MS |
921 | #endif |
922 | ||
f30432d7 | 923 | while (n > 0 && virtuals) |
8d08fdba | 924 | { |
f30432d7 | 925 | --n; |
8d08fdba | 926 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
927 | } |
928 | return virtuals; | |
929 | } | |
5566b478 | 930 | #endif |
8d08fdba MS |
931 | |
932 | /* Put new entry ENTRY into virtual function table initializer | |
933 | VIRTUALS. | |
934 | ||
935 | Also update DECL_VINDEX (FNDECL). */ | |
936 | ||
937 | static void | |
938 | modify_vtable_entry (old_entry_in_list, new_entry, fndecl) | |
939 | tree old_entry_in_list, new_entry, fndecl; | |
940 | { | |
7177d104 | 941 | tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0); |
8d08fdba MS |
942 | |
943 | #ifdef NOTQUITE | |
7177d104 MS |
944 | cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl), |
945 | DECL_ASSEMBLER_NAME (fndecl)); | |
8d08fdba | 946 | #endif |
8d08fdba | 947 | TREE_VALUE (old_entry_in_list) = new_entry; |
8d08fdba | 948 | |
7177d104 | 949 | /* Now assign virtual dispatch information, if unset. */ |
e92cc029 | 950 | /* We can dispatch this, through any overridden base function. */ |
7177d104 | 951 | if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) |
8d08fdba | 952 | { |
7177d104 MS |
953 | DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl); |
954 | DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl); | |
8d08fdba | 955 | } |
8d08fdba MS |
956 | } |
957 | ||
9a3b49ac | 958 | /* Access the virtual function table entry N. VIRTUALS is the virtual |
8d08fdba | 959 | function table's initializer. */ |
e92cc029 | 960 | |
8d08fdba | 961 | static tree |
f30432d7 | 962 | get_vtable_entry_n (virtuals, n) |
8d08fdba | 963 | tree virtuals; |
f30432d7 | 964 | unsigned HOST_WIDE_INT n; |
8d08fdba | 965 | { |
f30432d7 | 966 | while (n > 0) |
8d08fdba | 967 | { |
f30432d7 | 968 | --n; |
8d08fdba | 969 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
970 | } |
971 | return virtuals; | |
972 | } | |
973 | ||
7177d104 MS |
974 | /* Add a virtual function to all the appropriate vtables for the class |
975 | T. DECL_VINDEX(X) should be error_mark_node, if we want to | |
976 | allocate a new slot in our table. If it is error_mark_node, we | |
977 | know that no other function from another vtable is overridden by X. | |
978 | HAS_VIRTUAL keeps track of how many virtuals there are in our main | |
979 | vtable for the type, and we build upon the PENDING_VIRTUALS list | |
980 | and return it. */ | |
e92cc029 | 981 | |
aa598818 JM |
982 | static void |
983 | add_virtual_function (pv, phv, has_virtual, fndecl, t) | |
984 | tree *pv, *phv; | |
8d08fdba | 985 | int *has_virtual; |
7177d104 | 986 | tree fndecl; |
e92cc029 | 987 | tree t; /* Structure type. */ |
8d08fdba | 988 | { |
aa598818 JM |
989 | tree pending_virtuals = *pv; |
990 | tree pending_hard_virtuals = *phv; | |
991 | ||
8d08fdba MS |
992 | /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely |
993 | convert to void *. Make such a conversion here. */ | |
700f8a87 | 994 | tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl); |
8d08fdba MS |
995 | TREE_CONSTANT (vfn) = 1; |
996 | ||
7177d104 MS |
997 | #ifndef DUMB_USER |
998 | if (current_class_type == 0) | |
999 | cp_warning ("internal problem, current_class_type is zero when adding `%D', please report", | |
1000 | fndecl); | |
1001 | if (current_class_type && t != current_class_type) | |
1002 | cp_warning ("internal problem, current_class_type differs when adding `%D', please report", | |
1003 | fndecl); | |
1004 | #endif | |
1005 | ||
8d08fdba MS |
1006 | /* If the virtual function is a redefinition of a prior one, |
1007 | figure out in which base class the new definition goes, | |
1008 | and if necessary, make a fresh virtual function table | |
1009 | to hold that entry. */ | |
7177d104 | 1010 | if (DECL_VINDEX (fndecl) == error_mark_node) |
8d08fdba | 1011 | { |
8926095f | 1012 | tree entry; |
8d08fdba | 1013 | |
6b5fbb55 MS |
1014 | /* We remember that this was the base sub-object for rtti. */ |
1015 | CLASSTYPE_RTTI (t) = t; | |
8d08fdba | 1016 | |
f30432d7 MS |
1017 | /* If we are using thunks, use two slots at the front, one |
1018 | for the offset pointer, one for the tdesc pointer. */ | |
1019 | if (*has_virtual == 0 && flag_vtable_thunks) | |
1020 | { | |
1021 | *has_virtual = 1; | |
1022 | } | |
1023 | ||
8d08fdba | 1024 | /* Build a new INT_CST for this DECL_VINDEX. */ |
8d08fdba MS |
1025 | { |
1026 | static tree index_table[256]; | |
e92cc029 | 1027 | tree idx; |
f30432d7 | 1028 | /* We skip a slot for the offset/tdesc entry. */ |
8d08fdba MS |
1029 | int i = ++(*has_virtual); |
1030 | ||
1031 | if (i >= 256 || index_table[i] == 0) | |
1032 | { | |
e92cc029 | 1033 | idx = build_int_2 (i, 0); |
8d08fdba | 1034 | if (i < 256) |
e92cc029 | 1035 | index_table[i] = idx; |
8d08fdba MS |
1036 | } |
1037 | else | |
e92cc029 | 1038 | idx = index_table[i]; |
8d08fdba | 1039 | |
e92cc029 MS |
1040 | /* Now assign virtual dispatch information. */ |
1041 | DECL_VINDEX (fndecl) = idx; | |
7177d104 | 1042 | DECL_CONTEXT (fndecl) = t; |
8d08fdba | 1043 | } |
8926095f | 1044 | entry = build_vtable_entry (integer_zero_node, vfn); |
7177d104 | 1045 | pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals); |
8d08fdba | 1046 | } |
7177d104 MS |
1047 | /* Might already be INTEGER_CST if declared twice in class. We will |
1048 | give error later or we've already given it. */ | |
1049 | else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) | |
8d08fdba MS |
1050 | { |
1051 | /* Need an entry in some other virtual function table. | |
1052 | Deal with this after we have laid out our virtual base classes. */ | |
7177d104 | 1053 | pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals); |
8d08fdba | 1054 | } |
aa598818 JM |
1055 | *pv = pending_virtuals; |
1056 | *phv = pending_hard_virtuals; | |
8d08fdba MS |
1057 | } |
1058 | \f | |
1059 | /* Obstack on which to build the vector of class methods. */ | |
1060 | struct obstack class_obstack; | |
1061 | extern struct obstack *current_obstack; | |
1062 | ||
1063 | /* Add method METHOD to class TYPE. This is used when a method | |
1064 | has been defined which did not initially appear in the class definition, | |
1065 | and helps cut down on spurious error messages. | |
1066 | ||
1067 | FIELDS is the entry in the METHOD_VEC vector entry of the class type where | |
1068 | the method should be added. */ | |
e92cc029 | 1069 | |
8d08fdba MS |
1070 | void |
1071 | add_method (type, fields, method) | |
1072 | tree type, *fields, method; | |
1073 | { | |
1074 | /* We must make a copy of METHOD here, since we must be sure that | |
1075 | we have exclusive title to this method's DECL_CHAIN. */ | |
1076 | tree decl; | |
1077 | ||
1078 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
1079 | { | |
1080 | decl = copy_node (method); | |
1081 | if (DECL_RTL (decl) == 0 | |
1082 | && (!processing_template_decl | |
5566b478 | 1083 | || !uses_template_parms (decl))) |
8d08fdba MS |
1084 | { |
1085 | make_function_rtl (decl); | |
1086 | DECL_RTL (method) = DECL_RTL (decl); | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | if (fields && *fields) | |
1091 | { | |
1092 | /* Take care not to hide destructor. */ | |
1093 | DECL_CHAIN (decl) = DECL_CHAIN (*fields); | |
1094 | DECL_CHAIN (*fields) = decl; | |
1095 | } | |
1096 | else if (CLASSTYPE_METHOD_VEC (type) == 0) | |
1097 | { | |
1098 | tree method_vec = make_node (TREE_VEC); | |
1099 | if (TYPE_IDENTIFIER (type) == DECL_NAME (decl)) | |
1100 | { | |
fc378698 MS |
1101 | /* ??? Is it possible for there to have been enough room in the |
1102 | current chunk for the tree_vec structure but not a tree_vec | |
1103 | plus a tree*? Will this work in that case? */ | |
1104 | obstack_free (current_obstack, method_vec); | |
1105 | obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *)); | |
1106 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl))) | |
1107 | TREE_VEC_ELT (method_vec, 1) = decl; | |
1108 | else | |
1109 | TREE_VEC_ELT (method_vec, 0) = decl; | |
1110 | TREE_VEC_LENGTH (method_vec) = 2; | |
8d08fdba MS |
1111 | } |
1112 | else | |
1113 | { | |
1114 | /* ??? Is it possible for there to have been enough room in the | |
1115 | current chunk for the tree_vec structure but not a tree_vec | |
1116 | plus a tree*? Will this work in that case? */ | |
1117 | obstack_free (current_obstack, method_vec); | |
fc378698 MS |
1118 | obstack_blank (current_obstack, sizeof (struct tree_vec) + 2*sizeof (tree *)); |
1119 | TREE_VEC_ELT (method_vec, 2) = decl; | |
1120 | TREE_VEC_LENGTH (method_vec) = 3; | |
8d08fdba MS |
1121 | obstack_finish (current_obstack); |
1122 | } | |
1123 | CLASSTYPE_METHOD_VEC (type) = method_vec; | |
1124 | } | |
1125 | else | |
1126 | { | |
1127 | tree method_vec = CLASSTYPE_METHOD_VEC (type); | |
1128 | int len = TREE_VEC_LENGTH (method_vec); | |
1129 | ||
1130 | /* Adding a new ctor or dtor. This is easy because our | |
1131 | METHOD_VEC always has a slot for such entries. */ | |
1132 | if (TYPE_IDENTIFIER (type) == DECL_NAME (decl)) | |
1133 | { | |
e92cc029 MS |
1134 | int idx = !!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)); |
1135 | /* TREE_VEC_ELT (method_vec, idx) = decl; */ | |
1136 | if (decl != TREE_VEC_ELT (method_vec, idx)) | |
8d08fdba | 1137 | { |
e92cc029 MS |
1138 | DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, idx); |
1139 | TREE_VEC_ELT (method_vec, idx) = decl; | |
8d08fdba MS |
1140 | } |
1141 | } | |
1142 | else | |
1143 | { | |
1144 | /* This is trickier. We try to extend the TREE_VEC in-place, | |
1145 | but if that does not work, we copy all its data to a new | |
1146 | TREE_VEC that's large enough. */ | |
1147 | struct obstack *ob = &class_obstack; | |
1148 | tree *end = (tree *)obstack_next_free (ob); | |
1149 | ||
1150 | if (end != TREE_VEC_END (method_vec)) | |
1151 | { | |
1152 | ob = current_obstack; | |
1153 | TREE_VEC_LENGTH (method_vec) += 1; | |
1154 | TREE_VEC_ELT (method_vec, len) = NULL_TREE; | |
1155 | method_vec = copy_node (method_vec); | |
1156 | TREE_VEC_LENGTH (method_vec) -= 1; | |
1157 | } | |
1158 | else | |
1159 | { | |
1160 | tree tmp_vec = (tree) obstack_base (ob); | |
1161 | if (obstack_room (ob) < sizeof (tree)) | |
1162 | { | |
1163 | obstack_blank (ob, sizeof (struct tree_common) | |
1164 | + tree_code_length[(int) TREE_VEC] | |
1165 | * sizeof (char *) | |
1166 | + len * sizeof (tree)); | |
1167 | tmp_vec = (tree) obstack_base (ob); | |
1daa5dd8 | 1168 | bcopy ((char *) method_vec, (char *) tmp_vec, |
8d08fdba MS |
1169 | (sizeof (struct tree_common) |
1170 | + tree_code_length[(int) TREE_VEC] * sizeof (char *) | |
1171 | + (len-1) * sizeof (tree))); | |
1172 | method_vec = tmp_vec; | |
1173 | } | |
1174 | else | |
1175 | obstack_blank (ob, sizeof (tree)); | |
1176 | } | |
1177 | ||
1178 | obstack_finish (ob); | |
1179 | TREE_VEC_ELT (method_vec, len) = decl; | |
1180 | TREE_VEC_LENGTH (method_vec) = len + 1; | |
1181 | CLASSTYPE_METHOD_VEC (type) = method_vec; | |
1182 | ||
1183 | if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type)) | |
1184 | { | |
1185 | /* ??? May be better to know whether these can be extended? */ | |
1186 | tree baselink_vec = CLASSTYPE_BASELINK_VEC (type); | |
1187 | ||
1188 | TREE_VEC_LENGTH (baselink_vec) += 1; | |
1189 | CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec); | |
1190 | TREE_VEC_LENGTH (baselink_vec) -= 1; | |
1191 | ||
1192 | TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0; | |
1193 | } | |
1194 | } | |
1195 | } | |
1196 | DECL_CONTEXT (decl) = type; | |
1197 | DECL_CLASS_CONTEXT (decl) = type; | |
1198 | ||
1199 | pop_obstacks (); | |
1200 | } | |
1201 | ||
1202 | /* Subroutines of finish_struct. */ | |
1203 | ||
1204 | /* Look through the list of fields for this struct, deleting | |
1205 | duplicates as we go. This must be recursive to handle | |
1206 | anonymous unions. | |
1207 | ||
1208 | FIELD is the field which may not appear anywhere in FIELDS. | |
1209 | FIELD_PTR, if non-null, is the starting point at which | |
1210 | chained deletions may take place. | |
1211 | The value returned is the first acceptable entry found | |
1212 | in FIELDS. | |
1213 | ||
1214 | Note that anonymous fields which are not of UNION_TYPE are | |
1215 | not duplicates, they are just anonymous fields. This happens | |
1216 | when we have unnamed bitfields, for example. */ | |
e92cc029 | 1217 | |
8d08fdba | 1218 | static tree |
00595019 MS |
1219 | delete_duplicate_fields_1 (field, fields) |
1220 | tree field, fields; | |
8d08fdba MS |
1221 | { |
1222 | tree x; | |
00595019 | 1223 | tree prev = 0; |
8d08fdba MS |
1224 | if (DECL_NAME (field) == 0) |
1225 | { | |
1226 | if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE) | |
1227 | return fields; | |
1228 | ||
1229 | for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x)) | |
00595019 | 1230 | fields = delete_duplicate_fields_1 (x, fields); |
8d08fdba MS |
1231 | return fields; |
1232 | } | |
1233 | else | |
1234 | { | |
1235 | for (x = fields; x; prev = x, x = TREE_CHAIN (x)) | |
1236 | { | |
1237 | if (DECL_NAME (x) == 0) | |
1238 | { | |
1239 | if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE) | |
1240 | continue; | |
1241 | TYPE_FIELDS (TREE_TYPE (x)) | |
00595019 | 1242 | = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x))); |
8d08fdba MS |
1243 | if (TYPE_FIELDS (TREE_TYPE (x)) == 0) |
1244 | { | |
1245 | if (prev == 0) | |
1246 | fields = TREE_CHAIN (fields); | |
1247 | else | |
1248 | TREE_CHAIN (prev) = TREE_CHAIN (x); | |
1249 | } | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | if (DECL_NAME (field) == DECL_NAME (x)) | |
1254 | { | |
1255 | if (TREE_CODE (field) == CONST_DECL | |
1256 | && TREE_CODE (x) == CONST_DECL) | |
1257 | cp_error_at ("duplicate enum value `%D'", x); | |
1258 | else if (TREE_CODE (field) == CONST_DECL | |
1259 | || TREE_CODE (x) == CONST_DECL) | |
1260 | cp_error_at ("duplicate field `%D' (as enum and non-enum)", | |
1261 | x); | |
1262 | else if (TREE_CODE (field) == TYPE_DECL | |
1263 | && TREE_CODE (x) == TYPE_DECL) | |
fc378698 MS |
1264 | { |
1265 | if (TREE_TYPE (field) == TREE_TYPE (x)) | |
1266 | continue; | |
1267 | cp_error_at ("duplicate nested type `%D'", x); | |
1268 | } | |
8d08fdba MS |
1269 | else if (TREE_CODE (field) == TYPE_DECL |
1270 | || TREE_CODE (x) == TYPE_DECL) | |
5566b478 MS |
1271 | { |
1272 | /* Hide tag decls. */ | |
1273 | if ((TREE_CODE (field) == TYPE_DECL | |
1274 | && DECL_ARTIFICIAL (field)) | |
1275 | || (TREE_CODE (x) == TYPE_DECL | |
1276 | && DECL_ARTIFICIAL (x))) | |
1277 | continue; | |
1278 | cp_error_at ("duplicate field `%D' (as type and non-type)", | |
1279 | x); | |
1280 | } | |
8d08fdba MS |
1281 | else |
1282 | cp_error_at ("duplicate member `%D'", x); | |
1283 | if (prev == 0) | |
1284 | fields = TREE_CHAIN (fields); | |
1285 | else | |
1286 | TREE_CHAIN (prev) = TREE_CHAIN (x); | |
1287 | } | |
1288 | } | |
1289 | } | |
1290 | } | |
1291 | return fields; | |
1292 | } | |
1293 | ||
1294 | static void | |
1295 | delete_duplicate_fields (fields) | |
1296 | tree fields; | |
1297 | { | |
1298 | tree x; | |
1299 | for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x)) | |
00595019 | 1300 | TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x)); |
8d08fdba MS |
1301 | } |
1302 | ||
1303 | /* Change the access of FDECL to ACCESS in T. | |
1304 | Return 1 if change was legit, otherwise return 0. */ | |
e92cc029 | 1305 | |
8d08fdba MS |
1306 | static int |
1307 | alter_access (t, fdecl, access) | |
1308 | tree t; | |
1309 | tree fdecl; | |
be99da77 | 1310 | tree access; |
8d08fdba MS |
1311 | { |
1312 | tree elem = purpose_member (t, DECL_ACCESS (fdecl)); | |
38afd588 | 1313 | if (elem) |
8d08fdba | 1314 | { |
38afd588 | 1315 | if (TREE_VALUE (elem) != access) |
8d08fdba | 1316 | { |
38afd588 MM |
1317 | if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL) |
1318 | cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl)); | |
1319 | else | |
1320 | error ("conflicting access specifications for field `%s', ignored", | |
1321 | IDENTIFIER_POINTER (DECL_NAME (fdecl))); | |
8d08fdba MS |
1322 | } |
1323 | else | |
38afd588 MM |
1324 | /* They're changing the access to the same thing they changed |
1325 | it to before. That's OK. */ | |
1326 | ; | |
db5ae43f | 1327 | } |
38afd588 | 1328 | else |
8d08fdba | 1329 | { |
38afd588 MM |
1330 | enforce_access (TYPE_BINFO (t), fdecl); |
1331 | ||
8d08fdba | 1332 | alter: |
be99da77 | 1333 | DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); |
8d08fdba MS |
1334 | return 1; |
1335 | } | |
1336 | return 0; | |
1337 | } | |
1338 | ||
8d08fdba MS |
1339 | /* If FOR_TYPE needs to reinitialize virtual function table pointers |
1340 | for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST. | |
1341 | Returns BASE_INIT_LIST appropriately modified. */ | |
1342 | ||
1343 | static tree | |
1344 | maybe_fixup_vptrs (for_type, binfo, base_init_list) | |
1345 | tree for_type, binfo, base_init_list; | |
1346 | { | |
1347 | /* Now reinitialize any slots that don't fall under our virtual | |
1348 | function table pointer. */ | |
1349 | tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)); | |
1350 | while (vfields) | |
1351 | { | |
1352 | tree basetype = VF_NORMAL_VALUE (vfields) | |
1353 | ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields)) | |
1354 | : VF_BASETYPE_VALUE (vfields); | |
1355 | ||
1356 | tree base_binfo = get_binfo (basetype, for_type, 0); | |
e92cc029 | 1357 | /* Punt until this is implemented. */ |
8d08fdba MS |
1358 | if (1 /* BINFO_MODIFIED (base_binfo) */) |
1359 | { | |
1360 | tree base_offset = get_vfield_offset (base_binfo); | |
1361 | if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type))) | |
1362 | && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo))) | |
1363 | base_init_list = tree_cons (error_mark_node, base_binfo, | |
1364 | base_init_list); | |
1365 | } | |
1366 | vfields = TREE_CHAIN (vfields); | |
1367 | } | |
1368 | return base_init_list; | |
1369 | } | |
1370 | ||
1371 | /* If TYPE does not have a constructor, then the compiler must | |
1372 | manually deal with all of the initialization this type requires. | |
1373 | ||
1374 | If a base initializer exists only to fill in the virtual function | |
1375 | table pointer, then we mark that fact with the TREE_VIRTUAL bit. | |
1376 | This way, we avoid multiple initializations of the same field by | |
1377 | each virtual function table up the class hierarchy. | |
1378 | ||
1379 | Virtual base class pointers are not initialized here. They are | |
1380 | initialized only at the "top level" of object creation. If we | |
1381 | initialized them here, we would have to skip a lot of work. */ | |
1382 | ||
1383 | static void | |
1384 | build_class_init_list (type) | |
1385 | tree type; | |
1386 | { | |
1387 | tree base_init_list = NULL_TREE; | |
1388 | tree member_init_list = NULL_TREE; | |
1389 | ||
1390 | /* Since we build member_init_list and base_init_list using | |
1391 | tree_cons, backwards fields the all through work. */ | |
1392 | tree x; | |
1393 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (type)); | |
1394 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1395 | ||
1396 | for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x)) | |
1397 | { | |
1398 | if (TREE_CODE (x) != FIELD_DECL) | |
1399 | continue; | |
1400 | ||
1401 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x)) | |
1402 | || DECL_INITIAL (x) != NULL_TREE) | |
1403 | member_init_list = tree_cons (x, type, member_init_list); | |
1404 | } | |
1405 | member_init_list = nreverse (member_init_list); | |
1406 | ||
1407 | /* We will end up doing this last. Need special marker | |
1408 | to avoid infinite regress. */ | |
1409 | if (TYPE_VIRTUAL_P (type)) | |
1410 | { | |
1411 | base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type)); | |
1412 | if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0) | |
1413 | TREE_VALUE (base_init_list) = NULL_TREE; | |
1414 | TREE_ADDRESSABLE (base_init_list) = 1; | |
1415 | } | |
1416 | ||
1417 | /* Each base class which needs to have initialization | |
1418 | of some kind gets to make such requests known here. */ | |
1419 | for (i = n_baseclasses-1; i >= 0; i--) | |
1420 | { | |
1421 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1422 | tree blist; | |
1423 | ||
1424 | /* Don't initialize virtual baseclasses this way. */ | |
1425 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
1426 | continue; | |
1427 | ||
1428 | if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))) | |
1429 | { | |
1430 | /* ...and the last shall come first... */ | |
1431 | base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list); | |
1432 | base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list); | |
1433 | continue; | |
1434 | } | |
1435 | ||
1436 | if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE) | |
1437 | /* Nothing to initialize. */ | |
1438 | continue; | |
1439 | ||
1440 | /* ...ditto... */ | |
1441 | base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list); | |
1442 | ||
1443 | /* This is normally true for single inheritance. | |
1444 | The win is we can shrink the chain of initializations | |
1445 | to be done by only converting to the actual type | |
1446 | we are interested in. */ | |
1447 | if (TREE_VALUE (blist) | |
1448 | && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC | |
1449 | && tree_int_cst_equal (BINFO_OFFSET (base_binfo), | |
1450 | BINFO_OFFSET (TREE_VALUE (blist)))) | |
1451 | { | |
1452 | if (base_init_list) | |
1453 | { | |
1454 | /* Does it do more than just fill in a | |
1455 | virtual function table pointer? */ | |
1456 | if (! TREE_ADDRESSABLE (blist)) | |
1457 | base_init_list = build_tree_list (blist, base_init_list); | |
1458 | /* Can we get by just with the virtual function table | |
1459 | pointer that it fills in? */ | |
1460 | else if (TREE_ADDRESSABLE (base_init_list) | |
1461 | && TREE_VALUE (base_init_list) == 0) | |
1462 | base_init_list = blist; | |
1463 | /* Maybe, but it is not obvious as the previous case. */ | |
1464 | else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type)) | |
1465 | { | |
1466 | tree last = tree_last (base_init_list); | |
1467 | while (TREE_VALUE (last) | |
1468 | && TREE_CODE (TREE_VALUE (last)) == TREE_LIST) | |
1469 | last = tree_last (TREE_VALUE (last)); | |
1470 | if (TREE_VALUE (last) == 0) | |
1471 | base_init_list = build_tree_list (blist, base_init_list); | |
1472 | } | |
1473 | } | |
1474 | else | |
1475 | base_init_list = blist; | |
1476 | } | |
1477 | else | |
1478 | { | |
1479 | /* The function expand_aggr_init knows how to do the | |
1480 | initialization of `basetype' without getting | |
1481 | an explicit `blist'. */ | |
1482 | if (base_init_list) | |
1483 | base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list); | |
1484 | else | |
1485 | base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo)); | |
1486 | } | |
1487 | } | |
1488 | ||
1489 | if (base_init_list) | |
a703fb38 KG |
1490 | { |
1491 | if (member_init_list) | |
1492 | CLASSTYPE_BASE_INIT_LIST (type) = | |
1493 | build_tree_list (base_init_list, member_init_list); | |
1494 | else | |
1495 | CLASSTYPE_BASE_INIT_LIST (type) = base_init_list; | |
1496 | } | |
8d08fdba MS |
1497 | else if (member_init_list) |
1498 | CLASSTYPE_BASE_INIT_LIST (type) = member_init_list; | |
1499 | } | |
1500 | \f | |
1501 | struct base_info | |
1502 | { | |
1503 | int has_virtual; | |
1504 | int max_has_virtual; | |
1505 | int n_ancestors; | |
1506 | tree vfield; | |
1507 | tree vfields; | |
6b5fbb55 | 1508 | tree rtti; |
8d08fdba MS |
1509 | char cant_have_default_ctor; |
1510 | char cant_have_const_ctor; | |
8d08fdba | 1511 | char no_const_asn_ref; |
8d08fdba MS |
1512 | }; |
1513 | ||
1514 | /* Record information about type T derived from its base classes. | |
1515 | Store most of that information in T itself, and place the | |
1516 | remaining information in the struct BASE_INFO. | |
1517 | ||
1518 | Propagate basetype offsets throughout the lattice. Note that the | |
1519 | lattice topped by T is really a pair: it's a DAG that gives the | |
1520 | structure of the derivation hierarchy, and it's a list of the | |
1521 | virtual baseclasses that appear anywhere in the DAG. When a vbase | |
1522 | type appears in the DAG, it's offset is 0, and it's children start | |
1523 | their offsets from that point. When a vbase type appears in the list, | |
1524 | its offset is the offset it has in the hierarchy, and its children's | |
1525 | offsets include that offset in theirs. | |
1526 | ||
1527 | Returns the index of the first base class to have virtual functions, | |
1528 | or -1 if no such base class. | |
1529 | ||
1530 | Note that at this point TYPE_BINFO (t) != t_binfo. */ | |
1531 | ||
1532 | static int | |
1533 | finish_base_struct (t, b, t_binfo) | |
1534 | tree t; | |
1535 | struct base_info *b; | |
1536 | tree t_binfo; | |
1537 | { | |
1538 | tree binfos = BINFO_BASETYPES (t_binfo); | |
1539 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1540 | int first_vfn_base_index = -1; | |
1daa5dd8 | 1541 | bzero ((char *) b, sizeof (struct base_info)); |
8d08fdba MS |
1542 | |
1543 | for (i = 0; i < n_baseclasses; i++) | |
1544 | { | |
1545 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1546 | tree basetype = BINFO_TYPE (base_binfo); | |
1547 | ||
1548 | /* If the type of basetype is incomplete, then | |
1549 | we already complained about that fact | |
1550 | (and we should have fixed it up as well). */ | |
1551 | if (TYPE_SIZE (basetype) == 0) | |
1552 | { | |
1553 | int j; | |
1554 | /* The base type is of incomplete type. It is | |
1555 | probably best to pretend that it does not | |
1556 | exist. */ | |
1557 | if (i == n_baseclasses-1) | |
1558 | TREE_VEC_ELT (binfos, i) = NULL_TREE; | |
1559 | TREE_VEC_LENGTH (binfos) -= 1; | |
1560 | n_baseclasses -= 1; | |
1561 | for (j = i; j+1 < n_baseclasses; j++) | |
1562 | TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1); | |
1563 | } | |
1564 | ||
e349ee73 | 1565 | if (! TYPE_HAS_CONST_INIT_REF (basetype)) |
8d08fdba | 1566 | b->cant_have_const_ctor = 1; |
8d08fdba MS |
1567 | |
1568 | if (TYPE_HAS_CONSTRUCTOR (basetype) | |
1569 | && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)) | |
1570 | { | |
1571 | b->cant_have_default_ctor = 1; | |
1572 | if (! TYPE_HAS_CONSTRUCTOR (t)) | |
1573 | { | |
1574 | cp_pedwarn ("base `%T' with only non-default constructor", | |
1575 | basetype); | |
1576 | cp_pedwarn ("in class without a constructor"); | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | if (TYPE_HAS_ASSIGN_REF (basetype) | |
1581 | && !TYPE_HAS_CONST_ASSIGN_REF (basetype)) | |
1582 | b->no_const_asn_ref = 1; | |
8d08fdba MS |
1583 | |
1584 | b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype); | |
1585 | TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype); | |
1586 | TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype); | |
1587 | TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype); | |
e8abc66f | 1588 | TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype); |
8d08fdba MS |
1589 | |
1590 | TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype); | |
1591 | TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype); | |
1592 | TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype); | |
1593 | ||
1594 | if (! TREE_VIA_VIRTUAL (base_binfo) | |
8d08fdba MS |
1595 | && BINFO_BASETYPES (base_binfo)) |
1596 | { | |
1597 | tree base_binfos = BINFO_BASETYPES (base_binfo); | |
1598 | tree chain = NULL_TREE; | |
1599 | int j; | |
1600 | ||
1601 | /* Now unshare the structure beneath BASE_BINFO. */ | |
1602 | for (j = TREE_VEC_LENGTH (base_binfos)-1; | |
1603 | j >= 0; j--) | |
1604 | { | |
1605 | tree base_base_binfo = TREE_VEC_ELT (base_binfos, j); | |
1606 | if (! TREE_VIA_VIRTUAL (base_base_binfo)) | |
1607 | TREE_VEC_ELT (base_binfos, j) | |
1608 | = make_binfo (BINFO_OFFSET (base_base_binfo), | |
8926095f | 1609 | base_base_binfo, |
8d08fdba MS |
1610 | BINFO_VTABLE (base_base_binfo), |
1611 | BINFO_VIRTUALS (base_base_binfo), | |
1612 | chain); | |
1613 | chain = TREE_VEC_ELT (base_binfos, j); | |
1614 | TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo); | |
1615 | TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo); | |
8ccc31eb | 1616 | BINFO_INHERITANCE_CHAIN (chain) = base_binfo; |
8d08fdba MS |
1617 | } |
1618 | ||
1619 | /* Completely unshare potentially shared data, and | |
1620 | update what is ours. */ | |
1621 | propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo)); | |
1622 | } | |
1623 | ||
1624 | if (! TREE_VIA_VIRTUAL (base_binfo)) | |
1625 | CLASSTYPE_N_SUPERCLASSES (t) += 1; | |
1626 | ||
1627 | if (TYPE_VIRTUAL_P (basetype)) | |
1628 | { | |
6b5fbb55 MS |
1629 | /* Ensure that this is set from at least a virtual base |
1630 | class. */ | |
1631 | if (b->rtti == NULL_TREE) | |
1632 | b->rtti = CLASSTYPE_RTTI (basetype); | |
1633 | ||
8d08fdba MS |
1634 | /* Don't borrow virtuals from virtual baseclasses. */ |
1635 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
1636 | continue; | |
1637 | ||
1638 | if (first_vfn_base_index < 0) | |
1639 | { | |
1640 | tree vfields; | |
1641 | first_vfn_base_index = i; | |
1642 | ||
7177d104 MS |
1643 | /* Update these two, now that we know what vtable we are |
1644 | going to extend. This is so that we can add virtual | |
1645 | functions, and override them properly. */ | |
1646 | BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype); | |
1647 | BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype); | |
8d08fdba MS |
1648 | b->has_virtual = CLASSTYPE_VSIZE (basetype); |
1649 | b->vfield = CLASSTYPE_VFIELD (basetype); | |
1650 | b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype)); | |
1651 | vfields = b->vfields; | |
1652 | while (vfields) | |
1653 | { | |
1654 | if (VF_BINFO_VALUE (vfields) == NULL_TREE | |
1655 | || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))) | |
1656 | { | |
1657 | tree value = VF_BASETYPE_VALUE (vfields); | |
1658 | if (DECL_NAME (CLASSTYPE_VFIELD (value)) | |
1659 | == DECL_NAME (CLASSTYPE_VFIELD (basetype))) | |
1660 | VF_NORMAL_VALUE (b->vfields) = basetype; | |
1661 | else | |
1662 | VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields); | |
1663 | } | |
1664 | vfields = TREE_CHAIN (vfields); | |
1665 | } | |
1666 | CLASSTYPE_VFIELD (t) = b->vfield; | |
1667 | } | |
1668 | else | |
1669 | { | |
1670 | /* Only add unique vfields, and flatten them out as we go. */ | |
1671 | tree vfields = CLASSTYPE_VFIELDS (basetype); | |
1672 | while (vfields) | |
1673 | { | |
1674 | if (VF_BINFO_VALUE (vfields) == NULL_TREE | |
1675 | || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))) | |
1676 | { | |
1677 | tree value = VF_BASETYPE_VALUE (vfields); | |
1678 | b->vfields = tree_cons (base_binfo, value, b->vfields); | |
1679 | if (DECL_NAME (CLASSTYPE_VFIELD (value)) | |
1680 | == DECL_NAME (CLASSTYPE_VFIELD (basetype))) | |
1681 | VF_NORMAL_VALUE (b->vfields) = basetype; | |
1682 | else | |
1683 | VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields); | |
1684 | } | |
1685 | vfields = TREE_CHAIN (vfields); | |
1686 | } | |
1687 | ||
1688 | if (b->has_virtual == 0) | |
1689 | { | |
1690 | first_vfn_base_index = i; | |
2986ae00 MS |
1691 | |
1692 | /* Update these two, now that we know what vtable we are | |
1693 | going to extend. This is so that we can add virtual | |
1694 | functions, and override them properly. */ | |
1695 | BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype); | |
1696 | BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype); | |
8d08fdba MS |
1697 | b->has_virtual = CLASSTYPE_VSIZE (basetype); |
1698 | b->vfield = CLASSTYPE_VFIELD (basetype); | |
1699 | CLASSTYPE_VFIELD (t) = b->vfield; | |
1700 | /* When we install the first one, set the VF_NORMAL_VALUE | |
1701 | to be the current class, as this it is the most derived | |
1702 | class. Hopefully, this is not set to something else | |
1703 | later. (mrs) */ | |
1704 | vfields = b->vfields; | |
1705 | while (vfields) | |
1706 | { | |
1707 | if (DECL_NAME (CLASSTYPE_VFIELD (t)) | |
1708 | == DECL_NAME (CLASSTYPE_VFIELD (basetype))) | |
1709 | { | |
1710 | VF_NORMAL_VALUE (vfields) = t; | |
1711 | /* There should only be one of them! And it should | |
1712 | always be found, if we get into here. (mrs) */ | |
1713 | break; | |
1714 | } | |
1715 | vfields = TREE_CHAIN (vfields); | |
1716 | } | |
1717 | } | |
1718 | } | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | /* Must come after offsets are fixed for all bases. */ | |
1723 | for (i = 0; i < n_baseclasses; i++) | |
1724 | { | |
1725 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1726 | tree basetype = BINFO_TYPE (base_binfo); | |
1727 | ||
1728 | if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2) | |
1729 | { | |
1730 | cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity", | |
1731 | basetype, t); | |
8d08fdba MS |
1732 | } |
1733 | } | |
51c184be MS |
1734 | { |
1735 | tree v = get_vbase_types (t_binfo); | |
1736 | ||
1737 | for (; v; v = TREE_CHAIN (v)) | |
1738 | { | |
1739 | tree basetype = BINFO_TYPE (v); | |
1740 | if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2) | |
1741 | { | |
1742 | if (extra_warnings) | |
1743 | cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity", | |
1744 | basetype, t); | |
51c184be MS |
1745 | } |
1746 | } | |
1747 | } | |
8d08fdba MS |
1748 | |
1749 | { | |
1750 | tree vfields; | |
1751 | /* Find the base class with the largest number of virtual functions. */ | |
1752 | for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields)) | |
1753 | { | |
1754 | if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual) | |
1755 | b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)); | |
1756 | if (VF_DERIVED_VALUE (vfields) | |
1757 | && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual) | |
1758 | b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)); | |
1759 | } | |
1760 | } | |
1761 | ||
1762 | if (b->vfield == 0) | |
1763 | /* If all virtual functions come only from virtual baseclasses. */ | |
1764 | return -1; | |
6b5fbb55 MS |
1765 | |
1766 | /* Update the rtti base if we have a non-virtual base class version | |
1767 | of it. */ | |
1768 | b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index))); | |
1769 | ||
8d08fdba MS |
1770 | return first_vfn_base_index; |
1771 | } | |
1772 | ||
1773 | static int | |
1774 | typecode_p (type, code) | |
1775 | tree type; | |
1776 | enum tree_code code; | |
1777 | { | |
1778 | return (TREE_CODE (type) == code | |
1779 | || (TREE_CODE (type) == REFERENCE_TYPE | |
1780 | && TREE_CODE (TREE_TYPE (type)) == code)); | |
1781 | } | |
1782 | \f | |
1783 | /* Set memoizing fields and bits of T (and its variants) for later use. | |
1784 | MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */ | |
e92cc029 | 1785 | |
8d08fdba MS |
1786 | static void |
1787 | finish_struct_bits (t, max_has_virtual) | |
1788 | tree t; | |
1789 | int max_has_virtual; | |
1790 | { | |
1791 | int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t); | |
8d08fdba MS |
1792 | |
1793 | /* Fix up variants (if any). */ | |
1794 | tree variants = TYPE_NEXT_VARIANT (t); | |
1795 | while (variants) | |
1796 | { | |
1797 | /* These fields are in the _TYPE part of the node, not in | |
1798 | the TYPE_LANG_SPECIFIC component, so they are not shared. */ | |
1799 | TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t); | |
1800 | TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t); | |
1801 | TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t); | |
1802 | TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t); | |
1803 | ||
1804 | TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t); | |
1805 | TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t); | |
1806 | TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t); | |
1807 | /* Copy whatever these are holding today. */ | |
1808 | TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t); | |
1809 | TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t); | |
5566b478 | 1810 | TYPE_FIELDS (variants) = TYPE_FIELDS (t); |
e92cc029 | 1811 | TYPE_SIZE (variants) = TYPE_SIZE (t); |
8d08fdba MS |
1812 | variants = TYPE_NEXT_VARIANT (variants); |
1813 | } | |
1814 | ||
1815 | if (n_baseclasses && max_has_virtual) | |
1816 | { | |
1817 | /* Done by `finish_struct' for classes without baseclasses. */ | |
39211cd5 | 1818 | int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0; |
8d08fdba MS |
1819 | tree binfos = TYPE_BINFO_BASETYPES (t); |
1820 | for (i = n_baseclasses-1; i >= 0; i--) | |
1821 | { | |
39211cd5 | 1822 | might_have_abstract_virtuals |
8d08fdba | 1823 | |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0); |
39211cd5 | 1824 | if (might_have_abstract_virtuals) |
8d08fdba MS |
1825 | break; |
1826 | } | |
39211cd5 MS |
1827 | if (might_have_abstract_virtuals) |
1828 | { | |
1829 | /* We use error_mark_node from override_one_vtable to signal | |
e92cc029 | 1830 | an artificial abstract. */ |
39211cd5 MS |
1831 | if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node) |
1832 | CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE; | |
1833 | CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t); | |
1834 | } | |
8d08fdba MS |
1835 | } |
1836 | ||
1837 | if (n_baseclasses) | |
1838 | { | |
1839 | /* Notice whether this class has type conversion functions defined. */ | |
1840 | tree binfo = TYPE_BINFO (t); | |
1841 | tree binfos = BINFO_BASETYPES (binfo); | |
1842 | tree basetype; | |
1843 | ||
1844 | for (i = n_baseclasses-1; i >= 0; i--) | |
1845 | { | |
1846 | basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i)); | |
1847 | ||
1848 | if (TYPE_HAS_CONVERSION (basetype)) | |
1849 | { | |
1850 | TYPE_HAS_CONVERSION (t) = 1; | |
1851 | TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype); | |
1852 | TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype); | |
1853 | } | |
1854 | if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t)) | |
1855 | CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1; | |
1856 | } | |
1857 | } | |
1858 | ||
e8abc66f MS |
1859 | /* If this type has a copy constructor, force its mode to be BLKmode, and |
1860 | force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to | |
1861 | be passed by invisible reference and prevent it from being returned in | |
72b7eeff MS |
1862 | a register. |
1863 | ||
1864 | Also do this if the class has BLKmode but can still be returned in | |
1865 | registers, since function_cannot_inline_p won't let us inline | |
1866 | functions returning such a type. This affects the HP-PA. */ | |
1867 | if (! TYPE_HAS_TRIVIAL_INIT_REF (t) | |
1868 | || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t) | |
1869 | && CLASSTYPE_NON_AGGREGATE (t))) | |
8d08fdba | 1870 | { |
e8abc66f | 1871 | tree variants; |
d2e5ee5c | 1872 | DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode; |
e8abc66f | 1873 | for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) |
8d08fdba MS |
1874 | { |
1875 | TYPE_MODE (variants) = BLKmode; | |
1876 | TREE_ADDRESSABLE (variants) = 1; | |
8d08fdba MS |
1877 | } |
1878 | } | |
1879 | } | |
1880 | ||
fc378698 MS |
1881 | /* Add FNDECL to the method_vec growing on the class_obstack. Used by |
1882 | finish_struct_methods. Note, FNDECL cannot be a constructor or | |
1883 | destructor, those cases are handled by the caller. */ | |
e92cc029 | 1884 | |
e1cd6e56 | 1885 | static void |
fc378698 MS |
1886 | grow_method (fndecl, method_vec_ptr) |
1887 | tree fndecl; | |
8ccc31eb | 1888 | tree *method_vec_ptr; |
e1cd6e56 MS |
1889 | { |
1890 | tree method_vec = (tree)obstack_base (&class_obstack); | |
fc378698 MS |
1891 | |
1892 | /* Start off past the constructors and destructor. */ | |
1893 | tree *testp = &TREE_VEC_ELT (method_vec, 2); | |
1894 | ||
1895 | while (testp < (tree *) obstack_next_free (&class_obstack) | |
1896 | && (*testp == NULL_TREE || DECL_NAME (*testp) != DECL_NAME (fndecl))) | |
e1cd6e56 | 1897 | testp++; |
fc378698 MS |
1898 | |
1899 | if (testp < (tree *) obstack_next_free (&class_obstack)) | |
e1cd6e56 | 1900 | { |
98c1c668 JM |
1901 | tree *p; |
1902 | for (p = testp; *p; ) | |
1903 | p = &DECL_CHAIN (*p); | |
1904 | *p = fndecl; | |
e1cd6e56 MS |
1905 | } |
1906 | else | |
8ccc31eb | 1907 | { |
fc378698 | 1908 | obstack_ptr_grow (&class_obstack, fndecl); |
8ccc31eb MS |
1909 | *method_vec_ptr = (tree)obstack_base (&class_obstack); |
1910 | } | |
e1cd6e56 MS |
1911 | } |
1912 | ||
8d08fdba MS |
1913 | /* Warn about duplicate methods in fn_fields. Also compact method |
1914 | lists so that lookup can be made faster. | |
1915 | ||
1916 | Algorithm: Outer loop builds lists by method name. Inner loop | |
1917 | checks for redundant method names within a list. | |
1918 | ||
1919 | Data Structure: List of method lists. The outer list is a | |
1920 | TREE_LIST, whose TREE_PURPOSE field is the field name and the | |
e1cd6e56 MS |
1921 | TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN |
1922 | links the entire list of methods for TYPE_METHODS. Friends are | |
1923 | chained in the same way as member functions (? TREE_CHAIN or | |
1924 | DECL_CHAIN), but they live in the TREE_TYPE field of the outer | |
1925 | list. That allows them to be quickly deleted, and requires no | |
1926 | extra storage. | |
8d08fdba MS |
1927 | |
1928 | If there are any constructors/destructors, they are moved to the | |
1929 | front of the list. This makes pushclass more efficient. | |
1930 | ||
1931 | We also link each field which has shares a name with its baseclass | |
1932 | to the head of the list of fields for that base class. This allows | |
1933 | us to reduce search time in places like `build_method_call' to | |
1934 | consider only reasonably likely functions. */ | |
1935 | ||
72b7eeff | 1936 | tree |
8d08fdba MS |
1937 | finish_struct_methods (t, fn_fields, nonprivate_method) |
1938 | tree t; | |
1939 | tree fn_fields; | |
1940 | int nonprivate_method; | |
1941 | { | |
1942 | tree method_vec; | |
72b7eeff | 1943 | tree save_fn_fields = fn_fields; |
fc378698 | 1944 | tree ctor_name = constructor_name (t); |
8d08fdba MS |
1945 | int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t); |
1946 | ||
1947 | /* Now prepare to gather fn_fields into vector. */ | |
1948 | struct obstack *ambient_obstack = current_obstack; | |
1949 | current_obstack = &class_obstack; | |
fc378698 | 1950 | method_vec = make_tree_vec (2); |
8d08fdba | 1951 | current_obstack = ambient_obstack; |
fc378698 | 1952 | |
8d08fdba MS |
1953 | /* Now make this a live vector. */ |
1954 | obstack_free (&class_obstack, method_vec); | |
8d08fdba | 1955 | |
fc378698 MS |
1956 | /* Save room for constructors and destructors. */ |
1957 | obstack_blank (&class_obstack, sizeof (struct tree_vec) + sizeof (struct tree *)); | |
1958 | ||
1959 | /* First fill in entry 0 with the constructors, entry 1 with destructors, | |
1960 | and the next few with type conversion operators (if any). */ | |
e1cd6e56 | 1961 | |
72b7eeff | 1962 | for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields)) |
8d08fdba | 1963 | { |
8d08fdba | 1964 | tree fn_name = DECL_NAME (fn_fields); |
8d08fdba | 1965 | |
8d08fdba MS |
1966 | /* Clear out this flag. |
1967 | ||
1968 | @@ Doug may figure out how to break | |
1969 | @@ this with nested classes and friends. */ | |
1970 | DECL_IN_AGGR_P (fn_fields) = 0; | |
1971 | ||
1972 | /* Note here that a copy ctor is private, so we don't dare generate | |
1973 | a default copy constructor for a class that has a member | |
1974 | of this type without making sure they have access to it. */ | |
fc378698 | 1975 | if (fn_name == ctor_name) |
8d08fdba MS |
1976 | { |
1977 | tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields); | |
1978 | tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node; | |
1979 | ||
1980 | if (TREE_CODE (parmtype) == REFERENCE_TYPE | |
1981 | && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t) | |
1982 | { | |
1983 | if (TREE_CHAIN (parmtypes) == NULL_TREE | |
1984 | || TREE_CHAIN (parmtypes) == void_list_node | |
1985 | || TREE_PURPOSE (TREE_CHAIN (parmtypes))) | |
1986 | { | |
1987 | if (TREE_PROTECTED (fn_fields)) | |
1988 | TYPE_HAS_NONPUBLIC_CTOR (t) = 1; | |
1989 | else if (TREE_PRIVATE (fn_fields)) | |
1990 | TYPE_HAS_NONPUBLIC_CTOR (t) = 2; | |
1991 | } | |
1992 | } | |
fc378698 MS |
1993 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields))) |
1994 | { | |
1995 | /* Destructors go in slot 1. */ | |
1996 | DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 1); | |
1997 | TREE_VEC_ELT (method_vec, 1) = fn_fields; | |
1998 | } | |
1999 | else | |
2000 | { | |
2001 | /* Constructors go in slot 0. */ | |
2002 | DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 0); | |
2003 | TREE_VEC_ELT (method_vec, 0) = fn_fields; | |
2004 | } | |
8d08fdba | 2005 | } |
e1cd6e56 MS |
2006 | else if (IDENTIFIER_TYPENAME_P (fn_name)) |
2007 | { | |
2008 | tree return_type = TREE_TYPE (TREE_TYPE (fn_fields)); | |
2009 | ||
2010 | if (typecode_p (return_type, INTEGER_TYPE) | |
2011 | || typecode_p (return_type, BOOLEAN_TYPE) | |
2012 | || typecode_p (return_type, ENUMERAL_TYPE)) | |
2013 | TYPE_HAS_INT_CONVERSION (t) = 1; | |
2014 | else if (typecode_p (return_type, REAL_TYPE)) | |
2015 | TYPE_HAS_REAL_CONVERSION (t) = 1; | |
2016 | ||
8ccc31eb | 2017 | grow_method (fn_fields, &method_vec); |
e1cd6e56 | 2018 | } |
e1cd6e56 MS |
2019 | } |
2020 | ||
72b7eeff MS |
2021 | fn_fields = save_fn_fields; |
2022 | for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields)) | |
e1cd6e56 | 2023 | { |
e1cd6e56 | 2024 | tree fn_name = DECL_NAME (fn_fields); |
e1cd6e56 | 2025 | |
fc378698 | 2026 | if (fn_name == ctor_name || IDENTIFIER_TYPENAME_P (fn_name)) |
72b7eeff | 2027 | continue; |
e1cd6e56 MS |
2028 | |
2029 | if (fn_name == ansi_opname[(int) MODIFY_EXPR]) | |
8d08fdba MS |
2030 | { |
2031 | tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields)); | |
2032 | ||
a292b002 | 2033 | if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields))) |
8d08fdba MS |
2034 | { |
2035 | if (TREE_PROTECTED (fn_fields)) | |
2036 | TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1; | |
2037 | else if (TREE_PRIVATE (fn_fields)) | |
2038 | TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2; | |
2039 | } | |
2040 | } | |
2041 | ||
8ccc31eb | 2042 | grow_method (fn_fields, &method_vec); |
8d08fdba MS |
2043 | } |
2044 | ||
2045 | TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack) | |
2046 | - (&TREE_VEC_ELT (method_vec, 0)); | |
2047 | obstack_finish (&class_obstack); | |
2048 | CLASSTYPE_METHOD_VEC (t) = method_vec; | |
2049 | ||
2050 | if (nonprivate_method == 0 | |
2051 | && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE | |
d2e5ee5c | 2052 | && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE) |
8d08fdba MS |
2053 | { |
2054 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (t)); | |
2055 | for (i = 0; i < n_baseclasses; i++) | |
2056 | if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i)) | |
2057 | || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i))) | |
2058 | { | |
2059 | nonprivate_method = 1; | |
2060 | break; | |
2061 | } | |
42976354 BK |
2062 | if (nonprivate_method == 0 |
2063 | && warn_ctor_dtor_privacy) | |
8d08fdba MS |
2064 | cp_warning ("all member functions in class `%T' are private", t); |
2065 | } | |
2066 | ||
fc378698 MS |
2067 | /* Warn if all destructors are private (in which case this class is |
2068 | effectively unusable. */ | |
8d08fdba MS |
2069 | if (TYPE_HAS_DESTRUCTOR (t)) |
2070 | { | |
fc378698 | 2071 | tree dtor = TREE_VEC_ELT (method_vec, 1); |
8d08fdba MS |
2072 | |
2073 | /* Wild parse errors can cause this to happen. */ | |
2074 | if (dtor == NULL_TREE) | |
2075 | TYPE_HAS_DESTRUCTOR (t) = 0; | |
fc378698 MS |
2076 | else if (TREE_PRIVATE (dtor) |
2077 | && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE | |
d2e5ee5c | 2078 | && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE |
fc378698 MS |
2079 | && warn_ctor_dtor_privacy) |
2080 | cp_warning ("`%#T' only defines a private destructor and has no friends", | |
2081 | t); | |
8d08fdba MS |
2082 | } |
2083 | ||
2084 | /* Now for each member function (except for constructors and | |
2085 | destructors), compute where member functions of the same | |
2086 | name reside in base classes. */ | |
2087 | if (n_baseclasses != 0 | |
fc378698 | 2088 | && TREE_VEC_LENGTH (method_vec) > 2) |
8d08fdba MS |
2089 | { |
2090 | int len = TREE_VEC_LENGTH (method_vec); | |
2091 | tree baselink_vec = make_tree_vec (len); | |
2092 | int any_links = 0; | |
2093 | tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t)); | |
2094 | ||
fc378698 | 2095 | for (i = 2; i < len; i++) |
8d08fdba MS |
2096 | { |
2097 | TREE_VEC_ELT (baselink_vec, i) | |
2098 | = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i))); | |
2099 | if (TREE_VEC_ELT (baselink_vec, i) != 0) | |
2100 | any_links = 1; | |
2101 | } | |
2102 | if (any_links != 0) | |
2103 | CLASSTYPE_BASELINK_VEC (t) = baselink_vec; | |
2104 | else | |
2105 | obstack_free (current_obstack, baselink_vec); | |
2106 | } | |
2107 | ||
8d08fdba MS |
2108 | return method_vec; |
2109 | } | |
2110 | ||
e92cc029 | 2111 | /* Emit error when a duplicate definition of a type is seen. Patch up. */ |
8d08fdba MS |
2112 | |
2113 | void | |
2114 | duplicate_tag_error (t) | |
2115 | tree t; | |
2116 | { | |
2117 | cp_error ("redefinition of `%#T'", t); | |
b7484fbe | 2118 | cp_error_at ("previous definition here", t); |
8d08fdba MS |
2119 | |
2120 | /* Pretend we haven't defined this type. */ | |
2121 | ||
2122 | /* All of the component_decl's were TREE_CHAINed together in the parser. | |
2123 | finish_struct_methods walks these chains and assembles all methods with | |
2124 | the same base name into DECL_CHAINs. Now we don't need the parser chains | |
e92cc029 MS |
2125 | anymore, so we unravel them. */ |
2126 | ||
2127 | /* This used to be in finish_struct, but it turns out that the | |
2128 | TREE_CHAIN is used by dbxout_type_methods and perhaps some other | |
2129 | things... */ | |
fc378698 | 2130 | if (CLASSTYPE_METHOD_VEC (t)) |
8d08fdba | 2131 | { |
fc378698 MS |
2132 | tree method_vec = CLASSTYPE_METHOD_VEC (t); |
2133 | int i, len = TREE_VEC_LENGTH (method_vec); | |
8d08fdba MS |
2134 | for (i = 0; i < len; i++) |
2135 | { | |
fc378698 | 2136 | tree unchain = TREE_VEC_ELT (method_vec, i); |
8d08fdba MS |
2137 | while (unchain != NULL_TREE) |
2138 | { | |
2139 | TREE_CHAIN (unchain) = NULL_TREE; | |
fc378698 | 2140 | unchain = DECL_CHAIN (unchain); |
8d08fdba MS |
2141 | } |
2142 | } | |
2143 | } | |
2144 | ||
2145 | if (TYPE_LANG_SPECIFIC (t)) | |
2146 | { | |
2147 | tree as_list = CLASSTYPE_AS_LIST (t); | |
2148 | tree binfo = TYPE_BINFO (t); | |
2149 | tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t); | |
2150 | int interface_only = CLASSTYPE_INTERFACE_ONLY (t); | |
2151 | int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t); | |
2152 | ||
1daa5dd8 | 2153 | bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type)); |
8d08fdba MS |
2154 | BINFO_BASETYPES(binfo) = NULL_TREE; |
2155 | ||
2156 | CLASSTYPE_AS_LIST (t) = as_list; | |
2157 | TYPE_BINFO (t) = binfo; | |
2158 | CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list; | |
2159 | CLASSTYPE_INTERFACE_ONLY (t) = interface_only; | |
2160 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown); | |
2161 | CLASSTYPE_VBASE_SIZE (t) = integer_zero_node; | |
2162 | TYPE_REDEFINED (t) = 1; | |
2163 | } | |
2164 | TYPE_SIZE (t) = NULL_TREE; | |
2165 | TYPE_MODE (t) = VOIDmode; | |
2166 | TYPE_FIELDS (t) = NULL_TREE; | |
2167 | TYPE_METHODS (t) = NULL_TREE; | |
2168 | TYPE_VFIELD (t) = NULL_TREE; | |
2169 | TYPE_CONTEXT (t) = NULL_TREE; | |
2170 | } | |
2171 | ||
e92cc029 MS |
2172 | /* finish up all new vtables. */ |
2173 | ||
7177d104 MS |
2174 | static void |
2175 | finish_vtbls (binfo, do_self, t) | |
6b5fbb55 | 2176 | tree binfo; |
7177d104 | 2177 | int do_self; |
6b5fbb55 | 2178 | tree t; |
7177d104 MS |
2179 | { |
2180 | tree binfos = BINFO_BASETYPES (binfo); | |
2181 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2182 | ||
2183 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2184 | if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2185 | { | |
2186 | if (BINFO_NEW_VTABLE_MARKED (binfo)) | |
2187 | { | |
2188 | tree decl, context; | |
2189 | ||
2190 | decl = BINFO_VTABLE (binfo); | |
2191 | context = DECL_CONTEXT (decl); | |
2192 | DECL_CONTEXT (decl) = 0; | |
2193 | if (write_virtuals >= 0 | |
2194 | && DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo)) | |
2195 | DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, | |
2196 | BINFO_VIRTUALS (binfo)); | |
b3417a04 | 2197 | cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0); |
7177d104 MS |
2198 | DECL_CONTEXT (decl) = context; |
2199 | } | |
2200 | CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); | |
2201 | } | |
2202 | ||
2203 | for (i = 0; i < n_baselinks; i++) | |
2204 | { | |
2205 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
2206 | int is_not_base_vtable |
2207 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
7177d104 MS |
2208 | if (TREE_VIA_VIRTUAL (base_binfo)) |
2209 | { | |
2210 | base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t)); | |
2211 | } | |
44a8d0b3 | 2212 | finish_vtbls (base_binfo, is_not_base_vtable, t); |
7177d104 MS |
2213 | } |
2214 | } | |
2215 | ||
2216 | /* True if we should override the given BASE_FNDECL with the given | |
2217 | FNDECL. */ | |
e92cc029 | 2218 | |
7177d104 MS |
2219 | static int |
2220 | overrides (fndecl, base_fndecl) | |
2221 | tree fndecl, base_fndecl; | |
2222 | { | |
e92cc029 | 2223 | /* Destructors have special names. */ |
beb53fb8 JM |
2224 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) |
2225 | && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl))) | |
7177d104 | 2226 | return 1; |
beb53fb8 JM |
2227 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) |
2228 | || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl))) | |
7177d104 MS |
2229 | return 0; |
2230 | if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)) | |
2231 | { | |
5566b478 | 2232 | tree types, base_types; |
7177d104 MS |
2233 | #if 0 |
2234 | retypes = TREE_TYPE (TREE_TYPE (fndecl)); | |
2235 | base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl)); | |
2236 | #endif | |
2237 | types = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
2238 | base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl)); | |
2239 | if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (base_types))) | |
2240 | == TYPE_READONLY (TREE_TYPE (TREE_VALUE (types)))) | |
2241 | && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types), 3)) | |
2242 | return 1; | |
2243 | } | |
2244 | return 0; | |
2245 | } | |
2246 | ||
a292b002 MS |
2247 | static tree |
2248 | get_class_offset_1 (parent, binfo, context, t, fndecl) | |
2249 | tree parent, binfo, context, t, fndecl; | |
2250 | { | |
2251 | tree binfos = BINFO_BASETYPES (binfo); | |
2252 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2253 | tree rval = NULL_TREE; | |
2254 | ||
2255 | if (binfo == parent) | |
2256 | return error_mark_node; | |
2257 | ||
2258 | for (i = 0; i < n_baselinks; i++) | |
2259 | { | |
2260 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2261 | tree nrval; | |
2262 | ||
2263 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
2264 | base_binfo = binfo_member (BINFO_TYPE (base_binfo), | |
2265 | CLASSTYPE_VBASECLASSES (t)); | |
2266 | nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl); | |
2267 | /* See if we have a new value */ | |
2268 | if (nrval && (nrval != error_mark_node || rval==0)) | |
2269 | { | |
2270 | /* Only compare if we have two offsets */ | |
2271 | if (rval && rval != error_mark_node | |
2272 | && ! tree_int_cst_equal (nrval, rval)) | |
2273 | { | |
2274 | /* Only give error if the two offsets are different */ | |
2275 | error ("every virtual function must have a unique final overrider"); | |
2276 | cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t); | |
2277 | cp_error (" with virtual `%D' from virtual base class", fndecl); | |
2278 | return rval; | |
2279 | } | |
2280 | rval = nrval; | |
2281 | } | |
2282 | ||
2283 | if (rval && BINFO_TYPE (binfo) == context) | |
2284 | { | |
2285 | my_friendly_assert (rval == error_mark_node | |
2286 | || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999); | |
2287 | rval = BINFO_OFFSET (binfo); | |
2288 | } | |
2289 | } | |
2290 | return rval; | |
2291 | } | |
2292 | ||
2293 | /* Get the offset to the CONTEXT subobject that is related to the | |
2294 | given BINFO. */ | |
e92cc029 | 2295 | |
a292b002 MS |
2296 | static tree |
2297 | get_class_offset (context, t, binfo, fndecl) | |
2298 | tree context, t, binfo, fndecl; | |
2299 | { | |
2300 | tree first_binfo = binfo; | |
2301 | tree offset; | |
2302 | int i; | |
2303 | ||
2304 | if (context == t) | |
2305 | return integer_zero_node; | |
2306 | ||
2307 | if (BINFO_TYPE (binfo) == context) | |
2308 | return BINFO_OFFSET (binfo); | |
2309 | ||
2310 | /* Check less derived binfos first. */ | |
2311 | while (BINFO_BASETYPES (binfo) | |
2312 | && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1) | |
2313 | { | |
2314 | tree binfos = BINFO_BASETYPES (binfo); | |
2315 | binfo = TREE_VEC_ELT (binfos, i); | |
2316 | if (BINFO_TYPE (binfo) == context) | |
2317 | return BINFO_OFFSET (binfo); | |
2318 | } | |
2319 | ||
2320 | /* Ok, not found in the less derived binfos, now check the more | |
e92cc029 | 2321 | derived binfos. */ |
a292b002 MS |
2322 | offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl); |
2323 | if (offset==0 || TREE_CODE (offset) != INTEGER_CST) | |
2324 | my_friendly_abort (999); /* we have to find it. */ | |
2325 | return offset; | |
2326 | } | |
2327 | ||
f30432d7 | 2328 | /* Skip RTTI information at the front of the virtual list. */ |
e92cc029 | 2329 | |
f30432d7 MS |
2330 | unsigned HOST_WIDE_INT |
2331 | skip_rtti_stuff (virtuals) | |
2332 | tree *virtuals; | |
2333 | { | |
2334 | int n; | |
2335 | ||
2336 | n = 0; | |
2337 | if (*virtuals) | |
2338 | { | |
2339 | /* We always reserve a slot for the offset/tdesc entry. */ | |
2340 | ++n; | |
2341 | *virtuals = TREE_CHAIN (*virtuals); | |
2342 | } | |
2343 | if (flag_vtable_thunks && *virtuals) | |
2344 | { | |
2345 | /* The second slot is reserved for the tdesc pointer when thunks | |
2346 | are used. */ | |
2347 | ++n; | |
2348 | *virtuals = TREE_CHAIN (*virtuals); | |
2349 | } | |
2350 | return n; | |
2351 | } | |
2352 | ||
7177d104 MS |
2353 | static void |
2354 | modify_one_vtable (binfo, t, fndecl, pfn) | |
2355 | tree binfo, t, fndecl, pfn; | |
2356 | { | |
39211cd5 | 2357 | tree virtuals = BINFO_VIRTUALS (binfo); |
7177d104 MS |
2358 | unsigned HOST_WIDE_INT n; |
2359 | ||
db5ae43f MS |
2360 | /* update rtti entry */ |
2361 | if (flag_rtti) | |
2362 | { | |
2363 | if (binfo == TYPE_BINFO (t)) | |
2364 | { | |
2365 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2366 | build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t); | |
2367 | } | |
2368 | else | |
2369 | { | |
2370 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2371 | prepare_fresh_vtable (binfo, t); | |
2372 | } | |
db5ae43f | 2373 | } |
f30432d7 MS |
2374 | if (fndecl == NULL_TREE) |
2375 | return; | |
2376 | ||
2377 | n = skip_rtti_stuff (&virtuals); | |
db5ae43f | 2378 | |
7177d104 MS |
2379 | while (virtuals) |
2380 | { | |
2381 | tree current_fndecl = TREE_VALUE (virtuals); | |
2382 | current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl); | |
2383 | current_fndecl = TREE_OPERAND (current_fndecl, 0); | |
2384 | if (current_fndecl && overrides (fndecl, current_fndecl)) | |
2385 | { | |
2386 | tree base_offset, offset; | |
2387 | tree context = DECL_CLASS_CONTEXT (fndecl); | |
2388 | tree vfield = CLASSTYPE_VFIELD (t); | |
2389 | tree this_offset; | |
2390 | ||
a292b002 | 2391 | offset = get_class_offset (context, t, binfo, fndecl); |
7177d104 | 2392 | |
2986ae00 MS |
2393 | /* Find the right offset for the this pointer based on the |
2394 | base class we just found. We have to take into | |
2395 | consideration the virtual base class pointers that we | |
a0a33927 MS |
2396 | stick in before the virtual function table pointer. |
2397 | ||
ddd5a7c1 | 2398 | Also, we want just the delta between the most base class |
a0a33927 MS |
2399 | that we derived this vfield from and us. */ |
2400 | base_offset = size_binop (PLUS_EXPR, | |
13306d4f | 2401 | get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)), |
a0a33927 | 2402 | BINFO_OFFSET (binfo)); |
7177d104 MS |
2403 | this_offset = size_binop (MINUS_EXPR, offset, base_offset); |
2404 | ||
2405 | /* Make sure we can modify the derived association with immunity. */ | |
8ccc31eb | 2406 | if (TREE_USED (binfo)) |
7177d104 | 2407 | my_friendly_assert (0, 999); |
8ccc31eb | 2408 | |
7177d104 MS |
2409 | if (binfo == TYPE_BINFO (t)) |
2410 | { | |
2411 | /* In this case, it is *type*'s vtable we are modifying. | |
2412 | We start with the approximation that it's vtable is that | |
2413 | of the immediate base class. */ | |
2414 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2415 | build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t); | |
2416 | } | |
2417 | else | |
2418 | { | |
2419 | /* This is our very own copy of `basetype' to play with. | |
2420 | Later, we will fill in all the virtual functions | |
2421 | that override the virtual functions in these base classes | |
2422 | which are not defined by the current type. */ | |
2423 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2424 | prepare_fresh_vtable (binfo, t); | |
2425 | } | |
2426 | ||
2427 | #ifdef NOTQUITE | |
2428 | cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo))); | |
2429 | #endif | |
2430 | modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n), | |
2431 | build_vtable_entry (this_offset, pfn), | |
2432 | fndecl); | |
2433 | } | |
2434 | ++n; | |
2435 | virtuals = TREE_CHAIN (virtuals); | |
2436 | } | |
2437 | } | |
2438 | ||
e92cc029 MS |
2439 | /* These are the ones that are not through virtual base classes. */ |
2440 | ||
7177d104 MS |
2441 | static void |
2442 | modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn) | |
6b5fbb55 | 2443 | tree binfo; |
7177d104 | 2444 | int do_self; |
6b5fbb55 | 2445 | tree t, fndecl, pfn; |
7177d104 MS |
2446 | { |
2447 | tree binfos = BINFO_BASETYPES (binfo); | |
2448 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2449 | ||
2450 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2451 | if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2452 | { | |
2453 | modify_one_vtable (binfo, t, fndecl, pfn); | |
2454 | } | |
2455 | ||
2456 | for (i = 0; i < n_baselinks; i++) | |
2457 | { | |
2458 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
2459 | int is_not_base_vtable |
2460 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
7177d104 | 2461 | if (! TREE_VIA_VIRTUAL (base_binfo)) |
44a8d0b3 | 2462 | modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn); |
7177d104 MS |
2463 | } |
2464 | } | |
2465 | ||
43f2999d | 2466 | /* Fixup all the delta entries in this one vtable that need updating. */ |
e92cc029 | 2467 | |
21474714 | 2468 | static void |
43f2999d | 2469 | fixup_vtable_deltas1 (binfo, t) |
21474714 MS |
2470 | tree binfo, t; |
2471 | { | |
2472 | tree virtuals = BINFO_VIRTUALS (binfo); | |
2473 | unsigned HOST_WIDE_INT n; | |
2474 | ||
f30432d7 MS |
2475 | n = skip_rtti_stuff (&virtuals); |
2476 | ||
21474714 MS |
2477 | while (virtuals) |
2478 | { | |
2479 | tree fndecl = TREE_VALUE (virtuals); | |
2480 | tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl); | |
2481 | tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl); | |
2482 | fndecl = TREE_OPERAND (pfn, 0); | |
2483 | if (fndecl) | |
2484 | { | |
2485 | tree base_offset, offset; | |
2486 | tree context = DECL_CLASS_CONTEXT (fndecl); | |
2487 | tree vfield = CLASSTYPE_VFIELD (t); | |
2488 | tree this_offset; | |
2489 | ||
a292b002 | 2490 | offset = get_class_offset (context, t, binfo, fndecl); |
21474714 MS |
2491 | |
2492 | /* Find the right offset for the this pointer based on the | |
2493 | base class we just found. We have to take into | |
2494 | consideration the virtual base class pointers that we | |
2495 | stick in before the virtual function table pointer. | |
2496 | ||
ddd5a7c1 | 2497 | Also, we want just the delta between the most base class |
21474714 MS |
2498 | that we derived this vfield from and us. */ |
2499 | base_offset = size_binop (PLUS_EXPR, | |
13306d4f | 2500 | get_derived_offset (binfo, DECL_CONTEXT (fndecl)), |
21474714 MS |
2501 | BINFO_OFFSET (binfo)); |
2502 | this_offset = size_binop (MINUS_EXPR, offset, base_offset); | |
2503 | ||
2504 | if (! tree_int_cst_equal (this_offset, delta)) | |
2505 | { | |
2506 | /* Make sure we can modify the derived association with immunity. */ | |
2507 | if (TREE_USED (binfo)) | |
2508 | my_friendly_assert (0, 999); | |
2509 | ||
2510 | if (binfo == TYPE_BINFO (t)) | |
2511 | { | |
2512 | /* In this case, it is *type*'s vtable we are modifying. | |
2513 | We start with the approximation that it's vtable is that | |
2514 | of the immediate base class. */ | |
2515 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2516 | build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t); | |
2517 | } | |
2518 | else | |
2519 | { | |
2520 | /* This is our very own copy of `basetype' to play with. | |
2521 | Later, we will fill in all the virtual functions | |
2522 | that override the virtual functions in these base classes | |
2523 | which are not defined by the current type. */ | |
2524 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2525 | prepare_fresh_vtable (binfo, t); | |
2526 | } | |
2527 | ||
2528 | modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n), | |
2529 | build_vtable_entry (this_offset, pfn), | |
2530 | fndecl); | |
2531 | } | |
2532 | } | |
2533 | ++n; | |
2534 | virtuals = TREE_CHAIN (virtuals); | |
2535 | } | |
2536 | } | |
2537 | ||
43f2999d MS |
2538 | /* Fixup all the delta entries in all the direct vtables that need updating. |
2539 | This happens when we have non-overridden virtual functions from a | |
2540 | virtual base class, that are at a different offset, in the new | |
2541 | hierarchy, because the layout of the virtual bases has changed. */ | |
e92cc029 | 2542 | |
43f2999d MS |
2543 | static void |
2544 | fixup_vtable_deltas (binfo, init_self, t) | |
6b5fbb55 | 2545 | tree binfo; |
43f2999d | 2546 | int init_self; |
6b5fbb55 | 2547 | tree t; |
43f2999d MS |
2548 | { |
2549 | tree binfos = BINFO_BASETYPES (binfo); | |
2550 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2551 | ||
2552 | for (i = 0; i < n_baselinks; i++) | |
2553 | { | |
2554 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
2555 | int is_not_base_vtable |
2556 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
43f2999d MS |
2557 | if (! TREE_VIA_VIRTUAL (base_binfo)) |
2558 | fixup_vtable_deltas (base_binfo, is_not_base_vtable, t); | |
2559 | } | |
2560 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2561 | if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2562 | { | |
2563 | fixup_vtable_deltas1 (binfo, t); | |
2564 | } | |
2565 | } | |
2566 | ||
e92cc029 MS |
2567 | /* These are the ones that are through virtual base classes. */ |
2568 | ||
7177d104 MS |
2569 | static void |
2570 | modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn) | |
6b5fbb55 | 2571 | tree binfo; |
7177d104 | 2572 | int do_self, via_virtual; |
6b5fbb55 | 2573 | tree t, fndecl, pfn; |
7177d104 MS |
2574 | { |
2575 | tree binfos = BINFO_BASETYPES (binfo); | |
2576 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2577 | ||
2578 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2579 | if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2580 | { | |
2581 | modify_one_vtable (binfo, t, fndecl, pfn); | |
2582 | } | |
2583 | ||
2584 | for (i = 0; i < n_baselinks; i++) | |
2585 | { | |
2586 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
2587 | int is_not_base_vtable |
2588 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
7177d104 MS |
2589 | if (TREE_VIA_VIRTUAL (base_binfo)) |
2590 | { | |
2591 | via_virtual = 1; | |
2592 | base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t)); | |
2593 | } | |
2594 | modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn); | |
2595 | } | |
2596 | } | |
2597 | ||
2598 | static void | |
2599 | modify_all_vtables (t, fndecl, vfn) | |
2600 | tree t, fndecl, vfn; | |
2601 | { | |
2602 | /* Do these first, so that we will make use of any non-virtual class's | |
e92cc029 | 2603 | vtable, over a virtual classes vtable. */ |
7177d104 MS |
2604 | modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn); |
2605 | if (TYPE_USES_VIRTUAL_BASECLASSES (t)) | |
2606 | modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn); | |
2607 | } | |
2608 | ||
39211cd5 MS |
2609 | /* Here, we already know that they match in every respect. |
2610 | All we have to check is where they had their declarations. */ | |
e92cc029 | 2611 | |
39211cd5 MS |
2612 | static int |
2613 | strictly_overrides (fndecl1, fndecl2) | |
2614 | tree fndecl1, fndecl2; | |
2615 | { | |
2616 | int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2), | |
2617 | DECL_CLASS_CONTEXT (fndecl1), | |
2618 | 0, (tree *)0); | |
2619 | if (distance == -2 || distance > 0) | |
2620 | return 1; | |
2621 | return 0; | |
2622 | } | |
2623 | ||
2624 | /* Merge overrides for one vtable. | |
2625 | If we want to merge in same function, we are fine. | |
2626 | else | |
2627 | if one has a DECL_CLASS_CONTEXT that is a parent of the | |
2628 | other, than choose the more derived one | |
2629 | else | |
2630 | potentially ill-formed (see 10.3 [class.virtual]) | |
2631 | we have to check later to see if there was an | |
2632 | override in this class. If there was ok, if not | |
2633 | then it is ill-formed. (mrs) | |
2634 | ||
2635 | We take special care to reuse a vtable, if we can. */ | |
e92cc029 | 2636 | |
39211cd5 MS |
2637 | static void |
2638 | override_one_vtable (binfo, old, t) | |
2639 | tree binfo, old, t; | |
2640 | { | |
2641 | tree virtuals = BINFO_VIRTUALS (binfo); | |
2642 | tree old_virtuals = BINFO_VIRTUALS (old); | |
2643 | enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED; | |
2644 | ||
2645 | /* If we have already committed to modifying it, then don't try and | |
e92cc029 | 2646 | reuse another vtable. */ |
39211cd5 MS |
2647 | if (BINFO_NEW_VTABLE_MARKED (binfo)) |
2648 | choose = NEITHER; | |
2649 | ||
f30432d7 MS |
2650 | skip_rtti_stuff (&virtuals); |
2651 | skip_rtti_stuff (&old_virtuals); | |
39211cd5 MS |
2652 | |
2653 | while (virtuals) | |
2654 | { | |
2655 | tree fndecl = TREE_VALUE (virtuals); | |
2656 | tree old_fndecl = TREE_VALUE (old_virtuals); | |
2657 | fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl); | |
2658 | old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl); | |
2659 | fndecl = TREE_OPERAND (fndecl, 0); | |
2660 | old_fndecl = TREE_OPERAND (old_fndecl, 0); | |
e92cc029 | 2661 | /* First check to see if they are the same. */ |
39211cd5 MS |
2662 | if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl)) |
2663 | { | |
e92cc029 | 2664 | /* No need to do anything. */ |
39211cd5 MS |
2665 | } |
2666 | else if (strictly_overrides (fndecl, old_fndecl)) | |
2667 | { | |
2668 | if (choose == UNDECIDED) | |
2669 | choose = REUSE_NEW; | |
2670 | else if (choose == REUSE_OLD) | |
2671 | { | |
2672 | choose = NEITHER; | |
2673 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2674 | { | |
2675 | prepare_fresh_vtable (binfo, t); | |
2676 | override_one_vtable (binfo, old, t); | |
2677 | return; | |
2678 | } | |
2679 | } | |
2680 | } | |
2681 | else if (strictly_overrides (old_fndecl, fndecl)) | |
2682 | { | |
2683 | if (choose == UNDECIDED) | |
2684 | choose = REUSE_OLD; | |
2685 | else if (choose == REUSE_NEW) | |
2686 | { | |
2687 | choose = NEITHER; | |
2688 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2689 | { | |
2690 | prepare_fresh_vtable (binfo, t); | |
2691 | override_one_vtable (binfo, old, t); | |
2692 | return; | |
2693 | } | |
a0a33927 | 2694 | TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals); |
39211cd5 | 2695 | } |
a3203465 MS |
2696 | else if (choose == NEITHER) |
2697 | { | |
2698 | TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals); | |
2699 | } | |
39211cd5 MS |
2700 | } |
2701 | else | |
2702 | { | |
2703 | choose = NEITHER; | |
2704 | if (! BINFO_NEW_VTABLE_MARKED (binfo)) | |
2705 | { | |
2706 | prepare_fresh_vtable (binfo, t); | |
2707 | override_one_vtable (binfo, old, t); | |
2708 | return; | |
2709 | } | |
2710 | { | |
ddd5a7c1 | 2711 | /* This MUST be overridden, or the class is ill-formed. */ |
39211cd5 MS |
2712 | /* For now, we just make it abstract. */ |
2713 | tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0); | |
2714 | tree vfn; | |
2715 | ||
2716 | fndecl = copy_node (fndecl); | |
2717 | copy_lang_decl (fndecl); | |
2718 | DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1; | |
4a67c9e9 | 2719 | DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1; |
e92cc029 | 2720 | /* Make sure we search for it later. */ |
39211cd5 MS |
2721 | if (! CLASSTYPE_ABSTRACT_VIRTUALS (t)) |
2722 | CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node; | |
2723 | ||
700f8a87 | 2724 | vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl); |
39211cd5 MS |
2725 | TREE_CONSTANT (vfn) = 1; |
2726 | ||
2727 | /* We can use integer_zero_node, as we will will core dump | |
e92cc029 | 2728 | if this is used anyway. */ |
39211cd5 MS |
2729 | TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn); |
2730 | } | |
2731 | } | |
2732 | virtuals = TREE_CHAIN (virtuals); | |
2733 | old_virtuals = TREE_CHAIN (old_virtuals); | |
2734 | } | |
2735 | ||
e92cc029 | 2736 | /* Let's reuse the old vtable. */ |
39211cd5 MS |
2737 | if (choose == REUSE_OLD) |
2738 | { | |
2739 | BINFO_VTABLE (binfo) = BINFO_VTABLE (old); | |
2740 | BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old); | |
2741 | } | |
2742 | } | |
2743 | ||
2744 | /* Merge in overrides for virtual bases. | |
2745 | BINFO is the hierarchy we want to modify, and OLD has the potential | |
2746 | overrides. */ | |
e92cc029 | 2747 | |
39211cd5 MS |
2748 | static void |
2749 | merge_overrides (binfo, old, do_self, t) | |
6b5fbb55 | 2750 | tree binfo, old; |
39211cd5 | 2751 | int do_self; |
6b5fbb55 | 2752 | tree t; |
39211cd5 MS |
2753 | { |
2754 | tree binfos = BINFO_BASETYPES (binfo); | |
2755 | tree old_binfos = BINFO_BASETYPES (old); | |
2756 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2757 | ||
2758 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2759 | if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2760 | { | |
2761 | override_one_vtable (binfo, old, t); | |
2762 | } | |
2763 | ||
2764 | for (i = 0; i < n_baselinks; i++) | |
2765 | { | |
2766 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2767 | tree old_base_binfo = TREE_VEC_ELT (old_binfos, i); | |
beb53fb8 JM |
2768 | int is_not_base_vtable |
2769 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
39211cd5 MS |
2770 | if (! TREE_VIA_VIRTUAL (base_binfo)) |
2771 | merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t); | |
2772 | } | |
2773 | } | |
2774 | ||
9e9ff709 MS |
2775 | /* Get the base virtual function declarations in T that are either |
2776 | overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with | |
2777 | the overrider/hider. */ | |
e92cc029 | 2778 | |
5ddc28a5 | 2779 | static tree |
9e9ff709 MS |
2780 | get_basefndecls (fndecl, t) |
2781 | tree fndecl, t; | |
2782 | { | |
2783 | tree methods = TYPE_METHODS (t); | |
2784 | tree base_fndecls = NULL_TREE; | |
2785 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (t)); | |
2786 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2787 | ||
2788 | while (methods) | |
2789 | { | |
9e9ff709 MS |
2790 | if (TREE_CODE (methods) == FUNCTION_DECL |
2791 | && DECL_VINDEX (methods) != NULL_TREE | |
2792 | && DECL_NAME (fndecl) == DECL_NAME (methods)) | |
2793 | base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls); | |
2794 | ||
2795 | methods = TREE_CHAIN (methods); | |
2796 | } | |
2797 | ||
2798 | if (base_fndecls) | |
2799 | return base_fndecls; | |
2800 | ||
2801 | for (i = 0; i < n_baseclasses; i++) | |
2802 | { | |
2803 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2804 | tree basetype = BINFO_TYPE (base_binfo); | |
9e9ff709 MS |
2805 | |
2806 | base_fndecls = chainon (get_basefndecls (fndecl, basetype), | |
2807 | base_fndecls); | |
2808 | } | |
2809 | ||
2810 | return base_fndecls; | |
2811 | } | |
2812 | ||
2813 | /* Mark the functions that have been hidden with their overriders. | |
2814 | Since we start out with all functions already marked with a hider, | |
2815 | no need to mark functions that are just hidden. */ | |
e92cc029 | 2816 | |
bd6dd845 | 2817 | static void |
9e9ff709 MS |
2818 | mark_overriders (fndecl, base_fndecls) |
2819 | tree fndecl, base_fndecls; | |
2820 | { | |
2821 | while (base_fndecls) | |
2822 | { | |
2823 | if (overrides (TREE_VALUE (base_fndecls), fndecl)) | |
2824 | TREE_PURPOSE (base_fndecls) = fndecl; | |
2825 | ||
2826 | base_fndecls = TREE_CHAIN (base_fndecls); | |
2827 | } | |
2828 | } | |
2829 | ||
2ee887f2 MS |
2830 | /* If this declaration supersedes the declaration of |
2831 | a method declared virtual in the base class, then | |
2832 | mark this field as being virtual as well. */ | |
2833 | ||
bd6dd845 | 2834 | static void |
cffa8729 | 2835 | check_for_override (decl, ctype) |
2ee887f2 MS |
2836 | tree decl, ctype; |
2837 | { | |
2838 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype)); | |
2839 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2840 | int virtualp = DECL_VIRTUAL_P (decl); | |
2841 | ||
2842 | for (i = 0; i < n_baselinks; i++) | |
2843 | { | |
2844 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2845 | if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)) | |
2846 | || flag_all_virtual == 1) | |
2847 | { | |
2848 | tree tmp = get_matching_virtual | |
2849 | (base_binfo, decl, | |
2850 | DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl))); | |
2851 | if (tmp) | |
2852 | { | |
2853 | /* If this function overrides some virtual in some base | |
2854 | class, then the function itself is also necessarily | |
2855 | virtual, even if the user didn't explicitly say so. */ | |
2856 | DECL_VIRTUAL_P (decl) = 1; | |
2857 | ||
2858 | /* The TMP we really want is the one from the deepest | |
2859 | baseclass on this path, taking care not to | |
2860 | duplicate if we have already found it (via another | |
2861 | path to its virtual baseclass. */ | |
2862 | if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE) | |
2863 | { | |
2864 | cp_error_at ("method `%D' may not be declared static", | |
2865 | decl); | |
2866 | cp_error_at ("(since `%D' declared virtual in base class.)", | |
2867 | tmp); | |
2868 | break; | |
2869 | } | |
2870 | virtualp = 1; | |
2871 | ||
eb66be0e | 2872 | #if 0 /* The signature of an overriding function is not changed. */ |
2ee887f2 | 2873 | { |
e92cc029 | 2874 | /* The argument types may have changed... */ |
2ee887f2 MS |
2875 | tree type = TREE_TYPE (decl); |
2876 | tree argtypes = TYPE_ARG_TYPES (type); | |
2877 | tree base_variant = TREE_TYPE (TREE_VALUE (argtypes)); | |
2878 | tree raises = TYPE_RAISES_EXCEPTIONS (type); | |
2879 | ||
2880 | argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))), | |
2881 | TREE_CHAIN (argtypes)); | |
2882 | /* But the return type has not. */ | |
2883 | type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes); | |
2884 | if (raises) | |
2885 | type = build_exception_variant (type, raises); | |
2886 | TREE_TYPE (decl) = type; | |
2ee887f2 | 2887 | } |
eb66be0e MS |
2888 | #endif |
2889 | DECL_VINDEX (decl) | |
2890 | = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl)); | |
2ee887f2 MS |
2891 | break; |
2892 | } | |
2893 | } | |
2894 | } | |
2895 | if (virtualp) | |
2896 | { | |
2897 | if (DECL_VINDEX (decl) == NULL_TREE) | |
2898 | DECL_VINDEX (decl) = error_mark_node; | |
2899 | IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; | |
2900 | } | |
2901 | } | |
2902 | ||
fc378698 MS |
2903 | /* Warn about hidden virtual functions that are not overridden in t. |
2904 | We know that constructors and destructors don't apply. */ | |
e92cc029 | 2905 | |
9e9ff709 MS |
2906 | void |
2907 | warn_hidden (t) | |
2908 | tree t; | |
2909 | { | |
2910 | tree method_vec = CLASSTYPE_METHOD_VEC (t); | |
2911 | int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0; | |
2912 | int i; | |
2913 | ||
2914 | /* We go through each separately named virtual function. */ | |
fc378698 | 2915 | for (i = 2; i < n_methods; ++i) |
9e9ff709 MS |
2916 | { |
2917 | tree fndecl = TREE_VEC_ELT (method_vec, i); | |
2918 | ||
2919 | tree base_fndecls = NULL_TREE; | |
2920 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (t)); | |
2921 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2922 | ||
2923 | if (DECL_VINDEX (fndecl) == NULL_TREE) | |
2924 | continue; | |
2925 | ||
2926 | /* First we get a list of all possible functions that might be | |
2927 | hidden from each base class. */ | |
2928 | for (i = 0; i < n_baseclasses; i++) | |
2929 | { | |
2930 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2931 | tree basetype = BINFO_TYPE (base_binfo); | |
2932 | ||
2933 | base_fndecls = chainon (get_basefndecls (fndecl, basetype), | |
2934 | base_fndecls); | |
2935 | } | |
2936 | ||
2937 | if (TREE_CHAIN (fndecl) | |
2938 | && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl)) | |
2939 | fndecl = TREE_CHAIN (fndecl); | |
2940 | else | |
2941 | fndecl = NULL_TREE; | |
2942 | ||
2943 | /* ...then mark up all the base functions with overriders, preferring | |
2944 | overriders to hiders. */ | |
2945 | if (base_fndecls) | |
2946 | while (fndecl) | |
2947 | { | |
2948 | mark_overriders (fndecl, base_fndecls); | |
2949 | ||
2950 | if (TREE_CHAIN (fndecl) | |
2951 | && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl)) | |
2952 | fndecl = TREE_CHAIN (fndecl); | |
2953 | else | |
2954 | fndecl = NULL_TREE; | |
2955 | } | |
2956 | ||
2957 | /* Now give a warning for all base functions without overriders, | |
2958 | as they are hidden. */ | |
2959 | while (base_fndecls) | |
2960 | { | |
2961 | if (! overrides (TREE_VALUE (base_fndecls), | |
2962 | TREE_PURPOSE (base_fndecls))) | |
2963 | { | |
2964 | /* Here we know it is a hider, and no overrider exists. */ | |
2965 | cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls)); | |
2966 | cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls)); | |
2967 | } | |
2968 | ||
2969 | base_fndecls = TREE_CHAIN (base_fndecls); | |
2970 | } | |
2971 | } | |
2972 | } | |
2973 | ||
2974 | /* Check for things that are invalid. There are probably plenty of other | |
2975 | things we should check for also. */ | |
e92cc029 | 2976 | |
9e9ff709 MS |
2977 | static void |
2978 | finish_struct_anon (t) | |
2979 | tree t; | |
2980 | { | |
2981 | tree field; | |
2982 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
2983 | { | |
2984 | if (TREE_STATIC (field)) | |
2985 | continue; | |
2986 | if (TREE_CODE (field) != FIELD_DECL) | |
2987 | continue; | |
2988 | ||
2989 | if (DECL_NAME (field) == NULL_TREE | |
2990 | && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
2991 | { | |
2992 | tree* uelt = &TYPE_FIELDS (TREE_TYPE (field)); | |
2993 | for (; *uelt; uelt = &TREE_CHAIN (*uelt)) | |
2994 | { | |
9e9ff709 MS |
2995 | if (TREE_CODE (*uelt) != FIELD_DECL) |
2996 | continue; | |
2997 | ||
2998 | if (TREE_PRIVATE (*uelt)) | |
2999 | cp_pedwarn_at ("private member `%#D' in anonymous union", | |
3000 | *uelt); | |
3001 | else if (TREE_PROTECTED (*uelt)) | |
3002 | cp_pedwarn_at ("protected member `%#D' in anonymous union", | |
3003 | *uelt); | |
fc378698 MS |
3004 | |
3005 | TREE_PRIVATE (*uelt) = TREE_PRIVATE (field); | |
3006 | TREE_PROTECTED (*uelt) = TREE_PROTECTED (field); | |
9e9ff709 MS |
3007 | } |
3008 | } | |
3009 | } | |
3010 | } | |
3011 | ||
f30432d7 MS |
3012 | extern int interface_only, interface_unknown; |
3013 | ||
8d08fdba MS |
3014 | /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration |
3015 | (or C++ class declaration). | |
3016 | ||
3017 | For C++, we must handle the building of derived classes. | |
3018 | Also, C++ allows static class members. The way that this is | |
3019 | handled is to keep the field name where it is (as the DECL_NAME | |
3020 | of the field), and place the overloaded decl in the DECL_FIELD_BITPOS | |
3021 | of the field. layout_record and layout_union will know about this. | |
3022 | ||
3023 | More C++ hair: inline functions have text in their | |
3024 | DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into | |
3025 | meaningful tree structure. After the struct has been laid out, set | |
3026 | things up so that this can happen. | |
3027 | ||
3028 | And still more: virtual functions. In the case of single inheritance, | |
3029 | when a new virtual function is seen which redefines a virtual function | |
3030 | from the base class, the new virtual function is placed into | |
3031 | the virtual function table at exactly the same address that | |
3032 | it had in the base class. When this is extended to multiple | |
3033 | inheritance, the same thing happens, except that multiple virtual | |
3034 | function tables must be maintained. The first virtual function | |
3035 | table is treated in exactly the same way as in the case of single | |
3036 | inheritance. Additional virtual function tables have different | |
3037 | DELTAs, which tell how to adjust `this' to point to the right thing. | |
3038 | ||
3039 | LIST_OF_FIELDLISTS is just that. The elements of the list are | |
3040 | TREE_LIST elements, whose TREE_PURPOSE field tells what access | |
3041 | the list has, and the TREE_VALUE slot gives the actual fields. | |
3042 | ||
2ee887f2 MS |
3043 | ATTRIBUTES is the set of decl attributes to be applied, if any. |
3044 | ||
8d08fdba MS |
3045 | If flag_all_virtual == 1, then we lay all functions into |
3046 | the virtual function table, as though they were declared | |
3047 | virtual. Constructors do not lay down in the virtual function table. | |
3048 | ||
3049 | If flag_all_virtual == 2, then we lay all functions into | |
3050 | the virtual function table, such that virtual functions | |
3051 | occupy a space by themselves, and then all functions | |
3052 | of the class occupy a space by themselves. This is illustrated | |
3053 | in the following diagram: | |
3054 | ||
3055 | class A; class B : A; | |
3056 | ||
3057 | Class A's vtbl: Class B's vtbl: | |
3058 | -------------------------------------------------------------------- | |
3059 | | A's virtual functions| | B's virtual functions | | |
3060 | | | | (may inherit some from A). | | |
3061 | -------------------------------------------------------------------- | |
3062 | | All of A's functions | | All of A's functions | | |
3063 | | (such as a->A::f). | | (such as b->A::f) | | |
3064 | -------------------------------------------------------------------- | |
3065 | | B's new virtual functions | | |
3066 | | (not defined in A.) | | |
3067 | ------------------------------- | |
3068 | | All of B's functions | | |
3069 | | (such as b->B::f) | | |
3070 | ------------------------------- | |
3071 | ||
3072 | this allows the program to make references to any function, virtual | |
3073 | or otherwise in a type-consistent manner. */ | |
3074 | ||
3075 | tree | |
6467930b MS |
3076 | finish_struct_1 (t, warn_anon) |
3077 | tree t; | |
8d08fdba MS |
3078 | int warn_anon; |
3079 | { | |
8d08fdba | 3080 | int old; |
f30432d7 | 3081 | tree name = TYPE_IDENTIFIER (t); |
8d08fdba | 3082 | enum tree_code code = TREE_CODE (t); |
f30432d7 | 3083 | tree fields = TYPE_FIELDS (t); |
72b7eeff | 3084 | tree fn_fields = TYPE_METHODS (t); |
f30432d7 | 3085 | tree x, last_x, method_vec; |
8d08fdba MS |
3086 | int all_virtual; |
3087 | int has_virtual; | |
3088 | int max_has_virtual; | |
3089 | tree pending_virtuals = NULL_TREE; | |
aa598818 | 3090 | tree pending_hard_virtuals = NULL_TREE; |
8d08fdba MS |
3091 | tree abstract_virtuals = NULL_TREE; |
3092 | tree vfield; | |
3093 | tree vfields; | |
3094 | int cant_have_default_ctor; | |
3095 | int cant_have_const_ctor; | |
8d08fdba MS |
3096 | int no_const_asn_ref; |
3097 | ||
3098 | /* The index of the first base class which has virtual | |
3099 | functions. Only applied to non-virtual baseclasses. */ | |
3100 | int first_vfn_base_index; | |
3101 | ||
3102 | int n_baseclasses; | |
3103 | int any_default_members = 0; | |
3104 | int const_sans_init = 0; | |
3105 | int ref_sans_init = 0; | |
8d08fdba MS |
3106 | int nonprivate_method = 0; |
3107 | tree t_binfo = TYPE_BINFO (t); | |
a0a33927 | 3108 | tree access_decls = NULL_TREE; |
f30432d7 | 3109 | int aggregate = 1; |
691c003d | 3110 | int empty = 1; |
824b9a4c | 3111 | int has_pointers = 0; |
8d08fdba | 3112 | |
f30432d7 | 3113 | if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) |
a0a33927 | 3114 | pedwarn ("anonymous class type not used to declare any objects"); |
8d08fdba | 3115 | |
8d08fdba MS |
3116 | if (TYPE_SIZE (t)) |
3117 | { | |
3118 | if (IS_AGGR_TYPE (t)) | |
3119 | cp_error ("redefinition of `%#T'", t); | |
3120 | else | |
3121 | my_friendly_abort (172); | |
3122 | popclass (0); | |
3123 | return t; | |
3124 | } | |
3125 | ||
8d08fdba MS |
3126 | GNU_xref_decl (current_function_decl, t); |
3127 | ||
3128 | /* If this type was previously laid out as a forward reference, | |
3129 | make sure we lay it out again. */ | |
3130 | ||
a0a33927 | 3131 | TYPE_SIZE (t) = NULL_TREE; |
8d08fdba MS |
3132 | CLASSTYPE_GOT_SEMICOLON (t) = 0; |
3133 | ||
f376e137 | 3134 | #if 0 |
00595019 MS |
3135 | /* This is in general too late to do this. I moved the main case up to |
3136 | left_curly, what else needs to move? */ | |
3137 | if (! IS_SIGNATURE (t)) | |
8d08fdba | 3138 | { |
00595019 MS |
3139 | my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999); |
3140 | my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999); | |
8d08fdba | 3141 | } |
f376e137 | 3142 | #endif |
8d08fdba | 3143 | |
db5ae43f MS |
3144 | #if 0 |
3145 | if (flag_rtti) | |
8d08fdba | 3146 | build_t_desc (t, 0); |
db5ae43f | 3147 | #endif |
8d08fdba MS |
3148 | |
3149 | TYPE_BINFO (t) = NULL_TREE; | |
3150 | ||
3151 | old = suspend_momentary (); | |
3152 | ||
3153 | /* Install struct as DECL_FIELD_CONTEXT of each field decl. | |
3154 | Also process specified field sizes. | |
3155 | Set DECL_FIELD_SIZE to the specified size, or 0 if none specified. | |
3156 | The specified size is found in the DECL_INITIAL. | |
3157 | Store 0 there, except for ": 0" fields (so we can find them | |
3158 | and delete them, below). */ | |
3159 | ||
3160 | if (t_binfo && BINFO_BASETYPES (t_binfo)) | |
3161 | n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo)); | |
3162 | else | |
3163 | n_baseclasses = 0; | |
3164 | ||
3165 | if (n_baseclasses > 0) | |
3166 | { | |
3167 | struct base_info base_info; | |
3168 | ||
3169 | /* If using multiple inheritance, this may cause variants of our | |
3170 | basetypes to be used (instead of their canonical forms). */ | |
f30432d7 MS |
3171 | tree vf = layout_basetypes (t, BINFO_BASETYPES (t_binfo)); |
3172 | last_x = tree_last (vf); | |
3173 | fields = chainon (vf, fields); | |
8d08fdba MS |
3174 | |
3175 | first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo); | |
e92cc029 | 3176 | /* Remember where we got our vfield from. */ |
8d08fdba MS |
3177 | CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index; |
3178 | has_virtual = base_info.has_virtual; | |
3179 | max_has_virtual = base_info.max_has_virtual; | |
3180 | CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors; | |
3181 | vfield = base_info.vfield; | |
3182 | vfields = base_info.vfields; | |
6b5fbb55 | 3183 | CLASSTYPE_RTTI (t) = base_info.rtti; |
8d08fdba MS |
3184 | cant_have_default_ctor = base_info.cant_have_default_ctor; |
3185 | cant_have_const_ctor = base_info.cant_have_const_ctor; | |
8d08fdba | 3186 | no_const_asn_ref = base_info.no_const_asn_ref; |
8d08fdba | 3187 | n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo)); |
f30432d7 | 3188 | aggregate = 0; |
8d08fdba MS |
3189 | } |
3190 | else | |
3191 | { | |
3192 | first_vfn_base_index = -1; | |
3193 | has_virtual = 0; | |
3194 | max_has_virtual = has_virtual; | |
3195 | vfield = NULL_TREE; | |
3196 | vfields = NULL_TREE; | |
6b5fbb55 | 3197 | CLASSTYPE_RTTI (t) = NULL_TREE; |
8d08fdba MS |
3198 | last_x = NULL_TREE; |
3199 | cant_have_default_ctor = 0; | |
3200 | cant_have_const_ctor = 0; | |
8d08fdba | 3201 | no_const_asn_ref = 0; |
8d08fdba MS |
3202 | } |
3203 | ||
f376e137 | 3204 | #if 0 |
00595019 | 3205 | /* Both of these should be done before now. */ |
8d08fdba | 3206 | if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t) |
700f8a87 | 3207 | && ! IS_SIGNATURE (t)) |
8d08fdba | 3208 | { |
00595019 MS |
3209 | my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999); |
3210 | my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999); | |
8d08fdba | 3211 | } |
f376e137 | 3212 | #endif |
8d08fdba MS |
3213 | |
3214 | /* The three of these are approximations which may later be | |
3215 | modified. Needed at this point to make add_virtual_function | |
3216 | and modify_vtable_entries work. */ | |
3217 | TREE_CHAIN (t_binfo) = TYPE_BINFO (t); | |
3218 | TYPE_BINFO (t) = t_binfo; | |
3219 | CLASSTYPE_VFIELDS (t) = vfields; | |
3220 | CLASSTYPE_VFIELD (t) = vfield; | |
3221 | ||
8d08fdba MS |
3222 | if (IS_SIGNATURE (t)) |
3223 | all_virtual = 0; | |
277294d7 | 3224 | else if (flag_all_virtual == 1) |
8d08fdba MS |
3225 | all_virtual = 1; |
3226 | else | |
3227 | all_virtual = 0; | |
3228 | ||
72b7eeff | 3229 | for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) |
8d08fdba | 3230 | { |
f30432d7 | 3231 | GNU_xref_member (current_class_name, x); |
8d08fdba | 3232 | |
f30432d7 | 3233 | nonprivate_method |= ! TREE_PRIVATE (x); |
8d08fdba | 3234 | |
f30432d7 MS |
3235 | /* If this was an evil function, don't keep it in class. */ |
3236 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x))) | |
3237 | continue; | |
8d08fdba | 3238 | |
f30432d7 | 3239 | DECL_CLASS_CONTEXT (x) = t; |
8d2733ca | 3240 | |
f30432d7 MS |
3241 | /* Do both of these, even though they're in the same union; |
3242 | if the insn `r' member and the size `i' member are | |
3243 | different sizes, as on the alpha, the larger of the two | |
3244 | will end up with garbage in it. */ | |
3245 | DECL_SAVED_INSNS (x) = NULL_RTX; | |
3246 | DECL_FIELD_SIZE (x) = 0; | |
a4443a08 | 3247 | |
cffa8729 MS |
3248 | check_for_override (x, t); |
3249 | if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x)) | |
3250 | cp_error_at ("initializer specified for non-virtual method `%D'", x); | |
2ee887f2 | 3251 | |
f30432d7 MS |
3252 | /* The name of the field is the original field name |
3253 | Save this in auxiliary field for later overloading. */ | |
3254 | if (DECL_VINDEX (x) | |
3255 | || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x))) | |
3256 | { | |
aa598818 JM |
3257 | add_virtual_function (&pending_virtuals, &pending_hard_virtuals, |
3258 | &has_virtual, x, t); | |
f30432d7 MS |
3259 | if (DECL_ABSTRACT_VIRTUAL_P (x)) |
3260 | abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals); | |
5566b478 MS |
3261 | #if 0 |
3262 | /* XXX Why did I comment this out? (jason) */ | |
f30432d7 MS |
3263 | else |
3264 | TREE_USED (x) = 1; | |
5566b478 | 3265 | #endif |
f30432d7 MS |
3266 | } |
3267 | } | |
8d08fdba | 3268 | |
f30432d7 MS |
3269 | for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x)) |
3270 | { | |
3271 | GNU_xref_member (current_class_name, x); | |
8d08fdba | 3272 | |
c91a56d2 | 3273 | if (TREE_CODE (x) == FIELD_DECL) |
691c003d MS |
3274 | { |
3275 | DECL_PACKED (x) |= TYPE_PACKED (t); | |
3276 | empty = 0; | |
3277 | } | |
c91a56d2 | 3278 | |
f30432d7 | 3279 | /* Handle access declarations. */ |
cffa8729 | 3280 | if (TREE_CODE (x) == USING_DECL) |
f30432d7 | 3281 | { |
cffa8729 MS |
3282 | tree ctype = DECL_INITIAL (x); |
3283 | tree sname = DECL_NAME (x); | |
be99da77 | 3284 | tree access |
beb53fb8 JM |
3285 | = TREE_PRIVATE (x) ? access_private_node |
3286 | : TREE_PROTECTED (x) ? access_protected_node | |
3287 | : access_public_node; | |
cffa8729 | 3288 | tree fdecl, binfo; |
8d08fdba | 3289 | |
f30432d7 MS |
3290 | if (last_x) |
3291 | TREE_CHAIN (last_x) = TREE_CHAIN (x); | |
3292 | else | |
3293 | fields = TREE_CHAIN (x); | |
8d08fdba | 3294 | |
cffa8729 MS |
3295 | binfo = binfo_or_else (ctype, t); |
3296 | if (! binfo) | |
3297 | continue; | |
3298 | ||
3299 | if (sname == constructor_name (ctype) | |
3300 | || sname == constructor_name_full (ctype)) | |
3301 | cp_error_at ("using-declaration for constructor", x); | |
3302 | ||
3303 | fdecl = lookup_field (binfo, sname, 0, 0); | |
3304 | if (! fdecl) | |
3305 | fdecl = lookup_fnfields (binfo, sname, 0); | |
3306 | ||
3307 | if (fdecl) | |
e66d884e | 3308 | access_decls = scratch_tree_cons (access, fdecl, access_decls); |
cffa8729 MS |
3309 | else |
3310 | cp_error_at ("no members matching `%D' in `%#T'", x, ctype); | |
f30432d7 MS |
3311 | continue; |
3312 | } | |
8d08fdba | 3313 | |
f30432d7 | 3314 | last_x = x; |
8d08fdba | 3315 | |
050367a3 MM |
3316 | if (TREE_CODE (x) == TYPE_DECL |
3317 | || TREE_CODE (x) == TEMPLATE_DECL) | |
f30432d7 | 3318 | continue; |
8d08fdba | 3319 | |
f30432d7 | 3320 | /* If we've gotten this far, it's a data member, possibly static, |
e92cc029 | 3321 | or an enumerator. */ |
8d08fdba | 3322 | |
f30432d7 | 3323 | DECL_FIELD_CONTEXT (x) = t; |
8d08fdba | 3324 | |
f30432d7 MS |
3325 | /* ``A local class cannot have static data members.'' ARM 9.4 */ |
3326 | if (current_function_decl && TREE_STATIC (x)) | |
3327 | cp_error_at ("field `%D' in local class cannot be static", x); | |
8d08fdba | 3328 | |
f30432d7 MS |
3329 | /* Perform error checking that did not get done in |
3330 | grokdeclarator. */ | |
3331 | if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE) | |
3332 | { | |
3333 | cp_error_at ("field `%D' invalidly declared function type", | |
3334 | x); | |
3335 | TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x)); | |
3336 | } | |
3337 | else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE) | |
3338 | { | |
3339 | cp_error_at ("field `%D' invalidly declared method type", x); | |
3340 | TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x)); | |
3341 | } | |
3342 | else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE) | |
3343 | { | |
3344 | cp_error_at ("field `%D' invalidly declared offset type", x); | |
3345 | TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x)); | |
3346 | } | |
8d08fdba | 3347 | |
f714b8e5 | 3348 | #if 0 |
f30432d7 | 3349 | if (DECL_NAME (x) == constructor_name (t)) |
e349ee73 | 3350 | cant_have_default_ctor = 1; |
f714b8e5 | 3351 | #endif |
f376e137 | 3352 | |
f30432d7 MS |
3353 | if (TREE_TYPE (x) == error_mark_node) |
3354 | continue; | |
8d08fdba | 3355 | |
f30432d7 MS |
3356 | DECL_SAVED_INSNS (x) = NULL_RTX; |
3357 | DECL_FIELD_SIZE (x) = 0; | |
8d08fdba | 3358 | |
f30432d7 MS |
3359 | /* When this goes into scope, it will be a non-local reference. */ |
3360 | DECL_NONLOCAL (x) = 1; | |
8d08fdba | 3361 | |
f30432d7 MS |
3362 | if (TREE_CODE (x) == CONST_DECL) |
3363 | continue; | |
8d08fdba | 3364 | |
f30432d7 MS |
3365 | if (TREE_CODE (x) == VAR_DECL) |
3366 | { | |
3367 | if (TREE_CODE (t) == UNION_TYPE) | |
3368 | /* Unions cannot have static members. */ | |
3369 | cp_error_at ("field `%D' declared static in union", x); | |
8d08fdba | 3370 | |
f30432d7 MS |
3371 | continue; |
3372 | } | |
8d08fdba | 3373 | |
f30432d7 | 3374 | /* Now it can only be a FIELD_DECL. */ |
8d08fdba | 3375 | |
f30432d7 MS |
3376 | if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) |
3377 | aggregate = 0; | |
8d08fdba | 3378 | |
f30432d7 MS |
3379 | /* If this is of reference type, check if it needs an init. |
3380 | Also do a little ANSI jig if necessary. */ | |
3381 | if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE) | |
3382 | { | |
3383 | if (DECL_INITIAL (x) == NULL_TREE) | |
3384 | ref_sans_init = 1; | |
8d08fdba | 3385 | |
f30432d7 MS |
3386 | /* ARM $12.6.2: [A member initializer list] (or, for an |
3387 | aggregate, initialization by a brace-enclosed list) is the | |
3388 | only way to initialize nonstatic const and reference | |
3389 | members. */ | |
f30432d7 | 3390 | cant_have_default_ctor = 1; |
e349ee73 | 3391 | TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; |
f30432d7 MS |
3392 | |
3393 | if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings) | |
3394 | { | |
3395 | if (DECL_NAME (x)) | |
3396 | cp_warning_at ("non-static reference `%#D' in class without a constructor", x); | |
3397 | else | |
3398 | cp_warning_at ("non-static reference in class without a constructor", x); | |
8d08fdba | 3399 | } |
f30432d7 | 3400 | } |
8d08fdba | 3401 | |
824b9a4c MS |
3402 | if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE) |
3403 | has_pointers = 1; | |
3404 | ||
f30432d7 MS |
3405 | /* If any field is const, the structure type is pseudo-const. */ |
3406 | if (TREE_READONLY (x)) | |
3407 | { | |
3408 | C_TYPE_FIELDS_READONLY (t) = 1; | |
3409 | if (DECL_INITIAL (x) == NULL_TREE) | |
3410 | const_sans_init = 1; | |
3411 | ||
3412 | /* ARM $12.6.2: [A member initializer list] (or, for an | |
3413 | aggregate, initialization by a brace-enclosed list) is the | |
3414 | only way to initialize nonstatic const and reference | |
3415 | members. */ | |
f30432d7 | 3416 | cant_have_default_ctor = 1; |
e349ee73 | 3417 | TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; |
f30432d7 MS |
3418 | |
3419 | if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t) | |
3420 | && extra_warnings) | |
3421 | { | |
3422 | if (DECL_NAME (x)) | |
3423 | cp_warning_at ("non-static const member `%#D' in class without a constructor", x); | |
3424 | else | |
3425 | cp_warning_at ("non-static const member in class without a constructor", x); | |
3426 | } | |
3427 | } | |
3428 | else | |
3429 | { | |
3430 | /* A field that is pseudo-const makes the structure | |
3431 | likewise. */ | |
3432 | tree t1 = TREE_TYPE (x); | |
3433 | while (TREE_CODE (t1) == ARRAY_TYPE) | |
3434 | t1 = TREE_TYPE (t1); | |
3435 | if (IS_AGGR_TYPE (t1)) | |
8d08fdba | 3436 | { |
f30432d7 MS |
3437 | if (C_TYPE_FIELDS_READONLY (t1)) |
3438 | C_TYPE_FIELDS_READONLY (t) = 1; | |
3439 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1)) | |
8d08fdba | 3440 | const_sans_init = 1; |
f30432d7 MS |
3441 | } |
3442 | } | |
8d08fdba | 3443 | |
f30432d7 MS |
3444 | /* We set DECL_BIT_FIELD tentatively in grokbitfield. |
3445 | If the type and width are valid, we'll keep it set. | |
3446 | Otherwise, the flag is cleared. */ | |
3447 | if (DECL_BIT_FIELD (x)) | |
3448 | { | |
3449 | DECL_BIT_FIELD (x) = 0; | |
3450 | /* Invalid bit-field size done by grokfield. */ | |
3451 | /* Detect invalid bit-field type. */ | |
3452 | if (DECL_INITIAL (x) | |
3453 | && ! INTEGRAL_TYPE_P (TREE_TYPE (x))) | |
3454 | { | |
3455 | cp_error_at ("bit-field `%#D' with non-integral type", x); | |
3456 | DECL_INITIAL (x) = NULL; | |
3457 | } | |
8d08fdba | 3458 | |
f30432d7 MS |
3459 | /* Detect and ignore out of range field width. */ |
3460 | if (DECL_INITIAL (x)) | |
3461 | { | |
5566b478 | 3462 | tree w = DECL_INITIAL (x); |
a703fb38 | 3463 | register int width = 0; |
f30432d7 | 3464 | |
5566b478 MS |
3465 | /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ |
3466 | STRIP_NOPS (w); | |
3467 | ||
3468 | /* detect invalid field size. */ | |
3469 | if (TREE_CODE (w) == CONST_DECL) | |
3470 | w = DECL_INITIAL (w); | |
3471 | else if (TREE_READONLY_DECL_P (w)) | |
3472 | w = decl_constant_value (w); | |
3473 | ||
3474 | if (TREE_CODE (w) != INTEGER_CST) | |
3475 | { | |
3476 | cp_error_at ("bit-field `%D' width not an integer constant", | |
3477 | x); | |
3478 | DECL_INITIAL (x) = NULL_TREE; | |
3479 | } | |
3480 | else if (width = TREE_INT_CST_LOW (w), | |
3481 | width < 0) | |
8d08fdba | 3482 | { |
f30432d7 MS |
3483 | DECL_INITIAL (x) = NULL; |
3484 | cp_error_at ("negative width in bit-field `%D'", x); | |
8d08fdba | 3485 | } |
f30432d7 | 3486 | else if (width == 0 && DECL_NAME (x) != 0) |
8d08fdba | 3487 | { |
f30432d7 MS |
3488 | DECL_INITIAL (x) = NULL; |
3489 | cp_error_at ("zero width for bit-field `%D'", x); | |
8d08fdba | 3490 | } |
f30432d7 MS |
3491 | else if (width |
3492 | > TYPE_PRECISION (long_long_unsigned_type_node)) | |
8d08fdba | 3493 | { |
f30432d7 MS |
3494 | /* The backend will dump if you try to use something |
3495 | too big; avoid that. */ | |
8d08fdba | 3496 | DECL_INITIAL (x) = NULL; |
f30432d7 MS |
3497 | sorry ("bit-fields larger than %d bits", |
3498 | TYPE_PRECISION (long_long_unsigned_type_node)); | |
3499 | cp_error_at (" in declaration of `%D'", x); | |
8d08fdba | 3500 | } |
f30432d7 | 3501 | else if (width > TYPE_PRECISION (TREE_TYPE (x)) |
18922a6b JM |
3502 | && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE |
3503 | && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE) | |
8d08fdba | 3504 | { |
f30432d7 | 3505 | cp_warning_at ("width of `%D' exceeds its type", x); |
8d08fdba | 3506 | } |
f30432d7 MS |
3507 | else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE |
3508 | && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)), | |
3509 | TREE_UNSIGNED (TREE_TYPE (x))) > width) | |
3510 | || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)), | |
3511 | TREE_UNSIGNED (TREE_TYPE (x))) > width))) | |
8d08fdba | 3512 | { |
f30432d7 MS |
3513 | cp_warning_at ("`%D' is too small to hold all values of `%#T'", |
3514 | x, TREE_TYPE (x)); | |
3515 | } | |
f30432d7 | 3516 | |
e92cc029 MS |
3517 | if (DECL_INITIAL (x) == NULL_TREE) |
3518 | ; | |
3519 | else if (width == 0) | |
f30432d7 | 3520 | { |
8d08fdba | 3521 | #ifdef EMPTY_FIELD_BOUNDARY |
2ee887f2 | 3522 | DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY); |
8d08fdba MS |
3523 | #endif |
3524 | #ifdef PCC_BITFIELD_TYPE_MATTERS | |
f30432d7 MS |
3525 | DECL_ALIGN (x) = MAX (DECL_ALIGN (x), |
3526 | TYPE_ALIGN (TREE_TYPE (x))); | |
8d08fdba | 3527 | #endif |
8d08fdba MS |
3528 | } |
3529 | else | |
f30432d7 MS |
3530 | { |
3531 | DECL_INITIAL (x) = NULL_TREE; | |
3532 | DECL_FIELD_SIZE (x) = width; | |
3533 | DECL_BIT_FIELD (x) = 1; | |
f30432d7 | 3534 | } |
8d08fdba MS |
3535 | } |
3536 | else | |
f30432d7 MS |
3537 | /* Non-bit-fields are aligned for their type. */ |
3538 | DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x))); | |
3539 | } | |
3540 | else | |
3541 | { | |
3542 | tree type = TREE_TYPE (x); | |
8d08fdba | 3543 | |
72b7eeff | 3544 | while (TREE_CODE (type) == ARRAY_TYPE) |
f30432d7 | 3545 | type = TREE_TYPE (type); |
8d08fdba | 3546 | |
f30432d7 MS |
3547 | if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x) |
3548 | && ! TYPE_PTRMEMFUNC_P (type)) | |
3549 | { | |
3550 | /* Never let anything with uninheritable virtuals | |
3551 | make it through without complaint. */ | |
3552 | if (CLASSTYPE_ABSTRACT_VIRTUALS (type)) | |
3553 | abstract_virtuals_error (x, type); | |
8d08fdba | 3554 | |
f30432d7 MS |
3555 | /* Don't let signatures make it through either. */ |
3556 | if (IS_SIGNATURE (type)) | |
3557 | signature_error (x, type); | |
8d08fdba | 3558 | |
f30432d7 MS |
3559 | if (code == UNION_TYPE) |
3560 | { | |
3561 | char *fie = NULL; | |
3562 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
3563 | fie = "constructor"; | |
3564 | else if (TYPE_NEEDS_DESTRUCTOR (type)) | |
3565 | fie = "destructor"; | |
3566 | else if (TYPE_HAS_REAL_ASSIGNMENT (type)) | |
3567 | fie = "assignment operator"; | |
3568 | if (fie) | |
3569 | cp_error_at ("member `%#D' with %s not allowed in union", x, | |
3570 | fie); | |
3571 | } | |
3572 | else | |
3573 | { | |
3574 | TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); | |
3575 | TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type); | |
3576 | TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type); | |
3577 | TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type); | |
3578 | } | |
8d08fdba | 3579 | |
e349ee73 | 3580 | if (!TYPE_HAS_CONST_INIT_REF (type)) |
f30432d7 MS |
3581 | cant_have_const_ctor = 1; |
3582 | ||
e349ee73 | 3583 | if (!TYPE_HAS_CONST_ASSIGN_REF (type)) |
f30432d7 MS |
3584 | no_const_asn_ref = 1; |
3585 | ||
3586 | if (TYPE_HAS_CONSTRUCTOR (type) | |
3587 | && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
3588 | { | |
3589 | cant_have_default_ctor = 1; | |
c19a8067 | 3590 | #if 0 |
f30432d7 MS |
3591 | /* This is wrong for aggregates. */ |
3592 | if (! TYPE_HAS_CONSTRUCTOR (t)) | |
3593 | { | |
3594 | if (DECL_NAME (x)) | |
3595 | cp_pedwarn_at ("member `%#D' with only non-default constructor", x); | |
3596 | else | |
3597 | cp_pedwarn_at ("member with only non-default constructor", x); | |
3598 | cp_pedwarn_at ("in class without a constructor", | |
3599 | x); | |
8d08fdba | 3600 | } |
f30432d7 | 3601 | #endif |
8d08fdba MS |
3602 | } |
3603 | } | |
f30432d7 MS |
3604 | if (DECL_INITIAL (x) != NULL_TREE) |
3605 | { | |
3606 | /* `build_class_init_list' does not recognize | |
3607 | non-FIELD_DECLs. */ | |
3608 | if (code == UNION_TYPE && any_default_members != 0) | |
3609 | cp_error_at ("multiple fields in union `%T' initialized"); | |
3610 | any_default_members = 1; | |
3611 | } | |
8d08fdba MS |
3612 | } |
3613 | } | |
3614 | ||
8d08fdba MS |
3615 | /* If this type has any constant members which did not come |
3616 | with their own initialization, mark that fact here. It is | |
3617 | not an error here, since such types can be saved either by their | |
3618 | constructors, or by fortuitous initialization. */ | |
3619 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init; | |
3620 | CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init; | |
3621 | CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals; | |
3622 | ||
f376e137 MS |
3623 | /* Synthesize any needed methods. Note that methods will be synthesized |
3624 | for anonymous unions; grok_x_components undoes that. */ | |
3625 | ||
f30432d7 MS |
3626 | if (! fn_fields) |
3627 | nonprivate_method = 1; | |
3628 | ||
8d08fdba MS |
3629 | if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t) |
3630 | && !IS_SIGNATURE (t)) | |
3631 | { | |
3632 | /* Here we must cons up a destructor on the fly. */ | |
c91a56d2 | 3633 | tree dtor = cons_up_default_function (t, name, 0); |
cffa8729 | 3634 | check_for_override (dtor, t); |
8d08fdba MS |
3635 | |
3636 | /* If we couldn't make it work, then pretend we didn't need it. */ | |
3637 | if (dtor == void_type_node) | |
3638 | TYPE_NEEDS_DESTRUCTOR (t) = 0; | |
3639 | else | |
3640 | { | |
e92cc029 | 3641 | /* Link dtor onto end of fn_fields. */ |
f30432d7 MS |
3642 | |
3643 | TREE_CHAIN (dtor) = fn_fields; | |
3644 | fn_fields = dtor; | |
8d08fdba | 3645 | |
8d08fdba | 3646 | if (DECL_VINDEX (dtor)) |
aa598818 JM |
3647 | add_virtual_function (&pending_virtuals, &pending_hard_virtuals, |
3648 | &has_virtual, dtor, t); | |
8d08fdba MS |
3649 | nonprivate_method = 1; |
3650 | } | |
3651 | } | |
3652 | ||
824b9a4c | 3653 | /* Effective C++ rule 11. */ |
7834ab39 | 3654 | if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t) |
824b9a4c MS |
3655 | && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t))) |
3656 | { | |
3657 | cp_warning ("`%#T' has pointer data members", t); | |
3658 | ||
3659 | if (! TYPE_HAS_INIT_REF (t)) | |
3660 | { | |
3661 | cp_warning (" but does not override `%T(const %T&)'", t, t); | |
3662 | if (! TYPE_HAS_ASSIGN_REF (t)) | |
3663 | cp_warning (" or `operator=(const %T&)'", t); | |
3664 | } | |
3665 | else if (! TYPE_HAS_ASSIGN_REF (t)) | |
3666 | cp_warning (" but does not override `operator=(const %T&)'", t); | |
3667 | } | |
3668 | ||
8d08fdba MS |
3669 | TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t); |
3670 | ||
8d08fdba MS |
3671 | TYPE_HAS_COMPLEX_INIT_REF (t) |
3672 | |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t) | |
faf5394a | 3673 | || has_virtual || any_default_members); |
8d08fdba MS |
3674 | TYPE_NEEDS_CONSTRUCTING (t) |
3675 | |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t) | |
faf5394a | 3676 | || has_virtual || any_default_members); |
f30432d7 MS |
3677 | if (! IS_SIGNATURE (t)) |
3678 | CLASSTYPE_NON_AGGREGATE (t) | |
3679 | = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t); | |
8d08fdba MS |
3680 | |
3681 | /* ARM $12.1: A default constructor will be generated for a class X | |
3682 | only if no constructor has been declared for class X. So we | |
3683 | check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate | |
3684 | one if they declared a constructor in this class. */ | |
63718c49 GB |
3685 | if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor |
3686 | && ! IS_SIGNATURE (t)) | |
8d08fdba | 3687 | { |
f376e137 | 3688 | tree default_fn = cons_up_default_function (t, name, 2); |
8d08fdba MS |
3689 | TREE_CHAIN (default_fn) = fn_fields; |
3690 | fn_fields = default_fn; | |
3691 | } | |
3692 | ||
8d2733ca | 3693 | /* Create default copy constructor, if needed. */ |
e349ee73 | 3694 | if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t)) |
8d08fdba MS |
3695 | { |
3696 | /* ARM 12.18: You get either X(X&) or X(const X&), but | |
3697 | not both. --Chip */ | |
f376e137 MS |
3698 | tree default_fn = cons_up_default_function (t, name, |
3699 | 3 + cant_have_const_ctor); | |
8d08fdba MS |
3700 | TREE_CHAIN (default_fn) = fn_fields; |
3701 | fn_fields = default_fn; | |
3702 | } | |
3703 | ||
3704 | TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t); | |
3705 | TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t); | |
3706 | TYPE_HAS_COMPLEX_ASSIGN_REF (t) | |
e8abc66f | 3707 | |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t); |
8d08fdba | 3708 | |
e349ee73 | 3709 | if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t)) |
8d08fdba | 3710 | { |
f376e137 MS |
3711 | tree default_fn = cons_up_default_function (t, name, |
3712 | 5 + no_const_asn_ref); | |
8d08fdba MS |
3713 | TREE_CHAIN (default_fn) = fn_fields; |
3714 | fn_fields = default_fn; | |
3715 | } | |
3716 | ||
3717 | if (fn_fields) | |
3718 | { | |
f49422da | 3719 | TYPE_METHODS (t) = fn_fields; |
8d08fdba MS |
3720 | method_vec = finish_struct_methods (t, fn_fields, nonprivate_method); |
3721 | ||
3722 | if (TYPE_HAS_CONSTRUCTOR (t) | |
8d08fdba | 3723 | && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE |
d2e5ee5c | 3724 | && DECL_FRIENDLIST (TYPE_MAIN_DECL (t)) == NULL_TREE) |
8d08fdba MS |
3725 | { |
3726 | int nonprivate_ctor = 0; | |
3727 | tree ctor; | |
3728 | ||
3729 | for (ctor = TREE_VEC_ELT (method_vec, 0); | |
3730 | ctor; | |
3731 | ctor = DECL_CHAIN (ctor)) | |
3732 | if (! TREE_PRIVATE (ctor)) | |
3733 | { | |
3734 | nonprivate_ctor = 1; | |
3735 | break; | |
3736 | } | |
3737 | ||
3738 | if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy) | |
3739 | cp_warning ("`%#T' only defines private constructors and has no friends", | |
3740 | t); | |
3741 | } | |
3742 | } | |
3743 | else | |
3744 | { | |
3745 | method_vec = 0; | |
3746 | ||
3747 | /* Just in case these got accidentally | |
3748 | filled in by syntax errors. */ | |
3749 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
3750 | TYPE_HAS_DESTRUCTOR (t) = 0; | |
3751 | } | |
3752 | ||
3753 | { | |
63718c49 | 3754 | int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0; |
8d08fdba MS |
3755 | |
3756 | for (access_decls = nreverse (access_decls); access_decls; | |
3757 | access_decls = TREE_CHAIN (access_decls)) | |
3758 | { | |
3759 | tree fdecl = TREE_VALUE (access_decls); | |
3760 | tree flist = NULL_TREE; | |
3761 | tree name; | |
fc378698 MS |
3762 | tree access = TREE_PURPOSE (access_decls); |
3763 | int i = 2; | |
8d08fdba MS |
3764 | tree tmp; |
3765 | ||
3766 | if (TREE_CODE (fdecl) == TREE_LIST) | |
3767 | { | |
3768 | flist = fdecl; | |
3769 | fdecl = TREE_VALUE (flist); | |
3770 | } | |
3771 | ||
3772 | name = DECL_NAME (fdecl); | |
3773 | ||
3774 | for (; i < n_methods; i++) | |
3775 | if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name) | |
3776 | { | |
3777 | cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t); | |
a0a33927 | 3778 | cp_error_at (" because of local method `%#D' with same name", |
8d08fdba | 3779 | TREE_VEC_ELT (method_vec, i)); |
a0a33927 | 3780 | fdecl = NULL_TREE; |
8d08fdba MS |
3781 | break; |
3782 | } | |
3783 | ||
3784 | if (! fdecl) | |
3785 | continue; | |
3786 | ||
3787 | for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp)) | |
3788 | if (DECL_NAME (tmp) == name) | |
3789 | { | |
3790 | cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t); | |
a0a33927 MS |
3791 | cp_error_at (" because of local field `%#D' with same name", tmp); |
3792 | fdecl = NULL_TREE; | |
8d08fdba MS |
3793 | break; |
3794 | } | |
3795 | ||
3796 | if (!fdecl) | |
3797 | continue; | |
3798 | ||
3799 | /* Make type T see field decl FDECL with access ACCESS.*/ | |
3800 | if (flist) | |
3801 | { | |
3802 | fdecl = TREE_VALUE (flist); | |
3803 | while (fdecl) | |
3804 | { | |
3805 | if (alter_access (t, fdecl, access) == 0) | |
3806 | break; | |
3807 | fdecl = DECL_CHAIN (fdecl); | |
3808 | } | |
3809 | } | |
3810 | else | |
3811 | alter_access (t, fdecl, access); | |
3812 | } | |
3813 | ||
3814 | } | |
3815 | ||
3816 | if (vfield == NULL_TREE && has_virtual) | |
3817 | { | |
3818 | /* We build this decl with ptr_type_node, and | |
3819 | change the type when we know what it should be. */ | |
3820 | vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t), | |
3821 | ptr_type_node); | |
3822 | /* If you change any of the below, take a look at all the | |
3823 | other VFIELD_BASEs and VTABLE_BASEs in the code, and change | |
e92cc029 | 3824 | them too. */ |
8d08fdba MS |
3825 | DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE); |
3826 | CLASSTYPE_VFIELD (t) = vfield; | |
3827 | DECL_VIRTUAL_P (vfield) = 1; | |
d2e5ee5c | 3828 | DECL_ARTIFICIAL (vfield) = 1; |
8d08fdba MS |
3829 | DECL_FIELD_CONTEXT (vfield) = t; |
3830 | DECL_CLASS_CONTEXT (vfield) = t; | |
3831 | DECL_FCONTEXT (vfield) = t; | |
28cbf42c | 3832 | DECL_SAVED_INSNS (vfield) = NULL_RTX; |
8d08fdba MS |
3833 | DECL_FIELD_SIZE (vfield) = 0; |
3834 | DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node); | |
6b5fbb55 MS |
3835 | #if 0 |
3836 | /* This is more efficient, but breaks binary compatibility, turn | |
3837 | it on sometime when we don't care. If we turn it on, we also | |
3838 | have to enable the code in dfs_init_vbase_pointers. */ | |
3839 | /* vfield is always first entry in structure. */ | |
3840 | TREE_CHAIN (vfield) = fields; | |
3841 | fields = vfield; | |
3842 | #else | |
3843 | if (last_x) | |
8d08fdba | 3844 | { |
a0a33927 | 3845 | my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175); |
8d08fdba MS |
3846 | TREE_CHAIN (last_x) = vfield; |
3847 | last_x = vfield; | |
3848 | } | |
3849 | else | |
3850 | fields = vfield; | |
6b5fbb55 | 3851 | #endif |
691c003d | 3852 | empty = 0; |
8d08fdba MS |
3853 | vfields = chainon (vfields, CLASSTYPE_AS_LIST (t)); |
3854 | } | |
3855 | ||
3856 | /* Now DECL_INITIAL is null on all members except for zero-width bit-fields. | |
3857 | And they have already done their work. | |
3858 | ||
3859 | C++: maybe we will support default field initialization some day... */ | |
3860 | ||
3861 | /* Delete all zero-width bit-fields from the front of the fieldlist */ | |
3862 | while (fields && DECL_BIT_FIELD (fields) | |
3863 | && DECL_INITIAL (fields)) | |
3864 | fields = TREE_CHAIN (fields); | |
3865 | /* Delete all such fields from the rest of the fields. */ | |
3866 | for (x = fields; x;) | |
3867 | { | |
3868 | if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x)) | |
3869 | && DECL_INITIAL (TREE_CHAIN (x))) | |
3870 | TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x)); | |
3871 | else | |
3872 | x = TREE_CHAIN (x); | |
3873 | } | |
3874 | /* Delete all duplicate fields from the fields */ | |
3875 | delete_duplicate_fields (fields); | |
3876 | ||
f376e137 MS |
3877 | /* Catch function/field name conflict. We don't need to do this for a |
3878 | signature, since it can only contain the fields constructed in | |
3879 | append_signature_fields. */ | |
3880 | if (! IS_SIGNATURE (t)) | |
3881 | { | |
3882 | int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0; | |
3883 | for (x = fields; x; x = TREE_CHAIN (x)) | |
3884 | { | |
3885 | tree name = DECL_NAME (x); | |
fc378698 MS |
3886 | int i = 2; |
3887 | ||
5566b478 MS |
3888 | if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)) |
3889 | continue; | |
fc378698 | 3890 | |
f376e137 MS |
3891 | for (; i < n_methods; ++i) |
3892 | if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name) | |
3893 | { | |
3894 | cp_error_at ("data member `%#D' conflicts with", x); | |
3895 | cp_error_at ("function member `%#D'", | |
3896 | TREE_VEC_ELT (method_vec, i)); | |
3897 | break; | |
3898 | } | |
3899 | } | |
3900 | } | |
00595019 | 3901 | |
8d08fdba MS |
3902 | /* Now we have the final fieldlist for the data fields. Record it, |
3903 | then lay out the structure or union (including the fields). */ | |
3904 | ||
3905 | TYPE_FIELDS (t) = fields; | |
3906 | ||
8d08fdba | 3907 | /* Pass layout information about base classes to layout_type, if any. */ |
8d08fdba MS |
3908 | if (n_baseclasses) |
3909 | { | |
3910 | tree pseudo_basetype = TREE_TYPE (base_layout_decl); | |
3911 | ||
3912 | TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t); | |
3913 | TYPE_FIELDS (t) = base_layout_decl; | |
3914 | ||
3915 | TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t); | |
3916 | TYPE_MODE (pseudo_basetype) = TYPE_MODE (t); | |
3917 | TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t); | |
3918 | DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype); | |
e92cc029 | 3919 | /* Don't re-use old size. */ |
a0a33927 | 3920 | DECL_SIZE (base_layout_decl) = NULL_TREE; |
8d08fdba | 3921 | } |
691c003d MS |
3922 | else if (empty) |
3923 | { | |
3924 | /* C++: do not let empty structures exist. */ | |
3925 | tree decl = build_lang_field_decl | |
3926 | (FIELD_DECL, NULL_TREE, char_type_node); | |
3927 | TREE_CHAIN (decl) = TYPE_FIELDS (t); | |
3928 | TYPE_FIELDS (t) = decl; | |
3929 | } | |
8d08fdba MS |
3930 | |
3931 | layout_type (t); | |
3932 | ||
9e9ff709 | 3933 | finish_struct_anon (t); |
8d08fdba | 3934 | |
691c003d | 3935 | if (n_baseclasses || empty) |
8d08fdba MS |
3936 | TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t)); |
3937 | ||
8d08fdba MS |
3938 | /* Set the TYPE_DECL for this type to contain the right |
3939 | value for DECL_OFFSET, so that we can use it as part | |
3940 | of a COMPONENT_REF for multiple inheritance. */ | |
3941 | ||
d2e5ee5c | 3942 | layout_decl (TYPE_MAIN_DECL (t), 0); |
8d08fdba | 3943 | |
7177d104 MS |
3944 | /* Now fix up any virtual base class types that we left lying |
3945 | around. We must get these done before we try to lay out the | |
3946 | virtual function table. */ | |
8d08fdba MS |
3947 | pending_hard_virtuals = nreverse (pending_hard_virtuals); |
3948 | ||
3949 | if (TYPE_USES_VIRTUAL_BASECLASSES (t)) | |
3950 | { | |
3951 | tree vbases; | |
3952 | ||
3953 | max_has_virtual = layout_vbasetypes (t, max_has_virtual); | |
3954 | vbases = CLASSTYPE_VBASECLASSES (t); | |
3955 | CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases); | |
3956 | ||
39211cd5 MS |
3957 | { |
3958 | /* Now fixup overrides of all functions in vtables from all | |
3959 | direct or indirect virtual base classes. */ | |
3960 | tree binfos = BINFO_BASETYPES (TYPE_BINFO (t)); | |
3961 | int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
3962 | ||
3963 | for (i = 0; i < n_baseclasses; i++) | |
3964 | { | |
3965 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
3966 | tree basetype = BINFO_TYPE (base_binfo); | |
3967 | tree vbases; | |
3968 | ||
3969 | vbases = CLASSTYPE_VBASECLASSES (basetype); | |
3970 | while (vbases) | |
3971 | { | |
3972 | merge_overrides (binfo_member (BINFO_TYPE (vbases), | |
3973 | CLASSTYPE_VBASECLASSES (t)), | |
3974 | vbases, 1, t); | |
3975 | vbases = TREE_CHAIN (vbases); | |
3976 | } | |
3977 | } | |
3978 | } | |
8d08fdba MS |
3979 | } |
3980 | ||
2986ae00 MS |
3981 | /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we |
3982 | might need to know it for setting up the offsets in the vtable | |
3983 | (or in thunks) below. */ | |
3984 | if (vfield != NULL_TREE | |
3985 | && DECL_FIELD_CONTEXT (vfield) != t) | |
3986 | { | |
3987 | tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0); | |
3988 | tree offset = BINFO_OFFSET (binfo); | |
3989 | ||
3990 | vfield = copy_node (vfield); | |
3991 | copy_lang_decl (vfield); | |
3992 | ||
3993 | if (! integer_zerop (offset)) | |
3994 | offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT)); | |
3995 | DECL_FIELD_CONTEXT (vfield) = t; | |
3996 | DECL_CLASS_CONTEXT (vfield) = t; | |
3997 | DECL_FIELD_BITPOS (vfield) | |
3998 | = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield)); | |
3999 | CLASSTYPE_VFIELD (t) = vfield; | |
4000 | } | |
4001 | ||
8d08fdba MS |
4002 | #ifdef NOTQUITE |
4003 | cp_warning ("Doing hard virtuals for %T...", t); | |
4004 | #endif | |
db5ae43f MS |
4005 | |
4006 | if (has_virtual > max_has_virtual) | |
4007 | max_has_virtual = has_virtual; | |
4008 | if (max_has_virtual > 0) | |
4009 | TYPE_VIRTUAL_P (t) = 1; | |
4010 | ||
4011 | if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals) | |
4012 | modify_all_vtables (t, NULL_TREE, NULL_TREE); | |
4013 | ||
8d08fdba MS |
4014 | while (pending_hard_virtuals) |
4015 | { | |
7177d104 MS |
4016 | modify_all_vtables (t, |
4017 | TREE_PURPOSE (pending_hard_virtuals), | |
4018 | TREE_VALUE (pending_hard_virtuals)); | |
8d08fdba MS |
4019 | pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals); |
4020 | } | |
72b7eeff MS |
4021 | |
4022 | if (TYPE_USES_VIRTUAL_BASECLASSES (t)) | |
4023 | { | |
4024 | tree vbases; | |
4025 | /* Now fixup any virtual function entries from virtual bases | |
4026 | that have different deltas. This has to come after we do the | |
4027 | pending hard virtuals, as we might have a function that comes | |
4028 | from multiple virtual base instances that is only overridden | |
4029 | by a hard virtual above. */ | |
4030 | vbases = CLASSTYPE_VBASECLASSES (t); | |
4031 | while (vbases) | |
4032 | { | |
4033 | /* We might be able to shorten the amount of work we do by | |
4034 | only doing this for vtables that come from virtual bases | |
4035 | that have differing offsets, but don't want to miss any | |
4036 | entries. */ | |
4037 | fixup_vtable_deltas (vbases, 1, t); | |
4038 | vbases = TREE_CHAIN (vbases); | |
4039 | } | |
4040 | } | |
4041 | ||
8d08fdba MS |
4042 | /* Under our model of GC, every C++ class gets its own virtual |
4043 | function table, at least virtually. */ | |
6b5fbb55 | 4044 | if (pending_virtuals) |
8d08fdba MS |
4045 | { |
4046 | pending_virtuals = nreverse (pending_virtuals); | |
4047 | /* We must enter these virtuals into the table. */ | |
4048 | if (first_vfn_base_index < 0) | |
4049 | { | |
f30432d7 MS |
4050 | /* The second slot is for the tdesc pointer when thunks are used. */ |
4051 | if (flag_vtable_thunks) | |
4052 | pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals); | |
4053 | ||
6b5fbb55 MS |
4054 | /* The first slot is for the rtti offset. */ |
4055 | pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals); | |
4056 | ||
46b02c6d | 4057 | set_rtti_entry (pending_virtuals, size_zero_node, t); |
8d08fdba MS |
4058 | build_vtable (NULL_TREE, t); |
4059 | } | |
4060 | else | |
4061 | { | |
4062 | /* Here we know enough to change the type of our virtual | |
4063 | function table, but we will wait until later this function. */ | |
4064 | ||
4065 | if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) | |
4066 | build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t); | |
8d08fdba MS |
4067 | } |
4068 | ||
4069 | /* If this type has basetypes with constructors, then those | |
4070 | constructors might clobber the virtual function table. But | |
4071 | they don't if the derived class shares the exact vtable of the base | |
4072 | class. */ | |
4073 | ||
4074 | CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1; | |
4075 | } | |
4076 | else if (first_vfn_base_index >= 0) | |
4077 | { | |
4078 | tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index); | |
8d08fdba MS |
4079 | /* This class contributes nothing new to the virtual function |
4080 | table. However, it may have declared functions which | |
4081 | went into the virtual function table "inherited" from the | |
4082 | base class. If so, we grab a copy of those updated functions, | |
4083 | and pretend they are ours. */ | |
4084 | ||
4085 | /* See if we should steal the virtual info from base class. */ | |
4086 | if (TYPE_BINFO_VTABLE (t) == NULL_TREE) | |
4087 | TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo); | |
4088 | if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE) | |
4089 | TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo); | |
4090 | if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo)) | |
4091 | CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1; | |
4092 | } | |
4093 | ||
8d08fdba MS |
4094 | if (max_has_virtual || first_vfn_base_index >= 0) |
4095 | { | |
8d08fdba MS |
4096 | CLASSTYPE_VSIZE (t) = has_virtual; |
4097 | if (first_vfn_base_index >= 0) | |
4098 | { | |
4099 | if (pending_virtuals) | |
4100 | TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), | |
4101 | pending_virtuals); | |
4102 | } | |
4103 | else if (has_virtual) | |
4104 | { | |
4105 | TYPE_BINFO_VIRTUALS (t) = pending_virtuals; | |
4106 | if (write_virtuals >= 0) | |
4107 | DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1; | |
4108 | } | |
4109 | } | |
4110 | ||
4111 | /* Now lay out the virtual function table. */ | |
4112 | if (has_virtual) | |
4113 | { | |
4114 | tree atype, itype; | |
4115 | ||
4116 | if (TREE_TYPE (vfield) == ptr_type_node) | |
4117 | { | |
4118 | /* We must create a pointer to this table because | |
4119 | the one inherited from base class does not exist. | |
4120 | We will fill in the type when we know what it | |
4121 | should really be. Use `size_int' so values are memoized | |
4122 | in common cases. */ | |
4123 | itype = build_index_type (size_int (has_virtual)); | |
4124 | atype = build_array_type (vtable_entry_type, itype); | |
4125 | layout_type (atype); | |
4126 | TREE_TYPE (vfield) = build_pointer_type (atype); | |
4127 | } | |
4128 | else | |
4129 | { | |
4130 | atype = TREE_TYPE (TREE_TYPE (vfield)); | |
4131 | ||
4132 | if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype)))) | |
4133 | { | |
4134 | /* We must extend (or create) the boundaries on this array, | |
4135 | because we picked up virtual functions from multiple | |
4136 | base classes. */ | |
4137 | itype = build_index_type (size_int (has_virtual)); | |
4138 | atype = build_array_type (vtable_entry_type, itype); | |
4139 | layout_type (atype); | |
4140 | vfield = copy_node (vfield); | |
4141 | TREE_TYPE (vfield) = build_pointer_type (atype); | |
4142 | } | |
4143 | } | |
4144 | ||
4145 | CLASSTYPE_VFIELD (t) = vfield; | |
4146 | if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype) | |
4147 | { | |
4148 | TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype; | |
28cbf42c | 4149 | DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0; |
8d08fdba MS |
4150 | layout_decl (TYPE_BINFO_VTABLE (t), 0); |
4151 | /* At one time the vtable info was grabbed 2 words at a time. This | |
4152 | fails on sparc unless you have 8-byte alignment. (tiemann) */ | |
4153 | DECL_ALIGN (TYPE_BINFO_VTABLE (t)) | |
4154 | = MAX (TYPE_ALIGN (double_type_node), | |
4155 | DECL_ALIGN (TYPE_BINFO_VTABLE (t))); | |
4156 | } | |
4157 | } | |
4158 | else if (first_vfn_base_index >= 0) | |
4159 | CLASSTYPE_VFIELD (t) = vfield; | |
4160 | CLASSTYPE_VFIELDS (t) = vfields; | |
4161 | ||
4162 | finish_struct_bits (t, max_has_virtual); | |
4163 | ||
f30432d7 MS |
4164 | /* Complete the rtl for any static member objects of the type we're |
4165 | working on. */ | |
8d08fdba MS |
4166 | for (x = fields; x; x = TREE_CHAIN (x)) |
4167 | { | |
8d08fdba MS |
4168 | if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x) |
4169 | && TREE_TYPE (x) == t) | |
4170 | { | |
4171 | DECL_MODE (x) = TYPE_MODE (t); | |
4172 | make_decl_rtl (x, NULL, 0); | |
4173 | } | |
4174 | } | |
4175 | ||
8d08fdba MS |
4176 | if (TYPE_HAS_CONSTRUCTOR (t)) |
4177 | { | |
4178 | tree vfields = CLASSTYPE_VFIELDS (t); | |
4179 | ||
4180 | while (vfields) | |
4181 | { | |
4182 | /* Mark the fact that constructor for T | |
4183 | could affect anybody inheriting from T | |
4184 | who wants to initialize vtables for VFIELDS's type. */ | |
4185 | if (VF_DERIVED_VALUE (vfields)) | |
4186 | TREE_ADDRESSABLE (vfields) = 1; | |
4187 | vfields = TREE_CHAIN (vfields); | |
4188 | } | |
4189 | if (any_default_members != 0) | |
4190 | build_class_init_list (t); | |
4191 | } | |
4192 | else if (TYPE_NEEDS_CONSTRUCTING (t)) | |
4193 | build_class_init_list (t); | |
4194 | ||
700f8a87 MS |
4195 | /* Write out inline function definitions. */ |
4196 | do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t)); | |
4197 | CLASSTYPE_INLINE_FRIENDS (t) = 0; | |
8d08fdba MS |
4198 | |
4199 | if (CLASSTYPE_VSIZE (t) != 0) | |
4200 | { | |
2986ae00 | 4201 | #if 0 |
e92cc029 | 4202 | /* This is now done above. */ |
8d08fdba MS |
4203 | if (DECL_FIELD_CONTEXT (vfield) != t) |
4204 | { | |
4205 | tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0); | |
4206 | tree offset = BINFO_OFFSET (binfo); | |
4207 | ||
4208 | vfield = copy_node (vfield); | |
4209 | copy_lang_decl (vfield); | |
4210 | ||
4211 | if (! integer_zerop (offset)) | |
4212 | offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT)); | |
4213 | DECL_FIELD_CONTEXT (vfield) = t; | |
4214 | DECL_CLASS_CONTEXT (vfield) = t; | |
4215 | DECL_FIELD_BITPOS (vfield) | |
4216 | = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield)); | |
4217 | CLASSTYPE_VFIELD (t) = vfield; | |
4218 | } | |
2986ae00 | 4219 | #endif |
8d08fdba | 4220 | |
e92cc029 | 4221 | /* In addition to this one, all the other vfields should be listed. */ |
8d08fdba MS |
4222 | /* Before that can be done, we have to have FIELD_DECLs for them, and |
4223 | a place to find them. */ | |
4224 | TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield); | |
4225 | ||
4226 | if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t) | |
c91a56d2 | 4227 | && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE) |
8d08fdba MS |
4228 | cp_warning ("`%#T' has virtual functions but non-virtual destructor", |
4229 | t); | |
4230 | } | |
4231 | ||
4232 | /* Make the rtl for any new vtables we have created, and unmark | |
4233 | the base types we marked. */ | |
7177d104 | 4234 | finish_vtbls (TYPE_BINFO (t), 1, t); |
8145f082 | 4235 | hack_incomplete_structures (t); |
8d08fdba | 4236 | |
a28e3c7f | 4237 | #if 0 |
8d08fdba MS |
4238 | if (TYPE_NAME (t) && TYPE_IDENTIFIER (t)) |
4239 | undo_template_name_overload (TYPE_IDENTIFIER (t), 1); | |
a28e3c7f | 4240 | #endif |
8d08fdba | 4241 | |
8d08fdba MS |
4242 | resume_momentary (old); |
4243 | ||
9e9ff709 MS |
4244 | if (warn_overloaded_virtual) |
4245 | warn_hidden (t); | |
8d08fdba MS |
4246 | |
4247 | #if 0 | |
4248 | /* This has to be done after we have sorted out what to do with | |
4249 | the enclosing type. */ | |
4250 | if (write_symbols != DWARF_DEBUG) | |
4251 | { | |
4252 | /* Be smarter about nested classes here. If a type is nested, | |
4253 | only output it if we would output the enclosing type. */ | |
d2e5ee5c MS |
4254 | if (DECL_CONTEXT (TYPE_MAIN_DECL (t)) |
4255 | && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_MAIN_DECL (t)))) == 't') | |
4256 | DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t)); | |
8d08fdba MS |
4257 | } |
4258 | #endif | |
4259 | ||
faf5394a | 4260 | if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG) |
8d08fdba MS |
4261 | { |
4262 | /* If the type has methods, we want to think about cutting down | |
4263 | the amount of symbol table stuff we output. The value stored in | |
4264 | the TYPE_DECL's DECL_IGNORED_P slot is a first approximation. | |
4265 | For example, if a member function is seen and we decide to | |
4266 | write out that member function, then we can change the value | |
4267 | of the DECL_IGNORED_P slot, and the type will be output when | |
9a3b49ac MS |
4268 | that member function's debug info is written out. |
4269 | ||
4270 | We can't do this with DWARF, which does not support name | |
4271 | references between translation units. */ | |
8d08fdba MS |
4272 | if (CLASSTYPE_METHOD_VEC (t)) |
4273 | { | |
4274 | extern tree pending_vtables; | |
4275 | ||
4276 | /* Don't output full info about any type | |
4277 | which does not have its implementation defined here. */ | |
4278 | if (TYPE_VIRTUAL_P (t) && write_virtuals == 2) | |
d2e5ee5c | 4279 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) |
8d08fdba MS |
4280 | = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0); |
4281 | else if (CLASSTYPE_INTERFACE_ONLY (t)) | |
d2e5ee5c | 4282 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1; |
5566b478 MS |
4283 | #if 0 |
4284 | /* XXX do something about this. */ | |
8d08fdba MS |
4285 | else if (CLASSTYPE_INTERFACE_UNKNOWN (t)) |
4286 | /* Only a first approximation! */ | |
d2e5ee5c | 4287 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1; |
5566b478 | 4288 | #endif |
8d08fdba MS |
4289 | } |
4290 | else if (CLASSTYPE_INTERFACE_ONLY (t)) | |
d2e5ee5c | 4291 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1; |
9a3b49ac | 4292 | } |
8d08fdba | 4293 | |
d2e5ee5c MS |
4294 | /* Finish debugging output for this type. */ |
4295 | rest_of_type_compilation (t, toplevel_bindings_p ()); | |
4296 | ||
8d08fdba MS |
4297 | return t; |
4298 | } | |
f30432d7 MS |
4299 | |
4300 | tree | |
2ee887f2 MS |
4301 | finish_struct (t, list_of_fieldlists, attributes, warn_anon) |
4302 | tree t, list_of_fieldlists, attributes; | |
f30432d7 MS |
4303 | int warn_anon; |
4304 | { | |
72b7eeff MS |
4305 | tree fields = NULL_TREE; |
4306 | tree *tail = &TYPE_METHODS (t); | |
75650646 MM |
4307 | tree specializations = NULL_TREE; |
4308 | tree *specialization_tail = &specializations; | |
f30432d7 MS |
4309 | tree name = TYPE_NAME (t); |
4310 | tree x, last_x = NULL_TREE; | |
be99da77 | 4311 | tree access; |
c91a56d2 | 4312 | tree dummy = NULL_TREE; |
75650646 | 4313 | tree next_x = NULL_TREE; |
f30432d7 MS |
4314 | |
4315 | if (TREE_CODE (name) == TYPE_DECL) | |
4316 | { | |
4317 | extern int lineno; | |
4318 | ||
4319 | DECL_SOURCE_FILE (name) = input_filename; | |
4320 | /* For TYPE_DECL that are not typedefs (those marked with a line | |
4321 | number of zero, we don't want to mark them as real typedefs. | |
4322 | If this fails one needs to make sure real typedefs have a | |
4323 | previous line number, even if it is wrong, that way the below | |
4324 | will fill in the right line number. (mrs) */ | |
4325 | if (DECL_SOURCE_LINE (name)) | |
4326 | DECL_SOURCE_LINE (name) = lineno; | |
4327 | CLASSTYPE_SOURCE_LINE (t) = lineno; | |
4328 | name = DECL_NAME (name); | |
4329 | } | |
4330 | ||
4331 | /* Append the fields we need for constructing signature tables. */ | |
4332 | if (IS_SIGNATURE (t)) | |
4333 | append_signature_fields (list_of_fieldlists); | |
4334 | ||
c91a56d2 MS |
4335 | /* Move our self-reference declaration to the end of the field list so |
4336 | any real field with the same name takes precedence. */ | |
4337 | if (list_of_fieldlists | |
4338 | && TREE_VALUE (list_of_fieldlists) | |
4339 | && DECL_ARTIFICIAL (TREE_VALUE (list_of_fieldlists))) | |
f30432d7 | 4340 | { |
c91a56d2 MS |
4341 | dummy = TREE_VALUE (list_of_fieldlists); |
4342 | list_of_fieldlists = TREE_CHAIN (list_of_fieldlists); | |
f30432d7 | 4343 | } |
c91a56d2 MS |
4344 | |
4345 | if (last_x && list_of_fieldlists) | |
4346 | TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists); | |
f30432d7 MS |
4347 | |
4348 | while (list_of_fieldlists) | |
4349 | { | |
be99da77 | 4350 | access = TREE_PURPOSE (list_of_fieldlists); |
f30432d7 | 4351 | |
c91a56d2 MS |
4352 | /* For signatures, we made all methods `public' in the parser and |
4353 | reported an error if a access specifier was used. */ | |
4354 | if (access == access_default_node) | |
4355 | { | |
4356 | if (CLASSTYPE_DECLARED_CLASS (t) == 0) | |
4357 | access = access_public_node; | |
4358 | else | |
4359 | access = access_private_node; | |
4360 | } | |
4361 | ||
75650646 | 4362 | for (x = TREE_VALUE (list_of_fieldlists); x; x = next_x) |
f30432d7 | 4363 | { |
75650646 MM |
4364 | next_x = TREE_CHAIN (x); |
4365 | ||
be99da77 MS |
4366 | TREE_PRIVATE (x) = access == access_private_node; |
4367 | TREE_PROTECTED (x) = access == access_protected_node; | |
f30432d7 MS |
4368 | |
4369 | /* Check for inconsistent use of this name in the class body. | |
4370 | Enums, types and static vars have already been checked. */ | |
cffa8729 | 4371 | if (TREE_CODE (x) != TYPE_DECL && TREE_CODE (x) != USING_DECL |
8d019cef JM |
4372 | && ! (TREE_CODE (x) == TEMPLATE_DECL |
4373 | && TREE_CODE (DECL_RESULT (x)) == TYPE_DECL) | |
f30432d7 MS |
4374 | && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL) |
4375 | { | |
4376 | tree name = DECL_NAME (x); | |
4377 | tree icv; | |
4378 | ||
4379 | /* Don't get confused by access decls. */ | |
4380 | if (name && TREE_CODE (name) == IDENTIFIER_NODE) | |
4381 | icv = IDENTIFIER_CLASS_VALUE (name); | |
4382 | else | |
4383 | icv = NULL_TREE; | |
4384 | ||
4385 | if (icv | |
4386 | /* Don't complain about constructors. */ | |
4387 | && name != constructor_name (current_class_type) | |
4388 | /* Or inherited names. */ | |
4389 | && id_in_current_class (name) | |
4390 | /* Or shadowed tags. */ | |
4391 | && !(TREE_CODE (icv) == TYPE_DECL | |
4392 | && DECL_CONTEXT (icv) == t)) | |
4393 | { | |
4394 | cp_error_at ("declaration of identifier `%D' as `%+#D'", | |
4395 | name, x); | |
4396 | cp_error_at ("conflicts with other use in class as `%#D'", | |
4397 | icv); | |
4398 | } | |
4399 | } | |
4400 | ||
98c1c668 JM |
4401 | if (TREE_CODE (x) == FUNCTION_DECL |
4402 | || DECL_FUNCTION_TEMPLATE_P (x)) | |
f30432d7 | 4403 | { |
5566b478 | 4404 | DECL_CLASS_CONTEXT (x) = t; |
75650646 | 4405 | |
f30432d7 | 4406 | if (last_x) |
75650646 MM |
4407 | TREE_CHAIN (last_x) = next_x; |
4408 | ||
4409 | if (DECL_TEMPLATE_SPECIALIZATION (x)) | |
4410 | /* We don't enter the specialization into the class | |
4411 | method vector since specializations don't affect | |
4412 | overloading. Instead we keep track of the | |
4413 | specializations, and process them after the method | |
4414 | vector is complete. */ | |
4415 | { | |
4416 | *specialization_tail = x; | |
4417 | specialization_tail = &TREE_CHAIN (x); | |
4418 | TREE_CHAIN (x) = NULL_TREE; | |
4419 | continue; | |
4420 | } | |
4421 | ||
e92cc029 | 4422 | /* Link x onto end of TYPE_METHODS. */ |
f30432d7 MS |
4423 | *tail = x; |
4424 | tail = &TREE_CHAIN (x); | |
f30432d7 MS |
4425 | continue; |
4426 | } | |
4427 | ||
5566b478 MS |
4428 | if (TREE_CODE (x) != TYPE_DECL) |
4429 | DECL_FIELD_CONTEXT (x) = t; | |
f30432d7 MS |
4430 | |
4431 | if (! fields) | |
4432 | fields = x; | |
4433 | last_x = x; | |
4434 | } | |
4435 | list_of_fieldlists = TREE_CHAIN (list_of_fieldlists); | |
4436 | /* link the tail while we have it! */ | |
4437 | if (last_x) | |
4438 | { | |
4439 | TREE_CHAIN (last_x) = NULL_TREE; | |
4440 | ||
4441 | if (list_of_fieldlists | |
4442 | && TREE_VALUE (list_of_fieldlists) | |
4443 | && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL) | |
4444 | TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists); | |
4445 | } | |
4446 | } | |
4447 | ||
5566b478 MS |
4448 | /* Now add the tags, if any, to the list of TYPE_DECLs |
4449 | defined for this type. */ | |
c91a56d2 | 4450 | if (CLASSTYPE_TAGS (t) || dummy) |
5566b478 | 4451 | { |
5156628f MS |
4452 | /* The list of tags was built up in pushtag in reverse order; we need |
4453 | to fix that so that enumerators will be processed in forward order | |
4454 | in template instantiation. */ | |
4455 | CLASSTYPE_TAGS (t) = x = nreverse (CLASSTYPE_TAGS (t)); | |
5566b478 MS |
4456 | while (x) |
4457 | { | |
d2e5ee5c | 4458 | tree tag = TYPE_MAIN_DECL (TREE_VALUE (x)); |
5566b478 | 4459 | |
5566b478 MS |
4460 | TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0; |
4461 | x = TREE_CHAIN (x); | |
4462 | last_x = chainon (last_x, tag); | |
4463 | } | |
c91a56d2 MS |
4464 | if (dummy) |
4465 | last_x = chainon (last_x, dummy); | |
5566b478 MS |
4466 | if (fields == NULL_TREE) |
4467 | fields = last_x; | |
4468 | CLASSTYPE_LOCAL_TYPEDECLS (t) = 1; | |
4469 | } | |
4470 | ||
f30432d7 | 4471 | *tail = NULL_TREE; |
f30432d7 MS |
4472 | TYPE_FIELDS (t) = fields; |
4473 | ||
6467930b MS |
4474 | cplus_decl_attributes (t, attributes, NULL_TREE); |
4475 | ||
5566b478 | 4476 | if (processing_template_decl) |
f30432d7 | 4477 | { |
5566b478 MS |
4478 | tree d = getdecls (); |
4479 | for (; d; d = TREE_CHAIN (d)) | |
4480 | { | |
4481 | /* If this is the decl for the class or one of the template | |
4482 | parms, we've seen all the injected decls. */ | |
4483 | if ((TREE_CODE (d) == TYPE_DECL | |
4484 | && (TREE_TYPE (d) == t | |
73b0fce8 KL |
4485 | || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM |
4486 | || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM)) | |
5566b478 MS |
4487 | || TREE_CODE (d) == CONST_DECL) |
4488 | break; | |
a80e4195 MS |
4489 | /* Don't inject cache decls. */ |
4490 | else if (IDENTIFIER_TEMPLATE (DECL_NAME (d))) | |
5566b478 MS |
4491 | continue; |
4492 | DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)) | |
4493 | = tree_cons (NULL_TREE, d, | |
4494 | DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))); | |
4495 | } | |
72b7eeff MS |
4496 | CLASSTYPE_METHOD_VEC (t) |
4497 | = finish_struct_methods (t, TYPE_METHODS (t), 1); | |
5566b478 MS |
4498 | TYPE_SIZE (t) = integer_zero_node; |
4499 | } | |
f30432d7 | 4500 | else |
6467930b | 4501 | t = finish_struct_1 (t, warn_anon); |
5566b478 MS |
4502 | |
4503 | TYPE_BEING_DEFINED (t) = 0; | |
4504 | ||
b7a29012 | 4505 | /* Now, figure out which member templates we're specializing. */ |
75650646 MM |
4506 | for (x = specializations; x != NULL_TREE; x = TREE_CHAIN (x)) |
4507 | { | |
4508 | tree spec_args; | |
4509 | tree fn; | |
e1467ff2 | 4510 | int pending_specialization; |
75650646 MM |
4511 | |
4512 | if (uses_template_parms (t)) | |
4513 | /* If t is a template class, and x is a specialization, then x | |
4514 | is itself really a template. Due to the vagaries of the | |
4515 | parser, however, we will have a handle to a function | |
4516 | declaration, rather than the template declaration, at this | |
4517 | point. */ | |
4518 | { | |
4519 | my_friendly_assert (DECL_TEMPLATE_INFO (x) != NULL_TREE, 0); | |
4520 | my_friendly_assert (DECL_TI_TEMPLATE (x) != NULL_TREE, 0); | |
4521 | fn = DECL_TI_TEMPLATE (x); | |
4522 | } | |
4523 | else | |
4524 | fn = x; | |
4525 | ||
4526 | /* We want the specialization arguments, which will be the | |
4527 | innermost ones. */ | |
4528 | if (DECL_TI_ARGS (fn) && TREE_CODE (DECL_TI_ARGS (fn)) == TREE_VEC) | |
4529 | spec_args | |
4530 | = TREE_VEC_ELT (DECL_TI_ARGS (fn), 0); | |
4531 | else | |
4532 | spec_args = DECL_TI_ARGS (fn); | |
4533 | ||
e1467ff2 MM |
4534 | pending_specialization |
4535 | = TI_PENDING_SPECIALIZATION_FLAG (DECL_TEMPLATE_INFO (fn)); | |
75650646 MM |
4536 | check_explicit_specialization |
4537 | (lookup_template_function (DECL_NAME (fn), spec_args), | |
e1467ff2 MM |
4538 | fn, 0, 1 | (8 * pending_specialization)); |
4539 | TI_PENDING_SPECIALIZATION_FLAG (DECL_TEMPLATE_INFO (fn)) = 0; | |
75650646 MM |
4540 | |
4541 | /* Now, the assembler name will be correct for fn, so we | |
4542 | make its RTL. */ | |
4543 | DECL_RTL (fn) = 0; | |
4544 | make_decl_rtl (fn, NULL_PTR, 1); | |
4545 | ||
4546 | if (x != fn) | |
4547 | { | |
4548 | DECL_RTL (x) = 0; | |
4549 | make_decl_rtl (x, NULL_PTR, 1); | |
4550 | } | |
4551 | } | |
4552 | ||
5566b478 MS |
4553 | if (current_class_type) |
4554 | popclass (0); | |
4555 | else | |
4556 | error ("trying to finish struct, but kicked out due to previous parse errors."); | |
4557 | ||
4558 | return t; | |
f30432d7 | 4559 | } |
8d08fdba MS |
4560 | \f |
4561 | /* Return non-zero if the effective type of INSTANCE is static. | |
4562 | Used to determine whether the virtual function table is needed | |
4563 | or not. | |
4564 | ||
4565 | *NONNULL is set iff INSTANCE can be known to be nonnull, regardless | |
4566 | of our knowledge of its type. */ | |
e92cc029 | 4567 | |
8d08fdba MS |
4568 | int |
4569 | resolves_to_fixed_type_p (instance, nonnull) | |
4570 | tree instance; | |
4571 | int *nonnull; | |
4572 | { | |
4573 | switch (TREE_CODE (instance)) | |
4574 | { | |
4575 | case INDIRECT_REF: | |
4576 | /* Check that we are not going through a cast of some sort. */ | |
4577 | if (TREE_TYPE (instance) | |
4578 | == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0)))) | |
4579 | instance = TREE_OPERAND (instance, 0); | |
4580 | /* fall through... */ | |
4581 | case CALL_EXPR: | |
4582 | /* This is a call to a constructor, hence it's never zero. */ | |
4583 | if (TREE_HAS_CONSTRUCTOR (instance)) | |
4584 | { | |
4585 | if (nonnull) | |
4586 | *nonnull = 1; | |
4587 | return 1; | |
4588 | } | |
4589 | return 0; | |
4590 | ||
4591 | case SAVE_EXPR: | |
4592 | /* This is a call to a constructor, hence it's never zero. */ | |
4593 | if (TREE_HAS_CONSTRUCTOR (instance)) | |
4594 | { | |
4595 | if (nonnull) | |
4596 | *nonnull = 1; | |
4597 | return 1; | |
4598 | } | |
4599 | return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull); | |
4600 | ||
4601 | case RTL_EXPR: | |
8d08fdba MS |
4602 | return 0; |
4603 | ||
4604 | case PLUS_EXPR: | |
4605 | case MINUS_EXPR: | |
4606 | if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) | |
4607 | /* Propagate nonnull. */ | |
4608 | resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull); | |
4609 | if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) | |
4610 | return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull); | |
4611 | return 0; | |
4612 | ||
4613 | case NOP_EXPR: | |
4614 | case CONVERT_EXPR: | |
4615 | return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull); | |
4616 | ||
4617 | case ADDR_EXPR: | |
4618 | if (nonnull) | |
4619 | *nonnull = 1; | |
4620 | return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull); | |
4621 | ||
4622 | case COMPONENT_REF: | |
4623 | return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull); | |
4624 | ||
8d08fdba MS |
4625 | case VAR_DECL: |
4626 | case FIELD_DECL: | |
4627 | if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE | |
4628 | && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance)))) | |
4629 | { | |
4630 | if (nonnull) | |
4631 | *nonnull = 1; | |
4632 | return 1; | |
4633 | } | |
e92cc029 | 4634 | /* fall through... */ |
8d08fdba MS |
4635 | case TARGET_EXPR: |
4636 | case PARM_DECL: | |
4637 | if (IS_AGGR_TYPE (TREE_TYPE (instance))) | |
4638 | { | |
4639 | if (nonnull) | |
4640 | *nonnull = 1; | |
4641 | return 1; | |
4642 | } | |
4643 | else if (nonnull) | |
4644 | { | |
4ac14744 | 4645 | if (instance == current_class_ptr |
8d08fdba MS |
4646 | && flag_this_is_variable <= 0) |
4647 | { | |
4648 | /* Some people still use `this = 0' inside destructors. */ | |
4649 | *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl)); | |
4650 | /* In a constructor, we know our type. */ | |
4651 | if (flag_this_is_variable < 0) | |
4652 | return 1; | |
4653 | } | |
4654 | else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) | |
4655 | /* Reference variables should be references to objects. */ | |
4656 | *nonnull = 1; | |
4657 | } | |
4658 | return 0; | |
4659 | ||
4660 | default: | |
4661 | return 0; | |
4662 | } | |
4663 | } | |
4664 | \f | |
4665 | void | |
4666 | init_class_processing () | |
4667 | { | |
4668 | current_class_depth = 0; | |
4669 | current_class_stacksize = 10; | |
4670 | current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree)); | |
4671 | current_class_stack = current_class_base; | |
4672 | ||
4673 | current_lang_stacksize = 10; | |
4674 | current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree)); | |
4675 | current_lang_stack = current_lang_base; | |
4676 | ||
be99da77 MS |
4677 | access_default_node = build_int_2 (0, 0); |
4678 | access_public_node = build_int_2 (1, 0); | |
4679 | access_protected_node = build_int_2 (2, 0); | |
4680 | access_private_node = build_int_2 (3, 0); | |
4681 | access_default_virtual_node = build_int_2 (4, 0); | |
4682 | access_public_virtual_node = build_int_2 (5, 0); | |
d8b55a76 JM |
4683 | access_protected_virtual_node = build_int_2 (6, 0); |
4684 | access_private_virtual_node = build_int_2 (7, 0); | |
be99da77 | 4685 | |
8d08fdba | 4686 | /* Keep these values lying around. */ |
8d08fdba MS |
4687 | base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node); |
4688 | TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE); | |
4689 | ||
4690 | gcc_obstack_init (&class_obstack); | |
4691 | } | |
4692 | ||
4693 | /* Set current scope to NAME. CODE tells us if this is a | |
4694 | STRUCT, UNION, or ENUM environment. | |
4695 | ||
4696 | NAME may end up being NULL_TREE if this is an anonymous or | |
4697 | late-bound struct (as in "struct { ... } foo;") */ | |
4698 | ||
4699 | /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to | |
4700 | appropriate values, found by looking up the type definition of | |
4701 | NAME (as a CODE). | |
4702 | ||
4703 | If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names | |
4704 | which can be seen locally to the class. They are shadowed by | |
4705 | any subsequent local declaration (including parameter names). | |
4706 | ||
4707 | If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names | |
4708 | which have static meaning (i.e., static members, static | |
4709 | member functions, enum declarations, etc). | |
4710 | ||
4711 | If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names | |
4712 | which can be seen locally to the class (as in 1), but | |
4713 | know that we are doing this for declaration purposes | |
4714 | (i.e. friend foo::bar (int)). | |
4715 | ||
4716 | So that we may avoid calls to lookup_name, we cache the _TYPE | |
4717 | nodes of local TYPE_DECLs in the TREE_TYPE field of the name. | |
4718 | ||
4719 | For multiple inheritance, we perform a two-pass depth-first search | |
4720 | of the type lattice. The first pass performs a pre-order search, | |
4721 | marking types after the type has had its fields installed in | |
4722 | the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely | |
4723 | unmarks the marked types. If a field or member function name | |
4724 | appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of | |
4725 | that name becomes `error_mark_node'. */ | |
4726 | ||
4727 | void | |
4728 | pushclass (type, modify) | |
4729 | tree type; | |
4730 | int modify; | |
4731 | { | |
4732 | push_memoized_context (type, modify); | |
4733 | ||
4734 | current_class_depth++; | |
4735 | *current_class_stack++ = current_class_name; | |
4736 | *current_class_stack++ = current_class_type; | |
4737 | if (current_class_stack >= current_class_base + current_class_stacksize) | |
4738 | { | |
beb53fb8 JM |
4739 | current_class_base |
4740 | = (tree *)xrealloc (current_class_base, | |
4741 | sizeof (tree) * (current_class_stacksize + 10)); | |
8d08fdba MS |
4742 | current_class_stack = current_class_base + current_class_stacksize; |
4743 | current_class_stacksize += 10; | |
4744 | } | |
4745 | ||
4746 | current_class_name = TYPE_NAME (type); | |
4747 | if (TREE_CODE (current_class_name) == TYPE_DECL) | |
4748 | current_class_name = DECL_NAME (current_class_name); | |
4749 | current_class_type = type; | |
4750 | ||
4751 | if (previous_class_type != NULL_TREE | |
4752 | && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE) | |
4753 | && current_class_depth == 1) | |
4754 | { | |
4755 | /* Forcibly remove any old class remnants. */ | |
4756 | popclass (-1); | |
4757 | previous_class_type = NULL_TREE; | |
4758 | } | |
4759 | ||
4760 | pushlevel_class (); | |
4761 | ||
37c46b43 | 4762 | #if 0 |
5566b478 MS |
4763 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
4764 | overload_template_name (type); | |
37c46b43 | 4765 | #endif |
5566b478 | 4766 | |
8d08fdba MS |
4767 | if (modify) |
4768 | { | |
4769 | tree tags; | |
4770 | tree this_fndecl = current_function_decl; | |
4771 | ||
4772 | if (current_function_decl | |
4773 | && DECL_CONTEXT (current_function_decl) | |
4774 | && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL) | |
4775 | current_function_decl = DECL_CONTEXT (current_function_decl); | |
4776 | else | |
4777 | current_function_decl = NULL_TREE; | |
4778 | ||
5566b478 | 4779 | if (type != previous_class_type || current_class_depth > 1) |
8d08fdba | 4780 | { |
22531e51 | 4781 | #ifdef MI_MATRIX |
8d08fdba | 4782 | build_mi_matrix (type); |
45537677 | 4783 | push_class_decls (type); |
8d08fdba | 4784 | free_mi_matrix (); |
22531e51 JM |
4785 | #else |
4786 | push_class_decls (type); | |
4787 | #endif | |
8d08fdba MS |
4788 | } |
4789 | else | |
4790 | { | |
4791 | tree item; | |
4792 | ||
ddd5a7c1 | 4793 | /* Hooray, we successfully cached; let's just install the |
8d08fdba MS |
4794 | cached class_shadowed list, and walk through it to get the |
4795 | IDENTIFIER_TYPE_VALUEs correct. */ | |
4796 | set_class_shadows (previous_class_values); | |
4797 | for (item = previous_class_values; item; item = TREE_CHAIN (item)) | |
4798 | { | |
4799 | tree id = TREE_PURPOSE (item); | |
4800 | tree decl = IDENTIFIER_CLASS_VALUE (id); | |
4801 | ||
4802 | if (TREE_CODE (decl) == TYPE_DECL) | |
4803 | set_identifier_type_value (id, TREE_TYPE (decl)); | |
4804 | } | |
4805 | unuse_fields (type); | |
4806 | } | |
4807 | ||
4808 | for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags)) | |
4809 | { | |
4810 | TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1; | |
4811 | if (! TREE_PURPOSE (tags)) | |
4812 | continue; | |
4813 | pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0); | |
4814 | } | |
4815 | ||
4816 | current_function_decl = this_fndecl; | |
4817 | } | |
8d08fdba MS |
4818 | } |
4819 | ||
4820 | /* Get out of the current class scope. If we were in a class scope | |
700f8a87 MS |
4821 | previously, that is the one popped to. The flag MODIFY tells whether |
4822 | the current scope declarations needs to be modified as a result of | |
4823 | popping to the previous scope. 0 is used for class definitions. */ | |
e92cc029 | 4824 | |
8d08fdba MS |
4825 | void |
4826 | popclass (modify) | |
4827 | int modify; | |
4828 | { | |
8d08fdba MS |
4829 | if (modify < 0) |
4830 | { | |
4831 | /* Back this old class out completely. */ | |
4832 | tree tags = CLASSTYPE_TAGS (previous_class_type); | |
4833 | tree t; | |
4834 | ||
4835 | /* This code can be seen as a cache miss. When we've cached a | |
4836 | class' scope's bindings and we can't use them, we need to reset | |
4837 | them. This is it! */ | |
700f8a87 | 4838 | for (t = previous_class_values; t; t = TREE_CHAIN (t)) |
8d08fdba MS |
4839 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; |
4840 | while (tags) | |
4841 | { | |
4842 | TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0; | |
4843 | tags = TREE_CHAIN (tags); | |
4844 | } | |
4845 | goto ret; | |
4846 | } | |
4847 | ||
4848 | if (modify) | |
4849 | { | |
4850 | /* Just remove from this class what didn't make | |
4851 | it into IDENTIFIER_CLASS_VALUE. */ | |
4852 | tree tags = CLASSTYPE_TAGS (current_class_type); | |
4853 | ||
4854 | while (tags) | |
4855 | { | |
4856 | TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0; | |
4857 | tags = TREE_CHAIN (tags); | |
4858 | } | |
4859 | } | |
8d08fdba | 4860 | |
700f8a87 MS |
4861 | /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition, |
4862 | since not all class decls make it there currently. */ | |
4863 | poplevel_class (! modify); | |
8d08fdba MS |
4864 | |
4865 | /* Since poplevel_class does the popping of class decls nowadays, | |
4866 | this really only frees the obstack used for these decls. | |
4867 | That's why it had to be moved down here. */ | |
4868 | if (modify) | |
5566b478 | 4869 | pop_class_decls (); |
8d08fdba MS |
4870 | |
4871 | current_class_depth--; | |
4872 | current_class_type = *--current_class_stack; | |
4873 | current_class_name = *--current_class_stack; | |
4874 | ||
8d08fdba MS |
4875 | pop_memoized_context (modify); |
4876 | ||
4877 | ret: | |
4878 | ; | |
4879 | } | |
4880 | ||
4881 | /* When entering a class scope, all enclosing class scopes' names with | |
4882 | static meaning (static variables, static functions, types and enumerators) | |
4883 | have to be visible. This recursive function calls pushclass for all | |
4884 | enclosing class contexts until global or a local scope is reached. | |
4885 | TYPE is the enclosed class and MODIFY is equivalent with the pushclass | |
4886 | formal of the same name. */ | |
4887 | ||
4888 | void | |
4889 | push_nested_class (type, modify) | |
4890 | tree type; | |
4891 | int modify; | |
4892 | { | |
a28e3c7f MS |
4893 | tree context; |
4894 | ||
5566b478 | 4895 | if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type) |
73b0fce8 KL |
4896 | || TREE_CODE (type) == TEMPLATE_TYPE_PARM |
4897 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
a28e3c7f MS |
4898 | return; |
4899 | ||
d2e5ee5c | 4900 | context = DECL_CONTEXT (TYPE_MAIN_DECL (type)); |
8d08fdba MS |
4901 | |
4902 | if (context && TREE_CODE (context) == RECORD_TYPE) | |
4903 | push_nested_class (context, 2); | |
4904 | pushclass (type, modify); | |
4905 | } | |
4906 | ||
4907 | /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */ | |
4908 | ||
4909 | void | |
4910 | pop_nested_class (modify) | |
4911 | int modify; | |
4912 | { | |
d2e5ee5c | 4913 | tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); |
8d08fdba MS |
4914 | |
4915 | popclass (modify); | |
4916 | if (context && TREE_CODE (context) == RECORD_TYPE) | |
4917 | pop_nested_class (modify); | |
4918 | } | |
4919 | ||
4920 | /* Set global variables CURRENT_LANG_NAME to appropriate value | |
4921 | so that behavior of name-mangling machinery is correct. */ | |
4922 | ||
4923 | void | |
4924 | push_lang_context (name) | |
4925 | tree name; | |
4926 | { | |
4927 | *current_lang_stack++ = current_lang_name; | |
4928 | if (current_lang_stack >= current_lang_base + current_lang_stacksize) | |
4929 | { | |
beb53fb8 JM |
4930 | current_lang_base |
4931 | = (tree *)xrealloc (current_lang_base, | |
4932 | sizeof (tree) * (current_lang_stacksize + 10)); | |
8d08fdba MS |
4933 | current_lang_stack = current_lang_base + current_lang_stacksize; |
4934 | current_lang_stacksize += 10; | |
4935 | } | |
4936 | ||
4937 | if (name == lang_name_cplusplus) | |
4938 | { | |
4939 | strict_prototype = strict_prototypes_lang_cplusplus; | |
4940 | current_lang_name = name; | |
4941 | } | |
4942 | else if (name == lang_name_c) | |
4943 | { | |
4944 | strict_prototype = strict_prototypes_lang_c; | |
4945 | current_lang_name = name; | |
4946 | } | |
4947 | else | |
4948 | error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name)); | |
8d08fdba MS |
4949 | } |
4950 | ||
4951 | /* Get out of the current language scope. */ | |
e92cc029 | 4952 | |
8d08fdba MS |
4953 | void |
4954 | pop_lang_context () | |
4955 | { | |
8d08fdba MS |
4956 | current_lang_name = *--current_lang_stack; |
4957 | if (current_lang_name == lang_name_cplusplus) | |
4958 | strict_prototype = strict_prototypes_lang_cplusplus; | |
4959 | else if (current_lang_name == lang_name_c) | |
4960 | strict_prototype = strict_prototypes_lang_c; | |
4961 | } | |
8d08fdba MS |
4962 | \f |
4963 | /* Type instantiation routines. */ | |
4964 | ||
ec255269 MS |
4965 | /* This function will instantiate the type of the expression given in |
4966 | RHS to match the type of LHSTYPE. If errors exist, then return | |
4967 | error_mark_node. If only complain is COMPLAIN is set. If we are | |
4968 | not complaining, never modify rhs, as overload resolution wants to | |
4969 | try many possible instantiations, in hopes that at least one will | |
4970 | work. | |
8d08fdba MS |
4971 | |
4972 | This function is used in build_modify_expr, convert_arguments, | |
4973 | build_c_cast, and compute_conversion_costs. */ | |
e92cc029 | 4974 | |
8d08fdba MS |
4975 | tree |
4976 | instantiate_type (lhstype, rhs, complain) | |
4977 | tree lhstype, rhs; | |
4978 | int complain; | |
4979 | { | |
386b8a85 JM |
4980 | tree explicit_targs = NULL_TREE; |
4981 | ||
8d08fdba MS |
4982 | if (TREE_CODE (lhstype) == UNKNOWN_TYPE) |
4983 | { | |
4984 | if (complain) | |
4985 | error ("not enough type information"); | |
4986 | return error_mark_node; | |
4987 | } | |
4988 | ||
4989 | if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs))) | |
abff8e06 JM |
4990 | { |
4991 | if (comptypes (lhstype, TREE_TYPE (rhs), 1)) | |
4992 | return rhs; | |
4993 | if (complain) | |
4994 | cp_error ("argument of type `%T' does not match `%T'", | |
4995 | TREE_TYPE (rhs), lhstype); | |
4996 | return error_mark_node; | |
4997 | } | |
8d08fdba | 4998 | |
c73964b2 MS |
4999 | rhs = copy_node (rhs); |
5000 | ||
8d08fdba MS |
5001 | /* This should really only be used when attempting to distinguish |
5002 | what sort of a pointer to function we have. For now, any | |
5003 | arithmetic operation which is not supported on pointers | |
5004 | is rejected as an error. */ | |
5005 | ||
5006 | switch (TREE_CODE (rhs)) | |
5007 | { | |
5008 | case TYPE_EXPR: | |
5009 | case CONVERT_EXPR: | |
5010 | case SAVE_EXPR: | |
5011 | case CONSTRUCTOR: | |
5012 | case BUFFER_REF: | |
5013 | my_friendly_abort (177); | |
5014 | return error_mark_node; | |
5015 | ||
5016 | case INDIRECT_REF: | |
5017 | case ARRAY_REF: | |
ec255269 MS |
5018 | { |
5019 | tree new_rhs; | |
8d08fdba | 5020 | |
ec255269 MS |
5021 | new_rhs = instantiate_type (build_pointer_type (lhstype), |
5022 | TREE_OPERAND (rhs, 0), complain); | |
5023 | if (new_rhs == error_mark_node) | |
5024 | return error_mark_node; | |
5025 | ||
5026 | TREE_TYPE (rhs) = lhstype; | |
5027 | TREE_OPERAND (rhs, 0) = new_rhs; | |
5028 | return rhs; | |
5029 | } | |
8d08fdba MS |
5030 | |
5031 | case NOP_EXPR: | |
5032 | rhs = copy_node (TREE_OPERAND (rhs, 0)); | |
5033 | TREE_TYPE (rhs) = unknown_type_node; | |
5034 | return instantiate_type (lhstype, rhs, complain); | |
5035 | ||
5036 | case COMPONENT_REF: | |
5037 | { | |
5038 | tree field = TREE_OPERAND (rhs, 1); | |
5039 | if (TREE_CODE (field) == TREE_LIST) | |
5040 | { | |
5041 | tree function = instantiate_type (lhstype, field, complain); | |
5042 | if (function == error_mark_node) | |
5043 | return error_mark_node; | |
5044 | my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185); | |
5045 | if (DECL_VINDEX (function)) | |
5046 | { | |
5047 | tree base = TREE_OPERAND (rhs, 0); | |
5048 | tree base_ptr = build_unary_op (ADDR_EXPR, base, 0); | |
5049 | if (base_ptr == error_mark_node) | |
5050 | return error_mark_node; | |
5051 | base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr); | |
5052 | if (base_ptr == error_mark_node) | |
5053 | return error_mark_node; | |
5054 | return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function)); | |
5055 | } | |
72b7eeff | 5056 | mark_used (function); |
8d08fdba MS |
5057 | return function; |
5058 | } | |
5059 | ||
5060 | my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178); | |
5061 | my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE | |
5062 | || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE), | |
5063 | 179); | |
5064 | ||
5065 | TREE_TYPE (rhs) = lhstype; | |
5066 | /* First look for an exact match */ | |
5067 | ||
5068 | while (field && TREE_TYPE (field) != lhstype) | |
e1cd6e56 | 5069 | field = DECL_CHAIN (field); |
8d08fdba MS |
5070 | if (field) |
5071 | { | |
5072 | TREE_OPERAND (rhs, 1) = field; | |
72b7eeff | 5073 | mark_used (field); |
8d08fdba MS |
5074 | return rhs; |
5075 | } | |
5076 | ||
5077 | /* No exact match found, look for a compatible function. */ | |
5078 | field = TREE_OPERAND (rhs, 1); | |
5079 | while (field && ! comptypes (lhstype, TREE_TYPE (field), 0)) | |
e1cd6e56 | 5080 | field = DECL_CHAIN (field); |
8d08fdba MS |
5081 | if (field) |
5082 | { | |
5083 | TREE_OPERAND (rhs, 1) = field; | |
e1cd6e56 | 5084 | field = DECL_CHAIN (field); |
8d08fdba | 5085 | while (field && ! comptypes (lhstype, TREE_TYPE (field), 0)) |
e1cd6e56 | 5086 | field = DECL_CHAIN (field); |
8d08fdba MS |
5087 | if (field) |
5088 | { | |
5089 | if (complain) | |
5090 | error ("ambiguous overload for COMPONENT_REF requested"); | |
5091 | return error_mark_node; | |
5092 | } | |
5093 | } | |
5094 | else | |
5095 | { | |
5096 | if (complain) | |
5097 | error ("no appropriate overload exists for COMPONENT_REF"); | |
5098 | return error_mark_node; | |
5099 | } | |
5100 | return rhs; | |
5101 | } | |
5102 | ||
386b8a85 JM |
5103 | case TEMPLATE_ID_EXPR: |
5104 | { | |
5105 | explicit_targs = TREE_OPERAND (rhs, 1); | |
5106 | rhs = TREE_OPERAND (rhs, 0); | |
5107 | } | |
5108 | /* fall through */ | |
5109 | ||
8d08fdba MS |
5110 | case TREE_LIST: |
5111 | { | |
a703fb38 | 5112 | tree elem, baselink, name = NULL_TREE; |
8d08fdba MS |
5113 | int globals = overloaded_globals_p (rhs); |
5114 | ||
8d08fdba MS |
5115 | /* First look for an exact match. Search either overloaded |
5116 | functions or member functions. May have to undo what | |
5117 | `default_conversion' might do to lhstype. */ | |
5118 | ||
28cbf42c MS |
5119 | if (TYPE_PTRMEMFUNC_P (lhstype)) |
5120 | lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype); | |
5121 | ||
8d08fdba | 5122 | if (TREE_CODE (lhstype) == POINTER_TYPE) |
a703fb38 KG |
5123 | { |
5124 | if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE | |
5125 | || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE) | |
5126 | lhstype = TREE_TYPE (lhstype); | |
5127 | else | |
5128 | { | |
5129 | if (complain) | |
5130 | error ("invalid type combination for overload"); | |
5131 | return error_mark_node; | |
5132 | } | |
5133 | } | |
8d08fdba MS |
5134 | |
5135 | if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0) | |
5136 | { | |
5137 | if (complain) | |
5138 | cp_error ("cannot resolve overloaded function `%D' based on non-function type", | |
5139 | TREE_PURPOSE (rhs)); | |
5140 | return error_mark_node; | |
5141 | } | |
5142 | ||
5143 | if (globals > 0) | |
5144 | { | |
5145 | elem = get_first_fn (rhs); | |
386b8a85 JM |
5146 | /* If there are explicit_targs, only a template function |
5147 | can match. */ | |
5148 | if (explicit_targs == NULL_TREE) | |
5149 | while (elem) | |
a703fb38 KG |
5150 | { |
5151 | if (! comptypes (lhstype, TREE_TYPE (elem), 1)) | |
5152 | elem = DECL_CHAIN (elem); | |
5153 | else | |
5154 | { | |
5155 | mark_used (elem); | |
5156 | return elem; | |
5157 | } | |
5158 | } | |
00595019 MS |
5159 | |
5160 | /* No exact match found, look for a compatible template. */ | |
5161 | { | |
5162 | tree save_elem = 0; | |
5163 | for (elem = get_first_fn (rhs); elem; elem = DECL_CHAIN (elem)) | |
5164 | if (TREE_CODE (elem) == TEMPLATE_DECL) | |
5165 | { | |
98c1c668 | 5166 | int n = DECL_NTPARMS (elem); |
e66d884e | 5167 | tree t = make_scratch_vec (n); |
3b3ba9f0 | 5168 | int i; |
98c1c668 | 5169 | i = type_unification |
050367a3 MM |
5170 | (DECL_INNERMOST_TEMPLATE_PARMS (elem), t, |
5171 | TYPE_ARG_TYPES (TREE_TYPE (elem)), | |
3b3ba9f0 | 5172 | TYPE_ARG_TYPES (lhstype), explicit_targs, 1, 1); |
00595019 MS |
5173 | if (i == 0) |
5174 | { | |
5175 | if (save_elem) | |
5176 | { | |
5177 | cp_error ("ambiguous template instantiation converting to `%#T'", lhstype); | |
5178 | return error_mark_node; | |
5179 | } | |
5180 | save_elem = instantiate_template (elem, t); | |
5181 | /* Check the return type. */ | |
5182 | if (! comptypes (TREE_TYPE (lhstype), | |
54e97afb | 5183 | TREE_TYPE (TREE_TYPE (save_elem)), 1)) |
00595019 MS |
5184 | save_elem = 0; |
5185 | } | |
5186 | } | |
5187 | if (save_elem) | |
5566b478 MS |
5188 | { |
5189 | mark_used (save_elem); | |
5190 | return save_elem; | |
5191 | } | |
00595019 MS |
5192 | } |
5193 | ||
386b8a85 JM |
5194 | /* If there are explicit_targs, only a template function |
5195 | can match. */ | |
5196 | if (explicit_targs == NULL_TREE) | |
8d08fdba | 5197 | { |
386b8a85 JM |
5198 | /* No match found, look for a compatible function. */ |
5199 | elem = get_first_fn (rhs); | |
e1cd6e56 | 5200 | while (elem && comp_target_types (lhstype, |
386b8a85 | 5201 | TREE_TYPE (elem), 1) <= 0) |
8d08fdba MS |
5202 | elem = DECL_CHAIN (elem); |
5203 | if (elem) | |
5204 | { | |
386b8a85 JM |
5205 | tree save_elem = elem; |
5206 | elem = DECL_CHAIN (elem); | |
5207 | while (elem | |
5208 | && comp_target_types (lhstype, | |
5209 | TREE_TYPE (elem), 0) <= 0) | |
5210 | elem = DECL_CHAIN (elem); | |
5211 | if (elem) | |
8d08fdba | 5212 | { |
386b8a85 JM |
5213 | if (complain) |
5214 | { | |
5215 | cp_error | |
5216 | ("cannot resolve overload to target type `%#T'", | |
5217 | lhstype); | |
5218 | cp_error_at (" ambiguity between `%#D'", | |
5219 | save_elem); | |
5220 | cp_error_at (" and `%#D', at least", elem); | |
5221 | } | |
5222 | return error_mark_node; | |
8d08fdba | 5223 | } |
386b8a85 JM |
5224 | mark_used (save_elem); |
5225 | return save_elem; | |
8d08fdba | 5226 | } |
8d08fdba MS |
5227 | } |
5228 | if (complain) | |
5229 | { | |
a0a33927 | 5230 | cp_error ("cannot resolve overload to target type `%#T'", |
8d08fdba | 5231 | lhstype); |
386b8a85 JM |
5232 | cp_error |
5233 | (" because no suitable overload of function `%D' exists", | |
5234 | TREE_PURPOSE (rhs)); | |
8d08fdba MS |
5235 | } |
5236 | return error_mark_node; | |
5237 | } | |
5238 | ||
5239 | if (TREE_NONLOCAL_FLAG (rhs)) | |
5240 | { | |
5241 | /* Got to get it as a baselink. */ | |
5242 | rhs = lookup_fnfields (TYPE_BINFO (current_class_type), | |
5243 | TREE_PURPOSE (rhs), 0); | |
5244 | } | |
5245 | else | |
5246 | { | |
5247 | my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181); | |
5248 | if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST) | |
5249 | rhs = TREE_VALUE (rhs); | |
5250 | my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL, | |
5251 | 182); | |
5252 | } | |
5253 | ||
5254 | for (baselink = rhs; baselink; | |
5255 | baselink = next_baselink (baselink)) | |
5256 | { | |
5257 | elem = TREE_VALUE (baselink); | |
5258 | while (elem) | |
9a0e77ba | 5259 | if (comptypes (lhstype, TREE_TYPE (elem), 1)) |
72b7eeff MS |
5260 | { |
5261 | mark_used (elem); | |
5262 | return elem; | |
5263 | } | |
9a0e77ba | 5264 | else |
e1cd6e56 | 5265 | elem = DECL_CHAIN (elem); |
8d08fdba MS |
5266 | } |
5267 | ||
5268 | /* No exact match found, look for a compatible method. */ | |
5269 | for (baselink = rhs; baselink; | |
5270 | baselink = next_baselink (baselink)) | |
5271 | { | |
5272 | elem = TREE_VALUE (baselink); | |
e1cd6e56 MS |
5273 | while (elem && comp_target_types (lhstype, |
5274 | TREE_TYPE (elem), 1) <= 0) | |
5275 | elem = DECL_CHAIN (elem); | |
8d08fdba MS |
5276 | if (elem) |
5277 | { | |
5278 | tree save_elem = elem; | |
e1cd6e56 MS |
5279 | elem = DECL_CHAIN (elem); |
5280 | while (elem && comp_target_types (lhstype, | |
5281 | TREE_TYPE (elem), 0) <= 0) | |
5282 | elem = DECL_CHAIN (elem); | |
8d08fdba MS |
5283 | if (elem) |
5284 | { | |
5285 | if (complain) | |
5286 | error ("ambiguous overload for overloaded method requested"); | |
5287 | return error_mark_node; | |
5288 | } | |
72b7eeff | 5289 | mark_used (save_elem); |
8d08fdba MS |
5290 | return save_elem; |
5291 | } | |
5292 | name = DECL_NAME (TREE_VALUE (rhs)); | |
700f8a87 | 5293 | #if 0 |
8d08fdba MS |
5294 | if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0) |
5295 | { | |
5296 | /* Try to instantiate from non-member functions. */ | |
700f8a87 | 5297 | rhs = lookup_name_nonclass (name); |
8d08fdba MS |
5298 | if (rhs && TREE_CODE (rhs) == TREE_LIST) |
5299 | { | |
5300 | /* This code seems to be missing a `return'. */ | |
5301 | my_friendly_abort (4); | |
5302 | instantiate_type (lhstype, rhs, complain); | |
5303 | } | |
5304 | } | |
700f8a87 | 5305 | #endif |
8d08fdba MS |
5306 | } |
5307 | if (complain) | |
28cbf42c | 5308 | cp_error ("no compatible member functions named `%D'", name); |
8d08fdba MS |
5309 | return error_mark_node; |
5310 | } | |
5311 | ||
5312 | case CALL_EXPR: | |
5313 | /* This is too hard for now. */ | |
5314 | my_friendly_abort (183); | |
5315 | return error_mark_node; | |
5316 | ||
5317 | case PLUS_EXPR: | |
5318 | case MINUS_EXPR: | |
5319 | case COMPOUND_EXPR: | |
a0a33927 MS |
5320 | TREE_OPERAND (rhs, 0) |
5321 | = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain); | |
8d08fdba MS |
5322 | if (TREE_OPERAND (rhs, 0) == error_mark_node) |
5323 | return error_mark_node; | |
a0a33927 MS |
5324 | TREE_OPERAND (rhs, 1) |
5325 | = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain); | |
8d08fdba MS |
5326 | if (TREE_OPERAND (rhs, 1) == error_mark_node) |
5327 | return error_mark_node; | |
5328 | ||
5329 | TREE_TYPE (rhs) = lhstype; | |
5330 | return rhs; | |
5331 | ||
5332 | case MULT_EXPR: | |
5333 | case TRUNC_DIV_EXPR: | |
5334 | case FLOOR_DIV_EXPR: | |
5335 | case CEIL_DIV_EXPR: | |
5336 | case ROUND_DIV_EXPR: | |
5337 | case RDIV_EXPR: | |
5338 | case TRUNC_MOD_EXPR: | |
5339 | case FLOOR_MOD_EXPR: | |
5340 | case CEIL_MOD_EXPR: | |
5341 | case ROUND_MOD_EXPR: | |
5342 | case FIX_ROUND_EXPR: | |
5343 | case FIX_FLOOR_EXPR: | |
5344 | case FIX_CEIL_EXPR: | |
5345 | case FIX_TRUNC_EXPR: | |
5346 | case FLOAT_EXPR: | |
5347 | case NEGATE_EXPR: | |
5348 | case ABS_EXPR: | |
5349 | case MAX_EXPR: | |
5350 | case MIN_EXPR: | |
5351 | case FFS_EXPR: | |
5352 | ||
5353 | case BIT_AND_EXPR: | |
5354 | case BIT_IOR_EXPR: | |
5355 | case BIT_XOR_EXPR: | |
5356 | case LSHIFT_EXPR: | |
5357 | case RSHIFT_EXPR: | |
5358 | case LROTATE_EXPR: | |
5359 | case RROTATE_EXPR: | |
5360 | ||
5361 | case PREINCREMENT_EXPR: | |
5362 | case PREDECREMENT_EXPR: | |
5363 | case POSTINCREMENT_EXPR: | |
5364 | case POSTDECREMENT_EXPR: | |
5365 | if (complain) | |
db6f8fbe | 5366 | error ("invalid operation on uninstantiated type"); |
8d08fdba MS |
5367 | return error_mark_node; |
5368 | ||
5369 | case TRUTH_AND_EXPR: | |
5370 | case TRUTH_OR_EXPR: | |
5371 | case TRUTH_XOR_EXPR: | |
5372 | case LT_EXPR: | |
5373 | case LE_EXPR: | |
5374 | case GT_EXPR: | |
5375 | case GE_EXPR: | |
5376 | case EQ_EXPR: | |
5377 | case NE_EXPR: | |
5378 | case TRUTH_ANDIF_EXPR: | |
5379 | case TRUTH_ORIF_EXPR: | |
5380 | case TRUTH_NOT_EXPR: | |
5381 | if (complain) | |
5382 | error ("not enough type information"); | |
5383 | return error_mark_node; | |
5384 | ||
5385 | case COND_EXPR: | |
5386 | if (type_unknown_p (TREE_OPERAND (rhs, 0))) | |
5387 | { | |
5388 | if (complain) | |
5389 | error ("not enough type information"); | |
5390 | return error_mark_node; | |
5391 | } | |
a0a33927 MS |
5392 | TREE_OPERAND (rhs, 1) |
5393 | = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain); | |
8d08fdba MS |
5394 | if (TREE_OPERAND (rhs, 1) == error_mark_node) |
5395 | return error_mark_node; | |
a0a33927 MS |
5396 | TREE_OPERAND (rhs, 2) |
5397 | = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain); | |
8d08fdba MS |
5398 | if (TREE_OPERAND (rhs, 2) == error_mark_node) |
5399 | return error_mark_node; | |
5400 | ||
5401 | TREE_TYPE (rhs) = lhstype; | |
5402 | return rhs; | |
5403 | ||
5404 | case MODIFY_EXPR: | |
a0a33927 MS |
5405 | TREE_OPERAND (rhs, 1) |
5406 | = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain); | |
8d08fdba MS |
5407 | if (TREE_OPERAND (rhs, 1) == error_mark_node) |
5408 | return error_mark_node; | |
5409 | ||
5410 | TREE_TYPE (rhs) = lhstype; | |
5411 | return rhs; | |
5412 | ||
5413 | case ADDR_EXPR: | |
700f8a87 MS |
5414 | if (TYPE_PTRMEMFUNC_P (lhstype)) |
5415 | lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype); | |
5416 | else if (TREE_CODE (lhstype) != POINTER_TYPE) | |
8d08fdba MS |
5417 | { |
5418 | if (complain) | |
5419 | error ("type for resolving address of overloaded function must be pointer type"); | |
5420 | return error_mark_node; | |
5421 | } | |
f376e137 | 5422 | { |
ec255269 | 5423 | tree fn = instantiate_type (TREE_TYPE (lhstype), TREE_OPERAND (rhs, 0), complain); |
f376e137 MS |
5424 | if (fn == error_mark_node) |
5425 | return error_mark_node; | |
5426 | mark_addressable (fn); | |
ec255269 | 5427 | TREE_TYPE (rhs) = lhstype; |
f376e137 MS |
5428 | TREE_OPERAND (rhs, 0) = fn; |
5429 | TREE_CONSTANT (rhs) = staticp (fn); | |
beb53fb8 JM |
5430 | if (TREE_CODE (lhstype) == POINTER_TYPE |
5431 | && TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE) | |
c73964b2 MS |
5432 | { |
5433 | build_ptrmemfunc_type (lhstype); | |
5434 | rhs = build_ptrmemfunc (lhstype, rhs, 0); | |
5435 | } | |
f376e137 | 5436 | } |
8d08fdba MS |
5437 | return rhs; |
5438 | ||
5439 | case ENTRY_VALUE_EXPR: | |
5440 | my_friendly_abort (184); | |
5441 | return error_mark_node; | |
5442 | ||
5443 | case ERROR_MARK: | |
5444 | return error_mark_node; | |
5445 | ||
5446 | default: | |
5447 | my_friendly_abort (185); | |
5448 | return error_mark_node; | |
5449 | } | |
5450 | } | |
5451 | \f | |
5452 | /* Return the name of the virtual function pointer field | |
5453 | (as an IDENTIFIER_NODE) for the given TYPE. Note that | |
5454 | this may have to look back through base types to find the | |
5455 | ultimate field name. (For single inheritance, these could | |
5456 | all be the same name. Who knows for multiple inheritance). */ | |
e92cc029 | 5457 | |
8d08fdba MS |
5458 | static tree |
5459 | get_vfield_name (type) | |
5460 | tree type; | |
5461 | { | |
5462 | tree binfo = TYPE_BINFO (type); | |
5463 | char *buf; | |
5464 | ||
5465 | while (BINFO_BASETYPES (binfo) | |
5466 | && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0))) | |
5467 | && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0))) | |
5468 | binfo = BINFO_BASETYPE (binfo, 0); | |
5469 | ||
5470 | type = BINFO_TYPE (binfo); | |
2636fde4 JM |
5471 | buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) |
5472 | + TYPE_NAME_LENGTH (type) + 2); | |
8d08fdba MS |
5473 | sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type)); |
5474 | return get_identifier (buf); | |
5475 | } | |
5476 | ||
5477 | void | |
5478 | print_class_statistics () | |
5479 | { | |
5480 | #ifdef GATHER_STATISTICS | |
5481 | fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness); | |
5482 | fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs); | |
5483 | fprintf (stderr, "build_method_call = %d (inner = %d)\n", | |
5484 | n_build_method_call, n_inner_fields_searched); | |
5485 | if (n_vtables) | |
5486 | { | |
5487 | fprintf (stderr, "vtables = %d; vtable searches = %d\n", | |
5488 | n_vtables, n_vtable_searches); | |
5489 | fprintf (stderr, "vtable entries = %d; vtable elems = %d\n", | |
5490 | n_vtable_entries, n_vtable_elems); | |
5491 | } | |
5492 | #endif | |
5493 | } | |
5494 | ||
5495 | /* Push an obstack which is sufficiently long-lived to hold such class | |
5496 | decls that may be cached in the previous_class_values list. For now, let's | |
5497 | use the permanent obstack, later we may create a dedicated obstack just | |
5498 | for this purpose. The effect is undone by pop_obstacks. */ | |
c91a56d2 | 5499 | |
8d08fdba MS |
5500 | void |
5501 | maybe_push_cache_obstack () | |
5502 | { | |
5503 | push_obstacks_nochange (); | |
5504 | if (current_class_depth == 1) | |
5505 | current_obstack = &permanent_obstack; | |
5506 | } | |
c91a56d2 MS |
5507 | |
5508 | /* Build a dummy reference to ourselves so Derived::Base (and A::A) works, | |
5509 | according to [class]: | |
5510 | The class-name is also inserted | |
5511 | into the scope of the class itself. For purposes of access checking, | |
5512 | the inserted class name is treated as if it were a public member name. */ | |
5513 | ||
5514 | tree | |
5515 | build_self_reference () | |
5516 | { | |
5517 | tree name = constructor_name (current_class_type); | |
5518 | tree value = build_lang_decl (TYPE_DECL, name, current_class_type); | |
5519 | DECL_NONLOCAL (value) = 1; | |
5520 | DECL_CONTEXT (value) = current_class_type; | |
5521 | DECL_CLASS_CONTEXT (value) = current_class_type; | |
5522 | CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1; | |
5523 | DECL_ARTIFICIAL (value) = 1; | |
5524 | ||
5525 | pushdecl_class_level (value); | |
5526 | return value; | |
5527 | } |