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