]>
Commit | Line | Data |
---|---|---|
8d08fdba MS |
1 | /* Breadth-first and depth-first routines for |
2 | searching multiple-inheritance lattice for GNU C++. | |
0dd3962d | 3 | Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc. |
8d08fdba MS |
4 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba | 22 | |
e92cc029 | 23 | /* High-level class interface. */ |
8d08fdba MS |
24 | |
25 | #include "config.h" | |
8d052bc7 | 26 | #include "system.h" |
e7a587ef | 27 | #include "tree.h" |
8d08fdba MS |
28 | #include "cp-tree.h" |
29 | #include "obstack.h" | |
30 | #include "flags.h" | |
43f2999d | 31 | #include "rtl.h" |
e8abc66f | 32 | #include "output.h" |
54f92bfb | 33 | #include "toplev.h" |
d6479fe7 | 34 | #include "varray.h" |
8d08fdba MS |
35 | |
36 | #define obstack_chunk_alloc xmalloc | |
37 | #define obstack_chunk_free free | |
38 | ||
8d08fdba | 39 | extern struct obstack *current_obstack; |
e8abc66f | 40 | extern tree abort_fndecl; |
8d08fdba MS |
41 | |
42 | #include "stack.h" | |
43 | ||
44 | /* Obstack used for remembering decision points of breadth-first. */ | |
e92cc029 | 45 | |
8d08fdba MS |
46 | static struct obstack search_obstack; |
47 | ||
48 | /* Methods for pushing and popping objects to and from obstacks. */ | |
e92cc029 | 49 | |
8d08fdba MS |
50 | struct stack_level * |
51 | push_stack_level (obstack, tp, size) | |
52 | struct obstack *obstack; | |
53 | char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */ | |
54 | int size; | |
55 | { | |
56 | struct stack_level *stack; | |
57 | obstack_grow (obstack, tp, size); | |
58 | stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size); | |
59 | obstack_finish (obstack); | |
60 | stack->obstack = obstack; | |
61 | stack->first = (tree *) obstack_base (obstack); | |
62 | stack->limit = obstack_room (obstack) / sizeof (tree *); | |
63 | return stack; | |
64 | } | |
65 | ||
66 | struct stack_level * | |
67 | pop_stack_level (stack) | |
68 | struct stack_level *stack; | |
69 | { | |
70 | struct stack_level *tem = stack; | |
71 | struct obstack *obstack = tem->obstack; | |
72 | stack = tem->prev; | |
73 | obstack_free (obstack, tem); | |
74 | return stack; | |
75 | } | |
76 | ||
77 | #define search_level stack_level | |
78 | static struct search_level *search_stack; | |
79 | ||
49c249e1 | 80 | static tree get_abstract_virtuals_1 PROTO((tree, int, tree)); |
9e0781b5 | 81 | static tree next_baselink PROTO((tree)); |
49c249e1 JM |
82 | static tree get_vbase_1 PROTO((tree, tree, unsigned int *)); |
83 | static tree convert_pointer_to_vbase PROTO((tree, tree)); | |
84 | static tree lookup_field_1 PROTO((tree, tree)); | |
85 | static tree convert_pointer_to_single_level PROTO((tree, tree)); | |
49c249e1 JM |
86 | static int lookup_fnfields_here PROTO((tree, tree)); |
87 | static int is_subobject_of_p PROTO((tree, tree)); | |
88 | static int hides PROTO((tree, tree)); | |
89 | static tree virtual_context PROTO((tree, tree, tree)); | |
d6479fe7 MM |
90 | static tree dfs_check_overlap PROTO((tree, void *)); |
91 | static tree dfs_no_overlap_yet PROTO((tree, void *)); | |
49c249e1 | 92 | static int get_base_distance_recursive |
514a1f18 | 93 | PROTO((tree, int, int, int, int *, tree *, tree, |
49c249e1 JM |
94 | int, int *, int, int)); |
95 | static void expand_upcast_fixups | |
96 | PROTO((tree, tree, tree, tree, tree, tree, tree *)); | |
97 | static void fixup_virtual_upcast_offsets | |
98 | PROTO((tree, tree, int, int, tree, tree, tree, tree, | |
99 | tree *)); | |
d6479fe7 MM |
100 | static tree unmarkedp PROTO((tree, void *)); |
101 | static tree marked_vtable_pathp PROTO((tree, void *)); | |
102 | static tree unmarked_vtable_pathp PROTO((tree, void *)); | |
103 | static tree marked_new_vtablep PROTO((tree, void *)); | |
104 | static tree unmarked_new_vtablep PROTO((tree, void *)); | |
105 | static tree marked_pushdecls_p PROTO((tree, void *)); | |
106 | static tree unmarked_pushdecls_p PROTO((tree, void *)); | |
107 | static tree dfs_debug_unmarkedp PROTO((tree, void *)); | |
108 | static tree dfs_debug_mark PROTO((tree, void *)); | |
109 | static tree dfs_find_vbases PROTO((tree, void *)); | |
110 | static tree dfs_clear_vbase_slots PROTO((tree, void *)); | |
111 | static tree dfs_init_vbase_pointers PROTO((tree, void *)); | |
112 | static tree dfs_get_vbase_types PROTO((tree, void *)); | |
8f032717 MM |
113 | static tree dfs_push_type_decls PROTO((tree, void *)); |
114 | static tree dfs_push_decls PROTO((tree, void *)); | |
d6479fe7 | 115 | static tree dfs_unuse_fields PROTO((tree, void *)); |
7d4bdeed | 116 | static tree add_conversions PROTO((tree, void *)); |
d6479fe7 | 117 | static tree get_virtuals_named_this PROTO((tree, tree)); |
7d4bdeed | 118 | static tree get_virtual_destructor PROTO((tree, void *)); |
d6479fe7 | 119 | static tree tree_has_any_destructor_p PROTO((tree, void *)); |
c6160f8f | 120 | static int covariant_return_p PROTO((tree, tree)); |
49c249e1 JM |
121 | static struct search_level *push_search_level |
122 | PROTO((struct stack_level *, struct obstack *)); | |
123 | static struct search_level *pop_search_level | |
124 | PROTO((struct stack_level *)); | |
d6479fe7 MM |
125 | static tree bfs_walk |
126 | PROTO((tree, tree (*) (tree, void *), tree (*) (tree, void *), | |
127 | void *)); | |
128 | static tree lookup_field_queue_p PROTO((tree, void *)); | |
7d4bdeed | 129 | static tree lookup_field_r PROTO((tree, void *)); |
d6479fe7 MM |
130 | static tree dfs_walk_real PROTO ((tree, |
131 | tree (*) (tree, void *), | |
132 | tree (*) (tree, void *), | |
133 | tree (*) (tree, void *), | |
134 | void *)); | |
135 | static tree dfs_bfv_queue_p PROTO ((tree, void *)); | |
136 | static tree dfs_bfv_helper PROTO ((tree, void *)); | |
137 | static tree get_virtuals_named_this_r PROTO ((tree, void *)); | |
138 | static tree context_for_name_lookup PROTO ((tree)); | |
139 | static tree canonical_binfo PROTO ((tree)); | |
140 | static tree shared_marked_p PROTO ((tree, void *)); | |
141 | static tree shared_unmarked_p PROTO ((tree, void *)); | |
142 | static int dependent_base_p PROTO ((tree)); | |
143 | static tree dfs_accessible_queue_p PROTO ((tree, void *)); | |
144 | static tree dfs_accessible_p PROTO ((tree, void *)); | |
145 | static tree dfs_access_in_type PROTO ((tree, void *)); | |
146 | static tree access_in_type PROTO ((tree, tree)); | |
6cbd257e | 147 | static tree dfs_canonical_queue PROTO ((tree, void *)); |
1e61a9d9 MM |
148 | static tree dfs_assert_unmarked_p PROTO ((tree, void *)); |
149 | static void assert_canonical_unmarked PROTO ((tree)); | |
6a629cac MM |
150 | static int protected_accessible_p PROTO ((tree, tree, tree, tree)); |
151 | static int friend_accessible_p PROTO ((tree, tree, tree, tree)); | |
8f032717 MM |
152 | static void setup_class_bindings PROTO ((tree, int)); |
153 | static int template_self_reference_p PROTO ((tree, tree)); | |
8d08fdba MS |
154 | |
155 | /* Allocate a level of searching. */ | |
e92cc029 | 156 | |
8d08fdba MS |
157 | static struct search_level * |
158 | push_search_level (stack, obstack) | |
159 | struct stack_level *stack; | |
160 | struct obstack *obstack; | |
161 | { | |
162 | struct search_level tem; | |
163 | ||
164 | tem.prev = stack; | |
165 | return push_stack_level (obstack, (char *)&tem, sizeof (tem)); | |
166 | } | |
167 | ||
168 | /* Discard a level of search allocation. */ | |
e92cc029 | 169 | |
8d08fdba MS |
170 | static struct search_level * |
171 | pop_search_level (obstack) | |
172 | struct stack_level *obstack; | |
173 | { | |
174 | register struct search_level *stack = pop_stack_level (obstack); | |
175 | ||
176 | return stack; | |
177 | } | |
178 | \f | |
8d08fdba MS |
179 | static tree _vptr_name; |
180 | ||
8d08fdba | 181 | /* Variables for gathering statistics. */ |
5566b478 | 182 | #ifdef GATHER_STATISTICS |
8d08fdba MS |
183 | static int n_fields_searched; |
184 | static int n_calls_lookup_field, n_calls_lookup_field_1; | |
185 | static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1; | |
186 | static int n_calls_get_base_type; | |
187 | static int n_outer_fields_searched; | |
188 | static int n_contexts_saved; | |
fc378698 | 189 | #endif /* GATHER_STATISTICS */ |
8d08fdba | 190 | |
8d08fdba | 191 | \f |
acc9fe20 RK |
192 | /* Get a virtual binfo that is found inside BINFO's hierarchy that is |
193 | the same type as the type given in PARENT. To be optimal, we want | |
194 | the first one that is found by going through the least number of | |
514a1f18 JM |
195 | virtual bases. |
196 | ||
197 | This uses a clever algorithm that updates *depth when we find the vbase, | |
198 | and cuts off other paths of search when they reach that depth. */ | |
e92cc029 | 199 | |
acc9fe20 | 200 | static tree |
bd6dd845 | 201 | get_vbase_1 (parent, binfo, depth) |
acc9fe20 RK |
202 | tree parent, binfo; |
203 | unsigned int *depth; | |
204 | { | |
205 | tree binfos; | |
206 | int i, n_baselinks; | |
207 | tree rval = NULL_TREE; | |
208 | ||
acc9fe20 RK |
209 | if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo)) |
210 | { | |
211 | *depth = 0; | |
212 | return binfo; | |
213 | } | |
214 | ||
215 | *depth = *depth - 1; | |
216 | ||
217 | binfos = BINFO_BASETYPES (binfo); | |
218 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
219 | ||
220 | /* Process base types. */ | |
221 | for (i = 0; i < n_baselinks; i++) | |
222 | { | |
223 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
224 | tree nrval; | |
225 | ||
226 | if (*depth == 0) | |
227 | break; | |
228 | ||
bd6dd845 | 229 | nrval = get_vbase_1 (parent, base_binfo, depth); |
acc9fe20 RK |
230 | if (nrval) |
231 | rval = nrval; | |
232 | } | |
233 | *depth = *depth+1; | |
234 | return rval; | |
235 | } | |
236 | ||
514a1f18 JM |
237 | /* Return the shortest path to vbase PARENT within BINFO, ignoring |
238 | access and ambiguity. */ | |
239 | ||
bd6dd845 MS |
240 | tree |
241 | get_vbase (parent, binfo) | |
242 | tree parent; | |
243 | tree binfo; | |
244 | { | |
245 | unsigned int d = (unsigned int)-1; | |
246 | return get_vbase_1 (parent, binfo, &d); | |
247 | } | |
248 | ||
f30432d7 MS |
249 | /* Convert EXPR to a virtual base class of type TYPE. We know that |
250 | EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that | |
251 | the type of what expr points to has a virtual base of type TYPE. */ | |
e92cc029 | 252 | |
bd6dd845 | 253 | static tree |
f30432d7 MS |
254 | convert_pointer_to_vbase (type, expr) |
255 | tree type; | |
256 | tree expr; | |
257 | { | |
bd6dd845 | 258 | tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)))); |
f30432d7 MS |
259 | return convert_pointer_to_real (vb, expr); |
260 | } | |
261 | ||
8d08fdba MS |
262 | /* Check whether the type given in BINFO is derived from PARENT. If |
263 | it isn't, return 0. If it is, but the derivation is MI-ambiguous | |
264 | AND protect != 0, emit an error message and return error_mark_node. | |
265 | ||
266 | Otherwise, if TYPE is derived from PARENT, return the actual base | |
267 | information, unless a one of the protection violations below | |
268 | occurs, in which case emit an error message and return error_mark_node. | |
269 | ||
270 | If PROTECT is 1, then check if access to a public field of PARENT | |
271 | would be private. Also check for ambiguity. */ | |
272 | ||
273 | tree | |
274 | get_binfo (parent, binfo, protect) | |
275 | register tree parent, binfo; | |
276 | int protect; | |
277 | { | |
a703fb38 | 278 | tree type = NULL_TREE; |
8d08fdba MS |
279 | int dist; |
280 | tree rval = NULL_TREE; | |
281 | ||
282 | if (TREE_CODE (parent) == TREE_VEC) | |
283 | parent = BINFO_TYPE (parent); | |
71851aaa | 284 | else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent))) |
8d08fdba MS |
285 | my_friendly_abort (89); |
286 | ||
287 | if (TREE_CODE (binfo) == TREE_VEC) | |
288 | type = BINFO_TYPE (binfo); | |
71851aaa | 289 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo))) |
8d08fdba MS |
290 | type = binfo; |
291 | else | |
292 | my_friendly_abort (90); | |
293 | ||
294 | dist = get_base_distance (parent, binfo, protect, &rval); | |
295 | ||
296 | if (dist == -3) | |
297 | { | |
8251199e | 298 | cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance", |
8d08fdba MS |
299 | parent, type); |
300 | return error_mark_node; | |
301 | } | |
302 | else if (dist == -2 && protect) | |
303 | { | |
8251199e | 304 | cp_error ("type `%T' is ambiguous base class for type `%T'", parent, |
8d08fdba MS |
305 | type); |
306 | return error_mark_node; | |
307 | } | |
308 | ||
309 | return rval; | |
310 | } | |
311 | ||
312 | /* This is the newer depth first get_base_distance routine. */ | |
e92cc029 | 313 | |
8926095f | 314 | static int |
5566b478 | 315 | get_base_distance_recursive (binfo, depth, is_private, rval, |
514a1f18 | 316 | rval_private_ptr, new_binfo_ptr, parent, |
6b5fbb55 MS |
317 | protect, via_virtual_ptr, via_virtual, |
318 | current_scope_in_chain) | |
319 | tree binfo; | |
320 | int depth, is_private, rval; | |
321 | int *rval_private_ptr; | |
514a1f18 | 322 | tree *new_binfo_ptr, parent; |
6b5fbb55 MS |
323 | int protect, *via_virtual_ptr, via_virtual; |
324 | int current_scope_in_chain; | |
8d08fdba MS |
325 | { |
326 | tree binfos; | |
327 | int i, n_baselinks; | |
328 | ||
6b5fbb55 MS |
329 | if (protect |
330 | && !current_scope_in_chain | |
331 | && is_friend (BINFO_TYPE (binfo), current_scope ())) | |
332 | current_scope_in_chain = 1; | |
333 | ||
8d08fdba MS |
334 | if (BINFO_TYPE (binfo) == parent || binfo == parent) |
335 | { | |
514a1f18 JM |
336 | int better = 0; |
337 | ||
8d08fdba | 338 | if (rval == -1) |
514a1f18 JM |
339 | /* This is the first time we've found parent. */ |
340 | better = 1; | |
341 | else if (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr), | |
342 | BINFO_OFFSET (binfo)) | |
343 | && *via_virtual_ptr && via_virtual) | |
344 | { | |
345 | /* A new path to the same vbase. If this one has better | |
346 | access or is shorter, take it. */ | |
347 | ||
348 | if (protect) | |
349 | better = *rval_private_ptr - is_private; | |
350 | if (better == 0) | |
351 | better = rval - depth; | |
352 | } | |
353 | else | |
354 | { | |
355 | /* Ambiguous base class. */ | |
356 | rval = depth = -2; | |
357 | ||
358 | /* If we get an ambiguity between virtual and non-virtual base | |
359 | class, return the non-virtual in case we are ignoring | |
360 | ambiguity. */ | |
361 | better = *via_virtual_ptr - via_virtual; | |
362 | } | |
363 | ||
364 | if (better > 0) | |
8d08fdba MS |
365 | { |
366 | rval = depth; | |
367 | *rval_private_ptr = is_private; | |
368 | *new_binfo_ptr = binfo; | |
369 | *via_virtual_ptr = via_virtual; | |
370 | } | |
514a1f18 | 371 | |
8d08fdba MS |
372 | return rval; |
373 | } | |
374 | ||
375 | binfos = BINFO_BASETYPES (binfo); | |
376 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
377 | depth += 1; | |
378 | ||
379 | /* Process base types. */ | |
380 | for (i = 0; i < n_baselinks; i++) | |
381 | { | |
382 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
383 | ||
514a1f18 JM |
384 | int via_private |
385 | = (protect | |
386 | && (is_private | |
387 | || (!TREE_VIA_PUBLIC (base_binfo) | |
388 | && !(TREE_VIA_PROTECTED (base_binfo) | |
389 | && current_scope_in_chain) | |
390 | && !is_friend (BINFO_TYPE (binfo), current_scope ())))); | |
391 | int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo); | |
392 | ||
393 | rval = get_base_distance_recursive (base_binfo, depth, via_private, | |
394 | rval, rval_private_ptr, | |
395 | new_binfo_ptr, parent, | |
396 | protect, via_virtual_ptr, | |
397 | this_virtual, | |
398 | current_scope_in_chain); | |
399 | ||
400 | /* If we've found a non-virtual, ambiguous base class, we don't need | |
401 | to keep searching. */ | |
402 | if (rval == -2 && *via_virtual_ptr == 0) | |
403 | return rval; | |
8d08fdba MS |
404 | } |
405 | ||
406 | return rval; | |
407 | } | |
408 | ||
409 | /* Return the number of levels between type PARENT and the type given | |
410 | in BINFO, following the leftmost path to PARENT not found along a | |
411 | virtual path, if there are no real PARENTs (all come from virtual | |
514a1f18 | 412 | base classes), then follow the shortest public path to PARENT. |
8d08fdba MS |
413 | |
414 | Return -1 if TYPE is not derived from PARENT. | |
415 | Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is | |
416 | non-negative. | |
417 | Return -3 if PARENT is private to TYPE, and PROTECT is non-zero. | |
418 | ||
419 | If PATH_PTR is non-NULL, then also build the list of types | |
ddd5a7c1 | 420 | from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC |
8d08fdba MS |
421 | set. |
422 | ||
423 | PARENT can also be a binfo, in which case that exact parent is found | |
424 | and no other. convert_pointer_to_real uses this functionality. | |
425 | ||
39211cd5 | 426 | If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */ |
8d08fdba MS |
427 | |
428 | int | |
429 | get_base_distance (parent, binfo, protect, path_ptr) | |
430 | register tree parent, binfo; | |
431 | int protect; | |
432 | tree *path_ptr; | |
433 | { | |
8d08fdba | 434 | int rval; |
8d08fdba | 435 | int rval_private = 0; |
a703fb38 | 436 | tree type = NULL_TREE; |
8d08fdba MS |
437 | tree new_binfo = NULL_TREE; |
438 | int via_virtual; | |
439 | int watch_access = protect; | |
440 | ||
5566b478 | 441 | /* Should we be completing types here? */ |
8d08fdba | 442 | if (TREE_CODE (parent) != TREE_VEC) |
5566b478 MS |
443 | parent = complete_type (TYPE_MAIN_VARIANT (parent)); |
444 | else | |
445 | complete_type (TREE_TYPE (parent)); | |
8d08fdba MS |
446 | |
447 | if (TREE_CODE (binfo) == TREE_VEC) | |
448 | type = BINFO_TYPE (binfo); | |
f0e01782 | 449 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo))) |
8d08fdba | 450 | { |
e92cc029 | 451 | type = complete_type (binfo); |
8d08fdba MS |
452 | binfo = TYPE_BINFO (type); |
453 | ||
454 | if (path_ptr) | |
dfbcd65a JM |
455 | my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE, |
456 | 980827); | |
8d08fdba MS |
457 | } |
458 | else | |
459 | my_friendly_abort (92); | |
460 | ||
461 | if (parent == type || parent == binfo) | |
462 | { | |
463 | /* If the distance is 0, then we don't really need | |
464 | a path pointer, but we shouldn't let garbage go back. */ | |
465 | if (path_ptr) | |
466 | *path_ptr = binfo; | |
467 | return 0; | |
468 | } | |
469 | ||
470 | if (path_ptr) | |
471 | watch_access = 1; | |
472 | ||
5566b478 | 473 | rval = get_base_distance_recursive (binfo, 0, 0, -1, |
8d08fdba | 474 | &rval_private, &new_binfo, parent, |
514a1f18 | 475 | watch_access, &via_virtual, 0, |
6b5fbb55 | 476 | 0); |
8d08fdba | 477 | |
8d08fdba MS |
478 | /* Access restrictions don't count if we found an ambiguous basetype. */ |
479 | if (rval == -2 && protect >= 0) | |
480 | rval_private = 0; | |
481 | ||
482 | if (rval && protect && rval_private) | |
483 | return -3; | |
484 | ||
514a1f18 | 485 | /* If they gave us the real vbase binfo, which isn't in the main binfo |
18141e4c JM |
486 | tree, deal with it. This happens when we are called from |
487 | expand_upcast_fixups. */ | |
39211cd5 MS |
488 | if (rval == -1 && TREE_CODE (parent) == TREE_VEC |
489 | && parent == binfo_member (BINFO_TYPE (parent), | |
490 | CLASSTYPE_VBASECLASSES (type))) | |
491 | { | |
dfbcd65a | 492 | my_friendly_assert (BINFO_INHERITANCE_CHAIN (parent) == binfo, 980827); |
39211cd5 MS |
493 | new_binfo = parent; |
494 | rval = 1; | |
495 | } | |
496 | ||
8d08fdba MS |
497 | if (path_ptr) |
498 | *path_ptr = new_binfo; | |
499 | return rval; | |
500 | } | |
501 | ||
502 | /* Search for a member with name NAME in a multiple inheritance lattice | |
503 | specified by TYPE. If it does not exist, return NULL_TREE. | |
504 | If the member is ambiguously referenced, return `error_mark_node'. | |
505 | Otherwise, return the FIELD_DECL. */ | |
506 | ||
507 | /* Do a 1-level search for NAME as a member of TYPE. The caller must | |
508 | figure out whether it can access this field. (Since it is only one | |
509 | level, this is reasonable.) */ | |
e92cc029 | 510 | |
8d08fdba MS |
511 | static tree |
512 | lookup_field_1 (type, name) | |
513 | tree type, name; | |
514 | { | |
f84b4be9 JM |
515 | register tree field; |
516 | ||
517 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM | |
518 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
519 | /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all; | |
520 | instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously, | |
521 | the code often worked even when we treated the index as a list | |
522 | of fields!) */ | |
523 | return NULL_TREE; | |
524 | ||
525 | field = TYPE_FIELDS (type); | |
8d08fdba MS |
526 | |
527 | #ifdef GATHER_STATISTICS | |
528 | n_calls_lookup_field_1++; | |
fc378698 | 529 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
530 | while (field) |
531 | { | |
532 | #ifdef GATHER_STATISTICS | |
533 | n_fields_searched++; | |
fc378698 | 534 | #endif /* GATHER_STATISTICS */ |
f84b4be9 | 535 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field)) == 'd', 0); |
8d08fdba MS |
536 | if (DECL_NAME (field) == NULL_TREE |
537 | && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
538 | { | |
539 | tree temp = lookup_field_1 (TREE_TYPE (field), name); | |
540 | if (temp) | |
541 | return temp; | |
542 | } | |
2036a15c MM |
543 | if (TREE_CODE (field) == USING_DECL) |
544 | /* For now, we're just treating member using declarations as | |
545 | old ARM-style access declarations. Thus, there's no reason | |
546 | to return a USING_DECL, and the rest of the compiler can't | |
547 | handle it. Once the class is defined, these are purged | |
548 | from TYPE_FIELDS anyhow; see handle_using_decl. */ | |
549 | ; | |
550 | else if (DECL_NAME (field) == name) | |
8d08fdba MS |
551 | { |
552 | if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL) | |
553 | && DECL_ASSEMBLER_NAME (field) != NULL) | |
554 | GNU_xref_ref(current_function_decl, | |
555 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field))); | |
556 | return field; | |
557 | } | |
558 | field = TREE_CHAIN (field); | |
559 | } | |
560 | /* Not found. */ | |
561 | if (name == _vptr_name) | |
562 | { | |
563 | /* Give the user what s/he thinks s/he wants. */ | |
564 | if (TYPE_VIRTUAL_P (type)) | |
565 | return CLASSTYPE_VFIELD (type); | |
566 | } | |
567 | return NULL_TREE; | |
568 | } | |
569 | ||
7177d104 MS |
570 | /* There are a number of cases we need to be aware of here: |
571 | current_class_type current_function_decl | |
e92cc029 MS |
572 | global NULL NULL |
573 | fn-local NULL SET | |
574 | class-local SET NULL | |
575 | class->fn SET SET | |
576 | fn->class SET SET | |
7177d104 MS |
577 | |
578 | Those last two make life interesting. If we're in a function which is | |
579 | itself inside a class, we need decls to go into the fn's decls (our | |
580 | second case below). But if we're in a class and the class itself is | |
581 | inside a function, we need decls to go into the decls for the class. To | |
4ac14744 | 582 | achieve this last goal, we must see if, when both current_class_ptr and |
7177d104 MS |
583 | current_function_decl are set, the class was declared inside that |
584 | function. If so, we know to put the decls into the class's scope. */ | |
585 | ||
8d08fdba MS |
586 | tree |
587 | current_scope () | |
588 | { | |
589 | if (current_function_decl == NULL_TREE) | |
590 | return current_class_type; | |
591 | if (current_class_type == NULL_TREE) | |
592 | return current_function_decl; | |
593 | if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type) | |
594 | return current_function_decl; | |
595 | ||
596 | return current_class_type; | |
597 | } | |
598 | ||
d6479fe7 | 599 | /* Return the scope of DECL, as appropriate when doing name-lookup. */ |
8d08fdba | 600 | |
d6479fe7 MM |
601 | static tree |
602 | context_for_name_lookup (decl) | |
603 | tree decl; | |
604 | { | |
605 | /* [class.union] | |
606 | ||
607 | For the purposes of name lookup, after the anonymous union | |
608 | definition, the members of the anonymous union are considered to | |
609 | have been defined in the scope in which teh anonymous union is | |
610 | declared. */ | |
611 | tree context = DECL_REAL_CONTEXT (decl); | |
612 | ||
613 | while (TYPE_P (context) && ANON_UNION_TYPE_P (context)) | |
614 | context = TYPE_CONTEXT (context); | |
615 | if (!context) | |
616 | context = global_namespace; | |
8d08fdba | 617 | |
d6479fe7 MM |
618 | return context; |
619 | } | |
8d08fdba | 620 | |
d6479fe7 MM |
621 | /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO |
622 | otherwise. */ | |
8d08fdba | 623 | |
d6479fe7 MM |
624 | static tree |
625 | canonical_binfo (binfo) | |
626 | tree binfo; | |
627 | { | |
628 | return (TREE_VIA_VIRTUAL (binfo) | |
629 | ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo); | |
630 | } | |
8d08fdba | 631 | |
6cbd257e MM |
632 | /* A queue function that simply ensures that we walk into the |
633 | canonical versions of virtual bases. */ | |
634 | ||
635 | static tree | |
636 | dfs_canonical_queue (binfo, data) | |
637 | tree binfo; | |
638 | void *data ATTRIBUTE_UNUSED; | |
639 | { | |
640 | return canonical_binfo (binfo); | |
641 | } | |
642 | ||
643 | /* Called via dfs_walk from assert_canonical_unmarked. */ | |
644 | ||
645 | static tree | |
646 | dfs_assert_unmarked_p (binfo, data) | |
647 | tree binfo; | |
648 | void *data ATTRIBUTE_UNUSED; | |
649 | { | |
650 | my_friendly_assert (!BINFO_MARKED (binfo), 0); | |
651 | return NULL_TREE; | |
652 | } | |
653 | ||
654 | /* Asserts that all the nodes below BINFO (using the canonical | |
655 | versions of virtual bases) are unmarked. */ | |
656 | ||
657 | static void | |
658 | assert_canonical_unmarked (binfo) | |
659 | tree binfo; | |
660 | { | |
661 | dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0); | |
662 | } | |
663 | ||
d6479fe7 MM |
664 | /* If BINFO is marked, return a canonical version of BINFO. |
665 | Otherwise, return NULL_TREE. */ | |
8d08fdba | 666 | |
d6479fe7 MM |
667 | static tree |
668 | shared_marked_p (binfo, data) | |
669 | tree binfo; | |
670 | void *data; | |
671 | { | |
672 | binfo = canonical_binfo (binfo); | |
673 | return markedp (binfo, data) ? binfo : NULL_TREE; | |
674 | } | |
8d08fdba | 675 | |
d6479fe7 MM |
676 | /* If BINFO is not marked, return a canonical version of BINFO. |
677 | Otherwise, return NULL_TREE. */ | |
8d08fdba | 678 | |
d6479fe7 MM |
679 | static tree |
680 | shared_unmarked_p (binfo, data) | |
681 | tree binfo; | |
682 | void *data; | |
8d08fdba | 683 | { |
d6479fe7 MM |
684 | binfo = canonical_binfo (binfo); |
685 | return unmarkedp (binfo, data) ? binfo : NULL_TREE; | |
686 | } | |
8d08fdba | 687 | |
d6479fe7 MM |
688 | /* Called from access_in_type via dfs_walk. Calculate the access to |
689 | DATA (which is really a DECL) in BINFO. */ | |
eae89e04 | 690 | |
d6479fe7 MM |
691 | static tree |
692 | dfs_access_in_type (binfo, data) | |
693 | tree binfo; | |
694 | void *data; | |
695 | { | |
696 | tree decl = (tree) data; | |
697 | tree type = BINFO_TYPE (binfo); | |
698 | tree access = NULL_TREE; | |
8d08fdba | 699 | |
d6479fe7 | 700 | if (context_for_name_lookup (decl) == type) |
8d08fdba | 701 | { |
d6479fe7 MM |
702 | /* If we have desceneded to the scope of DECL, just note the |
703 | appropriate access. */ | |
704 | if (TREE_PRIVATE (decl)) | |
705 | access = access_private_node; | |
706 | else if (TREE_PROTECTED (decl)) | |
707 | access = access_protected_node; | |
708 | else | |
709 | access = access_public_node; | |
8d08fdba | 710 | } |
d6479fe7 MM |
711 | else |
712 | { | |
713 | /* First, check for an access-declaration that gives us more | |
714 | access to the DECL. The CONST_DECL for an enumeration | |
715 | constant will not have DECL_LANG_SPECIFIC, and thus no | |
716 | DECL_ACCESS. */ | |
717 | if (DECL_LANG_SPECIFIC (decl)) | |
718 | { | |
719 | access = purpose_member (type, DECL_ACCESS (decl)); | |
720 | if (access) | |
721 | access = TREE_VALUE (access); | |
722 | } | |
723 | ||
724 | if (!access) | |
725 | { | |
726 | int i; | |
727 | int n_baselinks; | |
728 | tree binfos; | |
729 | ||
730 | /* Otherwise, scan our baseclasses, and pick the most favorable | |
731 | access. */ | |
732 | binfos = BINFO_BASETYPES (binfo); | |
733 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
734 | for (i = 0; i < n_baselinks; ++i) | |
735 | { | |
736 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
737 | tree base_access = TREE_CHAIN (canonical_binfo (base_binfo)); | |
738 | ||
739 | if (!base_access || base_access == access_private_node) | |
740 | /* If it was not accessible in the base, or only | |
741 | accessible as a private member, we can't access it | |
742 | all. */ | |
743 | base_access = NULL_TREE; | |
744 | else if (TREE_VIA_PROTECTED (base_binfo)) | |
745 | /* Public and protected members in the base are | |
746 | protected here. */ | |
747 | base_access = access_protected_node; | |
748 | else if (!TREE_VIA_PUBLIC (base_binfo)) | |
749 | /* Public and protected members in the base are | |
750 | private here. */ | |
751 | base_access = access_private_node; | |
752 | ||
753 | /* See if the new access, via this base, gives more | |
754 | access than our previous best access. */ | |
755 | if (base_access && | |
756 | (base_access == access_public_node | |
757 | || (base_access == access_protected_node | |
758 | && access != access_public_node) | |
759 | || (base_access == access_private_node | |
760 | && !access))) | |
761 | { | |
762 | access = base_access; | |
8d08fdba | 763 | |
d6479fe7 MM |
764 | /* If the new access is public, we can't do better. */ |
765 | if (access == access_public_node) | |
766 | break; | |
767 | } | |
768 | } | |
769 | } | |
770 | } | |
faae18ab | 771 | |
d6479fe7 MM |
772 | /* Note the access to DECL in TYPE. */ |
773 | TREE_CHAIN (binfo) = access; | |
02020185 | 774 | |
d6479fe7 MM |
775 | /* Mark TYPE as visited so that if we reach it again we do not |
776 | duplicate our efforts here. */ | |
777 | SET_BINFO_MARKED (binfo); | |
8d08fdba | 778 | |
d6479fe7 MM |
779 | return NULL_TREE; |
780 | } | |
8d08fdba | 781 | |
d6479fe7 | 782 | /* Return the access to DECL in TYPE. */ |
8d08fdba | 783 | |
d6479fe7 MM |
784 | static tree |
785 | access_in_type (type, decl) | |
786 | tree type; | |
787 | tree decl; | |
788 | { | |
789 | tree binfo = TYPE_BINFO (type); | |
8d08fdba | 790 | |
d6479fe7 | 791 | /* We must take into account |
8d08fdba | 792 | |
d6479fe7 | 793 | [class.paths] |
8d08fdba | 794 | |
d6479fe7 MM |
795 | If a name can be reached by several paths through a multiple |
796 | inheritance graph, the access is that of the path that gives | |
797 | most access. | |
8d08fdba | 798 | |
d6479fe7 MM |
799 | The algorithm we use is to make a post-order depth-first traversal |
800 | of the base-class hierarchy. As we come up the tree, we annotate | |
801 | each node with the most lenient access. */ | |
802 | dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl); | |
803 | dfs_walk (binfo, dfs_unmark, shared_marked_p, 0); | |
6cbd257e | 804 | assert_canonical_unmarked (binfo); |
8d08fdba | 805 | |
d6479fe7 MM |
806 | return TREE_CHAIN (binfo); |
807 | } | |
808 | ||
809 | /* Called from dfs_accessible_p via dfs_walk. */ | |
810 | ||
811 | static tree | |
812 | dfs_accessible_queue_p (binfo, data) | |
813 | tree binfo; | |
814 | void *data ATTRIBUTE_UNUSED; | |
815 | { | |
816 | if (BINFO_MARKED (binfo)) | |
817 | return NULL_TREE; | |
818 | ||
819 | /* If this class is inherited via private or protected inheritance, | |
820 | then we can't see it, unless we are a friend of the subclass. */ | |
821 | if (!TREE_VIA_PUBLIC (binfo) | |
822 | && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), | |
823 | current_scope ())) | |
824 | return NULL_TREE; | |
825 | ||
826 | return canonical_binfo (binfo); | |
827 | } | |
828 | ||
829 | /* Called from dfs_accessible_p via dfs_walk. */ | |
830 | ||
831 | static tree | |
832 | dfs_accessible_p (binfo, data) | |
833 | tree binfo; | |
834 | void *data; | |
835 | { | |
836 | int protected_ok = data != 0; | |
837 | tree access; | |
838 | ||
839 | /* We marked the binfos while computing the access in each type. | |
840 | So, we unmark as we go now. */ | |
841 | SET_BINFO_MARKED (binfo); | |
842 | ||
843 | access = TREE_CHAIN (binfo); | |
844 | if (access == access_public_node | |
845 | || (access == access_protected_node && protected_ok)) | |
846 | return binfo; | |
847 | else if (access && is_friend (BINFO_TYPE (binfo), current_scope ())) | |
848 | return binfo; | |
849 | ||
850 | return NULL_TREE; | |
851 | } | |
852 | ||
6a629cac MM |
853 | /* Returns non-zero if it is OK to access DECL when named in TYPE |
854 | through an object indiated by BINFO in the context of DERIVED. */ | |
855 | ||
856 | static int | |
857 | protected_accessible_p (type, decl, derived, binfo) | |
858 | tree type; | |
859 | tree decl; | |
860 | tree derived; | |
861 | tree binfo; | |
862 | { | |
863 | tree access; | |
864 | ||
865 | /* We're checking this clause from [class.access.base] | |
866 | ||
867 | m as a member of N is protected, and the reference occurs in a | |
868 | member or friend of class N, or in a member or friend of a | |
869 | class P derived from N, where m as a member of P is private or | |
870 | protected. | |
871 | ||
872 | If DERIVED isn't derived from TYPE, then it certainly does not | |
873 | apply. */ | |
874 | if (!DERIVED_FROM_P (type, derived)) | |
875 | return 0; | |
876 | ||
877 | access = access_in_type (derived, decl); | |
878 | if (same_type_p (derived, type)) | |
879 | { | |
880 | if (access != access_private_node) | |
881 | return 0; | |
882 | } | |
883 | else if (access != access_private_node | |
884 | && access != access_protected_node) | |
885 | return 0; | |
886 | ||
887 | /* [class.protected] | |
888 | ||
889 | When a friend or a member function of a derived class references | |
890 | a protected nonstatic member of a base class, an access check | |
891 | applies in addition to those described earlier in clause | |
892 | _class.access_.4) Except when forming a pointer to member | |
893 | (_expr.unary.op_), the access must be through a pointer to, | |
894 | reference to, or object of the derived class itself (or any class | |
895 | derived from that class) (_expr.ref_). If the access is to form | |
896 | a pointer to member, the nested-name-specifier shall name the | |
897 | derived class (or any class derived from that class). */ | |
898 | if (DECL_NONSTATIC_MEMBER_P (decl)) | |
899 | { | |
900 | /* We can tell through what the reference is occurring by | |
901 | chasing BINFO up to the root. */ | |
902 | tree t = binfo; | |
903 | while (BINFO_INHERITANCE_CHAIN (t)) | |
904 | t = BINFO_INHERITANCE_CHAIN (t); | |
905 | ||
906 | if (!DERIVED_FROM_P (derived, BINFO_TYPE (t))) | |
907 | return 0; | |
908 | } | |
909 | ||
910 | return 1; | |
911 | } | |
912 | ||
913 | /* Returns non-zero if SCOPE is a friend of a type which would be able | |
914 | to acces DECL, named in TYPE, through the object indicated by | |
915 | BINFO. */ | |
916 | ||
917 | static int | |
918 | friend_accessible_p (scope, type, decl, binfo) | |
919 | tree scope; | |
920 | tree type; | |
921 | tree decl; | |
922 | tree binfo; | |
923 | { | |
924 | tree befriending_classes; | |
925 | tree t; | |
926 | ||
927 | if (!scope) | |
928 | return 0; | |
929 | ||
930 | if (TREE_CODE (scope) == FUNCTION_DECL | |
931 | || DECL_FUNCTION_TEMPLATE_P (scope)) | |
932 | befriending_classes = DECL_BEFRIENDING_CLASSES (scope); | |
933 | else if (TYPE_P (scope)) | |
934 | befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope); | |
935 | else | |
936 | return 0; | |
937 | ||
938 | for (t = befriending_classes; t; t = TREE_CHAIN (t)) | |
939 | if (protected_accessible_p (type, decl, TREE_VALUE (t), binfo)) | |
940 | return 1; | |
941 | ||
942 | if (TREE_CODE (scope) == FUNCTION_DECL | |
943 | || DECL_FUNCTION_TEMPLATE_P (scope)) | |
944 | { | |
945 | /* Perhaps this SCOPE is a member of a class which is a | |
946 | friend. */ | |
947 | if (friend_accessible_p (DECL_CLASS_CONTEXT (scope), type, | |
948 | decl, binfo)) | |
949 | return 1; | |
950 | ||
951 | /* Or an instantiation of something which is a friend. */ | |
952 | if (DECL_TEMPLATE_INFO (scope)) | |
953 | return friend_accessible_p (DECL_TI_TEMPLATE (scope), | |
954 | type, decl, binfo); | |
955 | } | |
956 | else if (CLASSTYPE_TEMPLATE_INFO (scope)) | |
957 | return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), | |
958 | type, decl, binfo); | |
959 | ||
960 | return 0; | |
961 | } | |
962 | ||
d6479fe7 MM |
963 | /* DECL is a declaration from a base class of TYPE, which was the |
964 | classs used to name DECL. Return non-zero if, in the current | |
965 | context, DECL is accessible. If TYPE is actually a BINFO node, | |
8084bf81 MM |
966 | then we can tell in what context the access is occurring by looking |
967 | at the most derived class along the path indicated by BINFO. */ | |
d6479fe7 MM |
968 | |
969 | int | |
970 | accessible_p (type, decl) | |
971 | tree type; | |
972 | tree decl; | |
973 | ||
974 | { | |
d6479fe7 MM |
975 | tree binfo; |
976 | tree t; | |
977 | ||
978 | /* Non-zero if it's OK to access DECL if it has protected | |
979 | accessibility in TYPE. */ | |
980 | int protected_ok = 0; | |
981 | ||
982 | /* If we're not checking access, everything is accessible. */ | |
983 | if (!flag_access_control) | |
984 | return 1; | |
985 | ||
986 | /* If this declaration is in a block or namespace scope, there's no | |
987 | access control. */ | |
988 | if (!TYPE_P (context_for_name_lookup (decl))) | |
989 | return 1; | |
990 | ||
991 | /* We don't do access control for types yet. */ | |
992 | if (TREE_CODE (decl) == TYPE_DECL) | |
993 | return 1; | |
994 | ||
995 | if (!TYPE_P (type)) | |
996 | { | |
997 | binfo = type; | |
998 | type = BINFO_TYPE (type); | |
8d08fdba | 999 | } |
d6479fe7 MM |
1000 | else |
1001 | binfo = TYPE_BINFO (type); | |
1002 | ||
1003 | /* [class.access.base] | |
1004 | ||
1005 | A member m is accessible when named in class N if | |
1006 | ||
1007 | --m as a member of N is public, or | |
8d08fdba | 1008 | |
d6479fe7 MM |
1009 | --m as a member of N is private, and the reference occurs in a |
1010 | member or friend of class N, or | |
8d08fdba | 1011 | |
d6479fe7 MM |
1012 | --m as a member of N is protected, and the reference occurs in a |
1013 | member or friend of class N, or in a member or friend of a | |
1014 | class P derived from N, where m as a member of P is private or | |
1015 | protected, or | |
1016 | ||
1017 | --there exists a base class B of N that is accessible at the point | |
1018 | of reference, and m is accessible when named in class B. | |
1019 | ||
1020 | We walk the base class hierarchy, checking these conditions. */ | |
1021 | ||
1022 | /* Figure out where the reference is occurring. Check to see if | |
1023 | DECL is private or protected in this scope, since that will | |
1024 | determine whether protected access in TYPE allowed. */ | |
6a629cac MM |
1025 | if (current_class_type) |
1026 | protected_ok | |
1027 | = protected_accessible_p (type, decl, current_class_type, | |
1028 | binfo); | |
8d08fdba | 1029 | |
6a629cac MM |
1030 | /* Now, loop through the classes of which we are a friend. */ |
1031 | if (!protected_ok) | |
1032 | protected_ok = friend_accessible_p (current_scope (), | |
1033 | type, decl, binfo); | |
8d08fdba | 1034 | |
d6479fe7 MM |
1035 | /* Standardize on the same that will access_in_type will use. We |
1036 | don't need to know what path was chosen from this point onwards. */ | |
1037 | binfo = TYPE_BINFO (type); | |
1038 | ||
1039 | /* Compute the accessibility of DECL in the class hierarchy | |
1040 | dominated by type. */ | |
1041 | access_in_type (type, decl); | |
1042 | /* Walk the hierarchy again, looking for a base class that allows | |
1043 | access. */ | |
1044 | t = dfs_walk (binfo, dfs_accessible_p, | |
1045 | dfs_accessible_queue_p, | |
1046 | protected_ok ? &protected_ok : 0); | |
6cbd257e MM |
1047 | /* Clear any mark bits. Note that we have to walk the whole tree |
1048 | here, since we have aborted the previous walk from some point | |
1049 | deep in the tree. */ | |
1050 | dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0); | |
1051 | assert_canonical_unmarked (binfo); | |
d6479fe7 MM |
1052 | |
1053 | return t != NULL_TREE; | |
8d08fdba MS |
1054 | } |
1055 | ||
1056 | /* Routine to see if the sub-object denoted by the binfo PARENT can be | |
1057 | found as a base class and sub-object of the object denoted by | |
1058 | BINFO. This routine relies upon binfos not being shared, except | |
1059 | for binfos for virtual bases. */ | |
e92cc029 | 1060 | |
8d08fdba MS |
1061 | static int |
1062 | is_subobject_of_p (parent, binfo) | |
1063 | tree parent, binfo; | |
1064 | { | |
1065 | tree binfos = BINFO_BASETYPES (binfo); | |
1066 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1067 | ||
d6479fe7 MM |
1068 | if (TREE_VIA_VIRTUAL (parent)) |
1069 | parent = TYPE_BINFO (TREE_TYPE (parent)); | |
1070 | if (TREE_VIA_VIRTUAL (binfo)) | |
1071 | binfo = TYPE_BINFO (TREE_TYPE (binfo)); | |
1072 | ||
8d08fdba MS |
1073 | if (parent == binfo) |
1074 | return 1; | |
1075 | ||
1076 | /* Process and/or queue base types. */ | |
1077 | for (i = 0; i < n_baselinks; i++) | |
1078 | { | |
d6479fe7 | 1079 | tree base_binfo = canonical_binfo (TREE_VEC_ELT (binfos, i)); |
8d08fdba MS |
1080 | if (is_subobject_of_p (parent, base_binfo)) |
1081 | return 1; | |
1082 | } | |
1083 | return 0; | |
1084 | } | |
1085 | ||
1086 | /* See if a one FIELD_DECL hides another. This routine is meant to | |
1087 | correspond to ANSI working paper Sept 17, 1992 10p4. The two | |
1088 | binfos given are the binfos corresponding to the particular places | |
1089 | the FIELD_DECLs are found. This routine relies upon binfos not | |
e92cc029 MS |
1090 | being shared, except for virtual bases. */ |
1091 | ||
8d08fdba MS |
1092 | static int |
1093 | hides (hider_binfo, hidee_binfo) | |
1094 | tree hider_binfo, hidee_binfo; | |
1095 | { | |
1096 | /* hider hides hidee, if hider has hidee as a base class and | |
1097 | the instance of hidee is a sub-object of hider. The first | |
1098 | part is always true is the second part is true. | |
1099 | ||
1100 | When hider and hidee are the same (two ways to get to the exact | |
e92cc029 | 1101 | same member) we consider either one as hiding the other. */ |
8d08fdba MS |
1102 | return is_subobject_of_p (hidee_binfo, hider_binfo); |
1103 | } | |
1104 | ||
1105 | /* Very similar to lookup_fnfields_1 but it ensures that at least one | |
1106 | function was declared inside the class given by TYPE. It really should | |
1107 | only return functions that match the given TYPE. */ | |
e92cc029 | 1108 | |
8d08fdba MS |
1109 | static int |
1110 | lookup_fnfields_here (type, name) | |
1111 | tree type, name; | |
1112 | { | |
e92cc029 | 1113 | int idx = lookup_fnfields_1 (type, name); |
8d08fdba MS |
1114 | tree fndecls; |
1115 | ||
fc378698 | 1116 | /* ctors and dtors are always only in the right class. */ |
e92cc029 MS |
1117 | if (idx <= 1) |
1118 | return idx; | |
1119 | fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); | |
8d08fdba MS |
1120 | while (fndecls) |
1121 | { | |
2c73f9f5 | 1122 | if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls))) |
8d08fdba | 1123 | == TYPE_MAIN_VARIANT (type)) |
e92cc029 | 1124 | return idx; |
2c73f9f5 | 1125 | fndecls = OVL_CHAIN (fndecls); |
8d08fdba MS |
1126 | } |
1127 | return -1; | |
1128 | } | |
1129 | ||
7d4bdeed | 1130 | struct lookup_field_info { |
d6479fe7 MM |
1131 | /* The type in which we're looking. */ |
1132 | tree type; | |
7d4bdeed MM |
1133 | /* The name of the field for which we're looking. */ |
1134 | tree name; | |
1135 | /* If non-NULL, the current result of the lookup. */ | |
1136 | tree rval; | |
1137 | /* The path to RVAL. */ | |
1138 | tree rval_binfo; | |
d6479fe7 MM |
1139 | /* If non-NULL, the lookup was ambiguous, and this is a list of the |
1140 | candidates. */ | |
7d4bdeed | 1141 | tree ambiguous; |
7d4bdeed MM |
1142 | /* If non-zero, we are looking for types, not data members. */ |
1143 | int want_type; | |
d6479fe7 MM |
1144 | /* If non-zero, RVAL was found by looking through a dependent base. */ |
1145 | int from_dep_base_p; | |
7d4bdeed | 1146 | /* If something went wrong, a message indicating what. */ |
d8e178a0 | 1147 | const char *errstr; |
7d4bdeed MM |
1148 | }; |
1149 | ||
1150 | /* Returns non-zero if BINFO is not hidden by the value found by the | |
1151 | lookup so far. If BINFO is hidden, then there's no need to look in | |
1152 | it. DATA is really a struct lookup_field_info. Called from | |
1153 | lookup_field via breadth_first_search. */ | |
1154 | ||
d6479fe7 | 1155 | static tree |
7d4bdeed MM |
1156 | lookup_field_queue_p (binfo, data) |
1157 | tree binfo; | |
1158 | void *data; | |
1159 | { | |
1160 | struct lookup_field_info *lfi = (struct lookup_field_info *) data; | |
d6479fe7 MM |
1161 | |
1162 | /* Don't look for constructors or destructors in base classes. */ | |
1163 | if (lfi->name == ctor_identifier || lfi->name == dtor_identifier) | |
1164 | return NULL_TREE; | |
1165 | ||
1166 | /* If this base class is hidden by the best-known value so far, we | |
1167 | don't need to look. */ | |
1168 | if (!lfi->from_dep_base_p && lfi->rval_binfo | |
1169 | && hides (lfi->rval_binfo, binfo)) | |
1170 | return NULL_TREE; | |
1171 | ||
1172 | if (TREE_VIA_VIRTUAL (binfo)) | |
1173 | return binfo_member (BINFO_TYPE (binfo), | |
1174 | CLASSTYPE_VBASECLASSES (lfi->type)); | |
1175 | else | |
1176 | return binfo; | |
7d4bdeed MM |
1177 | } |
1178 | ||
8f032717 MM |
1179 | /* Within the scope of a template class, you can refer to the |
1180 | particular to the current specialization with the name of the | |
1181 | template itself. For example: | |
1182 | ||
1183 | template <typename T> struct S { S* sp; } | |
1184 | ||
1185 | Returns non-zero if DECL is such a declaration in a class TYPE. */ | |
1186 | ||
1187 | static int | |
1188 | template_self_reference_p (type, decl) | |
1189 | tree type; | |
1190 | tree decl; | |
1191 | { | |
1192 | return (CLASSTYPE_USE_TEMPLATE (type) | |
3fc5037b | 1193 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) |
8f032717 MM |
1194 | && TREE_CODE (decl) == TYPE_DECL |
1195 | && DECL_ARTIFICIAL (decl) | |
1196 | && DECL_NAME (decl) == constructor_name (type)); | |
1197 | } | |
1198 | ||
7d4bdeed MM |
1199 | /* DATA is really a struct lookup_field_info. Look for a field with |
1200 | the name indicated there in BINFO. If this function returns a | |
1201 | non-NULL value it is the result of the lookup. Called from | |
1202 | lookup_field via breadth_first_search. */ | |
1203 | ||
1204 | static tree | |
1205 | lookup_field_r (binfo, data) | |
1206 | tree binfo; | |
1207 | void *data; | |
1208 | { | |
1209 | struct lookup_field_info *lfi = (struct lookup_field_info *) data; | |
1210 | tree type = BINFO_TYPE (binfo); | |
4bb0968f | 1211 | tree nval = NULL_TREE; |
d6479fe7 | 1212 | int from_dep_base_p; |
7d4bdeed | 1213 | |
d6479fe7 MM |
1214 | /* First, look for a function. There can't be a function and a data |
1215 | member with the same name, and if there's a function and a type | |
1216 | with the same name, the type is hidden by the function. */ | |
4bb0968f MM |
1217 | if (!lfi->want_type) |
1218 | { | |
1219 | int idx = lookup_fnfields_here (type, lfi->name); | |
1220 | if (idx >= 0) | |
1221 | nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); | |
1222 | } | |
1223 | ||
1224 | if (!nval) | |
d6479fe7 MM |
1225 | /* Look for a data member or type. */ |
1226 | nval = lookup_field_1 (type, lfi->name); | |
1227 | ||
1228 | /* If there is no declaration with the indicated name in this type, | |
1229 | then there's nothing to do. */ | |
7d4bdeed | 1230 | if (!nval) |
d6479fe7 | 1231 | return NULL_TREE; |
7d4bdeed | 1232 | |
4bb0968f MM |
1233 | /* If we're looking up a type (as with an elaborated type specifier) |
1234 | we ignore all non-types we find. */ | |
1235 | if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL) | |
1236 | { | |
1237 | nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type)); | |
1238 | if (nval) | |
1239 | nval = TYPE_MAIN_DECL (TREE_VALUE (nval)); | |
1240 | else | |
1241 | return NULL_TREE; | |
1242 | } | |
1243 | ||
8f032717 MM |
1244 | /* You must name a template base class with a template-id. */ |
1245 | if (!same_type_p (type, lfi->type) | |
1246 | && template_self_reference_p (type, nval)) | |
1247 | return NULL_TREE; | |
1248 | ||
d6479fe7 MM |
1249 | from_dep_base_p = dependent_base_p (binfo); |
1250 | if (lfi->from_dep_base_p && !from_dep_base_p) | |
1251 | { | |
1252 | /* If the new declaration is not found via a dependent base, and | |
1253 | the old one was, then we must prefer the new one. We weren't | |
1254 | really supposed to be able to find the old one, so we don't | |
1255 | want to be affected by a specialization. Consider: | |
1256 | ||
1257 | struct B { typedef int I; }; | |
1258 | template <typename T> struct D1 : virtual public B {}; | |
1259 | template <typename T> struct D : | |
1260 | public D1, virtual pubic B { I i; }; | |
1261 | ||
1262 | The `I' in `D<T>' is unambigousuly `B::I', regardless of how | |
1263 | D1 is specialized. */ | |
1264 | lfi->from_dep_base_p = 0; | |
1265 | lfi->rval = NULL_TREE; | |
1266 | lfi->rval_binfo = NULL_TREE; | |
1267 | lfi->ambiguous = NULL_TREE; | |
1268 | lfi->errstr = 0; | |
1269 | } | |
1270 | else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p) | |
1271 | /* Similarly, if the old declaration was not found via a dependent | |
1272 | base, and the new one is, ignore the new one. */ | |
7d4bdeed MM |
1273 | return NULL_TREE; |
1274 | ||
1275 | /* If the lookup already found a match, and the new value doesn't | |
1276 | hide the old one, we might have an ambiguity. */ | |
1277 | if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo)) | |
1278 | { | |
d6479fe7 | 1279 | if (nval == lfi->rval && SHARED_MEMBER_P (nval)) |
7d4bdeed MM |
1280 | /* The two things are really the same. */ |
1281 | ; | |
1282 | else if (hides (lfi->rval_binfo, binfo)) | |
1283 | /* The previous value hides the new one. */ | |
1284 | ; | |
1285 | else | |
1286 | { | |
1287 | /* We have a real ambiguity. We keep a chain of all the | |
1288 | candidates. */ | |
1289 | if (!lfi->ambiguous && lfi->rval) | |
1290 | /* This is the first time we noticed an ambiguity. Add | |
1291 | what we previously thought was a reasonable candidate | |
1292 | to the list. */ | |
1293 | lfi->ambiguous = scratch_tree_cons (NULL_TREE, lfi->rval, | |
1294 | NULL_TREE); | |
7d4bdeed MM |
1295 | /* Add the new value. */ |
1296 | lfi->ambiguous = scratch_tree_cons (NULL_TREE, nval, | |
1297 | lfi->ambiguous); | |
1298 | lfi->errstr = "request for member `%D' is ambiguous"; | |
1299 | } | |
1300 | } | |
1301 | else | |
1302 | { | |
4bb0968f MM |
1303 | /* If the thing we're looking for is a virtual base class, then |
1304 | we know we've got what we want at this point; there's no way | |
1305 | to get an ambiguity. */ | |
1306 | if (VBASE_NAME_P (lfi->name)) | |
7d4bdeed | 1307 | { |
4bb0968f MM |
1308 | lfi->rval = nval; |
1309 | return nval; | |
7d4bdeed MM |
1310 | } |
1311 | ||
4bb0968f MM |
1312 | if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL |
1313 | /* We need to return a member template class so we can | |
1314 | define partial specializations. Is there a better | |
1315 | way? */ | |
1316 | && !DECL_CLASS_TEMPLATE_P (nval)) | |
1317 | /* The thing we're looking for isn't a type, so the implicit | |
1318 | typename extension doesn't apply, so we just pretend we | |
1319 | didn't find anything. */ | |
1320 | return NULL_TREE; | |
7d4bdeed | 1321 | |
d6479fe7 MM |
1322 | lfi->rval = nval; |
1323 | lfi->from_dep_base_p = from_dep_base_p; | |
7d4bdeed MM |
1324 | lfi->rval_binfo = binfo; |
1325 | } | |
1326 | ||
d6479fe7 | 1327 | return NULL_TREE; |
7d4bdeed MM |
1328 | } |
1329 | ||
d6479fe7 | 1330 | /* Look for a memer named NAME in an inheritance lattice dominated by |
8f032717 MM |
1331 | XBASETYPE. PROTECT is 0 or two, we do not check access. If it is |
1332 | 1, we enforce accessibility. If PROTECT is zero, then, for an | |
1333 | ambiguous lookup, we return NULL. If PROTECT is 1, we issue an | |
1334 | error message. If PROTECT is two 2, we return a TREE_LIST whose | |
1335 | TREE_PURPOSE is error_mark_node and whose TREE_VALUE is the list of | |
1336 | ambiguous candidates. | |
8d08fdba | 1337 | |
8f032717 MM |
1338 | WANT_TYPE is 1 when we should only return TYPE_DECLs, if no |
1339 | TYPE_DECL can be found return NULL_TREE. */ | |
e92cc029 | 1340 | |
8d08fdba | 1341 | tree |
d6479fe7 | 1342 | lookup_member (xbasetype, name, protect, want_type) |
8d08fdba MS |
1343 | register tree xbasetype, name; |
1344 | int protect, want_type; | |
1345 | { | |
7d4bdeed MM |
1346 | tree rval, rval_binfo = NULL_TREE; |
1347 | tree type = NULL_TREE, basetype_path = NULL_TREE; | |
1348 | struct lookup_field_info lfi; | |
8d08fdba MS |
1349 | |
1350 | /* rval_binfo is the binfo associated with the found member, note, | |
1351 | this can be set with useful information, even when rval is not | |
1352 | set, because it must deal with ALL members, not just non-function | |
1353 | members. It is used for ambiguity checking and the hidden | |
1354 | checks. Whereas rval is only set if a proper (not hidden) | |
1355 | non-function member is found. */ | |
1356 | ||
d8e178a0 | 1357 | const char *errstr = 0; |
8d08fdba | 1358 | |
de22184b MS |
1359 | if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype) |
1360 | && IDENTIFIER_CLASS_VALUE (name)) | |
1361 | { | |
1362 | tree field = IDENTIFIER_CLASS_VALUE (name); | |
1363 | if (TREE_CODE (field) != FUNCTION_DECL | |
1364 | && ! (want_type && TREE_CODE (field) != TYPE_DECL)) | |
1365 | return field; | |
1366 | } | |
1367 | ||
8d08fdba MS |
1368 | if (TREE_CODE (xbasetype) == TREE_VEC) |
1369 | { | |
8d08fdba | 1370 | type = BINFO_TYPE (xbasetype); |
39211cd5 | 1371 | basetype_path = xbasetype; |
8d08fdba MS |
1372 | } |
1373 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype))) | |
39211cd5 | 1374 | { |
238109cd | 1375 | type = xbasetype; |
5566b478 | 1376 | basetype_path = TYPE_BINFO (type); |
dfbcd65a JM |
1377 | my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE, |
1378 | 980827); | |
39211cd5 | 1379 | } |
238109cd JM |
1380 | else |
1381 | my_friendly_abort (97); | |
1382 | ||
1383 | complete_type (type); | |
8d08fdba | 1384 | |
8d08fdba MS |
1385 | #ifdef GATHER_STATISTICS |
1386 | n_calls_lookup_field++; | |
fc378698 | 1387 | #endif /* GATHER_STATISTICS */ |
8d08fdba | 1388 | |
d8e178a0 | 1389 | bzero ((PTR) &lfi, sizeof (lfi)); |
d6479fe7 | 1390 | lfi.type = type; |
7d4bdeed | 1391 | lfi.name = name; |
7d4bdeed | 1392 | lfi.want_type = want_type; |
d6479fe7 | 1393 | bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi); |
7d4bdeed MM |
1394 | rval = lfi.rval; |
1395 | rval_binfo = lfi.rval_binfo; | |
1396 | if (rval_binfo) | |
1397 | type = BINFO_TYPE (rval_binfo); | |
1398 | errstr = lfi.errstr; | |
1399 | ||
1400 | /* If we are not interested in ambiguities, don't report them; | |
1401 | just return NULL_TREE. */ | |
1402 | if (!protect && lfi.ambiguous) | |
1403 | return NULL_TREE; | |
d6479fe7 | 1404 | |
8f032717 MM |
1405 | if (protect == 2) |
1406 | { | |
1407 | if (lfi.ambiguous) | |
1408 | { | |
280f9385 MM |
1409 | /* An ERROR_MARK for the TREE_TYPE tells hack_identifier |
1410 | that the lookup is ambiguous. */ | |
1411 | TREE_TYPE (lfi.ambiguous) = error_mark_node; | |
8f032717 MM |
1412 | return scratch_tree_cons (error_mark_node, |
1413 | lfi.ambiguous, | |
1414 | NULL_TREE); | |
1415 | } | |
1416 | else | |
1417 | protect = 0; | |
1418 | } | |
1419 | ||
d6479fe7 MM |
1420 | /* [class.access] |
1421 | ||
1422 | In the case of overloaded function names, access control is | |
1423 | applied to the function selected by overloaded resolution. */ | |
1424 | if (rval && protect && !is_overloaded_fn (rval) | |
1425 | && !IS_SIGNATURE_POINTER (DECL_REAL_CONTEXT (rval)) | |
1426 | && !IS_SIGNATURE_REFERENCE (DECL_REAL_CONTEXT (rval)) | |
1427 | && !enforce_access (xbasetype, rval)) | |
1428 | return error_mark_node; | |
9e9ff709 | 1429 | |
8251199e | 1430 | if (errstr && protect) |
8d08fdba | 1431 | { |
8251199e | 1432 | cp_error (errstr, name, type); |
7d4bdeed MM |
1433 | if (lfi.ambiguous) |
1434 | print_candidates (lfi.ambiguous); | |
8d08fdba MS |
1435 | rval = error_mark_node; |
1436 | } | |
b3709d9b | 1437 | |
d6479fe7 MM |
1438 | /* If the thing we found was found via the implicit typename |
1439 | extension, build the typename type. */ | |
1440 | if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval)) | |
1441 | rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path), | |
1442 | name, name, | |
1443 | TREE_TYPE (rval))); | |
1444 | ||
4bb0968f MM |
1445 | if (rval && is_overloaded_fn (rval)) |
1446 | { | |
1447 | rval = scratch_tree_cons (basetype_path, rval, NULL_TREE); | |
1448 | SET_BASELINK_P (rval); | |
1449 | } | |
d6479fe7 MM |
1450 | |
1451 | return rval; | |
1452 | } | |
1453 | ||
1454 | /* Like lookup_member, except that if we find a function member we | |
1455 | return NULL_TREE. */ | |
1456 | ||
1457 | tree | |
1458 | lookup_field (xbasetype, name, protect, want_type) | |
1459 | register tree xbasetype, name; | |
1460 | int protect, want_type; | |
1461 | { | |
1462 | tree rval = lookup_member (xbasetype, name, protect, want_type); | |
1463 | ||
1464 | /* Ignore functions. */ | |
1465 | if (rval && TREE_CODE (rval) == TREE_LIST) | |
1466 | return NULL_TREE; | |
1467 | ||
1468 | return rval; | |
1469 | } | |
1470 | ||
1471 | /* Like lookup_member, except that if we find a non-function member we | |
1472 | return NULL_TREE. */ | |
1473 | ||
1474 | tree | |
1475 | lookup_fnfields (xbasetype, name, protect) | |
1476 | register tree xbasetype, name; | |
1477 | int protect; | |
1478 | { | |
1479 | tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0); | |
1480 | ||
1481 | /* Ignore non-functions. */ | |
1482 | if (rval && TREE_CODE (rval) != TREE_LIST) | |
1483 | return NULL_TREE; | |
1484 | ||
8d08fdba MS |
1485 | return rval; |
1486 | } | |
1487 | ||
8d08fdba MS |
1488 | /* TYPE is a class type. Return the index of the fields within |
1489 | the method vector with name NAME, or -1 is no such field exists. */ | |
e92cc029 | 1490 | |
03017874 | 1491 | int |
8d08fdba MS |
1492 | lookup_fnfields_1 (type, name) |
1493 | tree type, name; | |
1494 | { | |
7ddedda4 MM |
1495 | register tree method_vec |
1496 | = CLASS_TYPE_P (type) ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE; | |
8d08fdba MS |
1497 | |
1498 | if (method_vec != 0) | |
1499 | { | |
1500 | register tree *methods = &TREE_VEC_ELT (method_vec, 0); | |
1501 | register tree *end = TREE_VEC_END (method_vec); | |
1502 | ||
1503 | #ifdef GATHER_STATISTICS | |
1504 | n_calls_lookup_fnfields_1++; | |
fc378698 MS |
1505 | #endif /* GATHER_STATISTICS */ |
1506 | ||
1507 | /* Constructors are first... */ | |
1508 | if (*methods && name == ctor_identifier) | |
8d08fdba MS |
1509 | return 0; |
1510 | ||
fc378698 MS |
1511 | /* and destructors are second. */ |
1512 | if (*++methods && name == dtor_identifier) | |
1513 | return 1; | |
1514 | ||
61a127b3 | 1515 | while (++methods != end && *methods) |
8d08fdba MS |
1516 | { |
1517 | #ifdef GATHER_STATISTICS | |
1518 | n_outer_fields_searched++; | |
fc378698 | 1519 | #endif /* GATHER_STATISTICS */ |
2c73f9f5 | 1520 | if (DECL_NAME (OVL_CURRENT (*methods)) == name) |
8d08fdba MS |
1521 | break; |
1522 | } | |
98c1c668 JM |
1523 | |
1524 | /* If we didn't find it, it might have been a template | |
1525 | conversion operator. (Note that we don't look for this case | |
1526 | above so that we will always find specializations first.) */ | |
61a127b3 | 1527 | if ((methods == end || !*methods) |
98c1c668 JM |
1528 | && IDENTIFIER_TYPENAME_P (name)) |
1529 | { | |
1530 | methods = &TREE_VEC_ELT (method_vec, 0) + 1; | |
1531 | ||
61a127b3 | 1532 | while (++methods != end && *methods) |
98c1c668 | 1533 | { |
61a127b3 MM |
1534 | tree method_name = DECL_NAME (OVL_CURRENT (*methods)); |
1535 | ||
1536 | if (!IDENTIFIER_TYPENAME_P (method_name)) | |
1537 | { | |
1538 | /* Since all conversion operators come first, we know | |
1539 | there is no such operator. */ | |
1540 | methods = end; | |
d6479fe7 | 1541 | break; |
8d08fdba | 1542 | } |
d6479fe7 MM |
1543 | else if (TREE_CODE (OVL_CURRENT (*methods)) == TEMPLATE_DECL) |
1544 | break; | |
8d08fdba MS |
1545 | } |
1546 | } | |
8d08fdba | 1547 | |
d6479fe7 MM |
1548 | if (methods != end && *methods) |
1549 | return methods - &TREE_VEC_ELT (method_vec, 0); | |
8d08fdba MS |
1550 | } |
1551 | ||
d6479fe7 | 1552 | return -1; |
d23a1bb1 | 1553 | } |
8d08fdba | 1554 | \f |
d6479fe7 MM |
1555 | /* Walk the class hierarchy dominated by TYPE. FN is called for each |
1556 | type in the hierarchy, in a breadth-first preorder traversal. . | |
1557 | If it ever returns a non-NULL value, that value is immediately | |
1558 | returned and the walk is terminated. At each node FN, is passed a | |
1559 | BINFO indicating the path from the curently visited base-class to | |
1560 | TYPE. The TREE_CHAINs of the BINFOs may be used for scratch space; | |
1561 | they are otherwise unused. Before each base-class is walked QFN is | |
1562 | called. If the value returned is non-zero, the base-class is | |
1563 | walked; otherwise it is not. If QFN is NULL, it is treated as a | |
1564 | function which always returns 1. Both FN and QFN are passed the | |
1565 | DATA whenever they are called. */ | |
8d08fdba | 1566 | |
72c4a2a6 | 1567 | static tree |
d6479fe7 | 1568 | bfs_walk (binfo, fn, qfn, data) |
8d08fdba | 1569 | tree binfo; |
d6479fe7 MM |
1570 | tree (*fn) PROTO((tree, void *)); |
1571 | tree (*qfn) PROTO((tree, void *)); | |
7d4bdeed | 1572 | void *data; |
8d08fdba | 1573 | { |
d6479fe7 MM |
1574 | size_t head; |
1575 | size_t tail; | |
72c4a2a6 | 1576 | tree rval = NULL_TREE; |
d6479fe7 MM |
1577 | /* An array of the base classes of BINFO. These will be built up in |
1578 | breadth-first order, except where QFN prunes the search. */ | |
1579 | varray_type bfs_bases; | |
8d08fdba | 1580 | |
d6479fe7 MM |
1581 | /* Start with enough room for ten base classes. That will be enough |
1582 | for most hierarchies. */ | |
1583 | VARRAY_TREE_INIT (bfs_bases, 10, "search_stack"); | |
8d08fdba | 1584 | |
d6479fe7 MM |
1585 | /* Put the first type into the stack. */ |
1586 | VARRAY_TREE (bfs_bases, 0) = binfo; | |
1587 | tail = 1; | |
72c4a2a6 | 1588 | |
d6479fe7 | 1589 | for (head = 0; head < tail; ++head) |
8d08fdba | 1590 | { |
8d08fdba | 1591 | int i; |
d6479fe7 MM |
1592 | int n_baselinks; |
1593 | tree binfos; | |
8d08fdba | 1594 | |
7d4bdeed | 1595 | /* Pull the next type out of the queue. */ |
d6479fe7 | 1596 | binfo = VARRAY_TREE (bfs_bases, head); |
7d4bdeed MM |
1597 | |
1598 | /* If this is the one we're looking for, we're done. */ | |
d6479fe7 | 1599 | rval = (*fn) (binfo, data); |
7d4bdeed MM |
1600 | if (rval) |
1601 | break; | |
1602 | ||
1603 | /* Queue up the base types. */ | |
1604 | binfos = BINFO_BASETYPES (binfo); | |
1605 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0; | |
8d08fdba MS |
1606 | for (i = 0; i < n_baselinks; i++) |
1607 | { | |
1608 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1609 | ||
d6479fe7 MM |
1610 | if (qfn) |
1611 | base_binfo = (*qfn) (base_binfo, data); | |
7d4bdeed | 1612 | |
d6479fe7 | 1613 | if (base_binfo) |
8d08fdba | 1614 | { |
d6479fe7 MM |
1615 | if (tail == VARRAY_SIZE (bfs_bases)) |
1616 | VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases)); | |
1617 | VARRAY_TREE (bfs_bases, tail) = base_binfo; | |
72c4a2a6 | 1618 | ++tail; |
8d08fdba MS |
1619 | } |
1620 | } | |
7d4bdeed | 1621 | } |
8d08fdba | 1622 | |
d6479fe7 MM |
1623 | /* Clean up. */ |
1624 | VARRAY_FREE (bfs_bases); | |
1625 | ||
1626 | return rval; | |
1627 | } | |
1628 | ||
1629 | /* Exactly like bfs_walk, except that a depth-first traversal is | |
1630 | performed, and PREFN is called in preorder, while POSTFN is called | |
1631 | in postorder. */ | |
1632 | ||
1633 | static tree | |
1634 | dfs_walk_real (binfo, prefn, postfn, qfn, data) | |
1635 | tree binfo; | |
1636 | tree (*prefn) PROTO((tree, void *)); | |
1637 | tree (*postfn) PROTO((tree, void *)); | |
1638 | tree (*qfn) PROTO((tree, void *)); | |
1639 | void *data; | |
1640 | { | |
1641 | int i; | |
1642 | int n_baselinks; | |
1643 | tree binfos; | |
1644 | tree rval = NULL_TREE; | |
1645 | ||
1646 | /* Call the pre-order walking function. */ | |
1647 | if (prefn) | |
7d4bdeed | 1648 | { |
d6479fe7 MM |
1649 | rval = (*prefn) (binfo, data); |
1650 | if (rval) | |
1651 | return rval; | |
8d08fdba | 1652 | } |
8d08fdba | 1653 | |
d6479fe7 MM |
1654 | /* Process the basetypes. */ |
1655 | binfos = BINFO_BASETYPES (binfo); | |
1656 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0; | |
1657 | for (i = 0; i < n_baselinks; i++) | |
1658 | { | |
1659 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1660 | ||
1661 | if (qfn) | |
1662 | base_binfo = (*qfn) (base_binfo, data); | |
1663 | ||
1664 | if (base_binfo) | |
1665 | { | |
1666 | rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data); | |
1667 | if (rval) | |
1668 | return rval; | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | /* Call the post-order walking function. */ | |
1673 | if (postfn) | |
1674 | rval = (*postfn) (binfo, data); | |
1675 | ||
8d08fdba MS |
1676 | return rval; |
1677 | } | |
1678 | ||
d6479fe7 MM |
1679 | /* Exactly like bfs_walk, except that a depth-first post-order traversal is |
1680 | performed. */ | |
1681 | ||
1682 | tree | |
1683 | dfs_walk (binfo, fn, qfn, data) | |
1684 | tree binfo; | |
1685 | tree (*fn) PROTO((tree, void *)); | |
1686 | tree (*qfn) PROTO((tree, void *)); | |
1687 | void *data; | |
1688 | { | |
1689 | return dfs_walk_real (binfo, 0, fn, qfn, data); | |
1690 | } | |
1691 | ||
1692 | struct gvnt_info | |
1693 | { | |
1694 | /* The name of the function we are looking for. */ | |
1695 | tree name; | |
1696 | /* The overloaded functions we have found. */ | |
1697 | tree fields; | |
1698 | }; | |
1699 | ||
1700 | /* Called from get_virtuals_named_this via bfs_walk. */ | |
1701 | ||
1702 | static tree | |
1703 | get_virtuals_named_this_r (binfo, data) | |
1704 | tree binfo; | |
1705 | void *data; | |
1706 | { | |
1707 | struct gvnt_info *gvnti = (struct gvnt_info *) data; | |
1708 | tree type = BINFO_TYPE (binfo); | |
1709 | int idx; | |
1710 | ||
1711 | idx = lookup_fnfields_here (BINFO_TYPE (binfo), gvnti->name); | |
1712 | if (idx >= 0) | |
1713 | gvnti->fields | |
1714 | = scratch_tree_cons (binfo, | |
1715 | TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), | |
1716 | idx), | |
1717 | gvnti->fields); | |
1718 | ||
1719 | return NULL_TREE; | |
1720 | } | |
8d08fdba | 1721 | |
d6479fe7 MM |
1722 | /* Return the virtual functions with the indicated NAME in the type |
1723 | indicated by BINFO. The result is a TREE_LIST whose TREE_PURPOSE | |
1724 | indicates the base class from which the TREE_VALUE (an OVERLOAD or | |
1725 | just a FUNCTION_DECL) originated. */ | |
8d08fdba MS |
1726 | |
1727 | static tree | |
d6479fe7 | 1728 | get_virtuals_named_this (binfo, name) |
8d08fdba | 1729 | tree binfo; |
d6479fe7 | 1730 | tree name; |
8d08fdba | 1731 | { |
d6479fe7 | 1732 | struct gvnt_info gvnti; |
8d08fdba MS |
1733 | tree fields; |
1734 | ||
d6479fe7 MM |
1735 | gvnti.name = name; |
1736 | gvnti.fields = NULL_TREE; | |
8d08fdba | 1737 | |
d6479fe7 | 1738 | bfs_walk (binfo, get_virtuals_named_this_r, 0, &gvnti); |
8d08fdba MS |
1739 | |
1740 | /* Get to the function decls, and return the first virtual function | |
1741 | with this name, if there is one. */ | |
d6479fe7 | 1742 | for (fields = gvnti.fields; fields; fields = next_baselink (fields)) |
8d08fdba MS |
1743 | { |
1744 | tree fndecl; | |
1745 | ||
2c73f9f5 ML |
1746 | for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl)) |
1747 | if (DECL_VINDEX (OVL_CURRENT (fndecl))) | |
8d08fdba | 1748 | return fields; |
8d08fdba MS |
1749 | } |
1750 | return NULL_TREE; | |
1751 | } | |
1752 | ||
fc378698 | 1753 | static tree |
7d4bdeed | 1754 | get_virtual_destructor (binfo, data) |
8d08fdba | 1755 | tree binfo; |
d6479fe7 | 1756 | void *data ATTRIBUTE_UNUSED; |
8d08fdba MS |
1757 | { |
1758 | tree type = BINFO_TYPE (binfo); | |
8d08fdba | 1759 | if (TYPE_HAS_DESTRUCTOR (type) |
fc378698 MS |
1760 | && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1))) |
1761 | return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1); | |
8d08fdba MS |
1762 | return 0; |
1763 | } | |
1764 | ||
d6479fe7 | 1765 | static tree |
7d4bdeed | 1766 | tree_has_any_destructor_p (binfo, data) |
8d08fdba | 1767 | tree binfo; |
d6479fe7 | 1768 | void *data ATTRIBUTE_UNUSED; |
8d08fdba MS |
1769 | { |
1770 | tree type = BINFO_TYPE (binfo); | |
d6479fe7 | 1771 | return TYPE_NEEDS_DESTRUCTOR (type) ? binfo : NULL_TREE; |
8d08fdba MS |
1772 | } |
1773 | ||
cc600f33 JM |
1774 | /* Returns > 0 if a function with type DRETTYPE overriding a function |
1775 | with type BRETTYPE is covariant, as defined in [class.virtual]. | |
1776 | ||
1777 | Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime | |
1778 | adjustment), or -1 if pedantically invalid covariance. */ | |
1779 | ||
c6160f8f | 1780 | static int |
cc600f33 JM |
1781 | covariant_return_p (brettype, drettype) |
1782 | tree brettype, drettype; | |
1783 | { | |
1784 | tree binfo; | |
1785 | ||
1786 | if (TREE_CODE (brettype) == FUNCTION_DECL | |
1787 | || TREE_CODE (brettype) == THUNK_DECL) | |
1788 | { | |
1789 | brettype = TREE_TYPE (TREE_TYPE (brettype)); | |
1790 | drettype = TREE_TYPE (TREE_TYPE (drettype)); | |
1791 | } | |
1792 | else if (TREE_CODE (brettype) == METHOD_TYPE) | |
1793 | { | |
1794 | brettype = TREE_TYPE (brettype); | |
1795 | drettype = TREE_TYPE (drettype); | |
1796 | } | |
1797 | ||
3bfdc719 | 1798 | if (same_type_p (brettype, drettype)) |
cc600f33 JM |
1799 | return 0; |
1800 | ||
1801 | if (! (TREE_CODE (brettype) == TREE_CODE (drettype) | |
1802 | && (TREE_CODE (brettype) == POINTER_TYPE | |
1803 | || TREE_CODE (brettype) == REFERENCE_TYPE) | |
91063b51 | 1804 | && TYPE_QUALS (brettype) == TYPE_QUALS (drettype))) |
cc600f33 JM |
1805 | return 0; |
1806 | ||
1807 | if (! can_convert (brettype, drettype)) | |
1808 | return 0; | |
1809 | ||
1810 | brettype = TREE_TYPE (brettype); | |
1811 | drettype = TREE_TYPE (drettype); | |
1812 | ||
1813 | /* If not pedantic, allow any standard pointer conversion. */ | |
1814 | if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype)) | |
1815 | return -1; | |
1816 | ||
e76e4a68 JM |
1817 | binfo = get_binfo (brettype, drettype, 1); |
1818 | ||
1819 | /* If we get an error_mark_node from get_binfo, it already complained, | |
1820 | so let's just succeed. */ | |
1821 | if (binfo == error_mark_node) | |
1822 | return 1; | |
cc600f33 JM |
1823 | |
1824 | if (! BINFO_OFFSET_ZEROP (binfo) || TREE_VIA_VIRTUAL (binfo)) | |
1825 | return 2; | |
1826 | return 1; | |
1827 | } | |
1828 | ||
7177d104 MS |
1829 | /* Given a class type TYPE, and a function decl FNDECL, look for a |
1830 | virtual function in TYPE's hierarchy which FNDECL could match as a | |
1831 | virtual function. It doesn't matter which one we find. | |
8d08fdba MS |
1832 | |
1833 | DTORP is nonzero if we are looking for a destructor. Destructors | |
1834 | need special treatment because they do not match by name. */ | |
e92cc029 | 1835 | |
8d08fdba | 1836 | tree |
7177d104 | 1837 | get_matching_virtual (binfo, fndecl, dtorp) |
8d08fdba MS |
1838 | tree binfo, fndecl; |
1839 | int dtorp; | |
1840 | { | |
1841 | tree tmp = NULL_TREE; | |
cc600f33 | 1842 | int i; |
8d08fdba | 1843 | |
5e795528 MM |
1844 | if (TREE_CODE (fndecl) == TEMPLATE_DECL) |
1845 | /* In [temp.mem] we have: | |
1846 | ||
1847 | A specialization of a member function template does not | |
1848 | override a virtual function from a base class. */ | |
1849 | return NULL_TREE; | |
1850 | ||
8d08fdba MS |
1851 | /* Breadth first search routines start searching basetypes |
1852 | of TYPE, so we must perform first ply of search here. */ | |
1853 | if (dtorp) | |
d6479fe7 MM |
1854 | return bfs_walk (binfo, get_virtual_destructor, |
1855 | tree_has_any_destructor_p, 0); | |
8d08fdba MS |
1856 | else |
1857 | { | |
1858 | tree drettype, dtypes, btypes, instptr_type; | |
1859 | tree basetype = DECL_CLASS_CONTEXT (fndecl); | |
1860 | tree baselink, best = NULL_TREE; | |
1861 | tree name = DECL_ASSEMBLER_NAME (fndecl); | |
d6479fe7 | 1862 | tree declarator = DECL_NAME (fndecl); |
8d08fdba MS |
1863 | if (IDENTIFIER_VIRTUAL_P (declarator) == 0) |
1864 | return NULL_TREE; | |
1865 | ||
d6479fe7 | 1866 | baselink = get_virtuals_named_this (binfo, declarator); |
a292b002 MS |
1867 | if (baselink == NULL_TREE) |
1868 | return NULL_TREE; | |
1869 | ||
8d08fdba MS |
1870 | drettype = TREE_TYPE (TREE_TYPE (fndecl)); |
1871 | dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
1872 | if (DECL_STATIC_FUNCTION_P (fndecl)) | |
1873 | instptr_type = NULL_TREE; | |
1874 | else | |
1875 | instptr_type = TREE_TYPE (TREE_VALUE (dtypes)); | |
1876 | ||
a292b002 | 1877 | for (; baselink; baselink = next_baselink (baselink)) |
8d08fdba | 1878 | { |
2c73f9f5 ML |
1879 | tree tmps; |
1880 | for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps)) | |
8d08fdba | 1881 | { |
2c73f9f5 | 1882 | tmp = OVL_CURRENT (tmps); |
8d08fdba MS |
1883 | if (! DECL_VINDEX (tmp)) |
1884 | continue; | |
1885 | ||
1886 | btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp)); | |
1887 | if (instptr_type == NULL_TREE) | |
1888 | { | |
91063b51 | 1889 | if (compparms (TREE_CHAIN (btypes), dtypes)) |
8d08fdba MS |
1890 | /* Caller knows to give error in this case. */ |
1891 | return tmp; | |
1892 | return NULL_TREE; | |
1893 | } | |
1894 | ||
91063b51 MM |
1895 | if (/* The first parameter is the `this' parameter, |
1896 | which has POINTER_TYPE, and we can therefore | |
1897 | safely use TYPE_QUALS, rather than | |
1898 | CP_TYPE_QUALS. */ | |
1899 | (TYPE_QUALS (TREE_TYPE (TREE_VALUE (btypes))) | |
1900 | == TYPE_QUALS (instptr_type)) | |
1901 | && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes))) | |
8d08fdba | 1902 | { |
e1cd6e56 | 1903 | tree brettype = TREE_TYPE (TREE_TYPE (tmp)); |
3bfdc719 | 1904 | if (same_type_p (brettype, drettype)) |
e1cd6e56 | 1905 | /* OK */; |
cc600f33 | 1906 | else if ((i = covariant_return_p (brettype, drettype))) |
e1cd6e56 | 1907 | { |
cc600f33 JM |
1908 | if (i == 2) |
1909 | sorry ("adjusting pointers for covariant returns"); | |
1910 | ||
1911 | if (pedantic && i == -1) | |
e1cd6e56 | 1912 | { |
8251199e JM |
1913 | cp_pedwarn_at ("invalid covariant return type for `%#D' (must be pointer or reference to class)", fndecl); |
1914 | cp_pedwarn_at (" overriding `%#D'", tmp); | |
e1cd6e56 MS |
1915 | } |
1916 | } | |
1917 | else if (IS_AGGR_TYPE_2 (brettype, drettype) | |
3bfdc719 | 1918 | && same_or_base_type_p (brettype, drettype)) |
e1cd6e56 | 1919 | { |
8251199e JM |
1920 | error ("invalid covariant return type (must use pointer or reference)"); |
1921 | cp_error_at (" overriding `%#D'", tmp); | |
1922 | cp_error_at (" with `%#D'", fndecl); | |
e1cd6e56 MS |
1923 | } |
1924 | else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE) | |
8d08fdba | 1925 | { |
8251199e JM |
1926 | cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl); |
1927 | cp_error_at (" overriding definition as `%#D'", tmp); | |
8d08fdba MS |
1928 | SET_IDENTIFIER_ERROR_LOCUS (name, basetype); |
1929 | } | |
ed70c426 MM |
1930 | |
1931 | /* FNDECL overrides this function. We continue to | |
1932 | check all the other functions in order to catch | |
1933 | errors; it might be that in some other baseclass | |
1934 | a virtual function was declared with the same | |
1935 | parameter types, but a different return type. */ | |
1936 | best = tmp; | |
8d08fdba MS |
1937 | } |
1938 | } | |
8d08fdba | 1939 | } |
8d08fdba | 1940 | |
8d08fdba MS |
1941 | return best; |
1942 | } | |
1943 | } | |
1944 | ||
7177d104 MS |
1945 | /* Return the list of virtual functions which are abstract in type |
1946 | TYPE that come from non virtual base classes. See | |
1947 | expand_direct_vtbls_init for the style of search we do. */ | |
e92cc029 | 1948 | |
8926095f MS |
1949 | static tree |
1950 | get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals) | |
6b5fbb55 | 1951 | tree binfo; |
8926095f | 1952 | int do_self; |
6b5fbb55 | 1953 | tree abstract_virtuals; |
8d08fdba | 1954 | { |
8926095f MS |
1955 | tree binfos = BINFO_BASETYPES (binfo); |
1956 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
8d08fdba | 1957 | |
8926095f | 1958 | for (i = 0; i < n_baselinks; i++) |
8d08fdba | 1959 | { |
8926095f | 1960 | tree base_binfo = TREE_VEC_ELT (binfos, i); |
beb53fb8 JM |
1961 | int is_not_base_vtable |
1962 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
8926095f MS |
1963 | if (! TREE_VIA_VIRTUAL (base_binfo)) |
1964 | abstract_virtuals | |
1965 | = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable, | |
1966 | abstract_virtuals); | |
1967 | } | |
1968 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
1969 | if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
1970 | { | |
f30432d7 MS |
1971 | tree virtuals = BINFO_VIRTUALS (binfo); |
1972 | ||
aff08c18 | 1973 | skip_rtti_stuff (&virtuals, BINFO_TYPE (binfo)); |
8d08fdba | 1974 | |
f30432d7 | 1975 | while (virtuals) |
8d08fdba | 1976 | { |
f30432d7 | 1977 | tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)); |
8d08fdba MS |
1978 | tree base_fndecl = TREE_OPERAND (base_pfn, 0); |
1979 | if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl)) | |
1980 | abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals); | |
f30432d7 | 1981 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
1982 | } |
1983 | } | |
8926095f MS |
1984 | return abstract_virtuals; |
1985 | } | |
1986 | ||
1987 | /* Return the list of virtual functions which are abstract in type TYPE. | |
1988 | This information is cached, and so must be built on a | |
1989 | non-temporary obstack. */ | |
e92cc029 | 1990 | |
8926095f MS |
1991 | tree |
1992 | get_abstract_virtuals (type) | |
1993 | tree type; | |
1994 | { | |
f30432d7 | 1995 | tree vbases; |
83f660b7 | 1996 | tree abstract_virtuals = NULL; |
8926095f | 1997 | |
e92cc029 | 1998 | /* First get all from non-virtual bases. */ |
8926095f MS |
1999 | abstract_virtuals |
2000 | = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals); | |
2001 | ||
8d08fdba MS |
2002 | for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases)) |
2003 | { | |
f30432d7 MS |
2004 | tree virtuals = BINFO_VIRTUALS (vbases); |
2005 | ||
aff08c18 | 2006 | skip_rtti_stuff (&virtuals, type); |
8d08fdba | 2007 | |
f30432d7 | 2008 | while (virtuals) |
8d08fdba | 2009 | { |
f30432d7 | 2010 | tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)); |
8d08fdba | 2011 | tree base_fndecl = TREE_OPERAND (base_pfn, 0); |
8ebeee52 JM |
2012 | if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl)) |
2013 | cp_error ("`%#D' needs a final overrider", base_fndecl); | |
2014 | else if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl)) | |
8d08fdba | 2015 | abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals); |
f30432d7 | 2016 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
2017 | } |
2018 | } | |
2019 | return nreverse (abstract_virtuals); | |
2020 | } | |
2021 | ||
9e0781b5 | 2022 | static tree |
8d08fdba MS |
2023 | next_baselink (baselink) |
2024 | tree baselink; | |
2025 | { | |
2026 | tree tmp = TREE_TYPE (baselink); | |
2027 | baselink = TREE_CHAIN (baselink); | |
2028 | while (tmp) | |
2029 | { | |
2030 | /* @@ does not yet add previous base types. */ | |
2031 | baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp), | |
2032 | baselink); | |
2033 | TREE_TYPE (baselink) = TREE_TYPE (tmp); | |
2034 | tmp = TREE_CHAIN (tmp); | |
2035 | } | |
2036 | return baselink; | |
2037 | } | |
2038 | \f | |
2039 | /* DEPTH-FIRST SEARCH ROUTINES. */ | |
2040 | ||
8d08fdba MS |
2041 | /* This routine converts a pointer to be a pointer of an immediate |
2042 | base class. The normal convert_pointer_to routine would diagnose | |
2043 | the conversion as ambiguous, under MI code that has the base class | |
e92cc029 MS |
2044 | as an ambiguous base class. */ |
2045 | ||
8d08fdba MS |
2046 | static tree |
2047 | convert_pointer_to_single_level (to_type, expr) | |
2048 | tree to_type, expr; | |
2049 | { | |
2050 | tree binfo_of_derived; | |
2051 | tree last; | |
2052 | ||
2053 | binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))); | |
2054 | last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0); | |
dfbcd65a JM |
2055 | my_friendly_assert (BINFO_INHERITANCE_CHAIN (last) == binfo_of_derived, |
2056 | 980827); | |
2057 | my_friendly_assert (BINFO_INHERITANCE_CHAIN (binfo_of_derived) == NULL_TREE, | |
2058 | 980827); | |
2059 | return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr, | |
2060 | last, 1); | |
8d08fdba MS |
2061 | } |
2062 | ||
d6479fe7 MM |
2063 | tree markedp (binfo, data) |
2064 | tree binfo; | |
2065 | void *data ATTRIBUTE_UNUSED; | |
2066 | { | |
2067 | return BINFO_MARKED (binfo) ? binfo : NULL_TREE; | |
2068 | } | |
2069 | ||
2070 | static tree | |
2071 | unmarkedp (binfo, data) | |
2072 | tree binfo; | |
2073 | void *data ATTRIBUTE_UNUSED; | |
2074 | { | |
2075 | return !BINFO_MARKED (binfo) ? binfo : NULL_TREE; | |
2076 | } | |
5566b478 | 2077 | |
d6479fe7 MM |
2078 | static tree |
2079 | marked_vtable_pathp (binfo, data) | |
2080 | tree binfo; | |
2081 | void *data ATTRIBUTE_UNUSED; | |
2082 | { | |
2083 | return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; | |
2084 | } | |
2085 | ||
2086 | static tree | |
2087 | unmarked_vtable_pathp (binfo, data) | |
2088 | tree binfo; | |
2089 | void *data ATTRIBUTE_UNUSED; | |
2090 | { | |
2091 | return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; | |
2092 | } | |
2093 | ||
2094 | static tree | |
2095 | marked_new_vtablep (binfo, data) | |
2096 | tree binfo; | |
2097 | void *data ATTRIBUTE_UNUSED; | |
2098 | { | |
2099 | return BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE; | |
2100 | } | |
2101 | ||
2102 | static tree | |
2103 | unmarked_new_vtablep (binfo, data) | |
2104 | tree binfo; | |
2105 | void *data ATTRIBUTE_UNUSED; | |
2106 | { | |
2107 | return !BINFO_NEW_VTABLE_MARKED (binfo) ? binfo : NULL_TREE; | |
2108 | } | |
2109 | ||
2110 | static tree | |
2111 | marked_pushdecls_p (binfo, data) | |
2112 | tree binfo; | |
2113 | void *data ATTRIBUTE_UNUSED; | |
2114 | { | |
2115 | return BINFO_PUSHDECLS_MARKED (binfo) ? binfo : NULL_TREE; | |
2116 | } | |
5566b478 | 2117 | |
d6479fe7 MM |
2118 | static tree |
2119 | unmarked_pushdecls_p (binfo, data) | |
2120 | tree binfo; | |
2121 | void *data ATTRIBUTE_UNUSED; | |
2122 | { | |
2123 | return !BINFO_PUSHDECLS_MARKED (binfo) ? binfo : NULL_TREE; | |
2124 | } | |
8d08fdba | 2125 | |
5566b478 | 2126 | #if 0 |
8d08fdba MS |
2127 | static int dfs_search_slot_nonempty_p (binfo) tree binfo; |
2128 | { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; } | |
5566b478 | 2129 | #endif |
8d08fdba | 2130 | |
d6479fe7 MM |
2131 | static tree |
2132 | dfs_debug_unmarkedp (binfo, data) | |
2133 | tree binfo; | |
2134 | void *data ATTRIBUTE_UNUSED; | |
2135 | { | |
2136 | return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) | |
2137 | ? binfo : NULL_TREE); | |
2138 | } | |
8d08fdba MS |
2139 | |
2140 | /* The worker functions for `dfs_walk'. These do not need to | |
2141 | test anything (vis a vis marking) if they are paired with | |
2142 | a predicate function (above). */ | |
2143 | ||
5566b478 | 2144 | #if 0 |
8d08fdba MS |
2145 | static void |
2146 | dfs_mark (binfo) tree binfo; | |
2147 | { SET_BINFO_MARKED (binfo); } | |
5566b478 | 2148 | #endif |
8d08fdba | 2149 | |
d6479fe7 MM |
2150 | tree |
2151 | dfs_unmark (binfo, data) | |
2152 | tree binfo; | |
2153 | void *data ATTRIBUTE_UNUSED; | |
2154 | { | |
2155 | CLEAR_BINFO_MARKED (binfo); | |
2156 | return NULL_TREE; | |
2157 | } | |
8d08fdba | 2158 | |
5566b478 | 2159 | #if 0 |
8d08fdba MS |
2160 | static void |
2161 | dfs_mark_vtable_path (binfo) tree binfo; | |
2162 | { SET_BINFO_VTABLE_PATH_MARKED (binfo); } | |
2163 | ||
2164 | static void | |
2165 | dfs_unmark_vtable_path (binfo) tree binfo; | |
2166 | { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); } | |
2167 | ||
2168 | static void | |
2169 | dfs_mark_new_vtable (binfo) tree binfo; | |
2170 | { SET_BINFO_NEW_VTABLE_MARKED (binfo); } | |
2171 | ||
2172 | static void | |
2173 | dfs_unmark_new_vtable (binfo) tree binfo; | |
2174 | { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); } | |
2175 | ||
2176 | static void | |
2177 | dfs_clear_search_slot (binfo) tree binfo; | |
2178 | { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; } | |
5566b478 | 2179 | #endif |
8d08fdba | 2180 | |
d6479fe7 MM |
2181 | static tree |
2182 | dfs_debug_mark (binfo, data) | |
8d08fdba | 2183 | tree binfo; |
d6479fe7 | 2184 | void *data ATTRIBUTE_UNUSED; |
8d08fdba MS |
2185 | { |
2186 | tree t = BINFO_TYPE (binfo); | |
2187 | ||
2188 | /* Use heuristic that if there are virtual functions, | |
2189 | ignore until we see a non-inline virtual function. */ | |
2190 | tree methods = CLASSTYPE_METHOD_VEC (t); | |
2191 | ||
2192 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
2193 | ||
9a3b49ac | 2194 | if (methods == 0) |
d6479fe7 | 2195 | return NULL_TREE; |
9a3b49ac | 2196 | |
9a3b49ac MS |
2197 | /* If interface info is known, either we've already emitted the debug |
2198 | info or we don't need to. */ | |
56ae6d77 | 2199 | if (CLASSTYPE_INTERFACE_KNOWN (t)) |
d6479fe7 | 2200 | return NULL_TREE; |
8d08fdba MS |
2201 | |
2202 | /* If debug info is requested from this context for this type, supply it. | |
2203 | If debug info is requested from another context for this type, | |
2204 | see if some third context can supply it. */ | |
2205 | if (current_function_decl == NULL_TREE | |
2206 | || DECL_CLASS_CONTEXT (current_function_decl) != t) | |
2207 | { | |
fc378698 MS |
2208 | if (TREE_VEC_ELT (methods, 1)) |
2209 | methods = TREE_VEC_ELT (methods, 1); | |
2210 | else if (TREE_VEC_ELT (methods, 0)) | |
8d08fdba MS |
2211 | methods = TREE_VEC_ELT (methods, 0); |
2212 | else | |
fc378698 | 2213 | methods = TREE_VEC_ELT (methods, 2); |
2c73f9f5 | 2214 | methods = OVL_CURRENT (methods); |
8d08fdba MS |
2215 | while (methods) |
2216 | { | |
2217 | if (DECL_VINDEX (methods) | |
44a8d0b3 | 2218 | && DECL_THIS_INLINE (methods) == 0 |
8d08fdba MS |
2219 | && DECL_ABSTRACT_VIRTUAL_P (methods) == 0) |
2220 | { | |
2221 | /* Somebody, somewhere is going to have to define this | |
2222 | virtual function. When they do, they will provide | |
2223 | the debugging info. */ | |
d6479fe7 | 2224 | return NULL_TREE; |
8d08fdba MS |
2225 | } |
2226 | methods = TREE_CHAIN (methods); | |
2227 | } | |
2228 | } | |
2229 | /* We cannot rely on some alien method to solve our problems, | |
2230 | so we must write out the debug info ourselves. */ | |
f0e01782 | 2231 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0; |
2c73f9f5 | 2232 | rest_of_type_compilation (t, toplevel_bindings_p ()); |
d6479fe7 MM |
2233 | |
2234 | return NULL_TREE; | |
8d08fdba MS |
2235 | } |
2236 | \f | |
d6479fe7 MM |
2237 | struct vbase_info |
2238 | { | |
2239 | tree decl_ptr; | |
2240 | tree inits; | |
2241 | tree vbase_types; | |
2242 | }; | |
7177d104 | 2243 | |
d6479fe7 MM |
2244 | /* Attach to the type of the virtual base class, the pointer to the |
2245 | virtual base class. */ | |
e92cc029 | 2246 | |
d6479fe7 MM |
2247 | static tree |
2248 | dfs_find_vbases (binfo, data) | |
8d08fdba | 2249 | tree binfo; |
d6479fe7 | 2250 | void *data; |
8d08fdba | 2251 | { |
d6479fe7 | 2252 | struct vbase_info *vi = (struct vbase_info *) data; |
8d08fdba MS |
2253 | tree binfos = BINFO_BASETYPES (binfo); |
2254 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2255 | ||
2256 | for (i = n_baselinks-1; i >= 0; i--) | |
2257 | { | |
2258 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2259 | ||
2260 | if (TREE_VIA_VIRTUAL (base_binfo) | |
2261 | && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0) | |
2262 | { | |
2263 | tree vbase = BINFO_TYPE (base_binfo); | |
d6479fe7 | 2264 | tree binfo = binfo_member (vbase, vi->vbase_types); |
8d08fdba MS |
2265 | |
2266 | CLASSTYPE_SEARCH_SLOT (vbase) | |
fc378698 | 2267 | = build (PLUS_EXPR, build_pointer_type (vbase), |
d6479fe7 | 2268 | vi->decl_ptr, BINFO_OFFSET (binfo)); |
8d08fdba MS |
2269 | } |
2270 | } | |
2271 | SET_BINFO_VTABLE_PATH_MARKED (binfo); | |
2272 | SET_BINFO_NEW_VTABLE_MARKED (binfo); | |
d6479fe7 MM |
2273 | |
2274 | return NULL_TREE; | |
8d08fdba MS |
2275 | } |
2276 | ||
d6479fe7 MM |
2277 | static tree |
2278 | dfs_init_vbase_pointers (binfo, data) | |
8d08fdba | 2279 | tree binfo; |
d6479fe7 | 2280 | void *data; |
8d08fdba | 2281 | { |
d6479fe7 | 2282 | struct vbase_info *vi = (struct vbase_info *) data; |
8d08fdba MS |
2283 | tree type = BINFO_TYPE (binfo); |
2284 | tree fields = TYPE_FIELDS (type); | |
8926095f | 2285 | tree this_vbase_ptr; |
8d08fdba MS |
2286 | |
2287 | CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); | |
2288 | ||
6b5fbb55 MS |
2289 | #if 0 |
2290 | /* See finish_struct_1 for when we can enable this. */ | |
2291 | /* If we have a vtable pointer first, skip it. */ | |
2292 | if (VFIELD_NAME_P (DECL_NAME (fields))) | |
8d08fdba | 2293 | fields = TREE_CHAIN (fields); |
6b5fbb55 | 2294 | #endif |
8d08fdba | 2295 | |
d6479fe7 MM |
2296 | if (BINFO_INHERITANCE_CHAIN (binfo)) |
2297 | { | |
2298 | this_vbase_ptr = TREE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)); | |
2299 | if (TREE_VIA_VIRTUAL (binfo)) | |
2300 | this_vbase_ptr = CLASSTYPE_SEARCH_SLOT (type); | |
2301 | else | |
2302 | this_vbase_ptr = convert_pointer_to_single_level (type, | |
2303 | this_vbase_ptr); | |
2304 | TREE_CHAIN (binfo) = this_vbase_ptr; | |
2305 | } | |
2306 | else | |
2307 | this_vbase_ptr = TREE_CHAIN (binfo); | |
2308 | ||
8d08fdba MS |
2309 | if (fields == NULL_TREE |
2310 | || DECL_NAME (fields) == NULL_TREE | |
2311 | || ! VBASE_NAME_P (DECL_NAME (fields))) | |
d6479fe7 | 2312 | return NULL_TREE; |
8d08fdba | 2313 | |
d6479fe7 MM |
2314 | if (build_pointer_type (type) |
2315 | != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr))) | |
8d08fdba MS |
2316 | my_friendly_abort (125); |
2317 | ||
d6479fe7 | 2318 | while (fields && DECL_NAME (fields) && VBASE_NAME_P (DECL_NAME (fields))) |
8d08fdba MS |
2319 | { |
2320 | tree ref = build (COMPONENT_REF, TREE_TYPE (fields), | |
2321 | build_indirect_ref (this_vbase_ptr, NULL_PTR), fields); | |
fc378698 | 2322 | tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields))); |
d6479fe7 MM |
2323 | vi->inits = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)), |
2324 | vi->vbase_types), | |
2325 | build_modify_expr (ref, NOP_EXPR, init), | |
2326 | vi->inits); | |
8d08fdba MS |
2327 | fields = TREE_CHAIN (fields); |
2328 | } | |
d6479fe7 MM |
2329 | |
2330 | return NULL_TREE; | |
8d08fdba MS |
2331 | } |
2332 | ||
2333 | /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other | |
2334 | times, just NEW_VTABLE, but optimizer should make both with equal | |
2335 | efficiency (though it does not currently). */ | |
e92cc029 | 2336 | |
d6479fe7 MM |
2337 | static tree |
2338 | dfs_clear_vbase_slots (binfo, data) | |
8d08fdba | 2339 | tree binfo; |
d6479fe7 | 2340 | void *data ATTRIBUTE_UNUSED; |
8d08fdba MS |
2341 | { |
2342 | tree type = BINFO_TYPE (binfo); | |
2343 | CLASSTYPE_SEARCH_SLOT (type) = 0; | |
2344 | CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); | |
2345 | CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); | |
d6479fe7 | 2346 | return NULL_TREE; |
8d08fdba MS |
2347 | } |
2348 | ||
2349 | tree | |
2350 | init_vbase_pointers (type, decl_ptr) | |
2351 | tree type; | |
2352 | tree decl_ptr; | |
2353 | { | |
2354 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
2355 | { | |
d6479fe7 | 2356 | struct vbase_info vi; |
8d08fdba MS |
2357 | int old_flag = flag_this_is_variable; |
2358 | tree binfo = TYPE_BINFO (type); | |
2359 | flag_this_is_variable = -2; | |
d6479fe7 MM |
2360 | |
2361 | /* Find all the virtual base classes, marking them for later | |
2362 | initialization. */ | |
2363 | vi.decl_ptr = decl_ptr; | |
2364 | vi.vbase_types = CLASSTYPE_VBASECLASSES (type); | |
2365 | vi.inits = NULL_TREE; | |
2366 | ||
2367 | dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp, &vi); | |
2368 | ||
2369 | /* Build up a list of the initializers. */ | |
2370 | TREE_CHAIN (binfo) = decl_ptr; | |
2371 | dfs_walk_real (binfo, | |
2372 | dfs_init_vbase_pointers, 0, | |
2373 | marked_vtable_pathp, | |
2374 | &vi); | |
2375 | ||
2376 | dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0); | |
8d08fdba | 2377 | flag_this_is_variable = old_flag; |
d6479fe7 | 2378 | return vi.inits; |
8d08fdba MS |
2379 | } |
2380 | return 0; | |
2381 | } | |
2382 | ||
43f2999d MS |
2383 | /* get the virtual context (the vbase that directly contains the |
2384 | DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in, | |
2385 | or NULL_TREE if there is none. | |
2386 | ||
2387 | FNDECL must come from a virtual table from a virtual base to ensure that | |
2388 | there is only one possible DECL_CLASS_CONTEXT. | |
2389 | ||
2390 | We know that if there is more than one place (binfo) the fndecl that the | |
2391 | declared, they all refer to the same binfo. See get_class_offset_1 for | |
2392 | the check that ensures this. */ | |
e92cc029 | 2393 | |
43f2999d MS |
2394 | static tree |
2395 | virtual_context (fndecl, t, vbase) | |
2396 | tree fndecl, t, vbase; | |
2397 | { | |
2398 | tree path; | |
2399 | if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0) | |
2400 | { | |
f30432d7 MS |
2401 | /* DECL_CLASS_CONTEXT can be ambiguous in t. */ |
2402 | if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0) | |
2403 | { | |
2404 | while (path) | |
2405 | { | |
2406 | /* Not sure if checking path == vbase is necessary here, but just in | |
2407 | case it is. */ | |
2408 | if (TREE_VIA_VIRTUAL (path) || path == vbase) | |
2409 | return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t)); | |
2410 | path = BINFO_INHERITANCE_CHAIN (path); | |
2411 | } | |
2412 | } | |
43f2999d | 2413 | /* This shouldn't happen, I don't want errors! */ |
8251199e | 2414 | warning ("recoverable compiler error, fixups for virtual function"); |
43f2999d MS |
2415 | return vbase; |
2416 | } | |
2417 | while (path) | |
2418 | { | |
2419 | if (TREE_VIA_VIRTUAL (path)) | |
2420 | return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t)); | |
2421 | path = BINFO_INHERITANCE_CHAIN (path); | |
2422 | } | |
2423 | return 0; | |
2424 | } | |
2425 | ||
2426 | /* Fixups upcast offsets for one vtable. | |
2427 | Entries may stay within the VBASE given, or | |
2428 | they may upcast into a direct base, or | |
2429 | they may upcast into a different vbase. | |
2430 | ||
45537677 MS |
2431 | We only need to do fixups in case 2 and 3. In case 2, we add in |
2432 | the virtual base offset to effect an upcast, in case 3, we add in | |
2433 | the virtual base offset to effect an upcast, then subtract out the | |
2434 | offset for the other virtual base, to effect a downcast into it. | |
43f2999d MS |
2435 | |
2436 | This routine mirrors fixup_vtable_deltas in functionality, though | |
2437 | this one is runtime based, and the other is compile time based. | |
2438 | Conceivably that routine could be removed entirely, and all fixups | |
2439 | done at runtime. | |
2440 | ||
2441 | VBASE_OFFSETS is an association list of virtual bases that contains | |
45537677 MS |
2442 | offset information for the virtual bases, so the offsets are only |
2443 | calculated once. The offsets are computed by where we think the | |
2444 | vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where | |
e92cc029 MS |
2445 | the vbase really is. */ |
2446 | ||
43f2999d | 2447 | static void |
45537677 MS |
2448 | expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t, |
2449 | vbase_offsets) | |
2450 | tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets; | |
43f2999d MS |
2451 | { |
2452 | tree virtuals = BINFO_VIRTUALS (binfo); | |
2453 | tree vc; | |
2454 | tree delta; | |
2455 | unsigned HOST_WIDE_INT n; | |
2456 | ||
2457 | delta = purpose_member (vbase, *vbase_offsets); | |
2458 | if (! delta) | |
2459 | { | |
fc378698 | 2460 | delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase)); |
45537677 | 2461 | delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr); |
43f2999d MS |
2462 | delta = save_expr (delta); |
2463 | delta = tree_cons (vbase, delta, *vbase_offsets); | |
2464 | *vbase_offsets = delta; | |
2465 | } | |
2466 | ||
aff08c18 | 2467 | n = skip_rtti_stuff (&virtuals, t); |
f30432d7 | 2468 | |
43f2999d MS |
2469 | while (virtuals) |
2470 | { | |
2471 | tree current_fndecl = TREE_VALUE (virtuals); | |
2472 | current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl); | |
2473 | current_fndecl = TREE_OPERAND (current_fndecl, 0); | |
2474 | if (current_fndecl | |
e8abc66f | 2475 | && current_fndecl != abort_fndecl |
43f2999d MS |
2476 | && (vc=virtual_context (current_fndecl, t, vbase)) != vbase) |
2477 | { | |
e92cc029 | 2478 | /* This may in fact need a runtime fixup. */ |
de22184b | 2479 | tree idx = build_int_2 (n, 0); |
43f2999d MS |
2480 | tree vtbl = BINFO_VTABLE (binfo); |
2481 | tree nvtbl = lookup_name (DECL_NAME (vtbl), 0); | |
2482 | tree aref, ref, naref; | |
2483 | tree old_delta, new_delta; | |
2484 | tree init; | |
2485 | ||
2486 | if (nvtbl == NULL_TREE | |
2487 | || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl))) | |
2488 | { | |
2489 | /* Dup it if it isn't in local scope yet. */ | |
8c90d611 JM |
2490 | nvtbl = build_decl |
2491 | (VAR_DECL, DECL_NAME (vtbl), | |
52bf7d5d | 2492 | TYPE_MAIN_VARIANT (TREE_TYPE (vtbl))); |
43f2999d MS |
2493 | DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node), |
2494 | DECL_ALIGN (nvtbl)); | |
2495 | TREE_READONLY (nvtbl) = 0; | |
6b5fbb55 | 2496 | DECL_ARTIFICIAL (nvtbl) = 1; |
43f2999d MS |
2497 | nvtbl = pushdecl (nvtbl); |
2498 | init = NULL_TREE; | |
8c90d611 JM |
2499 | cp_finish_decl (nvtbl, init, NULL_TREE, 0, |
2500 | LOOKUP_ONLYCONVERTING); | |
2501 | ||
2502 | /* We don't set DECL_VIRTUAL_P and DECL_CONTEXT on nvtbl | |
2503 | because they wouldn't be useful; everything that wants to | |
2504 | look at the vtable will look at the decl for the normal | |
2505 | vtable. Setting DECL_CONTEXT also screws up | |
2506 | decl_function_context. */ | |
2507 | ||
43f2999d MS |
2508 | init = build (MODIFY_EXPR, TREE_TYPE (nvtbl), |
2509 | nvtbl, vtbl); | |
2510 | TREE_SIDE_EFFECTS (init) = 1; | |
2511 | expand_expr_stmt (init); | |
e92cc029 | 2512 | /* Update the vtable pointers as necessary. */ |
8c90d611 JM |
2513 | ref = build_vfield_ref |
2514 | (build_indirect_ref (addr, NULL_PTR), | |
2515 | DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo)))); | |
2516 | expand_expr_stmt | |
a9e6be8e | 2517 | (build_modify_expr (ref, NOP_EXPR, nvtbl)); |
43f2999d MS |
2518 | } |
2519 | assemble_external (vtbl); | |
2520 | aref = build_array_ref (vtbl, idx); | |
2521 | naref = build_array_ref (nvtbl, idx); | |
8c90d611 JM |
2522 | old_delta = build_component_ref (aref, delta_identifier, |
2523 | NULL_TREE, 0); | |
2524 | new_delta = build_component_ref (naref, delta_identifier, | |
2525 | NULL_TREE, 0); | |
45537677 MS |
2526 | |
2527 | /* This is a upcast, so we have to add the offset for the | |
2528 | virtual base. */ | |
43f2999d | 2529 | old_delta = build_binary_op (PLUS_EXPR, old_delta, |
337c90cc | 2530 | TREE_VALUE (delta)); |
43f2999d MS |
2531 | if (vc) |
2532 | { | |
45537677 MS |
2533 | /* If this is set, we need to subtract out the delta |
2534 | adjustments for the other virtual base that we | |
2535 | downcast into. */ | |
43f2999d MS |
2536 | tree vc_delta = purpose_member (vc, *vbase_offsets); |
2537 | if (! vc_delta) | |
2538 | { | |
2539 | tree vc_addr = convert_pointer_to_real (vc, orig_addr); | |
fc378698 | 2540 | vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc)); |
43f2999d | 2541 | vc_delta = build (MINUS_EXPR, ptrdiff_type_node, |
45537677 | 2542 | vc_delta, vc_addr); |
43f2999d MS |
2543 | vc_delta = save_expr (vc_delta); |
2544 | *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets); | |
2545 | } | |
2546 | else | |
2547 | vc_delta = TREE_VALUE (vc_delta); | |
2548 | ||
45537677 MS |
2549 | /* This is a downcast, so we have to subtract the offset |
2550 | for the virtual base. */ | |
337c90cc | 2551 | old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta); |
43f2999d MS |
2552 | } |
2553 | ||
2554 | TREE_READONLY (new_delta) = 0; | |
6396c3a5 | 2555 | TREE_TYPE (new_delta) = |
91063b51 MM |
2556 | cp_build_qualified_type (TREE_TYPE (new_delta), |
2557 | CP_TYPE_QUALS (TREE_TYPE (new_delta)) | |
2558 | & ~TYPE_QUAL_CONST); | |
43f2999d MS |
2559 | expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR, |
2560 | old_delta)); | |
2561 | } | |
2562 | ++n; | |
2563 | virtuals = TREE_CHAIN (virtuals); | |
2564 | } | |
2565 | } | |
2566 | ||
2567 | /* Fixup upcast offsets for all direct vtables. Patterned after | |
2568 | expand_direct_vtbls_init. */ | |
e92cc029 | 2569 | |
43f2999d MS |
2570 | static void |
2571 | fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets) | |
6b5fbb55 | 2572 | tree real_binfo, binfo; |
43f2999d | 2573 | int init_self, can_elide; |
6b5fbb55 | 2574 | tree addr, orig_addr, type, vbase, *vbase_offsets; |
43f2999d MS |
2575 | { |
2576 | tree real_binfos = BINFO_BASETYPES (real_binfo); | |
2577 | tree binfos = BINFO_BASETYPES (binfo); | |
2578 | int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0; | |
2579 | ||
2580 | for (i = 0; i < n_baselinks; i++) | |
2581 | { | |
2582 | tree real_base_binfo = TREE_VEC_ELT (real_binfos, i); | |
2583 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
2584 | int is_not_base_vtable |
2585 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo)); | |
43f2999d MS |
2586 | if (! TREE_VIA_VIRTUAL (real_base_binfo)) |
2587 | fixup_virtual_upcast_offsets (real_base_binfo, base_binfo, | |
2588 | is_not_base_vtable, can_elide, addr, | |
2589 | orig_addr, type, vbase, vbase_offsets); | |
2590 | } | |
2591 | #if 0 | |
2592 | /* Before turning this on, make sure it is correct. */ | |
2593 | if (can_elide && ! BINFO_MODIFIED (binfo)) | |
2594 | return; | |
2595 | #endif | |
2596 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2597 | if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo))) | |
2598 | { | |
45537677 MS |
2599 | tree new_addr = convert_pointer_to_real (binfo, addr); |
2600 | expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr, | |
2601 | type, vbase_offsets); | |
43f2999d MS |
2602 | } |
2603 | } | |
2604 | ||
8d08fdba MS |
2605 | /* Build a COMPOUND_EXPR which when expanded will generate the code |
2606 | needed to initialize all the virtual function table slots of all | |
7177d104 MS |
2607 | the virtual baseclasses. MAIN_BINFO is the binfo which determines |
2608 | the virtual baseclasses to use; TYPE is the type of the object to | |
2609 | which the initialization applies. TRUE_EXP is the true object we | |
2610 | are initializing, and DECL_PTR is the pointer to the sub-object we | |
8d08fdba MS |
2611 | are initializing. |
2612 | ||
2613 | When USE_COMPUTED_OFFSETS is non-zero, we can assume that the | |
ddd5a7c1 | 2614 | object was laid out by a top-level constructor and the computed |
8d08fdba | 2615 | offsets are valid to store vtables. When zero, we must store new |
d6479fe7 | 2616 | vtables through virtual baseclass pointers. */ |
8d08fdba | 2617 | |
8926095f | 2618 | void |
9e9ff709 | 2619 | expand_indirect_vtbls_init (binfo, true_exp, decl_ptr) |
7177d104 | 2620 | tree binfo; |
8d08fdba | 2621 | tree true_exp, decl_ptr; |
8d08fdba | 2622 | { |
8d08fdba | 2623 | tree type = BINFO_TYPE (binfo); |
9e9ff709 | 2624 | |
8a21007c AM |
2625 | /* This function executes during the finish_function() segment, |
2626 | AFTER the auto variables and temporary stack space has been marked | |
2627 | unused...If space is needed for the virtual function tables, | |
2628 | some of them might fit within what the compiler now thinks | |
2629 | are available stack slots... These values are actually initialized at | |
2630 | the beginnning of the function, so when the automatics use their space, | |
2631 | they will overwrite the values that are placed here. Marking all | |
2632 | temporary space as unavailable prevents this from happening. */ | |
2633 | ||
2634 | mark_all_temps_used(); | |
2635 | ||
8d08fdba MS |
2636 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) |
2637 | { | |
43f2999d | 2638 | rtx fixup_insns = NULL_RTX; |
8d08fdba | 2639 | tree vbases = CLASSTYPE_VBASECLASSES (type); |
d6479fe7 MM |
2640 | struct vbase_info vi; |
2641 | vi.decl_ptr = (true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) | |
2642 | : decl_ptr); | |
2643 | vi.vbase_types = vbases; | |
8d08fdba | 2644 | |
d6479fe7 | 2645 | dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep, &vi); |
43f2999d | 2646 | |
8d08fdba | 2647 | /* Initialized with vtables of type TYPE. */ |
39211cd5 | 2648 | for (; vbases; vbases = TREE_CHAIN (vbases)) |
8d08fdba | 2649 | { |
7177d104 | 2650 | tree addr; |
9e9ff709 | 2651 | |
d6479fe7 | 2652 | addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vi.decl_ptr); |
8d08fdba | 2653 | |
e92cc029 | 2654 | /* Do all vtables from this virtual base. */ |
7177d104 | 2655 | /* This assumes that virtual bases can never serve as parent |
956d6950 | 2656 | binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */ |
7177d104 MS |
2657 | expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)), |
2658 | 1, 0, addr); | |
43f2999d | 2659 | |
e92cc029 MS |
2660 | /* Now we adjust the offsets for virtual functions that |
2661 | cross virtual boundaries on an implicit upcast on vf call | |
2662 | so that the layout of the most complete type is used, | |
2663 | instead of assuming the layout of the virtual bases from | |
2664 | our current type. */ | |
43f2999d MS |
2665 | |
2666 | if (flag_vtable_thunks) | |
2667 | { | |
5566b478 | 2668 | /* We don't have dynamic thunks yet! |
e92cc029 | 2669 | So for now, just fail silently. */ |
43f2999d MS |
2670 | } |
2671 | else | |
2672 | { | |
2673 | tree vbase_offsets = NULL_TREE; | |
2674 | push_to_sequence (fixup_insns); | |
2675 | fixup_virtual_upcast_offsets (vbases, | |
2676 | TYPE_BINFO (BINFO_TYPE (vbases)), | |
d6479fe7 | 2677 | 1, 0, addr, vi.decl_ptr, |
43f2999d MS |
2678 | type, vbases, &vbase_offsets); |
2679 | fixup_insns = get_insns (); | |
2680 | end_sequence (); | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | if (fixup_insns) | |
2685 | { | |
2686 | extern tree in_charge_identifier; | |
2687 | tree in_charge_node = lookup_name (in_charge_identifier, 0); | |
2688 | if (! in_charge_node) | |
2689 | { | |
8251199e | 2690 | warning ("recoverable internal compiler error, nobody's in charge!"); |
43f2999d MS |
2691 | in_charge_node = integer_zero_node; |
2692 | } | |
337c90cc | 2693 | in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node); |
43f2999d MS |
2694 | expand_start_cond (in_charge_node, 0); |
2695 | emit_insns (fixup_insns); | |
2696 | expand_end_cond (); | |
8d08fdba MS |
2697 | } |
2698 | ||
d6479fe7 | 2699 | dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep, 0); |
8d08fdba | 2700 | } |
8d08fdba MS |
2701 | } |
2702 | ||
8d08fdba MS |
2703 | /* get virtual base class types. |
2704 | This adds type to the vbase_types list in reverse dfs order. | |
2705 | Ordering is very important, so don't change it. */ | |
2706 | ||
d6479fe7 MM |
2707 | static tree |
2708 | dfs_get_vbase_types (binfo, data) | |
8d08fdba | 2709 | tree binfo; |
d6479fe7 | 2710 | void *data; |
8d08fdba | 2711 | { |
d6479fe7 MM |
2712 | tree *vbase_types = (tree *) data; |
2713 | ||
51c184be | 2714 | if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo)) |
8d08fdba | 2715 | { |
ca107ded MM |
2716 | tree new_vbase = make_binfo (integer_zero_node, binfo, |
2717 | BINFO_VTABLE (binfo), | |
2718 | BINFO_VIRTUALS (binfo)); | |
d6479fe7 | 2719 | TREE_CHAIN (new_vbase) = *vbase_types; |
ca107ded | 2720 | TREE_VIA_VIRTUAL (new_vbase) = 1; |
d6479fe7 | 2721 | *vbase_types = new_vbase; |
51c184be | 2722 | SET_BINFO_VBASE_MARKED (binfo); |
8d08fdba MS |
2723 | } |
2724 | SET_BINFO_MARKED (binfo); | |
d6479fe7 | 2725 | return NULL_TREE; |
8d08fdba MS |
2726 | } |
2727 | ||
d6479fe7 MM |
2728 | /* Return a list of binfos for the virtual base classes for TYPE, in |
2729 | depth-first search order. The list is freshly allocated, so | |
2730 | no modification is made to the current binfo hierarchy. */ | |
e92cc029 | 2731 | |
8d08fdba MS |
2732 | tree |
2733 | get_vbase_types (type) | |
2734 | tree type; | |
2735 | { | |
d6479fe7 | 2736 | tree vbase_types; |
8d08fdba | 2737 | tree vbases; |
51c184be MS |
2738 | tree binfo; |
2739 | ||
ca107ded | 2740 | binfo = TYPE_BINFO (type); |
8d08fdba | 2741 | vbase_types = NULL_TREE; |
d6479fe7 MM |
2742 | dfs_walk (binfo, dfs_get_vbase_types, unmarkedp, &vbase_types); |
2743 | dfs_walk (binfo, dfs_unmark, markedp, 0); | |
8d08fdba MS |
2744 | /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now |
2745 | reverse it so that we get normal dfs ordering. */ | |
2746 | vbase_types = nreverse (vbase_types); | |
2747 | ||
51c184be MS |
2748 | /* unmark marked vbases */ |
2749 | for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases)) | |
2750 | CLEAR_BINFO_VBASE_MARKED (vbases); | |
8d08fdba | 2751 | |
51c184be | 2752 | return vbase_types; |
8d08fdba MS |
2753 | } |
2754 | \f | |
8d08fdba MS |
2755 | /* If we want debug info for a type TYPE, make sure all its base types |
2756 | are also marked as being potentially interesting. This avoids | |
2757 | the problem of not writing any debug info for intermediate basetypes | |
a292b002 | 2758 | that have abstract virtual functions. Also mark member types. */ |
8d08fdba MS |
2759 | |
2760 | void | |
2761 | note_debug_info_needed (type) | |
2762 | tree type; | |
2763 | { | |
a292b002 | 2764 | tree field; |
9a3b49ac MS |
2765 | |
2766 | if (current_template_parms) | |
2767 | return; | |
8857f91e MM |
2768 | |
2769 | if (TYPE_BEING_DEFINED (type)) | |
2770 | /* We can't go looking for the base types and fields just yet. */ | |
2771 | return; | |
9a3b49ac | 2772 | |
d2e5ee5c MS |
2773 | /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which |
2774 | does not support name references between translation units. Well, we | |
2775 | could, but that would mean putting global labels in the debug output | |
2776 | before each exported type and each of its functions and static data | |
2777 | members. */ | |
2778 | if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG) | |
2779 | return; | |
2780 | ||
d6479fe7 | 2781 | dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0); |
a292b002 MS |
2782 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
2783 | { | |
2784 | tree ttype; | |
2785 | if (TREE_CODE (field) == FIELD_DECL | |
2786 | && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field))) | |
d6479fe7 | 2787 | && dfs_debug_unmarkedp (TYPE_BINFO (ttype), 0)) |
a292b002 MS |
2788 | note_debug_info_needed (ttype); |
2789 | } | |
8d08fdba MS |
2790 | } |
2791 | \f | |
2792 | /* Subroutines of push_class_decls (). */ | |
2793 | ||
c1def683 JM |
2794 | /* Returns 1 iff BINFO is a base we shouldn't really be able to see into, |
2795 | because it (or one of the intermediate bases) depends on template parms. */ | |
2796 | ||
2797 | static int | |
2798 | dependent_base_p (binfo) | |
2799 | tree binfo; | |
2800 | { | |
2801 | for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo)) | |
2802 | { | |
d6479fe7 | 2803 | if (currently_open_class (TREE_TYPE (binfo))) |
c1def683 JM |
2804 | break; |
2805 | if (uses_template_parms (TREE_TYPE (binfo))) | |
2806 | return 1; | |
2807 | } | |
2808 | return 0; | |
2809 | } | |
2810 | ||
8f032717 MM |
2811 | static void |
2812 | setup_class_bindings (name, type_binding_p) | |
2813 | tree name; | |
2814 | int type_binding_p; | |
8d08fdba | 2815 | { |
8f032717 MM |
2816 | tree type_binding = NULL_TREE; |
2817 | tree value_binding; | |
c1def683 | 2818 | |
8f032717 MM |
2819 | /* If we've already done the lookup for this declaration, we're |
2820 | done. */ | |
2821 | if (IDENTIFIER_CLASS_VALUE (name)) | |
2822 | return; | |
8d08fdba | 2823 | |
8f032717 MM |
2824 | /* First, deal with the type binding. */ |
2825 | if (type_binding_p) | |
8d08fdba | 2826 | { |
8f032717 MM |
2827 | type_binding = lookup_member (current_class_type, name, |
2828 | /*protect=*/2, | |
2829 | /*want_type=*/1); | |
2830 | if (TREE_CODE (type_binding) == TREE_LIST | |
2831 | && TREE_PURPOSE (type_binding) == error_mark_node) | |
2832 | /* NAME is ambiguous. */ | |
2833 | push_class_level_binding (name, TREE_VALUE (type_binding)); | |
2834 | else | |
2835 | pushdecl_class_level (type_binding); | |
8d08fdba MS |
2836 | } |
2837 | ||
8f032717 MM |
2838 | /* Now, do the value binding. */ |
2839 | value_binding = lookup_member (current_class_type, name, | |
2840 | /*protect=*/2, | |
2841 | /*want_type=*/0); | |
2842 | ||
2843 | if (type_binding_p | |
2844 | && (TREE_CODE (value_binding) == TYPE_DECL | |
2845 | || (TREE_CODE (value_binding) == TREE_LIST | |
2846 | && TREE_PURPOSE (value_binding) == error_mark_node | |
2847 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (value_binding))) | |
2848 | == TYPE_DECL)))) | |
2849 | /* We found a type-binding, even when looking for a non-type | |
2850 | binding. This means that we already processed this binding | |
2851 | above. */ | |
2852 | my_friendly_assert (type_binding_p, 19990401); | |
2853 | else | |
8d08fdba | 2854 | { |
8f032717 MM |
2855 | if (TREE_CODE (value_binding) == TREE_LIST |
2856 | && TREE_PURPOSE (value_binding) == error_mark_node) | |
2857 | /* NAME is ambiguous. */ | |
2858 | push_class_level_binding (name, TREE_VALUE (value_binding)); | |
2859 | else | |
8d08fdba | 2860 | { |
8f032717 MM |
2861 | if (TREE_CODE (value_binding) == TREE_LIST) |
2862 | /* NAME is some overloaded functions. */ | |
2863 | value_binding = TREE_VALUE (value_binding); | |
2864 | pushdecl_class_level (value_binding); | |
2865 | } | |
2866 | } | |
2867 | } | |
f30432d7 | 2868 | |
8f032717 MM |
2869 | /* Push class-level declarations for any names appearing in BINFO that |
2870 | are TYPE_DECLS. */ | |
7ddedda4 | 2871 | |
8f032717 MM |
2872 | static tree |
2873 | dfs_push_type_decls (binfo, data) | |
2874 | tree binfo; | |
2875 | void *data ATTRIBUTE_UNUSED; | |
2876 | { | |
2877 | tree type; | |
2878 | tree fields; | |
f30432d7 | 2879 | |
8f032717 MM |
2880 | type = BINFO_TYPE (binfo); |
2881 | for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) | |
2882 | if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL | |
908c4e83 MM |
2883 | && !(!same_type_p (type, current_class_type) |
2884 | && template_self_reference_p (type, fields))) | |
8f032717 | 2885 | setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1); |
0ec57017 JM |
2886 | |
2887 | /* We can't just use BINFO_MARKED because envelope_add_decl uses | |
2888 | DERIVED_FROM_P, which calls get_base_distance. */ | |
2889 | SET_BINFO_PUSHDECLS_MARKED (binfo); | |
8f032717 | 2890 | |
d6479fe7 | 2891 | return NULL_TREE; |
8d08fdba MS |
2892 | } |
2893 | ||
8f032717 MM |
2894 | /* Push class-level declarations for any names appearing in BINFO that |
2895 | are not TYPE_DECLS. */ | |
e92cc029 | 2896 | |
d6479fe7 | 2897 | static tree |
8f032717 | 2898 | dfs_push_decls (binfo, data) |
8d08fdba | 2899 | tree binfo; |
8f032717 | 2900 | void *data; |
8d08fdba | 2901 | { |
8f032717 MM |
2902 | tree type; |
2903 | tree method_vec; | |
2904 | int dep_base_p; | |
8d08fdba | 2905 | |
8f032717 MM |
2906 | type = BINFO_TYPE (binfo); |
2907 | dep_base_p = (processing_template_decl && type != current_class_type | |
2908 | && dependent_base_p (binfo)); | |
2909 | if (!dep_base_p) | |
8d08fdba | 2910 | { |
8f032717 MM |
2911 | tree fields; |
2912 | for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) | |
2913 | if (DECL_NAME (fields) | |
2914 | && TREE_CODE (fields) != TYPE_DECL | |
2915 | && TREE_CODE (fields) != USING_DECL) | |
2916 | setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0); | |
2917 | else if (TREE_CODE (fields) == FIELD_DECL | |
2918 | && ANON_UNION_TYPE_P (TREE_TYPE (fields))) | |
2919 | dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data); | |
2920 | ||
2921 | method_vec = (CLASS_TYPE_P (type) | |
2922 | ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE); | |
2923 | if (method_vec) | |
8d08fdba | 2924 | { |
8f032717 MM |
2925 | tree *methods; |
2926 | tree *end; | |
2927 | ||
2928 | /* Farm out constructors and destructors. */ | |
2929 | end = TREE_VEC_END (method_vec); | |
2930 | ||
2931 | for (methods = &TREE_VEC_ELT (method_vec, 2); | |
2932 | *methods && methods != end; | |
2933 | methods++) | |
2934 | setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)), | |
2935 | /*type_binding_p=*/0); | |
8d08fdba MS |
2936 | } |
2937 | } | |
8f032717 | 2938 | |
0ec57017 | 2939 | CLEAR_BINFO_PUSHDECLS_MARKED (binfo); |
d6479fe7 MM |
2940 | |
2941 | return NULL_TREE; | |
8d08fdba MS |
2942 | } |
2943 | ||
2944 | /* When entering the scope of a class, we cache all of the | |
2945 | fields that that class provides within its inheritance | |
2946 | lattice. Where ambiguities result, we mark them | |
2947 | with `error_mark_node' so that if they are encountered | |
2948 | without explicit qualification, we can emit an error | |
45537677 | 2949 | message. */ |
e92cc029 | 2950 | |
8d08fdba | 2951 | void |
45537677 | 2952 | push_class_decls (type) |
8d08fdba MS |
2953 | tree type; |
2954 | { | |
8d08fdba | 2955 | struct obstack *ambient_obstack = current_obstack; |
8d08fdba MS |
2956 | search_stack = push_search_level (search_stack, &search_obstack); |
2957 | ||
7ddedda4 MM |
2958 | /* Build up all the relevant bindings and such on the cache |
2959 | obstack. That way no memory is wasted when we throw away the | |
2960 | cache later. */ | |
2961 | maybe_push_cache_obstack (); | |
2962 | ||
8d08fdba | 2963 | /* Push class fields into CLASS_VALUE scope, and mark. */ |
8f032717 | 2964 | dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0); |
8d08fdba MS |
2965 | |
2966 | /* Compress fields which have only a single entry | |
2967 | by a given name, and unmark. */ | |
8f032717 | 2968 | dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0); |
013bc8af | 2969 | |
7ddedda4 MM |
2970 | /* Undo the call to maybe_push_cache_obstack above. */ |
2971 | pop_obstacks (); | |
2972 | ||
8d08fdba MS |
2973 | current_obstack = ambient_obstack; |
2974 | } | |
2975 | ||
2976 | /* Here's a subroutine we need because C lacks lambdas. */ | |
e92cc029 | 2977 | |
d6479fe7 MM |
2978 | static tree |
2979 | dfs_unuse_fields (binfo, data) | |
8d08fdba | 2980 | tree binfo; |
d6479fe7 | 2981 | void *data ATTRIBUTE_UNUSED; |
8d08fdba MS |
2982 | { |
2983 | tree type = TREE_TYPE (binfo); | |
2984 | tree fields; | |
2985 | ||
2986 | for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) | |
2987 | { | |
2988 | if (TREE_CODE (fields) != FIELD_DECL) | |
2989 | continue; | |
2990 | ||
2991 | TREE_USED (fields) = 0; | |
2992 | if (DECL_NAME (fields) == NULL_TREE | |
2993 | && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE) | |
2994 | unuse_fields (TREE_TYPE (fields)); | |
2995 | } | |
d6479fe7 MM |
2996 | |
2997 | return NULL_TREE; | |
8d08fdba MS |
2998 | } |
2999 | ||
3000 | void | |
3001 | unuse_fields (type) | |
3002 | tree type; | |
3003 | { | |
d6479fe7 | 3004 | dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0); |
8d08fdba MS |
3005 | } |
3006 | ||
3007 | void | |
5566b478 | 3008 | pop_class_decls () |
8d08fdba MS |
3009 | { |
3010 | /* We haven't pushed a search level when dealing with cached classes, | |
3011 | so we'd better not try to pop it. */ | |
3012 | if (search_stack) | |
3013 | search_stack = pop_search_level (search_stack); | |
3014 | } | |
3015 | ||
8d08fdba MS |
3016 | void |
3017 | print_search_statistics () | |
3018 | { | |
3019 | #ifdef GATHER_STATISTICS | |
8d08fdba MS |
3020 | fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n", |
3021 | n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1); | |
3022 | fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n", | |
3023 | n_outer_fields_searched, n_calls_lookup_fnfields); | |
3024 | fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type); | |
fc378698 | 3025 | #else /* GATHER_STATISTICS */ |
8d08fdba | 3026 | fprintf (stderr, "no search statistics\n"); |
fc378698 | 3027 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
3028 | } |
3029 | ||
3030 | void | |
3031 | init_search_processing () | |
3032 | { | |
3033 | gcc_obstack_init (&search_obstack); | |
8d08fdba MS |
3034 | _vptr_name = get_identifier ("_vptr"); |
3035 | } | |
3036 | ||
3037 | void | |
3038 | reinit_search_statistics () | |
3039 | { | |
5566b478 | 3040 | #ifdef GATHER_STATISTICS |
8d08fdba MS |
3041 | n_fields_searched = 0; |
3042 | n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0; | |
3043 | n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0; | |
3044 | n_calls_get_base_type = 0; | |
3045 | n_outer_fields_searched = 0; | |
3046 | n_contexts_saved = 0; | |
fc378698 | 3047 | #endif /* GATHER_STATISTICS */ |
8d08fdba | 3048 | } |
e1cd6e56 | 3049 | |
e66d884e JM |
3050 | #define scratch_tree_cons expr_tree_cons |
3051 | ||
72c4a2a6 | 3052 | static tree |
7d4bdeed | 3053 | add_conversions (binfo, data) |
e1cd6e56 | 3054 | tree binfo; |
7d4bdeed | 3055 | void *data; |
e1cd6e56 | 3056 | { |
72b7eeff | 3057 | int i; |
fc378698 | 3058 | tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo)); |
7d4bdeed | 3059 | tree *conversions = (tree *) data; |
72b7eeff | 3060 | |
fc378698 | 3061 | for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i) |
72b7eeff | 3062 | { |
fc378698 | 3063 | tree tmp = TREE_VEC_ELT (method_vec, i); |
37b6eb34 | 3064 | tree name; |
61a127b3 | 3065 | |
aa45967f | 3066 | if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp))) |
72b7eeff | 3067 | break; |
72c4a2a6 | 3068 | |
37b6eb34 | 3069 | name = DECL_NAME (OVL_CURRENT (tmp)); |
59e76fc6 | 3070 | |
72c4a2a6 | 3071 | /* Make sure we don't already have this conversion. */ |
37b6eb34 | 3072 | if (! IDENTIFIER_MARKED (name)) |
72c4a2a6 | 3073 | { |
7d4bdeed | 3074 | *conversions = scratch_tree_cons (binfo, tmp, *conversions); |
37b6eb34 | 3075 | IDENTIFIER_MARKED (name) = 1; |
72c4a2a6 | 3076 | } |
72b7eeff | 3077 | } |
72c4a2a6 | 3078 | return NULL_TREE; |
e1cd6e56 MS |
3079 | } |
3080 | ||
3081 | tree | |
3082 | lookup_conversions (type) | |
3083 | tree type; | |
3084 | { | |
72c4a2a6 | 3085 | tree t; |
7d4bdeed | 3086 | tree conversions = NULL_TREE; |
72c4a2a6 | 3087 | |
e92cc029 | 3088 | if (TYPE_SIZE (type)) |
d6479fe7 | 3089 | bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions); |
72c4a2a6 JM |
3090 | |
3091 | for (t = conversions; t; t = TREE_CHAIN (t)) | |
37b6eb34 | 3092 | IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0; |
72c4a2a6 | 3093 | |
e1cd6e56 MS |
3094 | return conversions; |
3095 | } | |
6467930b | 3096 | |
d6479fe7 MM |
3097 | struct overlap_info |
3098 | { | |
3099 | tree compare_type; | |
3100 | int found_overlap; | |
3101 | }; | |
3102 | ||
732dcb6f JM |
3103 | /* Check whether the empty class indicated by EMPTY_BINFO is also present |
3104 | at offset 0 in COMPARE_TYPE, and set found_overlap if so. */ | |
3105 | ||
d6479fe7 MM |
3106 | static tree |
3107 | dfs_check_overlap (empty_binfo, data) | |
732dcb6f | 3108 | tree empty_binfo; |
d6479fe7 | 3109 | void *data; |
732dcb6f | 3110 | { |
d6479fe7 | 3111 | struct overlap_info *oi = (struct overlap_info *) data; |
732dcb6f | 3112 | tree binfo; |
d6479fe7 MM |
3113 | for (binfo = TYPE_BINFO (oi->compare_type); |
3114 | ; | |
3115 | binfo = BINFO_BASETYPE (binfo, 0)) | |
732dcb6f JM |
3116 | { |
3117 | if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo)) | |
3118 | { | |
d6479fe7 | 3119 | oi->found_overlap = 1; |
732dcb6f JM |
3120 | break; |
3121 | } | |
3122 | else if (BINFO_BASETYPES (binfo) == NULL_TREE) | |
3123 | break; | |
3124 | } | |
d6479fe7 MM |
3125 | |
3126 | return NULL_TREE; | |
732dcb6f JM |
3127 | } |
3128 | ||
3129 | /* Trivial function to stop base traversal when we find something. */ | |
3130 | ||
d6479fe7 MM |
3131 | static tree |
3132 | dfs_no_overlap_yet (binfo, data) | |
3133 | tree binfo; | |
3134 | void *data; | |
732dcb6f | 3135 | { |
d6479fe7 MM |
3136 | struct overlap_info *oi = (struct overlap_info *) data; |
3137 | return !oi->found_overlap ? binfo : NULL_TREE; | |
732dcb6f JM |
3138 | } |
3139 | ||
3140 | /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at | |
3141 | offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */ | |
3142 | ||
3143 | int | |
3144 | types_overlap_p (empty_type, next_type) | |
3145 | tree empty_type, next_type; | |
3146 | { | |
d6479fe7 MM |
3147 | struct overlap_info oi; |
3148 | ||
732dcb6f JM |
3149 | if (! IS_AGGR_TYPE (next_type)) |
3150 | return 0; | |
d6479fe7 MM |
3151 | oi.compare_type = next_type; |
3152 | oi.found_overlap = 0; | |
3153 | dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap, | |
3154 | dfs_no_overlap_yet, &oi); | |
3155 | return oi.found_overlap; | |
3156 | } | |
3157 | ||
3158 | struct bfv_info { | |
3159 | tree vbases; | |
3160 | tree var; | |
3161 | }; | |
3162 | ||
3163 | static tree | |
3164 | dfs_bfv_queue_p (binfo, data) | |
3165 | tree binfo; | |
3166 | void *data; | |
3167 | { | |
3168 | struct bfv_info *bfvi = (struct bfv_info *) data; | |
3169 | ||
3170 | /* Use the real virtual base class objects, not the placeholders in | |
3171 | the usual hierarchy. */ | |
3172 | if (TREE_VIA_VIRTUAL (binfo)) | |
3173 | return binfo_member (BINFO_TYPE (binfo), bfvi->vbases); | |
3174 | ||
3175 | return binfo; | |
732dcb6f | 3176 | } |
ca107ded | 3177 | |
70c532b5 MM |
3178 | /* Passed to dfs_walk_real by binfo_for_vtable; determine if bvtable |
3179 | comes from BINFO. */ | |
a1dd0d36 | 3180 | |
a1dd0d36 | 3181 | static tree |
d6479fe7 | 3182 | dfs_bfv_helper (binfo, data) |
a1dd0d36 | 3183 | tree binfo; |
d6479fe7 | 3184 | void *data; |
a1dd0d36 | 3185 | { |
d6479fe7 MM |
3186 | struct bfv_info *bfvi = (struct bfv_info *) data; |
3187 | ||
3188 | if (BINFO_VTABLE (binfo) == bfvi->var) | |
a1dd0d36 JM |
3189 | return binfo; |
3190 | return NULL_TREE; | |
3191 | } | |
3192 | ||
d6479fe7 | 3193 | /* Given a vtable VAR, determine which binfo it comes from. */ |
a1dd0d36 JM |
3194 | |
3195 | tree | |
d6479fe7 MM |
3196 | binfo_for_vtable (var) |
3197 | tree var; | |
a1dd0d36 | 3198 | { |
d6479fe7 MM |
3199 | tree type; |
3200 | struct bfv_info bfvi; | |
3201 | ||
3202 | type = DECL_CONTEXT (var); | |
3203 | bfvi.vbases = CLASSTYPE_VBASECLASSES (type); | |
3204 | return dfs_walk_real (TYPE_BINFO (type), | |
3205 | 0, dfs_bfv_helper, dfs_bfv_queue_p, &bfvi); | |
a1dd0d36 | 3206 | } |