]>
Commit | Line | Data |
---|---|---|
8d08fdba MS |
1 | /* Breadth-first and depth-first routines for |
2 | searching multiple-inheritance lattice for GNU C++. | |
956d6950 | 3 | Copyright (C) 1987, 89, 92-96, 1997 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" |
8d08fdba MS |
34 | |
35 | #define obstack_chunk_alloc xmalloc | |
36 | #define obstack_chunk_free free | |
37 | ||
8d08fdba | 38 | extern struct obstack *current_obstack; |
e8abc66f | 39 | extern tree abort_fndecl; |
8d08fdba MS |
40 | |
41 | #include "stack.h" | |
42 | ||
43 | /* Obstack used for remembering decision points of breadth-first. */ | |
e92cc029 | 44 | |
8d08fdba MS |
45 | static struct obstack search_obstack; |
46 | ||
47 | /* Methods for pushing and popping objects to and from obstacks. */ | |
e92cc029 | 48 | |
8d08fdba MS |
49 | struct stack_level * |
50 | push_stack_level (obstack, tp, size) | |
51 | struct obstack *obstack; | |
52 | char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */ | |
53 | int size; | |
54 | { | |
55 | struct stack_level *stack; | |
56 | obstack_grow (obstack, tp, size); | |
57 | stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size); | |
58 | obstack_finish (obstack); | |
59 | stack->obstack = obstack; | |
60 | stack->first = (tree *) obstack_base (obstack); | |
61 | stack->limit = obstack_room (obstack) / sizeof (tree *); | |
62 | return stack; | |
63 | } | |
64 | ||
65 | struct stack_level * | |
66 | pop_stack_level (stack) | |
67 | struct stack_level *stack; | |
68 | { | |
69 | struct stack_level *tem = stack; | |
70 | struct obstack *obstack = tem->obstack; | |
71 | stack = tem->prev; | |
72 | obstack_free (obstack, tem); | |
73 | return stack; | |
74 | } | |
75 | ||
76 | #define search_level stack_level | |
77 | static struct search_level *search_stack; | |
78 | ||
49c249e1 JM |
79 | static void clear_memoized_cache PROTO((void)); |
80 | static tree make_memoized_table_entry PROTO((tree, tree, int)); | |
81 | static tree get_abstract_virtuals_1 PROTO((tree, int, tree)); | |
82 | static tree get_vbase_1 PROTO((tree, tree, unsigned int *)); | |
83 | static tree convert_pointer_to_vbase PROTO((tree, tree)); | |
84 | static tree lookup_field_1 PROTO((tree, tree)); | |
85 | static tree convert_pointer_to_single_level PROTO((tree, tree)); | |
6b5fbb55 | 86 | static int lookup_fnfields_1 PROTO((tree, tree)); |
49c249e1 JM |
87 | static int lookup_fnfields_here PROTO((tree, tree)); |
88 | static int is_subobject_of_p PROTO((tree, tree)); | |
89 | static int hides PROTO((tree, tree)); | |
90 | static tree virtual_context PROTO((tree, tree, tree)); | |
91 | static tree get_template_base_recursive | |
92 | PROTO((tree, tree, tree, int)); | |
93 | static void dfs_walk PROTO((tree, void (*) (tree), int (*) (tree))); | |
69ac77ce JL |
94 | static void dfs_check_overlap PROTO((tree)); |
95 | static int dfs_no_overlap_yet PROTO((tree)); | |
49c249e1 JM |
96 | static void envelope_add_decl PROTO((tree, tree, tree *)); |
97 | static int get_base_distance_recursive | |
98 | PROTO((tree, int, int, int, int *, tree *, tree, tree *, | |
99 | int, int *, int, int)); | |
100 | static void expand_upcast_fixups | |
101 | PROTO((tree, tree, tree, tree, tree, tree, tree *)); | |
102 | static void fixup_virtual_upcast_offsets | |
103 | PROTO((tree, tree, int, int, tree, tree, tree, tree, | |
104 | tree *)); | |
105 | static int markedp PROTO((tree)); | |
106 | static int unmarkedp PROTO((tree)); | |
769467b9 | 107 | #ifdef MI_MATRIX |
49c249e1 JM |
108 | static int numberedp PROTO((tree)); |
109 | static int unnumberedp PROTO((tree)); | |
769467b9 | 110 | #endif |
49c249e1 JM |
111 | static int marked_vtable_pathp PROTO((tree)); |
112 | static int unmarked_vtable_pathp PROTO((tree)); | |
113 | static int marked_new_vtablep PROTO((tree)); | |
114 | static int unmarked_new_vtablep PROTO((tree)); | |
115 | static int dfs_debug_unmarkedp PROTO((tree)); | |
769467b9 | 116 | #ifdef MI_MATRIX |
49c249e1 JM |
117 | static void dfs_number PROTO((tree)); |
118 | static void dfs_unnumber PROTO((tree)); | |
769467b9 JL |
119 | static void dfs_record_inheritance PROTO((tree)); |
120 | #endif | |
49c249e1 JM |
121 | static void dfs_debug_mark PROTO((tree)); |
122 | static void dfs_find_vbases PROTO((tree)); | |
123 | static void dfs_clear_vbase_slots PROTO((tree)); | |
124 | static void dfs_unmark PROTO((tree)); | |
125 | static void dfs_init_vbase_pointers PROTO((tree)); | |
126 | static void dfs_get_vbase_types PROTO((tree)); | |
49c249e1 JM |
127 | static void dfs_pushdecls PROTO((tree)); |
128 | static void dfs_compress_decls PROTO((tree)); | |
129 | static void dfs_unuse_fields PROTO((tree)); | |
130 | static void add_conversions PROTO((tree)); | |
131 | static tree get_virtuals_named_this PROTO((tree)); | |
132 | static tree get_virtual_destructor PROTO((tree, int)); | |
133 | static int tree_has_any_destructor_p PROTO((tree, int)); | |
134 | static struct search_level *push_search_level | |
135 | PROTO((struct stack_level *, struct obstack *)); | |
136 | static struct search_level *pop_search_level | |
137 | PROTO((struct stack_level *)); | |
138 | static struct type_level *push_type_level | |
139 | PROTO((struct stack_level *, struct obstack *)); | |
140 | static struct type_level *pop_type_level | |
141 | PROTO((struct type_level *)); | |
142 | static tree my_tree_cons PROTO((tree, tree, tree)); | |
143 | static tree my_build_string PROTO((char *)); | |
144 | static struct memoized_entry * my_new_memoized_entry | |
145 | PROTO((struct memoized_entry *)); | |
146 | static HOST_WIDE_INT breadth_first_search | |
147 | PROTO((tree, int (*) (tree, int), int (*) (tree, int))); | |
8d08fdba MS |
148 | |
149 | static tree vbase_types; | |
5566b478 | 150 | static tree vbase_decl_ptr_intermediate, vbase_decl_ptr; |
8d08fdba MS |
151 | static tree vbase_init_result; |
152 | ||
153 | /* Allocate a level of searching. */ | |
e92cc029 | 154 | |
8d08fdba MS |
155 | static struct search_level * |
156 | push_search_level (stack, obstack) | |
157 | struct stack_level *stack; | |
158 | struct obstack *obstack; | |
159 | { | |
160 | struct search_level tem; | |
161 | ||
162 | tem.prev = stack; | |
163 | return push_stack_level (obstack, (char *)&tem, sizeof (tem)); | |
164 | } | |
165 | ||
166 | /* Discard a level of search allocation. */ | |
e92cc029 | 167 | |
8d08fdba MS |
168 | static struct search_level * |
169 | pop_search_level (obstack) | |
170 | struct stack_level *obstack; | |
171 | { | |
172 | register struct search_level *stack = pop_stack_level (obstack); | |
173 | ||
174 | return stack; | |
175 | } | |
176 | \f | |
177 | /* Search memoization. */ | |
e92cc029 | 178 | |
8d08fdba MS |
179 | struct type_level |
180 | { | |
181 | struct stack_level base; | |
182 | ||
183 | /* First object allocated in obstack of entries. */ | |
184 | char *entries; | |
185 | ||
186 | /* Number of types memoized in this context. */ | |
187 | int len; | |
188 | ||
189 | /* Type being memoized; save this if we are saving | |
190 | memoized contexts. */ | |
191 | tree type; | |
192 | }; | |
193 | ||
194 | /* Obstack used for memoizing member and member function lookup. */ | |
195 | ||
196 | static struct obstack type_obstack, type_obstack_entries; | |
197 | static struct type_level *type_stack; | |
198 | static tree _vptr_name; | |
199 | ||
200 | /* Make things that look like tree nodes, but allocate them | |
201 | on type_obstack_entries. */ | |
202 | static int my_tree_node_counter; | |
8d08fdba MS |
203 | |
204 | extern int flag_memoize_lookups, flag_save_memoized_contexts; | |
205 | ||
206 | /* Variables for gathering statistics. */ | |
207 | static int my_memoized_entry_counter; | |
208 | static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2]; | |
209 | static int memoized_fields_searched[2]; | |
5566b478 | 210 | #ifdef GATHER_STATISTICS |
8d08fdba MS |
211 | static int n_fields_searched; |
212 | static int n_calls_lookup_field, n_calls_lookup_field_1; | |
213 | static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1; | |
214 | static int n_calls_get_base_type; | |
215 | static int n_outer_fields_searched; | |
216 | static int n_contexts_saved; | |
fc378698 | 217 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
218 | |
219 | /* Local variables to help save memoization contexts. */ | |
220 | static tree prev_type_memoized; | |
221 | static struct type_level *prev_type_stack; | |
222 | ||
223 | /* This list is used by push_class_decls to know what decls need to | |
224 | be pushed into class scope. */ | |
225 | static tree closed_envelopes = NULL_TREE; | |
226 | ||
227 | /* Allocate a level of type memoization context. */ | |
e92cc029 | 228 | |
8d08fdba MS |
229 | static struct type_level * |
230 | push_type_level (stack, obstack) | |
231 | struct stack_level *stack; | |
232 | struct obstack *obstack; | |
233 | { | |
234 | struct type_level tem; | |
235 | ||
236 | tem.base.prev = stack; | |
237 | ||
238 | obstack_finish (&type_obstack_entries); | |
239 | tem.entries = (char *) obstack_base (&type_obstack_entries); | |
240 | tem.len = 0; | |
241 | tem.type = NULL_TREE; | |
242 | ||
243 | return (struct type_level *)push_stack_level (obstack, (char *)&tem, sizeof (tem)); | |
244 | } | |
245 | ||
246 | /* Discard a level of type memoization context. */ | |
247 | ||
248 | static struct type_level * | |
249 | pop_type_level (stack) | |
250 | struct type_level *stack; | |
251 | { | |
252 | obstack_free (&type_obstack_entries, stack->entries); | |
253 | return (struct type_level *)pop_stack_level ((struct stack_level *)stack); | |
254 | } | |
255 | ||
256 | /* Make something that looks like a TREE_LIST, but | |
257 | do it on the type_obstack_entries obstack. */ | |
e92cc029 | 258 | |
8d08fdba MS |
259 | static tree |
260 | my_tree_cons (purpose, value, chain) | |
261 | tree purpose, value, chain; | |
262 | { | |
263 | tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list)); | |
264 | ++my_tree_node_counter; | |
265 | TREE_TYPE (p) = NULL_TREE; | |
936f0e02 JL |
266 | /* The type of the last on the LHS of this statement must be a pointer |
267 | to the same type as the bitfields in struct tree_common. Otherwise | |
268 | we may write beyond our intended area. */ | |
269 | ((unsigned *)p)[3] = 0; | |
8d08fdba MS |
270 | TREE_SET_CODE (p, TREE_LIST); |
271 | TREE_PURPOSE (p) = purpose; | |
272 | TREE_VALUE (p) = value; | |
273 | TREE_CHAIN (p) = chain; | |
274 | return p; | |
275 | } | |
276 | ||
277 | static tree | |
278 | my_build_string (str) | |
279 | char *str; | |
280 | { | |
281 | tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string)); | |
282 | ++my_tree_node_counter; | |
283 | TREE_TYPE (p) = 0; | |
284 | ((int *)p)[3] = 0; | |
285 | TREE_SET_CODE (p, STRING_CST); | |
286 | TREE_STRING_POINTER (p) = str; | |
287 | TREE_STRING_LENGTH (p) = strlen (str); | |
288 | return p; | |
289 | } | |
290 | \f | |
291 | /* Memoizing machinery to make searches for multiple inheritance | |
292 | reasonably efficient. */ | |
e92cc029 | 293 | |
8d08fdba MS |
294 | #define MEMOIZE_HASHSIZE 8 |
295 | typedef struct memoized_entry | |
296 | { | |
297 | struct memoized_entry *chain; | |
298 | int uid; | |
299 | tree data_members[MEMOIZE_HASHSIZE]; | |
300 | tree function_members[MEMOIZE_HASHSIZE]; | |
301 | } *ME; | |
302 | ||
303 | #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain) | |
304 | #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid) | |
305 | #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX]) | |
306 | #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX]) | |
307 | /* The following is probably a lousy hash function. */ | |
308 | #define MEMOIZED_HASH_FN(NODE) (((long)(NODE)>>4)&(MEMOIZE_HASHSIZE - 1)) | |
309 | ||
310 | static struct memoized_entry * | |
311 | my_new_memoized_entry (chain) | |
312 | struct memoized_entry *chain; | |
313 | { | |
beb53fb8 JM |
314 | struct memoized_entry *p |
315 | = (struct memoized_entry *)obstack_alloc (&type_obstack_entries, | |
316 | sizeof (struct memoized_entry)); | |
1daa5dd8 | 317 | bzero ((char *) p, sizeof (struct memoized_entry)); |
8d08fdba MS |
318 | MEMOIZED_CHAIN (p) = chain; |
319 | MEMOIZED_UID (p) = ++my_memoized_entry_counter; | |
320 | return p; | |
321 | } | |
322 | ||
6b5fbb55 | 323 | /* Clears the deferred pop from pop_memoized_context, if any. */ |
e92cc029 | 324 | |
6b5fbb55 MS |
325 | static void |
326 | clear_memoized_cache () | |
327 | { | |
328 | if (prev_type_stack) | |
329 | { | |
330 | type_stack = pop_type_level (prev_type_stack); | |
331 | prev_type_memoized = 0; | |
332 | prev_type_stack = 0; | |
333 | } | |
334 | } | |
335 | ||
8d08fdba MS |
336 | /* Make an entry in the memoized table for type TYPE |
337 | that the entry for NAME is FIELD. */ | |
338 | ||
5566b478 | 339 | static tree |
8d08fdba MS |
340 | make_memoized_table_entry (type, name, function_p) |
341 | tree type, name; | |
342 | int function_p; | |
343 | { | |
e92cc029 | 344 | int idx = MEMOIZED_HASH_FN (name); |
8d08fdba MS |
345 | tree entry, *prev_entry; |
346 | ||
6b5fbb55 MS |
347 | /* Since we allocate from the type_obstack, we must pop any deferred |
348 | levels. */ | |
349 | clear_memoized_cache (); | |
350 | ||
8d08fdba MS |
351 | memoized_adds[function_p] += 1; |
352 | if (CLASSTYPE_MTABLE_ENTRY (type) == 0) | |
353 | { | |
354 | obstack_ptr_grow (&type_obstack, type); | |
355 | obstack_blank (&type_obstack, sizeof (struct memoized_entry *)); | |
356 | CLASSTYPE_MTABLE_ENTRY (type) = (char *)my_new_memoized_entry ((struct memoized_entry *)0); | |
357 | type_stack->len++; | |
358 | if (type_stack->len * 2 >= type_stack->base.limit) | |
359 | my_friendly_abort (88); | |
360 | } | |
361 | if (function_p) | |
e92cc029 | 362 | prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), idx); |
8d08fdba | 363 | else |
e92cc029 | 364 | prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), idx); |
8d08fdba MS |
365 | |
366 | entry = my_tree_cons (name, NULL_TREE, *prev_entry); | |
367 | *prev_entry = entry; | |
368 | ||
369 | /* Don't know the error message to give yet. */ | |
370 | TREE_TYPE (entry) = error_mark_node; | |
371 | ||
372 | return entry; | |
373 | } | |
374 | ||
375 | /* When a new function or class context is entered, we build | |
376 | a table of types which have been searched for members. | |
377 | The table is an array (obstack) of types. When a type is | |
378 | entered into the obstack, its CLASSTYPE_MTABLE_ENTRY | |
379 | field is set to point to a new record, of type struct memoized_entry. | |
380 | ||
381 | A non-NULL TREE_TYPE of the entry contains an access control error message. | |
382 | ||
383 | The slots for the data members are arrays of tree nodes. | |
384 | These tree nodes are lists, with the TREE_PURPOSE | |
385 | of this list the known member name, and the TREE_VALUE | |
386 | as the FIELD_DECL for the member. | |
387 | ||
388 | For member functions, the TREE_PURPOSE is again the | |
389 | name of the member functions for that class, | |
390 | and the TREE_VALUE of the list is a pairs | |
391 | whose TREE_PURPOSE is a member functions of this name, | |
392 | and whose TREE_VALUE is a list of known argument lists this | |
393 | member function has been called with. The TREE_TYPE of the pair, | |
394 | if non-NULL, is an error message to print. */ | |
395 | ||
396 | /* Tell search machinery that we are entering a new context, and | |
397 | to update tables appropriately. | |
398 | ||
399 | TYPE is the type of the context we are entering, which can | |
400 | be NULL_TREE if we are not in a class's scope. | |
401 | ||
402 | USE_OLD, if nonzero tries to use previous context. */ | |
e92cc029 | 403 | |
8d08fdba MS |
404 | void |
405 | push_memoized_context (type, use_old) | |
406 | tree type; | |
407 | int use_old; | |
408 | { | |
409 | int len; | |
410 | tree *tem; | |
411 | ||
412 | if (prev_type_stack) | |
413 | { | |
414 | if (use_old && prev_type_memoized == type) | |
415 | { | |
416 | #ifdef GATHER_STATISTICS | |
417 | n_contexts_saved++; | |
fc378698 | 418 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
419 | type_stack = prev_type_stack; |
420 | prev_type_stack = 0; | |
421 | ||
422 | tem = &type_stack->base.first[0]; | |
423 | len = type_stack->len; | |
424 | while (len--) | |
425 | CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = (char *)tem[len*2+1]; | |
426 | return; | |
427 | } | |
428 | /* Otherwise, need to pop old stack here. */ | |
6b5fbb55 | 429 | clear_memoized_cache (); |
8d08fdba MS |
430 | } |
431 | ||
432 | type_stack = push_type_level ((struct stack_level *)type_stack, | |
433 | &type_obstack); | |
434 | type_stack->type = type; | |
435 | } | |
436 | ||
437 | /* Tell search machinery that we have left a context. | |
438 | We do not currently save these contexts for later use. | |
439 | If we wanted to, we could not use pop_search_level, since | |
440 | poping that level allows the data we have collected to | |
441 | be clobbered; a stack of obstacks would be needed. */ | |
e92cc029 | 442 | |
8d08fdba MS |
443 | void |
444 | pop_memoized_context (use_old) | |
445 | int use_old; | |
446 | { | |
447 | int len; | |
448 | tree *tem = &type_stack->base.first[0]; | |
449 | ||
450 | if (! flag_save_memoized_contexts) | |
451 | use_old = 0; | |
452 | else if (use_old) | |
453 | { | |
454 | len = type_stack->len; | |
455 | while (len--) | |
456 | tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]); | |
457 | ||
6b5fbb55 MS |
458 | /* If there was a deferred pop, we need to pop it now. */ |
459 | clear_memoized_cache (); | |
460 | ||
8d08fdba MS |
461 | prev_type_stack = type_stack; |
462 | prev_type_memoized = type_stack->type; | |
463 | } | |
464 | ||
465 | if (flag_memoize_lookups) | |
466 | { | |
467 | len = type_stack->len; | |
468 | while (len--) | |
469 | CLASSTYPE_MTABLE_ENTRY (tem[len*2]) | |
470 | = (char *)MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2])); | |
471 | } | |
472 | if (! use_old) | |
473 | type_stack = pop_type_level (type_stack); | |
474 | else | |
475 | type_stack = (struct type_level *)type_stack->base.prev; | |
476 | } | |
477 | \f | |
acc9fe20 RK |
478 | /* Get a virtual binfo that is found inside BINFO's hierarchy that is |
479 | the same type as the type given in PARENT. To be optimal, we want | |
480 | the first one that is found by going through the least number of | |
bd6dd845 | 481 | virtual bases. */ |
e92cc029 | 482 | |
acc9fe20 | 483 | static tree |
bd6dd845 | 484 | get_vbase_1 (parent, binfo, depth) |
acc9fe20 RK |
485 | tree parent, binfo; |
486 | unsigned int *depth; | |
487 | { | |
488 | tree binfos; | |
489 | int i, n_baselinks; | |
490 | tree rval = NULL_TREE; | |
491 | ||
acc9fe20 RK |
492 | if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo)) |
493 | { | |
494 | *depth = 0; | |
495 | return binfo; | |
496 | } | |
497 | ||
498 | *depth = *depth - 1; | |
499 | ||
500 | binfos = BINFO_BASETYPES (binfo); | |
501 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
502 | ||
503 | /* Process base types. */ | |
504 | for (i = 0; i < n_baselinks; i++) | |
505 | { | |
506 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
507 | tree nrval; | |
508 | ||
509 | if (*depth == 0) | |
510 | break; | |
511 | ||
bd6dd845 | 512 | nrval = get_vbase_1 (parent, base_binfo, depth); |
acc9fe20 RK |
513 | if (nrval) |
514 | rval = nrval; | |
515 | } | |
516 | *depth = *depth+1; | |
517 | return rval; | |
518 | } | |
519 | ||
bd6dd845 MS |
520 | tree |
521 | get_vbase (parent, binfo) | |
522 | tree parent; | |
523 | tree binfo; | |
524 | { | |
525 | unsigned int d = (unsigned int)-1; | |
526 | return get_vbase_1 (parent, binfo, &d); | |
527 | } | |
528 | ||
f30432d7 MS |
529 | /* Convert EXPR to a virtual base class of type TYPE. We know that |
530 | EXPR is a non-null POINTER_TYPE to RECORD_TYPE. We also know that | |
531 | the type of what expr points to has a virtual base of type TYPE. */ | |
e92cc029 | 532 | |
bd6dd845 | 533 | static tree |
f30432d7 MS |
534 | convert_pointer_to_vbase (type, expr) |
535 | tree type; | |
536 | tree expr; | |
537 | { | |
bd6dd845 | 538 | tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)))); |
f30432d7 MS |
539 | return convert_pointer_to_real (vb, expr); |
540 | } | |
541 | ||
8d08fdba MS |
542 | /* Check whether the type given in BINFO is derived from PARENT. If |
543 | it isn't, return 0. If it is, but the derivation is MI-ambiguous | |
544 | AND protect != 0, emit an error message and return error_mark_node. | |
545 | ||
546 | Otherwise, if TYPE is derived from PARENT, return the actual base | |
547 | information, unless a one of the protection violations below | |
548 | occurs, in which case emit an error message and return error_mark_node. | |
549 | ||
550 | If PROTECT is 1, then check if access to a public field of PARENT | |
551 | would be private. Also check for ambiguity. */ | |
552 | ||
553 | tree | |
554 | get_binfo (parent, binfo, protect) | |
555 | register tree parent, binfo; | |
556 | int protect; | |
557 | { | |
a703fb38 | 558 | tree type = NULL_TREE; |
8d08fdba MS |
559 | int dist; |
560 | tree rval = NULL_TREE; | |
561 | ||
562 | if (TREE_CODE (parent) == TREE_VEC) | |
563 | parent = BINFO_TYPE (parent); | |
71851aaa | 564 | else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent))) |
8d08fdba MS |
565 | my_friendly_abort (89); |
566 | ||
567 | if (TREE_CODE (binfo) == TREE_VEC) | |
568 | type = BINFO_TYPE (binfo); | |
71851aaa | 569 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo))) |
8d08fdba MS |
570 | type = binfo; |
571 | else | |
572 | my_friendly_abort (90); | |
573 | ||
574 | dist = get_base_distance (parent, binfo, protect, &rval); | |
575 | ||
576 | if (dist == -3) | |
577 | { | |
578 | cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance", | |
579 | parent, type); | |
580 | return error_mark_node; | |
581 | } | |
582 | else if (dist == -2 && protect) | |
583 | { | |
584 | cp_error ("type `%T' is ambiguous base class for type `%T'", parent, | |
585 | type); | |
586 | return error_mark_node; | |
587 | } | |
588 | ||
589 | return rval; | |
590 | } | |
591 | ||
592 | /* This is the newer depth first get_base_distance routine. */ | |
e92cc029 | 593 | |
8926095f | 594 | static int |
5566b478 | 595 | get_base_distance_recursive (binfo, depth, is_private, rval, |
8d08fdba | 596 | rval_private_ptr, new_binfo_ptr, parent, path_ptr, |
6b5fbb55 MS |
597 | protect, via_virtual_ptr, via_virtual, |
598 | current_scope_in_chain) | |
599 | tree binfo; | |
600 | int depth, is_private, rval; | |
601 | int *rval_private_ptr; | |
602 | tree *new_binfo_ptr, parent, *path_ptr; | |
603 | int protect, *via_virtual_ptr, via_virtual; | |
604 | int current_scope_in_chain; | |
8d08fdba MS |
605 | { |
606 | tree binfos; | |
607 | int i, n_baselinks; | |
608 | ||
6b5fbb55 MS |
609 | if (protect |
610 | && !current_scope_in_chain | |
611 | && is_friend (BINFO_TYPE (binfo), current_scope ())) | |
612 | current_scope_in_chain = 1; | |
613 | ||
8d08fdba MS |
614 | if (BINFO_TYPE (binfo) == parent || binfo == parent) |
615 | { | |
616 | if (rval == -1) | |
617 | { | |
618 | rval = depth; | |
619 | *rval_private_ptr = is_private; | |
620 | *new_binfo_ptr = binfo; | |
621 | *via_virtual_ptr = via_virtual; | |
622 | } | |
623 | else | |
624 | { | |
51c184be MS |
625 | int same_object = (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr), |
626 | BINFO_OFFSET (binfo)) | |
627 | && *via_virtual_ptr && via_virtual); | |
628 | ||
8d08fdba MS |
629 | if (*via_virtual_ptr && via_virtual==0) |
630 | { | |
631 | *rval_private_ptr = is_private; | |
632 | *new_binfo_ptr = binfo; | |
633 | *via_virtual_ptr = via_virtual; | |
634 | } | |
635 | else if (same_object) | |
636 | { | |
637 | if (*rval_private_ptr && ! is_private) | |
638 | { | |
639 | *rval_private_ptr = is_private; | |
640 | *new_binfo_ptr = binfo; | |
641 | *via_virtual_ptr = via_virtual; | |
642 | } | |
643 | return rval; | |
644 | } | |
645 | ||
646 | rval = -2; | |
647 | } | |
648 | return rval; | |
649 | } | |
650 | ||
651 | binfos = BINFO_BASETYPES (binfo); | |
652 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
653 | depth += 1; | |
654 | ||
655 | /* Process base types. */ | |
656 | for (i = 0; i < n_baselinks; i++) | |
657 | { | |
658 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
659 | ||
660 | /* Find any specific instance of a virtual base, when searching with | |
e92cc029 | 661 | a binfo... */ |
8d08fdba MS |
662 | if (BINFO_MARKED (base_binfo) == 0 || TREE_CODE (parent) == TREE_VEC) |
663 | { | |
664 | int via_private | |
665 | = (protect | |
666 | && (is_private | |
667 | || (!TREE_VIA_PUBLIC (base_binfo) | |
6b5fbb55 MS |
668 | && !(TREE_VIA_PROTECTED (base_binfo) |
669 | && current_scope_in_chain) | |
8d08fdba MS |
670 | && !is_friend (BINFO_TYPE (binfo), current_scope ())))); |
671 | int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo); | |
672 | int was; | |
673 | ||
674 | /* When searching for a non-virtual, we cannot mark | |
e92cc029 | 675 | virtually found binfos. */ |
8d08fdba MS |
676 | if (! this_virtual) |
677 | SET_BINFO_MARKED (base_binfo); | |
678 | ||
679 | #define WATCH_VALUES(rval, via_private) (rval == -1 ? 3 : via_private) | |
680 | ||
681 | was = WATCH_VALUES (rval, *via_virtual_ptr); | |
682 | rval = get_base_distance_recursive (base_binfo, depth, via_private, | |
5566b478 | 683 | rval, rval_private_ptr, |
8d08fdba MS |
684 | new_binfo_ptr, parent, path_ptr, |
685 | protect, via_virtual_ptr, | |
6b5fbb55 MS |
686 | this_virtual, |
687 | current_scope_in_chain); | |
e92cc029 | 688 | /* watch for updates; only update if path is good. */ |
8d08fdba MS |
689 | if (path_ptr && WATCH_VALUES (rval, *via_virtual_ptr) != was) |
690 | BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; | |
691 | if (rval == -2 && *via_virtual_ptr == 0) | |
692 | return rval; | |
693 | ||
694 | #undef WATCH_VALUES | |
695 | ||
696 | } | |
697 | } | |
698 | ||
699 | return rval; | |
700 | } | |
701 | ||
702 | /* Return the number of levels between type PARENT and the type given | |
703 | in BINFO, following the leftmost path to PARENT not found along a | |
704 | virtual path, if there are no real PARENTs (all come from virtual | |
705 | base classes), then follow the leftmost path to PARENT. | |
706 | ||
707 | Return -1 if TYPE is not derived from PARENT. | |
708 | Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is | |
709 | non-negative. | |
710 | Return -3 if PARENT is private to TYPE, and PROTECT is non-zero. | |
711 | ||
712 | If PATH_PTR is non-NULL, then also build the list of types | |
ddd5a7c1 | 713 | from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC |
8d08fdba MS |
714 | set. |
715 | ||
716 | PARENT can also be a binfo, in which case that exact parent is found | |
717 | and no other. convert_pointer_to_real uses this functionality. | |
718 | ||
39211cd5 | 719 | If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone. */ |
8d08fdba MS |
720 | |
721 | int | |
722 | get_base_distance (parent, binfo, protect, path_ptr) | |
723 | register tree parent, binfo; | |
724 | int protect; | |
725 | tree *path_ptr; | |
726 | { | |
8d08fdba | 727 | int rval; |
8d08fdba | 728 | int rval_private = 0; |
a703fb38 | 729 | tree type = NULL_TREE; |
8d08fdba MS |
730 | tree new_binfo = NULL_TREE; |
731 | int via_virtual; | |
732 | int watch_access = protect; | |
733 | ||
5566b478 | 734 | /* Should we be completing types here? */ |
8d08fdba | 735 | if (TREE_CODE (parent) != TREE_VEC) |
5566b478 MS |
736 | parent = complete_type (TYPE_MAIN_VARIANT (parent)); |
737 | else | |
738 | complete_type (TREE_TYPE (parent)); | |
8d08fdba MS |
739 | |
740 | if (TREE_CODE (binfo) == TREE_VEC) | |
741 | type = BINFO_TYPE (binfo); | |
f0e01782 | 742 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo))) |
8d08fdba | 743 | { |
e92cc029 | 744 | type = complete_type (binfo); |
8d08fdba MS |
745 | binfo = TYPE_BINFO (type); |
746 | ||
747 | if (path_ptr) | |
748 | BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE; | |
749 | } | |
750 | else | |
751 | my_friendly_abort (92); | |
752 | ||
753 | if (parent == type || parent == binfo) | |
754 | { | |
755 | /* If the distance is 0, then we don't really need | |
756 | a path pointer, but we shouldn't let garbage go back. */ | |
757 | if (path_ptr) | |
758 | *path_ptr = binfo; | |
759 | return 0; | |
760 | } | |
761 | ||
762 | if (path_ptr) | |
763 | watch_access = 1; | |
764 | ||
5566b478 | 765 | rval = get_base_distance_recursive (binfo, 0, 0, -1, |
8d08fdba | 766 | &rval_private, &new_binfo, parent, |
6b5fbb55 MS |
767 | path_ptr, watch_access, &via_virtual, 0, |
768 | 0); | |
8d08fdba MS |
769 | |
770 | dfs_walk (binfo, dfs_unmark, markedp); | |
771 | ||
772 | /* Access restrictions don't count if we found an ambiguous basetype. */ | |
773 | if (rval == -2 && protect >= 0) | |
774 | rval_private = 0; | |
775 | ||
776 | if (rval && protect && rval_private) | |
777 | return -3; | |
778 | ||
e92cc029 | 779 | /* find real virtual base classes. */ |
39211cd5 MS |
780 | if (rval == -1 && TREE_CODE (parent) == TREE_VEC |
781 | && parent == binfo_member (BINFO_TYPE (parent), | |
782 | CLASSTYPE_VBASECLASSES (type))) | |
783 | { | |
784 | BINFO_INHERITANCE_CHAIN (parent) = binfo; | |
785 | new_binfo = parent; | |
786 | rval = 1; | |
787 | } | |
788 | ||
8d08fdba MS |
789 | if (path_ptr) |
790 | *path_ptr = new_binfo; | |
791 | return rval; | |
792 | } | |
793 | ||
794 | /* Search for a member with name NAME in a multiple inheritance lattice | |
795 | specified by TYPE. If it does not exist, return NULL_TREE. | |
796 | If the member is ambiguously referenced, return `error_mark_node'. | |
797 | Otherwise, return the FIELD_DECL. */ | |
798 | ||
799 | /* Do a 1-level search for NAME as a member of TYPE. The caller must | |
800 | figure out whether it can access this field. (Since it is only one | |
801 | level, this is reasonable.) */ | |
e92cc029 | 802 | |
8d08fdba MS |
803 | static tree |
804 | lookup_field_1 (type, name) | |
805 | tree type, name; | |
806 | { | |
f84b4be9 JM |
807 | register tree field; |
808 | ||
809 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM | |
810 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
811 | /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM are not fields at all; | |
812 | instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously, | |
813 | the code often worked even when we treated the index as a list | |
814 | of fields!) */ | |
815 | return NULL_TREE; | |
816 | ||
817 | field = TYPE_FIELDS (type); | |
8d08fdba MS |
818 | |
819 | #ifdef GATHER_STATISTICS | |
820 | n_calls_lookup_field_1++; | |
fc378698 | 821 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
822 | while (field) |
823 | { | |
824 | #ifdef GATHER_STATISTICS | |
825 | n_fields_searched++; | |
fc378698 | 826 | #endif /* GATHER_STATISTICS */ |
f84b4be9 | 827 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (field)) == 'd', 0); |
8d08fdba MS |
828 | if (DECL_NAME (field) == NULL_TREE |
829 | && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
830 | { | |
831 | tree temp = lookup_field_1 (TREE_TYPE (field), name); | |
832 | if (temp) | |
833 | return temp; | |
834 | } | |
835 | if (DECL_NAME (field) == name) | |
836 | { | |
837 | if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL) | |
838 | && DECL_ASSEMBLER_NAME (field) != NULL) | |
839 | GNU_xref_ref(current_function_decl, | |
840 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field))); | |
841 | return field; | |
842 | } | |
843 | field = TREE_CHAIN (field); | |
844 | } | |
845 | /* Not found. */ | |
846 | if (name == _vptr_name) | |
847 | { | |
848 | /* Give the user what s/he thinks s/he wants. */ | |
849 | if (TYPE_VIRTUAL_P (type)) | |
850 | return CLASSTYPE_VFIELD (type); | |
851 | } | |
852 | return NULL_TREE; | |
853 | } | |
854 | ||
7177d104 MS |
855 | /* There are a number of cases we need to be aware of here: |
856 | current_class_type current_function_decl | |
e92cc029 MS |
857 | global NULL NULL |
858 | fn-local NULL SET | |
859 | class-local SET NULL | |
860 | class->fn SET SET | |
861 | fn->class SET SET | |
7177d104 MS |
862 | |
863 | Those last two make life interesting. If we're in a function which is | |
864 | itself inside a class, we need decls to go into the fn's decls (our | |
865 | second case below). But if we're in a class and the class itself is | |
866 | inside a function, we need decls to go into the decls for the class. To | |
4ac14744 | 867 | achieve this last goal, we must see if, when both current_class_ptr and |
7177d104 MS |
868 | current_function_decl are set, the class was declared inside that |
869 | function. If so, we know to put the decls into the class's scope. */ | |
870 | ||
8d08fdba MS |
871 | tree |
872 | current_scope () | |
873 | { | |
874 | if (current_function_decl == NULL_TREE) | |
875 | return current_class_type; | |
876 | if (current_class_type == NULL_TREE) | |
877 | return current_function_decl; | |
878 | if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type) | |
879 | return current_function_decl; | |
880 | ||
881 | return current_class_type; | |
882 | } | |
883 | ||
884 | /* Compute the access of FIELD. This is done by computing | |
885 | the access available to each type in BASETYPES (which comes | |
886 | as a list of [via_public/basetype] in reverse order, namely base | |
887 | class before derived class). The first one which defines a | |
888 | access defines the access for the field. Otherwise, the | |
889 | access of the field is that which occurs normally. | |
890 | ||
891 | Uses global variables CURRENT_CLASS_TYPE and | |
892 | CURRENT_FUNCTION_DECL to use friend relationships | |
893 | if necessary. | |
894 | ||
895 | This will be static when lookup_fnfield comes into this file. | |
896 | ||
be99da77 | 897 | access_public_node means that the field can be accessed by the current lexical |
8d08fdba MS |
898 | scope. |
899 | ||
be99da77 | 900 | access_protected_node means that the field cannot be accessed by the current |
8d08fdba MS |
901 | lexical scope because it is protected. |
902 | ||
be99da77 | 903 | access_private_node means that the field cannot be accessed by the current |
e92cc029 | 904 | lexical scope because it is private. */ |
8d08fdba MS |
905 | |
906 | #if 0 | |
be99da77 MS |
907 | #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public_node |
908 | #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected_node | |
909 | #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private_node | |
8d08fdba | 910 | #else |
be99da77 MS |
911 | #define PUBLIC_RETURN return access_public_node |
912 | #define PROTECTED_RETURN return access_protected_node | |
913 | #define PRIVATE_RETURN return access_private_node | |
8d08fdba MS |
914 | #endif |
915 | ||
916 | #if 0 | |
917 | /* Disabled with DECL_PUBLIC &c. */ | |
918 | static tree previous_scope = NULL_TREE; | |
919 | #endif | |
920 | ||
be99da77 | 921 | tree |
8d08fdba MS |
922 | compute_access (basetype_path, field) |
923 | tree basetype_path, field; | |
924 | { | |
be99da77 | 925 | tree access; |
8d08fdba MS |
926 | tree types; |
927 | tree context; | |
928 | int protected_ok, via_protected; | |
eae89e04 | 929 | extern int flag_access_control; |
8d08fdba MS |
930 | #if 1 |
931 | /* Replaces static decl above. */ | |
932 | tree previous_scope; | |
933 | #endif | |
beb53fb8 JM |
934 | int static_mem |
935 | = ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field)) | |
936 | || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field))); | |
8d08fdba | 937 | |
eae89e04 | 938 | if (! flag_access_control) |
be99da77 | 939 | return access_public_node; |
eae89e04 | 940 | |
8d08fdba MS |
941 | /* The field lives in the current class. */ |
942 | if (BINFO_TYPE (basetype_path) == current_class_type) | |
be99da77 | 943 | return access_public_node; |
8d08fdba MS |
944 | |
945 | #if 0 | |
946 | /* Disabled until pushing function scope clears these out. If ever. */ | |
947 | /* Make these special cases fast. */ | |
948 | if (current_scope () == previous_scope) | |
949 | { | |
950 | if (DECL_PUBLIC (field)) | |
be99da77 | 951 | return access_public_node; |
8d08fdba | 952 | if (DECL_PROTECTED (field)) |
be99da77 | 953 | return access_protected_node; |
8d08fdba | 954 | if (DECL_PRIVATE (field)) |
be99da77 | 955 | return access_private_node; |
8d08fdba MS |
956 | } |
957 | #endif | |
958 | ||
faae18ab MS |
959 | /* We don't currently support access control on nested types. */ |
960 | if (TREE_CODE (field) == TYPE_DECL) | |
be99da77 | 961 | return access_public_node; |
faae18ab | 962 | |
8d08fdba MS |
963 | previous_scope = current_scope (); |
964 | ||
965 | context = DECL_CLASS_CONTEXT (field); | |
966 | if (context == NULL_TREE) | |
967 | context = DECL_CONTEXT (field); | |
968 | ||
969 | /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT | |
970 | slot set to the union type rather than the record type containing | |
fc378698 | 971 | the anonymous union. */ |
8d08fdba MS |
972 | if (context && TREE_CODE (context) == UNION_TYPE |
973 | && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context))) | |
fc378698 | 974 | context = TYPE_CONTEXT (context); |
8d08fdba MS |
975 | |
976 | /* Virtual function tables are never private. But we should know that | |
977 | we are looking for this, and not even try to hide it. */ | |
978 | if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1) | |
979 | PUBLIC_RETURN; | |
980 | ||
981 | /* Member found immediately within object. */ | |
700f8a87 | 982 | if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE) |
8d08fdba MS |
983 | { |
984 | /* Are we (or an enclosing scope) friends with the class that has | |
985 | FIELD? */ | |
986 | if (is_friend (context, previous_scope)) | |
987 | PUBLIC_RETURN; | |
988 | ||
989 | /* If it's private, it's private, you letch. */ | |
990 | if (TREE_PRIVATE (field)) | |
991 | PRIVATE_RETURN; | |
992 | ||
993 | /* ARM $11.5. Member functions of a derived class can access the | |
994 | non-static protected members of a base class only through a | |
995 | pointer to the derived class, a reference to it, or an object | |
996 | of it. Also any subsequently derived classes also have | |
997 | access. */ | |
998 | else if (TREE_PROTECTED (field)) | |
999 | { | |
1000 | if (current_class_type | |
eabb2eba | 1001 | && (static_mem || DECL_CONSTRUCTOR_P (field)) |
6467930b | 1002 | && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type)) |
8d08fdba MS |
1003 | PUBLIC_RETURN; |
1004 | else | |
1005 | PROTECTED_RETURN; | |
1006 | } | |
1007 | else | |
1008 | PUBLIC_RETURN; | |
1009 | } | |
1010 | ||
1011 | /* must reverse more than one element */ | |
1012 | basetype_path = reverse_path (basetype_path); | |
1013 | types = basetype_path; | |
1014 | via_protected = 0; | |
be99da77 | 1015 | access = access_default_node; |
700f8a87 MS |
1016 | protected_ok = static_mem && current_class_type |
1017 | && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type); | |
8d08fdba MS |
1018 | |
1019 | while (1) | |
1020 | { | |
1021 | tree member; | |
1022 | tree binfo = types; | |
1023 | tree type = BINFO_TYPE (binfo); | |
1024 | int private_ok = 0; | |
1025 | ||
1026 | /* Friends of a class can see protected members of its bases. | |
1027 | Note that classes are their own friends. */ | |
1028 | if (is_friend (type, previous_scope)) | |
1029 | { | |
1030 | protected_ok = 1; | |
1031 | private_ok = 1; | |
1032 | } | |
1033 | ||
1034 | member = purpose_member (type, DECL_ACCESS (field)); | |
1035 | if (member) | |
1036 | { | |
be99da77 | 1037 | access = TREE_VALUE (member); |
8d08fdba MS |
1038 | break; |
1039 | } | |
1040 | ||
1041 | types = BINFO_INHERITANCE_CHAIN (types); | |
1042 | ||
1043 | /* If the next type was VIA_PROTECTED, then fields of all remaining | |
1044 | classes past that one are *at least* protected. */ | |
1045 | if (types) | |
1046 | { | |
1047 | if (TREE_VIA_PROTECTED (types)) | |
1048 | via_protected = 1; | |
1049 | else if (! TREE_VIA_PUBLIC (types) && ! private_ok) | |
1050 | { | |
be99da77 | 1051 | access = access_private_node; |
8d08fdba MS |
1052 | break; |
1053 | } | |
1054 | } | |
1055 | else | |
1056 | break; | |
1057 | } | |
1058 | reverse_path (basetype_path); | |
1059 | ||
1060 | /* No special visibilities apply. Use normal rules. */ | |
1061 | ||
be99da77 | 1062 | if (access == access_default_node) |
8d08fdba MS |
1063 | { |
1064 | if (is_friend (context, previous_scope)) | |
be99da77 | 1065 | access = access_public_node; |
8d08fdba | 1066 | else if (TREE_PRIVATE (field)) |
be99da77 | 1067 | access = access_private_node; |
8d08fdba | 1068 | else if (TREE_PROTECTED (field)) |
be99da77 | 1069 | access = access_protected_node; |
8d08fdba | 1070 | else |
be99da77 | 1071 | access = access_public_node; |
8d08fdba MS |
1072 | } |
1073 | ||
be99da77 MS |
1074 | if (access == access_public_node && via_protected) |
1075 | access = access_protected_node; | |
8d08fdba | 1076 | |
be99da77 MS |
1077 | if (access == access_protected_node && protected_ok) |
1078 | access = access_public_node; | |
8d08fdba MS |
1079 | |
1080 | #if 0 | |
be99da77 | 1081 | if (access == access_public_node) |
8d08fdba | 1082 | DECL_PUBLIC (field) = 1; |
be99da77 | 1083 | else if (access == access_protected_node) |
8d08fdba | 1084 | DECL_PROTECTED (field) = 1; |
be99da77 | 1085 | else if (access == access_private_node) |
8d08fdba MS |
1086 | DECL_PRIVATE (field) = 1; |
1087 | else my_friendly_abort (96); | |
1088 | #endif | |
1089 | return access; | |
1090 | } | |
1091 | ||
1092 | /* Routine to see if the sub-object denoted by the binfo PARENT can be | |
1093 | found as a base class and sub-object of the object denoted by | |
1094 | BINFO. This routine relies upon binfos not being shared, except | |
1095 | for binfos for virtual bases. */ | |
e92cc029 | 1096 | |
8d08fdba MS |
1097 | static int |
1098 | is_subobject_of_p (parent, binfo) | |
1099 | tree parent, binfo; | |
1100 | { | |
1101 | tree binfos = BINFO_BASETYPES (binfo); | |
1102 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1103 | ||
1104 | if (parent == binfo) | |
1105 | return 1; | |
1106 | ||
1107 | /* Process and/or queue base types. */ | |
1108 | for (i = 0; i < n_baselinks; i++) | |
1109 | { | |
1110 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1111 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
1112 | base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo)); | |
1113 | if (is_subobject_of_p (parent, base_binfo)) | |
1114 | return 1; | |
1115 | } | |
1116 | return 0; | |
1117 | } | |
1118 | ||
1119 | /* See if a one FIELD_DECL hides another. This routine is meant to | |
1120 | correspond to ANSI working paper Sept 17, 1992 10p4. The two | |
1121 | binfos given are the binfos corresponding to the particular places | |
1122 | the FIELD_DECLs are found. This routine relies upon binfos not | |
e92cc029 MS |
1123 | being shared, except for virtual bases. */ |
1124 | ||
8d08fdba MS |
1125 | static int |
1126 | hides (hider_binfo, hidee_binfo) | |
1127 | tree hider_binfo, hidee_binfo; | |
1128 | { | |
1129 | /* hider hides hidee, if hider has hidee as a base class and | |
1130 | the instance of hidee is a sub-object of hider. The first | |
1131 | part is always true is the second part is true. | |
1132 | ||
1133 | When hider and hidee are the same (two ways to get to the exact | |
e92cc029 | 1134 | same member) we consider either one as hiding the other. */ |
8d08fdba MS |
1135 | return is_subobject_of_p (hidee_binfo, hider_binfo); |
1136 | } | |
1137 | ||
1138 | /* Very similar to lookup_fnfields_1 but it ensures that at least one | |
1139 | function was declared inside the class given by TYPE. It really should | |
1140 | only return functions that match the given TYPE. */ | |
e92cc029 | 1141 | |
8d08fdba MS |
1142 | static int |
1143 | lookup_fnfields_here (type, name) | |
1144 | tree type, name; | |
1145 | { | |
e92cc029 | 1146 | int idx = lookup_fnfields_1 (type, name); |
8d08fdba MS |
1147 | tree fndecls; |
1148 | ||
fc378698 | 1149 | /* ctors and dtors are always only in the right class. */ |
e92cc029 MS |
1150 | if (idx <= 1) |
1151 | return idx; | |
1152 | fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); | |
8d08fdba MS |
1153 | while (fndecls) |
1154 | { | |
2c73f9f5 | 1155 | if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls))) |
8d08fdba | 1156 | == TYPE_MAIN_VARIANT (type)) |
e92cc029 | 1157 | return idx; |
2c73f9f5 | 1158 | fndecls = OVL_CHAIN (fndecls); |
8d08fdba MS |
1159 | } |
1160 | return -1; | |
1161 | } | |
1162 | ||
1163 | /* Look for a field named NAME in an inheritance lattice dominated by | |
1164 | XBASETYPE. PROTECT is zero if we can avoid computing access | |
1165 | information, otherwise it is 1. WANT_TYPE is 1 when we should only | |
1166 | return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE. | |
1167 | ||
1168 | It was not clear what should happen if WANT_TYPE is set, and an | |
1169 | ambiguity is found. At least one use (lookup_name) to not see | |
1170 | the error. */ | |
e92cc029 | 1171 | |
8d08fdba MS |
1172 | tree |
1173 | lookup_field (xbasetype, name, protect, want_type) | |
1174 | register tree xbasetype, name; | |
1175 | int protect, want_type; | |
1176 | { | |
1177 | int head = 0, tail = 0; | |
a703fb38 KG |
1178 | tree rval, rval_binfo = NULL_TREE, rval_binfo_h = NULL_TREE; |
1179 | tree type = NULL_TREE, basetype_chain, basetype_path = NULL_TREE; | |
be99da77 | 1180 | tree this_v = access_default_node; |
8d08fdba | 1181 | tree entry, binfo, binfo_h; |
be99da77 | 1182 | tree own_access = access_default_node; |
8d08fdba MS |
1183 | int vbase_name_p = VBASE_NAME_P (name); |
1184 | ||
1185 | /* rval_binfo is the binfo associated with the found member, note, | |
1186 | this can be set with useful information, even when rval is not | |
1187 | set, because it must deal with ALL members, not just non-function | |
1188 | members. It is used for ambiguity checking and the hidden | |
1189 | checks. Whereas rval is only set if a proper (not hidden) | |
1190 | non-function member is found. */ | |
1191 | ||
1192 | /* rval_binfo_h and binfo_h are binfo values used when we perform the | |
1193 | hiding checks, as virtual base classes may not be shared. The strategy | |
38e01259 | 1194 | is we always go into the binfo hierarchy owned by TYPE_BINFO of |
8d08fdba MS |
1195 | virtual base classes, as we cross virtual base class lines. This way |
1196 | we know that binfo of a virtual base class will always == itself when | |
1197 | found along any line. (mrs) */ | |
1198 | ||
8d08fdba MS |
1199 | char *errstr = 0; |
1200 | ||
1201 | /* Set this to nonzero if we don't know how to compute | |
1202 | accurate error messages for access control. */ | |
e92cc029 | 1203 | int idx = MEMOIZED_HASH_FN (name); |
8d08fdba | 1204 | |
fc378698 MS |
1205 | #if 0 |
1206 | /* We cannot search for constructor/destructor names like this. */ | |
1207 | /* This can't go here, but where should it go? */ | |
8d08fdba MS |
1208 | /* If we are looking for a constructor in a templated type, use the |
1209 | unspecialized name, as that is how we store it. */ | |
1210 | if (IDENTIFIER_TEMPLATE (name)) | |
1211 | name = constructor_name (name); | |
fc378698 | 1212 | #endif |
8d08fdba | 1213 | |
de22184b MS |
1214 | if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype) |
1215 | && IDENTIFIER_CLASS_VALUE (name)) | |
1216 | { | |
1217 | tree field = IDENTIFIER_CLASS_VALUE (name); | |
1218 | if (TREE_CODE (field) != FUNCTION_DECL | |
1219 | && ! (want_type && TREE_CODE (field) != TYPE_DECL)) | |
1220 | return field; | |
1221 | } | |
1222 | ||
8d08fdba MS |
1223 | if (TREE_CODE (xbasetype) == TREE_VEC) |
1224 | { | |
8d08fdba | 1225 | type = BINFO_TYPE (xbasetype); |
39211cd5 | 1226 | basetype_path = xbasetype; |
8d08fdba MS |
1227 | } |
1228 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype))) | |
39211cd5 | 1229 | { |
238109cd | 1230 | type = xbasetype; |
5566b478 | 1231 | basetype_path = TYPE_BINFO (type); |
39211cd5 MS |
1232 | BINFO_VIA_PUBLIC (basetype_path) = 1; |
1233 | BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE; | |
1234 | } | |
238109cd JM |
1235 | else |
1236 | my_friendly_abort (97); | |
1237 | ||
1238 | complete_type (type); | |
8d08fdba MS |
1239 | |
1240 | if (CLASSTYPE_MTABLE_ENTRY (type)) | |
1241 | { | |
e92cc029 | 1242 | tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), idx); |
8d08fdba MS |
1243 | |
1244 | while (tem && TREE_PURPOSE (tem) != name) | |
1245 | { | |
1246 | memoized_fields_searched[0]++; | |
1247 | tem = TREE_CHAIN (tem); | |
1248 | } | |
1249 | if (tem) | |
1250 | { | |
1251 | if (protect && TREE_TYPE (tem)) | |
1252 | { | |
1253 | error (TREE_STRING_POINTER (TREE_TYPE (tem)), | |
1254 | IDENTIFIER_POINTER (name), | |
1255 | TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem)))); | |
1256 | return error_mark_node; | |
1257 | } | |
1258 | if (TREE_VALUE (tem) == NULL_TREE) | |
1259 | memoized_fast_rejects[0] += 1; | |
1260 | else | |
1261 | memoized_fast_finds[0] += 1; | |
1262 | return TREE_VALUE (tem); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | #ifdef GATHER_STATISTICS | |
1267 | n_calls_lookup_field++; | |
fc378698 | 1268 | #endif /* GATHER_STATISTICS */ |
2c73f9f5 | 1269 | if (protect && flag_memoize_lookups && ! toplevel_bindings_p ()) |
8d08fdba MS |
1270 | entry = make_memoized_table_entry (type, name, 0); |
1271 | else | |
1272 | entry = 0; | |
1273 | ||
1274 | rval = lookup_field_1 (type, name); | |
8d08fdba | 1275 | |
e1cd6e56 | 1276 | if (rval || lookup_fnfields_here (type, name) >= 0) |
8d08fdba | 1277 | { |
1559e387 JM |
1278 | if (entry) |
1279 | TREE_VALUE (entry) = rval; | |
1280 | ||
e1cd6e56 MS |
1281 | if (rval) |
1282 | { | |
1283 | if (want_type) | |
1284 | { | |
1285 | if (TREE_CODE (rval) != TYPE_DECL) | |
1286 | { | |
fc378698 | 1287 | rval = purpose_member (name, CLASSTYPE_TAGS (type)); |
e1cd6e56 MS |
1288 | if (rval) |
1289 | rval = TYPE_MAIN_DECL (TREE_VALUE (rval)); | |
1290 | } | |
1291 | } | |
1292 | else | |
1293 | { | |
1294 | if (TREE_CODE (rval) == TYPE_DECL | |
1295 | && lookup_fnfields_here (type, name) >= 0) | |
1296 | rval = NULL_TREE; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | if (protect && rval) | |
8d08fdba MS |
1301 | { |
1302 | if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval)) | |
1303 | this_v = compute_access (basetype_path, rval); | |
1304 | if (TREE_CODE (rval) == CONST_DECL) | |
1305 | { | |
be99da77 | 1306 | if (this_v == access_private_node) |
a28e3c7f | 1307 | errstr = "enum `%D' is a private value of class `%T'"; |
be99da77 | 1308 | else if (this_v == access_protected_node) |
a28e3c7f | 1309 | errstr = "enum `%D' is a protected value of class `%T'"; |
8d08fdba MS |
1310 | } |
1311 | else | |
1312 | { | |
be99da77 | 1313 | if (this_v == access_private_node) |
a28e3c7f | 1314 | errstr = "member `%D' is a private member of class `%T'"; |
be99da77 | 1315 | else if (this_v == access_protected_node) |
a28e3c7f | 1316 | errstr = "member `%D' is a protected member of class `%T'"; |
8d08fdba MS |
1317 | } |
1318 | } | |
1319 | ||
b3709d9b JM |
1320 | rval_binfo = basetype_path; |
1321 | goto out; | |
8d08fdba MS |
1322 | } |
1323 | ||
e66d884e | 1324 | basetype_chain = build_expr_list (NULL_TREE, basetype_path); |
39211cd5 MS |
1325 | TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path); |
1326 | TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path); | |
1327 | TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path); | |
8d08fdba | 1328 | |
e92cc029 | 1329 | /* The ambiguity check relies upon breadth first searching. */ |
8d08fdba MS |
1330 | |
1331 | search_stack = push_search_level (search_stack, &search_obstack); | |
8d08fdba MS |
1332 | binfo = basetype_path; |
1333 | binfo_h = binfo; | |
1334 | ||
1335 | while (1) | |
1336 | { | |
1337 | tree binfos = BINFO_BASETYPES (binfo); | |
1338 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1339 | tree nval; | |
1340 | ||
1341 | /* Process and/or queue base types. */ | |
1342 | for (i = 0; i < n_baselinks; i++) | |
1343 | { | |
1344 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1345 | if (BINFO_FIELDS_MARKED (base_binfo) == 0) | |
1346 | { | |
1347 | tree btypes; | |
1348 | ||
1349 | SET_BINFO_FIELDS_MARKED (base_binfo); | |
1350 | btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain); | |
1351 | TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo); | |
1352 | TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo); | |
1353 | TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo); | |
1354 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
e66d884e | 1355 | btypes = my_tree_cons (NULL_TREE, |
8d08fdba MS |
1356 | TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))), |
1357 | btypes); | |
1358 | else | |
e66d884e | 1359 | btypes = my_tree_cons (NULL_TREE, |
8d08fdba MS |
1360 | TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i), |
1361 | btypes); | |
1362 | obstack_ptr_grow (&search_obstack, btypes); | |
1363 | tail += 1; | |
1364 | if (tail >= search_stack->limit) | |
1365 | my_friendly_abort (98); | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | /* Process head of queue, if one exists. */ | |
1370 | if (head >= tail) | |
1371 | break; | |
1372 | ||
1373 | basetype_chain = search_stack->first[head++]; | |
1374 | binfo_h = TREE_VALUE (basetype_chain); | |
1375 | basetype_chain = TREE_CHAIN (basetype_chain); | |
1376 | basetype_path = TREE_VALUE (basetype_chain); | |
1377 | if (TREE_CHAIN (basetype_chain)) | |
1378 | BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain)); | |
1379 | else | |
1380 | BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE; | |
1381 | ||
1382 | binfo = basetype_path; | |
1383 | type = BINFO_TYPE (binfo); | |
1384 | ||
1385 | /* See if we can find NAME in TYPE. If RVAL is nonzero, | |
1386 | and we do find NAME in TYPE, verify that such a second | |
59be0cdd | 1387 | sighting is in fact valid. */ |
8d08fdba MS |
1388 | |
1389 | nval = lookup_field_1 (type, name); | |
1390 | ||
1391 | if (nval || lookup_fnfields_here (type, name)>=0) | |
1392 | { | |
700f8a87 MS |
1393 | if (nval && nval == rval && SHARED_MEMBER_P (nval)) |
1394 | { | |
1395 | /* This is ok, the member found is the same [class.ambig] */ | |
1396 | } | |
1397 | else if (rval_binfo && hides (rval_binfo_h, binfo_h)) | |
8d08fdba MS |
1398 | { |
1399 | /* This is ok, the member found is in rval_binfo, not | |
e92cc029 | 1400 | here (binfo). */ |
8d08fdba MS |
1401 | } |
1402 | else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h)) | |
1403 | { | |
1404 | /* This is ok, the member found is here (binfo), not in | |
e92cc029 | 1405 | rval_binfo. */ |
8d08fdba MS |
1406 | if (nval) |
1407 | { | |
1408 | rval = nval; | |
1409 | if (entry || protect) | |
1410 | this_v = compute_access (basetype_path, rval); | |
1411 | /* These may look ambiguous, but they really are not. */ | |
1412 | if (vbase_name_p) | |
1413 | break; | |
1414 | } | |
1415 | else | |
1416 | { | |
e92cc029 | 1417 | /* Undo finding it before, as something else hides it. */ |
8d08fdba MS |
1418 | rval = NULL_TREE; |
1419 | } | |
1420 | rval_binfo = binfo; | |
1421 | rval_binfo_h = binfo_h; | |
1422 | } | |
1423 | else | |
1424 | { | |
e92cc029 | 1425 | /* This is ambiguous. */ |
a28e3c7f | 1426 | errstr = "request for member `%D' is ambiguous"; |
9e9ff709 | 1427 | protect += 2; |
8d08fdba MS |
1428 | break; |
1429 | } | |
1430 | } | |
1431 | } | |
1432 | { | |
1433 | tree *tp = search_stack->first; | |
1434 | tree *search_tail = tp + tail; | |
1435 | ||
1436 | if (entry) | |
1437 | TREE_VALUE (entry) = rval; | |
1438 | ||
e1cd6e56 | 1439 | if (rval_binfo) |
8d08fdba | 1440 | { |
e1cd6e56 MS |
1441 | type = BINFO_TYPE (rval_binfo); |
1442 | ||
1443 | if (rval) | |
1444 | { | |
1445 | if (want_type) | |
1446 | { | |
1447 | if (TREE_CODE (rval) != TYPE_DECL) | |
1448 | { | |
fc378698 | 1449 | rval = purpose_member (name, CLASSTYPE_TAGS (type)); |
e1cd6e56 MS |
1450 | if (rval) |
1451 | rval = TYPE_MAIN_DECL (TREE_VALUE (rval)); | |
1452 | } | |
1453 | } | |
1454 | else | |
1455 | { | |
1456 | if (TREE_CODE (rval) == TYPE_DECL | |
1457 | && lookup_fnfields_here (type, name) >= 0) | |
1458 | rval = NULL_TREE; | |
1459 | } | |
1460 | } | |
8d08fdba MS |
1461 | } |
1462 | ||
e1cd6e56 MS |
1463 | if (rval == NULL_TREE) |
1464 | errstr = 0; | |
1465 | ||
8d08fdba MS |
1466 | /* If this FIELD_DECL defines its own access level, deal with that. */ |
1467 | if (rval && errstr == 0 | |
1468 | && ((protect&1) || entry) | |
1469 | && DECL_LANG_SPECIFIC (rval) | |
1470 | && DECL_ACCESS (rval)) | |
1471 | { | |
1472 | while (tp < search_tail) | |
1473 | { | |
1474 | /* If is possible for one of the derived types on the path to | |
1475 | have defined special access for this field. Look for such | |
1476 | declarations and report an error if a conflict is found. */ | |
a703fb38 | 1477 | tree new_v = NULL_TREE; |
8d08fdba | 1478 | |
be99da77 | 1479 | if (this_v != access_default_node) |
8d08fdba | 1480 | new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval); |
be99da77 | 1481 | if (this_v != access_default_node && new_v != this_v) |
8d08fdba | 1482 | { |
a28e3c7f | 1483 | errstr = "conflicting access to member `%D'"; |
be99da77 | 1484 | this_v = access_default_node; |
8d08fdba MS |
1485 | } |
1486 | own_access = new_v; | |
1487 | CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp))); | |
1488 | tp += 1; | |
1489 | } | |
1490 | } | |
1491 | else | |
1492 | { | |
1493 | while (tp < search_tail) | |
1494 | { | |
1495 | CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp))); | |
1496 | tp += 1; | |
1497 | } | |
1498 | } | |
1499 | } | |
1500 | search_stack = pop_search_level (search_stack); | |
1501 | ||
1502 | if (errstr == 0) | |
1503 | { | |
be99da77 | 1504 | if (own_access == access_private_node) |
a28e3c7f | 1505 | errstr = "member `%D' declared private"; |
be99da77 | 1506 | else if (own_access == access_protected_node) |
a28e3c7f | 1507 | errstr = "member `%D' declared protected"; |
be99da77 | 1508 | else if (this_v == access_private_node) |
8d08fdba | 1509 | errstr = TREE_PRIVATE (rval) |
a28e3c7f MS |
1510 | ? "member `%D' is private" |
1511 | : "member `%D' is from private base class"; | |
be99da77 | 1512 | else if (this_v == access_protected_node) |
8d08fdba | 1513 | errstr = TREE_PROTECTED (rval) |
a28e3c7f MS |
1514 | ? "member `%D' is protected" |
1515 | : "member `%D' is from protected base class"; | |
8d08fdba MS |
1516 | } |
1517 | ||
b3709d9b | 1518 | out: |
8d08fdba MS |
1519 | if (entry) |
1520 | { | |
1521 | if (errstr) | |
1522 | { | |
1523 | tree error_string = my_build_string (errstr); | |
1524 | /* Save error message with entry. */ | |
1525 | TREE_TYPE (entry) = error_string; | |
1526 | } | |
1527 | else | |
1528 | { | |
1529 | /* Mark entry as having no error string. */ | |
1530 | TREE_TYPE (entry) = NULL_TREE; | |
1531 | } | |
1532 | } | |
1533 | ||
9e9ff709 MS |
1534 | if (protect == 2) |
1535 | { | |
1536 | /* If we are not interested in ambiguities, don't report them, | |
1537 | just return NULL_TREE. */ | |
1538 | rval = NULL_TREE; | |
1539 | protect = 0; | |
1540 | } | |
1541 | ||
8d08fdba MS |
1542 | if (errstr && protect) |
1543 | { | |
a28e3c7f | 1544 | cp_error (errstr, name, type); |
8d08fdba MS |
1545 | rval = error_mark_node; |
1546 | } | |
b3709d9b JM |
1547 | |
1548 | /* Do implicit typename stuff. */ | |
1549 | if (rval && TREE_CODE (rval) == TYPE_DECL | |
1550 | && ! DECL_ARTIFICIAL (rval) | |
1551 | && processing_template_decl | |
b9082e8a | 1552 | && ! currently_open_class (BINFO_TYPE (rval_binfo)) |
b3709d9b JM |
1553 | && uses_template_parms (type)) |
1554 | { | |
1555 | binfo = rval_binfo; | |
1556 | for (; ; binfo = BINFO_INHERITANCE_CHAIN (binfo)) | |
1557 | if (BINFO_INHERITANCE_CHAIN (binfo) == NULL_TREE | |
1558 | || (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)) | |
1559 | == current_class_type)) | |
1560 | break; | |
1561 | ||
1562 | entry = make_typename_type (BINFO_TYPE (binfo), name); | |
1563 | TREE_TYPE (entry) = TREE_TYPE (rval); | |
1564 | rval = TYPE_MAIN_DECL (entry); | |
1565 | } | |
1566 | ||
8d08fdba MS |
1567 | return rval; |
1568 | } | |
1569 | ||
1570 | /* Try to find NAME inside a nested class. */ | |
e92cc029 | 1571 | |
8d08fdba MS |
1572 | tree |
1573 | lookup_nested_field (name, complain) | |
1574 | tree name; | |
1575 | int complain; | |
1576 | { | |
1577 | register tree t; | |
1578 | ||
1579 | tree id = NULL_TREE; | |
b9082e8a | 1580 | if (TYPE_MAIN_DECL (current_class_type)) |
8d08fdba MS |
1581 | { |
1582 | /* Climb our way up the nested ladder, seeing if we're trying to | |
1583 | modify a field in an enclosing class. If so, we should only | |
1584 | be able to modify if it's static. */ | |
b9082e8a | 1585 | for (t = TYPE_MAIN_DECL (current_class_type); |
8d08fdba | 1586 | t && DECL_CONTEXT (t); |
b9082e8a | 1587 | t = TYPE_MAIN_DECL (DECL_CONTEXT (t))) |
8d08fdba MS |
1588 | { |
1589 | if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE) | |
1590 | break; | |
1591 | ||
1592 | /* N.B.: lookup_field will do the access checking for us */ | |
1593 | id = lookup_field (DECL_CONTEXT (t), name, complain, 0); | |
1594 | if (id == error_mark_node) | |
1595 | { | |
1596 | id = NULL_TREE; | |
1597 | continue; | |
1598 | } | |
1599 | ||
1600 | if (id != NULL_TREE) | |
1601 | { | |
1602 | if (TREE_CODE (id) == FIELD_DECL | |
1603 | && ! TREE_STATIC (id) | |
1604 | && TREE_TYPE (id) != error_mark_node) | |
1605 | { | |
1606 | if (complain) | |
1607 | { | |
1608 | /* At parse time, we don't want to give this error, since | |
1609 | we won't have enough state to make this kind of | |
1610 | decision properly. But there are times (e.g., with | |
1611 | enums in nested classes) when we do need to call | |
1612 | this fn at parse time. So, in those cases, we pass | |
1613 | complain as a 0 and just return a NULL_TREE. */ | |
fc378698 MS |
1614 | cp_error ("assignment to non-static member `%D' of enclosing class `%T'", |
1615 | id, DECL_CONTEXT (t)); | |
8d08fdba MS |
1616 | /* Mark this for do_identifier(). It would otherwise |
1617 | claim that the variable was undeclared. */ | |
1618 | TREE_TYPE (id) = error_mark_node; | |
1619 | } | |
1620 | else | |
1621 | { | |
1622 | id = NULL_TREE; | |
1623 | continue; | |
1624 | } | |
1625 | } | |
1626 | break; | |
1627 | } | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | return id; | |
1632 | } | |
1633 | ||
1634 | /* TYPE is a class type. Return the index of the fields within | |
1635 | the method vector with name NAME, or -1 is no such field exists. */ | |
e92cc029 | 1636 | |
8d08fdba MS |
1637 | static int |
1638 | lookup_fnfields_1 (type, name) | |
1639 | tree type, name; | |
1640 | { | |
1641 | register tree method_vec = CLASSTYPE_METHOD_VEC (type); | |
1642 | ||
1643 | if (method_vec != 0) | |
1644 | { | |
1645 | register tree *methods = &TREE_VEC_ELT (method_vec, 0); | |
1646 | register tree *end = TREE_VEC_END (method_vec); | |
1647 | ||
1648 | #ifdef GATHER_STATISTICS | |
1649 | n_calls_lookup_fnfields_1++; | |
fc378698 MS |
1650 | #endif /* GATHER_STATISTICS */ |
1651 | ||
1652 | /* Constructors are first... */ | |
1653 | if (*methods && name == ctor_identifier) | |
8d08fdba MS |
1654 | return 0; |
1655 | ||
fc378698 MS |
1656 | /* and destructors are second. */ |
1657 | if (*++methods && name == dtor_identifier) | |
1658 | return 1; | |
1659 | ||
8d08fdba MS |
1660 | while (++methods != end) |
1661 | { | |
1662 | #ifdef GATHER_STATISTICS | |
1663 | n_outer_fields_searched++; | |
fc378698 | 1664 | #endif /* GATHER_STATISTICS */ |
2c73f9f5 | 1665 | if (DECL_NAME (OVL_CURRENT (*methods)) == name) |
8d08fdba MS |
1666 | break; |
1667 | } | |
98c1c668 JM |
1668 | |
1669 | /* If we didn't find it, it might have been a template | |
1670 | conversion operator. (Note that we don't look for this case | |
1671 | above so that we will always find specializations first.) */ | |
1672 | if (methods == end | |
1673 | && IDENTIFIER_TYPENAME_P (name)) | |
1674 | { | |
1675 | methods = &TREE_VEC_ELT (method_vec, 0) + 1; | |
1676 | ||
1677 | while (++methods != end) | |
1678 | { | |
2c73f9f5 ML |
1679 | if (TREE_CODE (OVL_CURRENT (*methods)) == TEMPLATE_DECL |
1680 | && IDENTIFIER_TYPENAME_P (DECL_NAME (OVL_CURRENT (*methods)))) | |
98c1c668 JM |
1681 | break; |
1682 | } | |
1683 | } | |
1684 | ||
8d08fdba MS |
1685 | if (methods != end) |
1686 | return methods - &TREE_VEC_ELT (method_vec, 0); | |
1687 | } | |
1688 | ||
1689 | return -1; | |
1690 | } | |
1691 | ||
1692 | /* Starting from BASETYPE, return a TREE_BASELINK-like object | |
1693 | which gives the following information (in a list): | |
1694 | ||
1695 | TREE_TYPE: list of basetypes needed to get to... | |
6467930b | 1696 | TREE_VALUE: list of all functions in a given type |
8d08fdba MS |
1697 | which have name NAME. |
1698 | ||
1699 | No access information is computed by this function, | |
1700 | other then to adorn the list of basetypes with | |
1701 | TREE_VIA_PUBLIC. | |
1702 | ||
1703 | If there are two ways to find a name (two members), if COMPLAIN is | |
1704 | non-zero, then error_mark_node is returned, and an error message is | |
1705 | printed, otherwise, just an error_mark_node is returned. | |
1706 | ||
1707 | As a special case, is COMPLAIN is -1, we don't complain, and we | |
1708 | don't return error_mark_node, but rather the complete list of | |
1709 | virtuals. This is used by get_virtuals_named_this. */ | |
e92cc029 | 1710 | |
8d08fdba MS |
1711 | tree |
1712 | lookup_fnfields (basetype_path, name, complain) | |
1713 | tree basetype_path, name; | |
1714 | int complain; | |
1715 | { | |
1716 | int head = 0, tail = 0; | |
a703fb38 KG |
1717 | tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE; |
1718 | tree rval_binfo_h = NULL_TREE, entry, binfo, basetype_chain, binfo_h; | |
8d08fdba MS |
1719 | int find_all = 0; |
1720 | ||
1721 | /* rval_binfo is the binfo associated with the found member, note, | |
1722 | this can be set with useful information, even when rval is not | |
1723 | set, because it must deal with ALL members, not just function | |
1724 | members. It is used for ambiguity checking and the hidden | |
1725 | checks. Whereas rval is only set if a proper (not hidden) | |
1726 | function member is found. */ | |
1727 | ||
1728 | /* rval_binfo_h and binfo_h are binfo values used when we perform the | |
1729 | hiding checks, as virtual base classes may not be shared. The strategy | |
38e01259 | 1730 | is we always go into the binfo hierarchy owned by TYPE_BINFO of |
8d08fdba MS |
1731 | virtual base classes, as we cross virtual base class lines. This way |
1732 | we know that binfo of a virtual base class will always == itself when | |
1733 | found along any line. (mrs) */ | |
1734 | ||
1735 | /* For now, don't try this. */ | |
1736 | int protect = complain; | |
1737 | ||
8d08fdba MS |
1738 | char *errstr = 0; |
1739 | ||
1740 | /* Set this to nonzero if we don't know how to compute | |
1741 | accurate error messages for access control. */ | |
e92cc029 | 1742 | int idx = MEMOIZED_HASH_FN (name); |
8d08fdba MS |
1743 | |
1744 | if (complain == -1) | |
1745 | { | |
1746 | find_all = 1; | |
1747 | protect = complain = 0; | |
1748 | } | |
1749 | ||
fc378698 MS |
1750 | #if 0 |
1751 | /* We cannot search for constructor/destructor names like this. */ | |
1752 | /* This can't go here, but where should it go? */ | |
8d08fdba MS |
1753 | /* If we are looking for a constructor in a templated type, use the |
1754 | unspecialized name, as that is how we store it. */ | |
1755 | if (IDENTIFIER_TEMPLATE (name)) | |
1756 | name = constructor_name (name); | |
fc378698 | 1757 | #endif |
8d08fdba MS |
1758 | |
1759 | binfo = basetype_path; | |
1760 | binfo_h = binfo; | |
5566b478 | 1761 | type = complete_type (BINFO_TYPE (basetype_path)); |
8d08fdba | 1762 | |
e92cc029 | 1763 | /* The memoization code is in need of maintenance. */ |
8d08fdba MS |
1764 | if (!find_all && CLASSTYPE_MTABLE_ENTRY (type)) |
1765 | { | |
e92cc029 | 1766 | tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), idx); |
8d08fdba MS |
1767 | |
1768 | while (tem && TREE_PURPOSE (tem) != name) | |
1769 | { | |
1770 | memoized_fields_searched[1]++; | |
1771 | tem = TREE_CHAIN (tem); | |
1772 | } | |
1773 | if (tem) | |
1774 | { | |
1775 | if (protect && TREE_TYPE (tem)) | |
1776 | { | |
1777 | error (TREE_STRING_POINTER (TREE_TYPE (tem)), | |
1778 | IDENTIFIER_POINTER (name), | |
1779 | TYPE_NAME_STRING (DECL_CLASS_CONTEXT (TREE_VALUE (TREE_VALUE (tem))))); | |
1780 | return error_mark_node; | |
1781 | } | |
1782 | if (TREE_VALUE (tem) == NULL_TREE) | |
1783 | { | |
1784 | memoized_fast_rejects[1] += 1; | |
1785 | return NULL_TREE; | |
1786 | } | |
1787 | else | |
1788 | { | |
1789 | /* Want to return this, but we must make sure | |
1790 | that access information is consistent. */ | |
1791 | tree baselink = TREE_VALUE (tem); | |
1792 | tree memoized_basetypes = TREE_PURPOSE (baselink); | |
1793 | tree these_basetypes = basetype_path; | |
1794 | while (memoized_basetypes && these_basetypes) | |
1795 | { | |
1796 | memoized_fields_searched[1]++; | |
1797 | if (TREE_VALUE (memoized_basetypes) != these_basetypes) | |
1798 | break; | |
1799 | memoized_basetypes = TREE_CHAIN (memoized_basetypes); | |
1800 | these_basetypes = BINFO_INHERITANCE_CHAIN (these_basetypes); | |
1801 | } | |
1802 | /* The following statement is true only when both are NULL. */ | |
1803 | if (memoized_basetypes == these_basetypes) | |
1804 | { | |
1805 | memoized_fast_finds[1] += 1; | |
1806 | return TREE_VALUE (tem); | |
1807 | } | |
1808 | /* else, we must re-find this field by hand. */ | |
1809 | baselink = tree_cons (basetype_path, TREE_VALUE (baselink), TREE_CHAIN (baselink)); | |
1810 | return baselink; | |
1811 | } | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | #ifdef GATHER_STATISTICS | |
1816 | n_calls_lookup_fnfields++; | |
fc378698 | 1817 | #endif /* GATHER_STATISTICS */ |
2c73f9f5 | 1818 | if (protect && flag_memoize_lookups && ! toplevel_bindings_p ()) |
8d08fdba MS |
1819 | entry = make_memoized_table_entry (type, name, 1); |
1820 | else | |
1821 | entry = 0; | |
1822 | ||
e92cc029 MS |
1823 | idx = lookup_fnfields_here (type, name); |
1824 | if (idx >= 0 || lookup_field_1 (type, name)) | |
8d08fdba MS |
1825 | { |
1826 | rval_binfo = basetype_path; | |
1827 | rval_binfo_h = rval_binfo; | |
1828 | } | |
1829 | ||
e92cc029 | 1830 | if (idx >= 0) |
8d08fdba | 1831 | { |
e92cc029 | 1832 | rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); |
8d08fdba MS |
1833 | rvals = my_tree_cons (basetype_path, rval, rvals); |
1834 | if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type)) | |
e92cc029 | 1835 | TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx); |
8d08fdba MS |
1836 | |
1837 | if (entry) | |
1838 | { | |
1839 | TREE_VALUE (entry) = rvals; | |
1840 | TREE_TYPE (entry) = NULL_TREE; | |
1841 | } | |
1842 | ||
8d08fdba MS |
1843 | return rvals; |
1844 | } | |
1845 | rval = NULL_TREE; | |
1846 | ||
fc378698 MS |
1847 | if (name == ctor_identifier || name == dtor_identifier) |
1848 | { | |
1849 | /* Don't allow lookups of constructors and destructors to go | |
1850 | deeper than the first place we look. */ | |
1851 | if (entry) | |
1852 | TREE_TYPE (entry) = TREE_VALUE (entry) = NULL_TREE; | |
1853 | ||
1854 | return NULL_TREE; | |
1855 | } | |
1856 | ||
39211cd5 MS |
1857 | if (basetype_path == TYPE_BINFO (type)) |
1858 | { | |
1859 | basetype_chain = CLASSTYPE_BINFO_AS_LIST (type); | |
1860 | TREE_VIA_PUBLIC (basetype_chain) = 1; | |
1861 | BINFO_VIA_PUBLIC (basetype_path) = 1; | |
1862 | BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE; | |
1863 | } | |
1864 | else | |
1865 | { | |
e66d884e | 1866 | basetype_chain = build_expr_list (NULL_TREE, basetype_path); |
39211cd5 MS |
1867 | TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path); |
1868 | TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path); | |
1869 | TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path); | |
1870 | } | |
8d08fdba | 1871 | |
e92cc029 | 1872 | /* The ambiguity check relies upon breadth first searching. */ |
8d08fdba MS |
1873 | |
1874 | search_stack = push_search_level (search_stack, &search_obstack); | |
8d08fdba MS |
1875 | binfo = basetype_path; |
1876 | binfo_h = binfo; | |
1877 | ||
1878 | while (1) | |
1879 | { | |
1880 | tree binfos = BINFO_BASETYPES (binfo); | |
1881 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
e92cc029 | 1882 | int idx; |
8d08fdba MS |
1883 | |
1884 | /* Process and/or queue base types. */ | |
1885 | for (i = 0; i < n_baselinks; i++) | |
1886 | { | |
1887 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
1888 | if (BINFO_FIELDS_MARKED (base_binfo) == 0) | |
1889 | { | |
1890 | tree btypes; | |
1891 | ||
1892 | SET_BINFO_FIELDS_MARKED (base_binfo); | |
1893 | btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain); | |
1894 | TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo); | |
1895 | TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo); | |
1896 | TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo); | |
1897 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
e66d884e | 1898 | btypes = my_tree_cons (NULL_TREE, |
8d08fdba MS |
1899 | TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))), |
1900 | btypes); | |
1901 | else | |
e66d884e | 1902 | btypes = my_tree_cons (NULL_TREE, |
8d08fdba MS |
1903 | TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i), |
1904 | btypes); | |
1905 | obstack_ptr_grow (&search_obstack, btypes); | |
1906 | tail += 1; | |
1907 | if (tail >= search_stack->limit) | |
1908 | my_friendly_abort (99); | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | /* Process head of queue, if one exists. */ | |
1913 | if (head >= tail) | |
1914 | break; | |
1915 | ||
1916 | basetype_chain = search_stack->first[head++]; | |
1917 | binfo_h = TREE_VALUE (basetype_chain); | |
1918 | basetype_chain = TREE_CHAIN (basetype_chain); | |
1919 | basetype_path = TREE_VALUE (basetype_chain); | |
1920 | if (TREE_CHAIN (basetype_chain)) | |
1921 | BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain)); | |
1922 | else | |
1923 | BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE; | |
1924 | ||
1925 | binfo = basetype_path; | |
1926 | type = BINFO_TYPE (binfo); | |
1927 | ||
1928 | /* See if we can find NAME in TYPE. If RVAL is nonzero, | |
1929 | and we do find NAME in TYPE, verify that such a second | |
59be0cdd | 1930 | sighting is in fact valid. */ |
8d08fdba | 1931 | |
e92cc029 | 1932 | idx = lookup_fnfields_here (type, name); |
8d08fdba | 1933 | |
e92cc029 | 1934 | if (idx >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all)) |
8d08fdba MS |
1935 | { |
1936 | if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h)) | |
1937 | { | |
1938 | /* This is ok, the member found is in rval_binfo, not | |
e92cc029 | 1939 | here (binfo). */ |
8d08fdba MS |
1940 | } |
1941 | else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h)) | |
1942 | { | |
1943 | /* This is ok, the member found is here (binfo), not in | |
e92cc029 MS |
1944 | rval_binfo. */ |
1945 | if (idx >= 0) | |
8d08fdba | 1946 | { |
e92cc029 | 1947 | rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); |
8d08fdba MS |
1948 | /* Note, rvals can only be previously set if find_all is |
1949 | true. */ | |
1950 | rvals = my_tree_cons (basetype_path, rval, rvals); | |
1951 | if (TYPE_BINFO_BASETYPES (type) | |
1952 | && CLASSTYPE_BASELINK_VEC (type)) | |
e92cc029 | 1953 | TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx); |
8d08fdba MS |
1954 | } |
1955 | else | |
1956 | { | |
e92cc029 | 1957 | /* Undo finding it before, as something else hides it. */ |
8d08fdba MS |
1958 | rval = NULL_TREE; |
1959 | rvals = NULL_TREE; | |
1960 | } | |
1961 | rval_binfo = binfo; | |
1962 | rval_binfo_h = binfo_h; | |
1963 | } | |
1964 | else | |
1965 | { | |
e92cc029 | 1966 | /* This is ambiguous. */ |
700f8a87 | 1967 | errstr = "request for method `%D' is ambiguous"; |
8d08fdba MS |
1968 | rvals = error_mark_node; |
1969 | break; | |
1970 | } | |
1971 | } | |
1972 | } | |
1973 | { | |
1974 | tree *tp = search_stack->first; | |
1975 | tree *search_tail = tp + tail; | |
1976 | ||
1977 | while (tp < search_tail) | |
1978 | { | |
1979 | CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp))); | |
1980 | tp += 1; | |
1981 | } | |
1982 | } | |
1983 | search_stack = pop_search_level (search_stack); | |
1984 | ||
1985 | if (entry) | |
1986 | { | |
1987 | if (errstr) | |
1988 | { | |
1989 | tree error_string = my_build_string (errstr); | |
1990 | /* Save error message with entry. */ | |
1991 | TREE_TYPE (entry) = error_string; | |
1992 | } | |
1993 | else | |
1994 | { | |
1995 | /* Mark entry as having no error string. */ | |
1996 | TREE_TYPE (entry) = NULL_TREE; | |
1997 | TREE_VALUE (entry) = rvals; | |
1998 | } | |
1999 | } | |
2000 | ||
2001 | if (errstr && protect) | |
2002 | { | |
700f8a87 | 2003 | cp_error (errstr, name); |
8d08fdba MS |
2004 | rvals = error_mark_node; |
2005 | } | |
2006 | ||
2007 | return rvals; | |
2008 | } | |
2009 | \f | |
2010 | /* BREADTH-FIRST SEARCH ROUTINES. */ | |
2011 | ||
2012 | /* Search a multiple inheritance hierarchy by breadth-first search. | |
2013 | ||
6467930b | 2014 | BINFO is an aggregate type, possibly in a multiple-inheritance hierarchy. |
8d08fdba MS |
2015 | TESTFN is a function, which, if true, means that our condition has been met, |
2016 | and its return value should be returned. | |
2017 | QFN, if non-NULL, is a predicate dictating whether the type should | |
2018 | even be queued. */ | |
2019 | ||
5566b478 | 2020 | static HOST_WIDE_INT |
8d08fdba MS |
2021 | breadth_first_search (binfo, testfn, qfn) |
2022 | tree binfo; | |
49c249e1 JM |
2023 | int (*testfn) PROTO((tree, int)); |
2024 | int (*qfn) PROTO((tree, int)); | |
8d08fdba MS |
2025 | { |
2026 | int head = 0, tail = 0; | |
2027 | int rval = 0; | |
2028 | ||
2029 | search_stack = push_search_level (search_stack, &search_obstack); | |
2030 | ||
2031 | while (1) | |
2032 | { | |
2033 | tree binfos = BINFO_BASETYPES (binfo); | |
2034 | int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2035 | int i; | |
2036 | ||
2037 | /* Process and/or queue base types. */ | |
2038 | for (i = 0; i < n_baselinks; i++) | |
2039 | { | |
2040 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2041 | ||
2042 | if (BINFO_MARKED (base_binfo) == 0 | |
2043 | && (qfn == 0 || (*qfn) (binfo, i))) | |
2044 | { | |
2045 | SET_BINFO_MARKED (base_binfo); | |
2046 | obstack_ptr_grow (&search_obstack, binfo); | |
2047 | obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i); | |
2048 | tail += 2; | |
2049 | if (tail >= search_stack->limit) | |
2050 | my_friendly_abort (100); | |
2051 | } | |
2052 | } | |
2053 | /* Process head of queue, if one exists. */ | |
2054 | if (head >= tail) | |
2055 | { | |
2056 | rval = 0; | |
2057 | break; | |
2058 | } | |
2059 | ||
2060 | binfo = search_stack->first[head++]; | |
2061 | i = (HOST_WIDE_INT) search_stack->first[head++]; | |
a703fb38 | 2062 | if ((rval = (*testfn) (binfo, i))) |
8d08fdba MS |
2063 | break; |
2064 | binfo = BINFO_BASETYPE (binfo, i); | |
2065 | } | |
2066 | { | |
2067 | tree *tp = search_stack->first; | |
2068 | tree *search_tail = tp + tail; | |
2069 | while (tp < search_tail) | |
2070 | { | |
2071 | tree binfo = *tp++; | |
2072 | int i = (HOST_WIDE_INT)(*tp++); | |
2073 | CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i)); | |
2074 | } | |
2075 | } | |
2076 | ||
2077 | search_stack = pop_search_level (search_stack); | |
2078 | return rval; | |
2079 | } | |
2080 | ||
2081 | /* Functions to use in breadth first searches. */ | |
49c249e1 | 2082 | typedef int (*pfi) PROTO((tree, int)); |
8d08fdba | 2083 | |
8d08fdba MS |
2084 | static tree declarator; |
2085 | ||
2086 | static tree | |
2087 | get_virtuals_named_this (binfo) | |
2088 | tree binfo; | |
2089 | { | |
2090 | tree fields; | |
2091 | ||
2092 | fields = lookup_fnfields (binfo, declarator, -1); | |
2093 | /* fields cannot be error_mark_node */ | |
2094 | ||
2095 | if (fields == 0) | |
2096 | return 0; | |
2097 | ||
2098 | /* Get to the function decls, and return the first virtual function | |
2099 | with this name, if there is one. */ | |
2100 | while (fields) | |
2101 | { | |
2102 | tree fndecl; | |
2103 | ||
2c73f9f5 ML |
2104 | for (fndecl = TREE_VALUE (fields); fndecl; fndecl = OVL_NEXT (fndecl)) |
2105 | if (DECL_VINDEX (OVL_CURRENT (fndecl))) | |
8d08fdba MS |
2106 | return fields; |
2107 | fields = next_baselink (fields); | |
2108 | } | |
2109 | return NULL_TREE; | |
2110 | } | |
2111 | ||
fc378698 MS |
2112 | static tree |
2113 | get_virtual_destructor (binfo, i) | |
8d08fdba MS |
2114 | tree binfo; |
2115 | int i; | |
2116 | { | |
2117 | tree type = BINFO_TYPE (binfo); | |
2118 | if (i >= 0) | |
2119 | type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i)); | |
2120 | if (TYPE_HAS_DESTRUCTOR (type) | |
fc378698 MS |
2121 | && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1))) |
2122 | return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1); | |
8d08fdba MS |
2123 | return 0; |
2124 | } | |
2125 | ||
5566b478 MS |
2126 | static int |
2127 | tree_has_any_destructor_p (binfo, i) | |
8d08fdba MS |
2128 | tree binfo; |
2129 | int i; | |
2130 | { | |
2131 | tree type = BINFO_TYPE (binfo); | |
2132 | if (i >= 0) | |
2133 | type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i)); | |
2134 | return TYPE_NEEDS_DESTRUCTOR (type); | |
2135 | } | |
2136 | ||
7177d104 MS |
2137 | /* Given a class type TYPE, and a function decl FNDECL, look for a |
2138 | virtual function in TYPE's hierarchy which FNDECL could match as a | |
2139 | virtual function. It doesn't matter which one we find. | |
8d08fdba MS |
2140 | |
2141 | DTORP is nonzero if we are looking for a destructor. Destructors | |
2142 | need special treatment because they do not match by name. */ | |
e92cc029 | 2143 | |
8d08fdba | 2144 | tree |
7177d104 | 2145 | get_matching_virtual (binfo, fndecl, dtorp) |
8d08fdba MS |
2146 | tree binfo, fndecl; |
2147 | int dtorp; | |
2148 | { | |
2149 | tree tmp = NULL_TREE; | |
2150 | ||
2151 | /* Breadth first search routines start searching basetypes | |
2152 | of TYPE, so we must perform first ply of search here. */ | |
2153 | if (dtorp) | |
2154 | { | |
2155 | if (tree_has_any_destructor_p (binfo, -1)) | |
2156 | tmp = get_virtual_destructor (binfo, -1); | |
2157 | ||
2158 | if (tmp) | |
7177d104 | 2159 | return tmp; |
8d08fdba MS |
2160 | |
2161 | tmp = (tree) breadth_first_search (binfo, | |
2162 | (pfi) get_virtual_destructor, | |
2163 | tree_has_any_destructor_p); | |
8d08fdba MS |
2164 | return tmp; |
2165 | } | |
2166 | else | |
2167 | { | |
2168 | tree drettype, dtypes, btypes, instptr_type; | |
2169 | tree basetype = DECL_CLASS_CONTEXT (fndecl); | |
2170 | tree baselink, best = NULL_TREE; | |
2171 | tree name = DECL_ASSEMBLER_NAME (fndecl); | |
2172 | ||
2173 | declarator = DECL_NAME (fndecl); | |
2174 | if (IDENTIFIER_VIRTUAL_P (declarator) == 0) | |
2175 | return NULL_TREE; | |
2176 | ||
a292b002 MS |
2177 | baselink = get_virtuals_named_this (binfo); |
2178 | if (baselink == NULL_TREE) | |
2179 | return NULL_TREE; | |
2180 | ||
8d08fdba MS |
2181 | drettype = TREE_TYPE (TREE_TYPE (fndecl)); |
2182 | dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
2183 | if (DECL_STATIC_FUNCTION_P (fndecl)) | |
2184 | instptr_type = NULL_TREE; | |
2185 | else | |
2186 | instptr_type = TREE_TYPE (TREE_VALUE (dtypes)); | |
2187 | ||
a292b002 | 2188 | for (; baselink; baselink = next_baselink (baselink)) |
8d08fdba | 2189 | { |
2c73f9f5 ML |
2190 | tree tmps; |
2191 | for (tmps = TREE_VALUE (baselink); tmps; tmps = OVL_NEXT (tmps)) | |
8d08fdba | 2192 | { |
2c73f9f5 | 2193 | tmp = OVL_CURRENT (tmps); |
8d08fdba MS |
2194 | if (! DECL_VINDEX (tmp)) |
2195 | continue; | |
2196 | ||
2197 | btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp)); | |
2198 | if (instptr_type == NULL_TREE) | |
2199 | { | |
2200 | if (compparms (TREE_CHAIN (btypes), dtypes, 3)) | |
2201 | /* Caller knows to give error in this case. */ | |
2202 | return tmp; | |
2203 | return NULL_TREE; | |
2204 | } | |
2205 | ||
2206 | if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes))) | |
2207 | == TYPE_READONLY (instptr_type)) | |
2208 | && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3)) | |
2209 | { | |
e1cd6e56 MS |
2210 | tree brettype = TREE_TYPE (TREE_TYPE (tmp)); |
2211 | if (comptypes (brettype, drettype, 1)) | |
2212 | /* OK */; | |
2213 | else if | |
2214 | (TREE_CODE (brettype) == TREE_CODE (drettype) | |
2215 | && (TREE_CODE (brettype) == POINTER_TYPE | |
2216 | || TREE_CODE (brettype) == REFERENCE_TYPE) | |
2217 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (brettype)), | |
2218 | TYPE_MAIN_VARIANT (TREE_TYPE (drettype)), | |
2219 | 0)) | |
2220 | /* covariant return type */ | |
2221 | { | |
2222 | tree b = TREE_TYPE (brettype), d = TREE_TYPE (drettype); | |
2223 | if (TYPE_MAIN_VARIANT (b) != TYPE_MAIN_VARIANT (d)) | |
2224 | { | |
2225 | tree binfo = get_binfo (b, d, 1); | |
2226 | if (binfo != error_mark_node | |
1052cd65 JM |
2227 | && (! BINFO_OFFSET_ZEROP (binfo) |
2228 | || TREE_VIA_VIRTUAL (binfo))) | |
e1cd6e56 MS |
2229 | sorry ("adjusting pointers for covariant returns"); |
2230 | } | |
2231 | if (TYPE_READONLY (d) > TYPE_READONLY (b)) | |
2232 | { | |
cffa8729 | 2233 | cp_error_at ("return type of `%#D' adds const", fndecl); |
e1cd6e56 MS |
2234 | cp_error_at (" overriding definition as `%#D'", |
2235 | tmp); | |
2236 | } | |
2237 | else if (TYPE_VOLATILE (d) > TYPE_VOLATILE (b)) | |
2238 | { | |
cffa8729 | 2239 | cp_error_at ("return type of `%#D' adds volatile", |
e1cd6e56 MS |
2240 | fndecl); |
2241 | cp_error_at (" overriding definition as `%#D'", | |
2242 | tmp); | |
2243 | } | |
2244 | } | |
2245 | else if (IS_AGGR_TYPE_2 (brettype, drettype) | |
2246 | && comptypes (brettype, drettype, 0)) | |
2247 | { | |
2248 | error ("invalid covariant return type (must use pointer or reference)"); | |
2249 | cp_error_at (" overriding `%#D'", tmp); | |
cffa8729 | 2250 | cp_error_at (" with `%#D'", fndecl); |
e1cd6e56 MS |
2251 | } |
2252 | else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE) | |
8d08fdba | 2253 | { |
cffa8729 | 2254 | cp_error_at ("conflicting return type specified for virtual function `%#D'", fndecl); |
e1cd6e56 | 2255 | cp_error_at (" overriding definition as `%#D'", tmp); |
8d08fdba MS |
2256 | SET_IDENTIFIER_ERROR_LOCUS (name, basetype); |
2257 | } | |
2258 | break; | |
2259 | } | |
2260 | } | |
2c73f9f5 ML |
2261 | /* If not at the end */ |
2262 | if (tmps) | |
8d08fdba | 2263 | { |
7177d104 MS |
2264 | best = tmp; |
2265 | break; | |
8d08fdba MS |
2266 | } |
2267 | } | |
8d08fdba | 2268 | |
8d08fdba MS |
2269 | return best; |
2270 | } | |
2271 | } | |
2272 | ||
7177d104 MS |
2273 | /* Return the list of virtual functions which are abstract in type |
2274 | TYPE that come from non virtual base classes. See | |
2275 | expand_direct_vtbls_init for the style of search we do. */ | |
e92cc029 | 2276 | |
8926095f MS |
2277 | static tree |
2278 | get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals) | |
6b5fbb55 | 2279 | tree binfo; |
8926095f | 2280 | int do_self; |
6b5fbb55 | 2281 | tree abstract_virtuals; |
8d08fdba | 2282 | { |
8926095f MS |
2283 | tree binfos = BINFO_BASETYPES (binfo); |
2284 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
8d08fdba | 2285 | |
8926095f | 2286 | for (i = 0; i < n_baselinks; i++) |
8d08fdba | 2287 | { |
8926095f | 2288 | tree base_binfo = TREE_VEC_ELT (binfos, i); |
beb53fb8 JM |
2289 | int is_not_base_vtable |
2290 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo)); | |
8926095f MS |
2291 | if (! TREE_VIA_VIRTUAL (base_binfo)) |
2292 | abstract_virtuals | |
2293 | = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable, | |
2294 | abstract_virtuals); | |
2295 | } | |
2296 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
2297 | if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))) | |
2298 | { | |
f30432d7 MS |
2299 | tree virtuals = BINFO_VIRTUALS (binfo); |
2300 | ||
2301 | skip_rtti_stuff (&virtuals); | |
8d08fdba | 2302 | |
f30432d7 | 2303 | while (virtuals) |
8d08fdba | 2304 | { |
f30432d7 | 2305 | tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)); |
8d08fdba MS |
2306 | tree base_fndecl = TREE_OPERAND (base_pfn, 0); |
2307 | if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl)) | |
2308 | abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals); | |
f30432d7 | 2309 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
2310 | } |
2311 | } | |
8926095f MS |
2312 | return abstract_virtuals; |
2313 | } | |
2314 | ||
2315 | /* Return the list of virtual functions which are abstract in type TYPE. | |
2316 | This information is cached, and so must be built on a | |
2317 | non-temporary obstack. */ | |
e92cc029 | 2318 | |
8926095f MS |
2319 | tree |
2320 | get_abstract_virtuals (type) | |
2321 | tree type; | |
2322 | { | |
f30432d7 | 2323 | tree vbases; |
8926095f MS |
2324 | tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type); |
2325 | ||
e92cc029 | 2326 | /* First get all from non-virtual bases. */ |
8926095f MS |
2327 | abstract_virtuals |
2328 | = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals); | |
2329 | ||
8d08fdba MS |
2330 | for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases)) |
2331 | { | |
f30432d7 MS |
2332 | tree virtuals = BINFO_VIRTUALS (vbases); |
2333 | ||
2334 | skip_rtti_stuff (&virtuals); | |
8d08fdba | 2335 | |
f30432d7 | 2336 | while (virtuals) |
8d08fdba | 2337 | { |
f30432d7 | 2338 | tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)); |
8d08fdba MS |
2339 | tree base_fndecl = TREE_OPERAND (base_pfn, 0); |
2340 | if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl)) | |
2341 | abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals); | |
f30432d7 | 2342 | virtuals = TREE_CHAIN (virtuals); |
8d08fdba MS |
2343 | } |
2344 | } | |
2345 | return nreverse (abstract_virtuals); | |
2346 | } | |
2347 | ||
2348 | /* For the type TYPE, return a list of member functions available from | |
2349 | base classes with name NAME. The TREE_VALUE of the list is a chain of | |
2350 | member functions with name NAME. The TREE_PURPOSE of the list is a | |
2351 | basetype, or a list of base types (in reverse order) which were | |
2352 | traversed to reach the chain of member functions. If we reach a base | |
2353 | type which provides a member function of name NAME, and which has at | |
2354 | most one base type itself, then we can terminate the search. */ | |
2355 | ||
2356 | tree | |
2357 | get_baselinks (type_as_binfo_list, type, name) | |
2358 | tree type_as_binfo_list; | |
2359 | tree type, name; | |
2360 | { | |
e92cc029 | 2361 | int head = 0, tail = 0, idx; |
8d08fdba MS |
2362 | tree rval = 0, nval = 0; |
2363 | tree basetypes = type_as_binfo_list; | |
2364 | tree binfo = TYPE_BINFO (type); | |
2365 | ||
2366 | search_stack = push_search_level (search_stack, &search_obstack); | |
2367 | ||
2368 | while (1) | |
2369 | { | |
2370 | tree binfos = BINFO_BASETYPES (binfo); | |
2371 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2372 | ||
2373 | /* Process and/or queue base types. */ | |
2374 | for (i = 0; i < n_baselinks; i++) | |
2375 | { | |
2376 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2377 | tree btypes; | |
2378 | ||
2379 | btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo), | |
2380 | TREE_VIA_VIRTUAL (base_binfo), | |
2381 | TREE_VIA_PROTECTED (base_binfo), | |
2382 | NULL_TREE, base_binfo, | |
2383 | basetypes); | |
2384 | obstack_ptr_grow (&search_obstack, btypes); | |
2385 | search_stack->first = (tree *)obstack_base (&search_obstack); | |
2386 | tail += 1; | |
2387 | } | |
2388 | ||
2389 | dont_queue: | |
2390 | /* Process head of queue, if one exists. */ | |
2391 | if (head >= tail) | |
2392 | break; | |
2393 | ||
2394 | basetypes = search_stack->first[head++]; | |
2395 | binfo = TREE_VALUE (basetypes); | |
2396 | type = BINFO_TYPE (binfo); | |
e92cc029 MS |
2397 | idx = lookup_fnfields_1 (type, name); |
2398 | if (idx >= 0) | |
8d08fdba | 2399 | { |
e92cc029 | 2400 | nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); |
8d08fdba MS |
2401 | rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval); |
2402 | if (TYPE_BINFO_BASETYPES (type) == 0) | |
2403 | goto dont_queue; | |
2404 | else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1) | |
2405 | { | |
2406 | if (CLASSTYPE_BASELINK_VEC (type)) | |
e92cc029 | 2407 | TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), idx); |
8d08fdba MS |
2408 | goto dont_queue; |
2409 | } | |
2410 | } | |
2411 | nval = NULL_TREE; | |
2412 | } | |
2413 | ||
2414 | search_stack = pop_search_level (search_stack); | |
2415 | return rval; | |
2416 | } | |
2417 | ||
2418 | tree | |
2419 | next_baselink (baselink) | |
2420 | tree baselink; | |
2421 | { | |
2422 | tree tmp = TREE_TYPE (baselink); | |
2423 | baselink = TREE_CHAIN (baselink); | |
2424 | while (tmp) | |
2425 | { | |
2426 | /* @@ does not yet add previous base types. */ | |
2427 | baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp), | |
2428 | baselink); | |
2429 | TREE_TYPE (baselink) = TREE_TYPE (tmp); | |
2430 | tmp = TREE_CHAIN (tmp); | |
2431 | } | |
2432 | return baselink; | |
2433 | } | |
2434 | \f | |
2435 | /* DEPTH-FIRST SEARCH ROUTINES. */ | |
2436 | ||
22531e51 | 2437 | #ifdef MI_MATRIX |
8d08fdba MS |
2438 | /* Assign unique numbers to _CLASSTYPE members of the lattice |
2439 | specified by TYPE. The root nodes are marked first; the nodes | |
2440 | are marked depth-fisrt, left-right. */ | |
2441 | ||
2442 | static int cid; | |
2443 | ||
2444 | /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT. | |
2445 | Relation yields 1 if C1 <= C2, 0 otherwise. */ | |
2446 | typedef char mi_boolean; | |
2447 | static mi_boolean *mi_matrix; | |
2448 | ||
2449 | /* Type for which this matrix is defined. */ | |
2450 | static tree mi_type; | |
2451 | ||
2452 | /* Size of the matrix for indexing purposes. */ | |
2453 | static int mi_size; | |
2454 | ||
2455 | /* Return nonzero if class C2 derives from class C1. */ | |
2456 | #define BINFO_DERIVES_FROM(C1, C2) \ | |
2457 | ((mi_matrix+mi_size*(BINFO_CID (C1)-1))[BINFO_CID (C2)-1]) | |
2458 | #define TYPE_DERIVES_FROM(C1, C2) \ | |
2459 | ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1]) | |
2460 | #define BINFO_DERIVES_FROM_STAR(C) \ | |
2461 | (mi_matrix+(BINFO_CID (C)-1)) | |
22531e51 | 2462 | #endif |
8d08fdba MS |
2463 | |
2464 | /* This routine converts a pointer to be a pointer of an immediate | |
2465 | base class. The normal convert_pointer_to routine would diagnose | |
2466 | the conversion as ambiguous, under MI code that has the base class | |
e92cc029 MS |
2467 | as an ambiguous base class. */ |
2468 | ||
8d08fdba MS |
2469 | static tree |
2470 | convert_pointer_to_single_level (to_type, expr) | |
2471 | tree to_type, expr; | |
2472 | { | |
2473 | tree binfo_of_derived; | |
2474 | tree last; | |
2475 | ||
2476 | binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))); | |
2477 | last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0); | |
2478 | BINFO_INHERITANCE_CHAIN (last) = binfo_of_derived; | |
2479 | BINFO_INHERITANCE_CHAIN (binfo_of_derived) = NULL_TREE; | |
f30432d7 | 2480 | return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr, last, 1); |
8d08fdba MS |
2481 | } |
2482 | ||
2483 | /* The main function which implements depth first search. | |
2484 | ||
2485 | This routine has to remember the path it walked up, when | |
2486 | dfs_init_vbase_pointers is the work function, as otherwise there | |
e92cc029 MS |
2487 | would be no record. */ |
2488 | ||
8d08fdba MS |
2489 | static void |
2490 | dfs_walk (binfo, fn, qfn) | |
2491 | tree binfo; | |
49c249e1 JM |
2492 | void (*fn) PROTO((tree)); |
2493 | int (*qfn) PROTO((tree)); | |
8d08fdba MS |
2494 | { |
2495 | tree binfos = BINFO_BASETYPES (binfo); | |
2496 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2497 | ||
2498 | for (i = 0; i < n_baselinks; i++) | |
2499 | { | |
2500 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2501 | ||
e1cd6e56 | 2502 | if (qfn == 0 || (*qfn)(base_binfo)) |
8d08fdba | 2503 | { |
73b0fce8 KL |
2504 | if (TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TYPE_PARM |
2505 | || TREE_CODE (BINFO_TYPE (base_binfo)) == TEMPLATE_TEMPLATE_PARM) | |
5566b478 MS |
2506 | /* Pass */; |
2507 | else if (fn == dfs_init_vbase_pointers) | |
8d08fdba MS |
2508 | { |
2509 | /* When traversing an arbitrary MI hierarchy, we need to keep | |
2510 | a record of the path we took to get down to the final base | |
2511 | type, as otherwise there would be no record of it, and just | |
2512 | trying to blindly convert at the bottom would be ambiguous. | |
2513 | ||
2514 | The easiest way is to do the conversions one step at a time, | |
2515 | as we know we want the immediate base class at each step. | |
2516 | ||
2517 | The only special trick to converting one step at a time, | |
2518 | is that when we hit the last virtual base class, we must | |
2519 | use the SLOT value for it, and not use the normal convert | |
2520 | routine. We use the last virtual base class, as in our | |
2521 | implementation, we have pointers to all virtual base | |
2522 | classes in the base object. */ | |
2523 | ||
2524 | tree saved_vbase_decl_ptr_intermediate | |
2525 | = vbase_decl_ptr_intermediate; | |
2526 | ||
2527 | if (TREE_VIA_VIRTUAL (base_binfo)) | |
2528 | { | |
2529 | /* No need for the conversion here, as we know it is the | |
2530 | right type. */ | |
2531 | vbase_decl_ptr_intermediate | |
fc378698 | 2532 | = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)); |
8d08fdba MS |
2533 | } |
2534 | else | |
2535 | { | |
2536 | vbase_decl_ptr_intermediate | |
2537 | = convert_pointer_to_single_level (BINFO_TYPE (base_binfo), | |
2538 | vbase_decl_ptr_intermediate); | |
2539 | } | |
2540 | ||
2541 | dfs_walk (base_binfo, fn, qfn); | |
2542 | ||
2543 | vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate; | |
5566b478 MS |
2544 | } |
2545 | else | |
2546 | dfs_walk (base_binfo, fn, qfn); | |
8d08fdba MS |
2547 | } |
2548 | } | |
2549 | ||
2550 | fn (binfo); | |
2551 | } | |
2552 | ||
22531e51 | 2553 | #ifdef MI_MATRIX |
8d08fdba MS |
2554 | /* Predicate functions which serve for dfs_walk. */ |
2555 | static int numberedp (binfo) tree binfo; | |
2556 | { return BINFO_CID (binfo); } | |
2557 | static int unnumberedp (binfo) tree binfo; | |
2558 | { return BINFO_CID (binfo) == 0; } | |
22531e51 | 2559 | #endif |
8d08fdba MS |
2560 | |
2561 | static int markedp (binfo) tree binfo; | |
2562 | { return BINFO_MARKED (binfo); } | |
8d08fdba MS |
2563 | static int unmarkedp (binfo) tree binfo; |
2564 | { return BINFO_MARKED (binfo) == 0; } | |
5566b478 MS |
2565 | |
2566 | #if 0 | |
2567 | static int bfs_markedp (binfo, i) tree binfo; int i; | |
2568 | { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); } | |
8d08fdba MS |
2569 | static int bfs_unmarkedp (binfo, i) tree binfo; int i; |
2570 | { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; } | |
8d08fdba MS |
2571 | static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i; |
2572 | { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); } | |
8d08fdba MS |
2573 | static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i; |
2574 | { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; } | |
8d08fdba MS |
2575 | static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i; |
2576 | { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); } | |
8d08fdba MS |
2577 | static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i; |
2578 | { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; } | |
5566b478 MS |
2579 | #endif |
2580 | ||
2581 | static int marked_vtable_pathp (binfo) tree binfo; | |
2582 | { return BINFO_VTABLE_PATH_MARKED (binfo); } | |
2583 | static int unmarked_vtable_pathp (binfo) tree binfo; | |
2584 | { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; } | |
2585 | static int marked_new_vtablep (binfo) tree binfo; | |
2586 | { return BINFO_NEW_VTABLE_MARKED (binfo); } | |
2587 | static int unmarked_new_vtablep (binfo) tree binfo; | |
2588 | { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; } | |
8d08fdba | 2589 | |
5566b478 | 2590 | #if 0 |
8d08fdba MS |
2591 | static int dfs_search_slot_nonempty_p (binfo) tree binfo; |
2592 | { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; } | |
5566b478 | 2593 | #endif |
8d08fdba MS |
2594 | |
2595 | static int dfs_debug_unmarkedp (binfo) tree binfo; | |
2596 | { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; } | |
2597 | ||
2598 | /* The worker functions for `dfs_walk'. These do not need to | |
2599 | test anything (vis a vis marking) if they are paired with | |
2600 | a predicate function (above). */ | |
2601 | ||
22531e51 | 2602 | #ifdef MI_MATRIX |
8d08fdba MS |
2603 | /* Assign each type within the lattice a number which is unique |
2604 | in the lattice. The first number assigned is 1. */ | |
2605 | ||
2606 | static void | |
2607 | dfs_number (binfo) | |
2608 | tree binfo; | |
2609 | { | |
2610 | BINFO_CID (binfo) = ++cid; | |
2611 | } | |
2612 | ||
2613 | static void | |
2614 | dfs_unnumber (binfo) | |
2615 | tree binfo; | |
2616 | { | |
2617 | BINFO_CID (binfo) = 0; | |
2618 | } | |
22531e51 | 2619 | #endif |
8d08fdba | 2620 | |
5566b478 | 2621 | #if 0 |
8d08fdba MS |
2622 | static void |
2623 | dfs_mark (binfo) tree binfo; | |
2624 | { SET_BINFO_MARKED (binfo); } | |
5566b478 | 2625 | #endif |
8d08fdba MS |
2626 | |
2627 | static void | |
2628 | dfs_unmark (binfo) tree binfo; | |
2629 | { CLEAR_BINFO_MARKED (binfo); } | |
2630 | ||
5566b478 | 2631 | #if 0 |
8d08fdba MS |
2632 | static void |
2633 | dfs_mark_vtable_path (binfo) tree binfo; | |
2634 | { SET_BINFO_VTABLE_PATH_MARKED (binfo); } | |
2635 | ||
2636 | static void | |
2637 | dfs_unmark_vtable_path (binfo) tree binfo; | |
2638 | { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); } | |
2639 | ||
2640 | static void | |
2641 | dfs_mark_new_vtable (binfo) tree binfo; | |
2642 | { SET_BINFO_NEW_VTABLE_MARKED (binfo); } | |
2643 | ||
2644 | static void | |
2645 | dfs_unmark_new_vtable (binfo) tree binfo; | |
2646 | { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); } | |
2647 | ||
2648 | static void | |
2649 | dfs_clear_search_slot (binfo) tree binfo; | |
2650 | { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; } | |
5566b478 | 2651 | #endif |
8d08fdba MS |
2652 | |
2653 | static void | |
2654 | dfs_debug_mark (binfo) | |
2655 | tree binfo; | |
2656 | { | |
2657 | tree t = BINFO_TYPE (binfo); | |
2658 | ||
2659 | /* Use heuristic that if there are virtual functions, | |
2660 | ignore until we see a non-inline virtual function. */ | |
2661 | tree methods = CLASSTYPE_METHOD_VEC (t); | |
2662 | ||
2663 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
2664 | ||
9a3b49ac MS |
2665 | if (methods == 0) |
2666 | return; | |
2667 | ||
9a3b49ac MS |
2668 | /* If interface info is known, either we've already emitted the debug |
2669 | info or we don't need to. */ | |
2670 | if (CLASSTYPE_INTERFACE_KNOWN (t) | |
8d08fdba MS |
2671 | || (write_virtuals == 2 && TYPE_VIRTUAL_P (t))) |
2672 | return; | |
2673 | ||
2674 | /* If debug info is requested from this context for this type, supply it. | |
2675 | If debug info is requested from another context for this type, | |
2676 | see if some third context can supply it. */ | |
2677 | if (current_function_decl == NULL_TREE | |
2678 | || DECL_CLASS_CONTEXT (current_function_decl) != t) | |
2679 | { | |
fc378698 MS |
2680 | if (TREE_VEC_ELT (methods, 1)) |
2681 | methods = TREE_VEC_ELT (methods, 1); | |
2682 | else if (TREE_VEC_ELT (methods, 0)) | |
8d08fdba MS |
2683 | methods = TREE_VEC_ELT (methods, 0); |
2684 | else | |
fc378698 | 2685 | methods = TREE_VEC_ELT (methods, 2); |
2c73f9f5 | 2686 | methods = OVL_CURRENT (methods); |
8d08fdba MS |
2687 | while (methods) |
2688 | { | |
2689 | if (DECL_VINDEX (methods) | |
44a8d0b3 | 2690 | && DECL_THIS_INLINE (methods) == 0 |
8d08fdba MS |
2691 | && DECL_ABSTRACT_VIRTUAL_P (methods) == 0) |
2692 | { | |
2693 | /* Somebody, somewhere is going to have to define this | |
2694 | virtual function. When they do, they will provide | |
2695 | the debugging info. */ | |
2696 | return; | |
2697 | } | |
2698 | methods = TREE_CHAIN (methods); | |
2699 | } | |
2700 | } | |
2701 | /* We cannot rely on some alien method to solve our problems, | |
2702 | so we must write out the debug info ourselves. */ | |
f0e01782 | 2703 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0; |
2c73f9f5 | 2704 | rest_of_type_compilation (t, toplevel_bindings_p ()); |
8d08fdba MS |
2705 | } |
2706 | \f | |
2707 | /* Attach to the type of the virtual base class, the pointer to the | |
7177d104 MS |
2708 | virtual base class, given the global pointer vbase_decl_ptr. |
2709 | ||
2710 | We use the global vbase_types. ICK! */ | |
e92cc029 | 2711 | |
8d08fdba MS |
2712 | static void |
2713 | dfs_find_vbases (binfo) | |
2714 | tree binfo; | |
2715 | { | |
2716 | tree binfos = BINFO_BASETYPES (binfo); | |
2717 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
2718 | ||
2719 | for (i = n_baselinks-1; i >= 0; i--) | |
2720 | { | |
2721 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
2722 | ||
2723 | if (TREE_VIA_VIRTUAL (base_binfo) | |
2724 | && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0) | |
2725 | { | |
2726 | tree vbase = BINFO_TYPE (base_binfo); | |
2727 | tree binfo = binfo_member (vbase, vbase_types); | |
2728 | ||
2729 | CLASSTYPE_SEARCH_SLOT (vbase) | |
fc378698 MS |
2730 | = build (PLUS_EXPR, build_pointer_type (vbase), |
2731 | vbase_decl_ptr, BINFO_OFFSET (binfo)); | |
8d08fdba MS |
2732 | } |
2733 | } | |
2734 | SET_BINFO_VTABLE_PATH_MARKED (binfo); | |
2735 | SET_BINFO_NEW_VTABLE_MARKED (binfo); | |
2736 | } | |
2737 | ||
2738 | static void | |
2739 | dfs_init_vbase_pointers (binfo) | |
2740 | tree binfo; | |
2741 | { | |
2742 | tree type = BINFO_TYPE (binfo); | |
2743 | tree fields = TYPE_FIELDS (type); | |
8926095f | 2744 | tree this_vbase_ptr; |
8d08fdba MS |
2745 | |
2746 | CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); | |
2747 | ||
6b5fbb55 MS |
2748 | #if 0 |
2749 | /* See finish_struct_1 for when we can enable this. */ | |
2750 | /* If we have a vtable pointer first, skip it. */ | |
2751 | if (VFIELD_NAME_P (DECL_NAME (fields))) | |
8d08fdba | 2752 | fields = TREE_CHAIN (fields); |
6b5fbb55 | 2753 | #endif |
8d08fdba MS |
2754 | |
2755 | if (fields == NULL_TREE | |
2756 | || DECL_NAME (fields) == NULL_TREE | |
2757 | || ! VBASE_NAME_P (DECL_NAME (fields))) | |
2758 | return; | |
2759 | ||
2760 | this_vbase_ptr = vbase_decl_ptr_intermediate; | |
2761 | ||
f30432d7 | 2762 | if (build_pointer_type (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr))) |
8d08fdba MS |
2763 | my_friendly_abort (125); |
2764 | ||
2765 | while (fields && DECL_NAME (fields) | |
2766 | && VBASE_NAME_P (DECL_NAME (fields))) | |
2767 | { | |
2768 | tree ref = build (COMPONENT_REF, TREE_TYPE (fields), | |
2769 | build_indirect_ref (this_vbase_ptr, NULL_PTR), fields); | |
fc378698 | 2770 | tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields))); |
8d08fdba MS |
2771 | vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)), |
2772 | vbase_types), | |
2773 | build_modify_expr (ref, NOP_EXPR, init), | |
2774 | vbase_init_result); | |
2775 | fields = TREE_CHAIN (fields); | |
2776 | } | |
2777 | } | |
2778 | ||
2779 | /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE. Other | |
2780 | times, just NEW_VTABLE, but optimizer should make both with equal | |
2781 | efficiency (though it does not currently). */ | |
e92cc029 | 2782 | |
8d08fdba MS |
2783 | static void |
2784 | dfs_clear_vbase_slots (binfo) | |
2785 | tree binfo; | |
2786 | { | |
2787 | tree type = BINFO_TYPE (binfo); | |
2788 | CLASSTYPE_SEARCH_SLOT (type) = 0; | |
2789 | CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); | |
2790 | CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); | |
2791 | } | |
2792 | ||
2793 | tree | |
2794 | init_vbase_pointers (type, decl_ptr) | |
2795 | tree type; | |
2796 | tree decl_ptr; | |
2797 | { | |
2798 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
2799 | { | |
2800 | int old_flag = flag_this_is_variable; | |
2801 | tree binfo = TYPE_BINFO (type); | |
2802 | flag_this_is_variable = -2; | |
2803 | vbase_types = CLASSTYPE_VBASECLASSES (type); | |
5566b478 | 2804 | vbase_decl_ptr = vbase_decl_ptr_intermediate = decl_ptr; |
8d08fdba MS |
2805 | vbase_init_result = NULL_TREE; |
2806 | dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp); | |
2807 | dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp); | |
2808 | dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep); | |
2809 | flag_this_is_variable = old_flag; | |
2810 | return vbase_init_result; | |
2811 | } | |
2812 | return 0; | |
2813 | } | |
2814 | ||
43f2999d MS |
2815 | /* get the virtual context (the vbase that directly contains the |
2816 | DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in, | |
2817 | or NULL_TREE if there is none. | |
2818 | ||
2819 | FNDECL must come from a virtual table from a virtual base to ensure that | |
2820 | there is only one possible DECL_CLASS_CONTEXT. | |
2821 | ||
2822 | We know that if there is more than one place (binfo) the fndecl that the | |
2823 | declared, they all refer to the same binfo. See get_class_offset_1 for | |
2824 | the check that ensures this. */ | |
e92cc029 | 2825 | |
43f2999d MS |
2826 | static tree |
2827 | virtual_context (fndecl, t, vbase) | |
2828 | tree fndecl, t, vbase; | |
2829 | { | |
2830 | tree path; | |
2831 | if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0) | |
2832 | { | |
f30432d7 MS |
2833 | /* DECL_CLASS_CONTEXT can be ambiguous in t. */ |
2834 | if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0) | |
2835 | { | |
2836 | while (path) | |
2837 | { | |
2838 | /* Not sure if checking path == vbase is necessary here, but just in | |
2839 | case it is. */ | |
2840 | if (TREE_VIA_VIRTUAL (path) || path == vbase) | |
2841 | return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t)); | |
2842 | path = BINFO_INHERITANCE_CHAIN (path); | |
2843 | } | |
2844 | } | |
43f2999d MS |
2845 | /* This shouldn't happen, I don't want errors! */ |
2846 | warning ("recoverable compiler error, fixups for virtual function"); | |
2847 | return vbase; | |
2848 | } | |
2849 | while (path) | |
2850 | { | |
2851 | if (TREE_VIA_VIRTUAL (path)) | |
2852 | return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t)); | |
2853 | path = BINFO_INHERITANCE_CHAIN (path); | |
2854 | } | |
2855 | return 0; | |
2856 | } | |
2857 | ||
2858 | /* Fixups upcast offsets for one vtable. | |
2859 | Entries may stay within the VBASE given, or | |
2860 | they may upcast into a direct base, or | |
2861 | they may upcast into a different vbase. | |
2862 | ||
45537677 MS |
2863 | We only need to do fixups in case 2 and 3. In case 2, we add in |
2864 | the virtual base offset to effect an upcast, in case 3, we add in | |
2865 | the virtual base offset to effect an upcast, then subtract out the | |
2866 | offset for the other virtual base, to effect a downcast into it. | |
43f2999d MS |
2867 | |
2868 | This routine mirrors fixup_vtable_deltas in functionality, though | |
2869 | this one is runtime based, and the other is compile time based. | |
2870 | Conceivably that routine could be removed entirely, and all fixups | |
2871 | done at runtime. | |
2872 | ||
2873 | VBASE_OFFSETS is an association list of virtual bases that contains | |
45537677 MS |
2874 | offset information for the virtual bases, so the offsets are only |
2875 | calculated once. The offsets are computed by where we think the | |
2876 | vbase should be (as noted by the CLASSTYPE_SEARCH_SLOT) minus where | |
e92cc029 MS |
2877 | the vbase really is. */ |
2878 | ||
43f2999d | 2879 | static void |
45537677 MS |
2880 | expand_upcast_fixups (binfo, addr, orig_addr, vbase, vbase_addr, t, |
2881 | vbase_offsets) | |
2882 | tree binfo, addr, orig_addr, vbase, vbase_addr, t, *vbase_offsets; | |
43f2999d MS |
2883 | { |
2884 | tree virtuals = BINFO_VIRTUALS (binfo); | |
2885 | tree vc; | |
2886 | tree delta; | |
2887 | unsigned HOST_WIDE_INT n; | |
2888 | ||
2889 | delta = purpose_member (vbase, *vbase_offsets); | |
2890 | if (! delta) | |
2891 | { | |
fc378698 | 2892 | delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase)); |
45537677 | 2893 | delta = build (MINUS_EXPR, ptrdiff_type_node, delta, vbase_addr); |
43f2999d MS |
2894 | delta = save_expr (delta); |
2895 | delta = tree_cons (vbase, delta, *vbase_offsets); | |
2896 | *vbase_offsets = delta; | |
2897 | } | |
2898 | ||
f30432d7 MS |
2899 | n = skip_rtti_stuff (&virtuals); |
2900 | ||
43f2999d MS |
2901 | while (virtuals) |
2902 | { | |
2903 | tree current_fndecl = TREE_VALUE (virtuals); | |
2904 | current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl); | |
2905 | current_fndecl = TREE_OPERAND (current_fndecl, 0); | |
2906 | if (current_fndecl | |
e8abc66f | 2907 | && current_fndecl != abort_fndecl |
43f2999d MS |
2908 | && (vc=virtual_context (current_fndecl, t, vbase)) != vbase) |
2909 | { | |
e92cc029 | 2910 | /* This may in fact need a runtime fixup. */ |
de22184b | 2911 | tree idx = build_int_2 (n, 0); |
43f2999d MS |
2912 | tree vtbl = BINFO_VTABLE (binfo); |
2913 | tree nvtbl = lookup_name (DECL_NAME (vtbl), 0); | |
2914 | tree aref, ref, naref; | |
2915 | tree old_delta, new_delta; | |
2916 | tree init; | |
2917 | ||
2918 | if (nvtbl == NULL_TREE | |
2919 | || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl))) | |
2920 | { | |
2921 | /* Dup it if it isn't in local scope yet. */ | |
2922 | nvtbl = build_decl (VAR_DECL, | |
2923 | DECL_NAME (vtbl), | |
2924 | TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo)))); | |
2925 | DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node), | |
2926 | DECL_ALIGN (nvtbl)); | |
2927 | TREE_READONLY (nvtbl) = 0; | |
6b5fbb55 | 2928 | DECL_ARTIFICIAL (nvtbl) = 1; |
43f2999d MS |
2929 | nvtbl = pushdecl (nvtbl); |
2930 | init = NULL_TREE; | |
b3417a04 | 2931 | cp_finish_decl (nvtbl, init, NULL_TREE, 0, LOOKUP_ONLYCONVERTING); |
43f2999d MS |
2932 | DECL_VIRTUAL_P (nvtbl) = 1; |
2933 | DECL_CONTEXT (nvtbl) = t; | |
2934 | init = build (MODIFY_EXPR, TREE_TYPE (nvtbl), | |
2935 | nvtbl, vtbl); | |
2936 | TREE_SIDE_EFFECTS (init) = 1; | |
2937 | expand_expr_stmt (init); | |
e92cc029 | 2938 | /* Update the vtable pointers as necessary. */ |
43f2999d MS |
2939 | ref = build_vfield_ref (build_indirect_ref (addr, NULL_PTR), DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo)))); |
2940 | expand_expr_stmt (build_modify_expr (ref, NOP_EXPR, | |
2941 | build_unary_op (ADDR_EXPR, nvtbl, 0))); | |
2942 | } | |
2943 | assemble_external (vtbl); | |
2944 | aref = build_array_ref (vtbl, idx); | |
2945 | naref = build_array_ref (nvtbl, idx); | |
4dabb379 MS |
2946 | old_delta = build_component_ref (aref, delta_identifier, NULL_TREE, 0); |
2947 | new_delta = build_component_ref (naref, delta_identifier, NULL_TREE, 0); | |
45537677 MS |
2948 | |
2949 | /* This is a upcast, so we have to add the offset for the | |
2950 | virtual base. */ | |
43f2999d MS |
2951 | old_delta = build_binary_op (PLUS_EXPR, old_delta, |
2952 | TREE_VALUE (delta), 0); | |
2953 | if (vc) | |
2954 | { | |
45537677 MS |
2955 | /* If this is set, we need to subtract out the delta |
2956 | adjustments for the other virtual base that we | |
2957 | downcast into. */ | |
43f2999d MS |
2958 | tree vc_delta = purpose_member (vc, *vbase_offsets); |
2959 | if (! vc_delta) | |
2960 | { | |
2961 | tree vc_addr = convert_pointer_to_real (vc, orig_addr); | |
fc378698 | 2962 | vc_delta = CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc)); |
43f2999d | 2963 | vc_delta = build (MINUS_EXPR, ptrdiff_type_node, |
45537677 | 2964 | vc_delta, vc_addr); |
43f2999d MS |
2965 | vc_delta = save_expr (vc_delta); |
2966 | *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets); | |
2967 | } | |
2968 | else | |
2969 | vc_delta = TREE_VALUE (vc_delta); | |
2970 | ||
45537677 MS |
2971 | /* This is a downcast, so we have to subtract the offset |
2972 | for the virtual base. */ | |
2973 | old_delta = build_binary_op (MINUS_EXPR, old_delta, vc_delta, 0); | |
43f2999d MS |
2974 | } |
2975 | ||
2976 | TREE_READONLY (new_delta) = 0; | |
2977 | expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR, | |
2978 | old_delta)); | |
2979 | } | |
2980 | ++n; | |
2981 | virtuals = TREE_CHAIN (virtuals); | |
2982 | } | |
2983 | } | |
2984 | ||
2985 | /* Fixup upcast offsets for all direct vtables. Patterned after | |
2986 | expand_direct_vtbls_init. */ | |
e92cc029 | 2987 | |
43f2999d MS |
2988 | static void |
2989 | fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets) | |
6b5fbb55 | 2990 | tree real_binfo, binfo; |
43f2999d | 2991 | int init_self, can_elide; |
6b5fbb55 | 2992 | tree addr, orig_addr, type, vbase, *vbase_offsets; |
43f2999d MS |
2993 | { |
2994 | tree real_binfos = BINFO_BASETYPES (real_binfo); | |
2995 | tree binfos = BINFO_BASETYPES (binfo); | |
2996 | int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0; | |
2997 | ||
2998 | for (i = 0; i < n_baselinks; i++) | |
2999 | { | |
3000 | tree real_base_binfo = TREE_VEC_ELT (real_binfos, i); | |
3001 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
beb53fb8 JM |
3002 | int is_not_base_vtable |
3003 | = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo)); | |
43f2999d MS |
3004 | if (! TREE_VIA_VIRTUAL (real_base_binfo)) |
3005 | fixup_virtual_upcast_offsets (real_base_binfo, base_binfo, | |
3006 | is_not_base_vtable, can_elide, addr, | |
3007 | orig_addr, type, vbase, vbase_offsets); | |
3008 | } | |
3009 | #if 0 | |
3010 | /* Before turning this on, make sure it is correct. */ | |
3011 | if (can_elide && ! BINFO_MODIFIED (binfo)) | |
3012 | return; | |
3013 | #endif | |
3014 | /* Should we use something besides CLASSTYPE_VFIELDS? */ | |
3015 | if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo))) | |
3016 | { | |
45537677 MS |
3017 | tree new_addr = convert_pointer_to_real (binfo, addr); |
3018 | expand_upcast_fixups (real_binfo, new_addr, orig_addr, vbase, addr, | |
3019 | type, vbase_offsets); | |
43f2999d MS |
3020 | } |
3021 | } | |
3022 | ||
8d08fdba MS |
3023 | /* Build a COMPOUND_EXPR which when expanded will generate the code |
3024 | needed to initialize all the virtual function table slots of all | |
7177d104 MS |
3025 | the virtual baseclasses. MAIN_BINFO is the binfo which determines |
3026 | the virtual baseclasses to use; TYPE is the type of the object to | |
3027 | which the initialization applies. TRUE_EXP is the true object we | |
3028 | are initializing, and DECL_PTR is the pointer to the sub-object we | |
8d08fdba MS |
3029 | are initializing. |
3030 | ||
3031 | When USE_COMPUTED_OFFSETS is non-zero, we can assume that the | |
ddd5a7c1 | 3032 | object was laid out by a top-level constructor and the computed |
8d08fdba | 3033 | offsets are valid to store vtables. When zero, we must store new |
7177d104 MS |
3034 | vtables through virtual baseclass pointers. |
3035 | ||
5566b478 | 3036 | We setup and use the globals: vbase_decl_ptr, vbase_types |
7177d104 | 3037 | ICK! */ |
8d08fdba | 3038 | |
8926095f | 3039 | void |
9e9ff709 | 3040 | expand_indirect_vtbls_init (binfo, true_exp, decl_ptr) |
7177d104 | 3041 | tree binfo; |
8d08fdba | 3042 | tree true_exp, decl_ptr; |
8d08fdba | 3043 | { |
8d08fdba | 3044 | tree type = BINFO_TYPE (binfo); |
9e9ff709 | 3045 | |
8d08fdba MS |
3046 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) |
3047 | { | |
43f2999d | 3048 | rtx fixup_insns = NULL_RTX; |
8d08fdba | 3049 | tree vbases = CLASSTYPE_VBASECLASSES (type); |
7177d104 | 3050 | vbase_types = vbases; |
8d08fdba | 3051 | vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr; |
8d08fdba | 3052 | |
43f2999d MS |
3053 | dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep); |
3054 | ||
8d08fdba | 3055 | /* Initialized with vtables of type TYPE. */ |
39211cd5 | 3056 | for (; vbases; vbases = TREE_CHAIN (vbases)) |
8d08fdba | 3057 | { |
7177d104 | 3058 | tree addr; |
9e9ff709 MS |
3059 | |
3060 | addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vbase_decl_ptr); | |
8d08fdba | 3061 | |
e92cc029 | 3062 | /* Do all vtables from this virtual base. */ |
7177d104 | 3063 | /* This assumes that virtual bases can never serve as parent |
956d6950 | 3064 | binfos. (in the CLASSTYPE_VFIELD_PARENT sense) */ |
7177d104 MS |
3065 | expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)), |
3066 | 1, 0, addr); | |
43f2999d | 3067 | |
e92cc029 MS |
3068 | /* Now we adjust the offsets for virtual functions that |
3069 | cross virtual boundaries on an implicit upcast on vf call | |
3070 | so that the layout of the most complete type is used, | |
3071 | instead of assuming the layout of the virtual bases from | |
3072 | our current type. */ | |
43f2999d MS |
3073 | |
3074 | if (flag_vtable_thunks) | |
3075 | { | |
5566b478 | 3076 | /* We don't have dynamic thunks yet! |
e92cc029 | 3077 | So for now, just fail silently. */ |
43f2999d MS |
3078 | } |
3079 | else | |
3080 | { | |
3081 | tree vbase_offsets = NULL_TREE; | |
3082 | push_to_sequence (fixup_insns); | |
3083 | fixup_virtual_upcast_offsets (vbases, | |
3084 | TYPE_BINFO (BINFO_TYPE (vbases)), | |
3085 | 1, 0, addr, vbase_decl_ptr, | |
3086 | type, vbases, &vbase_offsets); | |
3087 | fixup_insns = get_insns (); | |
3088 | end_sequence (); | |
3089 | } | |
3090 | } | |
3091 | ||
3092 | if (fixup_insns) | |
3093 | { | |
3094 | extern tree in_charge_identifier; | |
3095 | tree in_charge_node = lookup_name (in_charge_identifier, 0); | |
3096 | if (! in_charge_node) | |
3097 | { | |
3098 | warning ("recoverable internal compiler error, nobody's in charge!"); | |
3099 | in_charge_node = integer_zero_node; | |
3100 | } | |
3101 | in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1); | |
3102 | expand_start_cond (in_charge_node, 0); | |
3103 | emit_insns (fixup_insns); | |
3104 | expand_end_cond (); | |
8d08fdba MS |
3105 | } |
3106 | ||
3107 | dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep); | |
8d08fdba | 3108 | } |
8d08fdba MS |
3109 | } |
3110 | ||
8d08fdba MS |
3111 | /* get virtual base class types. |
3112 | This adds type to the vbase_types list in reverse dfs order. | |
3113 | Ordering is very important, so don't change it. */ | |
3114 | ||
3115 | static void | |
3116 | dfs_get_vbase_types (binfo) | |
3117 | tree binfo; | |
3118 | { | |
51c184be | 3119 | if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo)) |
8d08fdba | 3120 | { |
8926095f | 3121 | vbase_types = make_binfo (integer_zero_node, binfo, |
51c184be MS |
3122 | BINFO_VTABLE (binfo), |
3123 | BINFO_VIRTUALS (binfo), vbase_types); | |
3124 | TREE_VIA_VIRTUAL (vbase_types) = 1; | |
3125 | SET_BINFO_VBASE_MARKED (binfo); | |
8d08fdba MS |
3126 | } |
3127 | SET_BINFO_MARKED (binfo); | |
3128 | } | |
3129 | ||
51c184be | 3130 | /* get a list of virtual base classes in dfs order. */ |
e92cc029 | 3131 | |
8d08fdba MS |
3132 | tree |
3133 | get_vbase_types (type) | |
3134 | tree type; | |
3135 | { | |
8d08fdba | 3136 | tree vbases; |
51c184be MS |
3137 | tree binfo; |
3138 | ||
3139 | if (TREE_CODE (type) == TREE_VEC) | |
3140 | binfo = type; | |
3141 | else | |
3142 | binfo = TYPE_BINFO (type); | |
8d08fdba MS |
3143 | |
3144 | vbase_types = NULL_TREE; | |
51c184be MS |
3145 | dfs_walk (binfo, dfs_get_vbase_types, unmarkedp); |
3146 | dfs_walk (binfo, dfs_unmark, markedp); | |
8d08fdba MS |
3147 | /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now |
3148 | reverse it so that we get normal dfs ordering. */ | |
3149 | vbase_types = nreverse (vbase_types); | |
3150 | ||
51c184be MS |
3151 | /* unmark marked vbases */ |
3152 | for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases)) | |
3153 | CLEAR_BINFO_VBASE_MARKED (vbases); | |
8d08fdba | 3154 | |
51c184be | 3155 | return vbase_types; |
8d08fdba MS |
3156 | } |
3157 | \f | |
22531e51 | 3158 | #ifdef MI_MATRIX |
8d08fdba MS |
3159 | static void |
3160 | dfs_record_inheritance (binfo) | |
3161 | tree binfo; | |
3162 | { | |
3163 | tree binfos = BINFO_BASETYPES (binfo); | |
3164 | int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
3165 | mi_boolean *derived_row = BINFO_DERIVES_FROM_STAR (binfo); | |
3166 | ||
3167 | for (i = n_baselinks-1; i >= 0; i--) | |
3168 | { | |
3169 | int j; | |
3170 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
3171 | tree baseclass = BINFO_TYPE (base_binfo); | |
3172 | mi_boolean *base_row = BINFO_DERIVES_FROM_STAR (base_binfo); | |
3173 | ||
73b0fce8 KL |
3174 | if (TREE_CODE (baseclass) == TEMPLATE_TYPE_PARM |
3175 | || TREE_CODE (baseclass) == TEMPLATE_TEMPLATE_PARM) | |
37130ac3 JM |
3176 | continue; |
3177 | my_friendly_assert (CLASSTYPE_CID (baseclass) != 0, 2365); | |
3178 | ||
8d08fdba MS |
3179 | /* Don't search if there's nothing there! MI_SIZE can be |
3180 | zero as a result of parse errors. */ | |
3181 | if (TYPE_BINFO_BASETYPES (baseclass) && mi_size > 0) | |
3182 | for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size) | |
3183 | derived_row[j] |= base_row[j]; | |
3184 | TYPE_DERIVES_FROM (baseclass, BINFO_TYPE (binfo)) = 1; | |
3185 | } | |
3186 | ||
3187 | SET_BINFO_MARKED (binfo); | |
3188 | } | |
3189 | ||
3190 | /* Given a _CLASSTYPE node in a multiple inheritance lattice, | |
3191 | convert the lattice into a simple relation such that, | |
3192 | given to CIDs, C1 and C2, one can determine if C1 <= C2 | |
3193 | or C2 <= C1 or C1 <> C2. | |
3194 | ||
3195 | Once constructed, we walk the lattice depth fisrt, | |
3196 | applying various functions to elements as they are encountered. | |
3197 | ||
3198 | We use xmalloc here, in case we want to randomly free these tables. */ | |
3199 | ||
3200 | #define SAVE_MI_MATRIX | |
3201 | ||
3202 | void | |
3203 | build_mi_matrix (type) | |
3204 | tree type; | |
3205 | { | |
3206 | tree binfo = TYPE_BINFO (type); | |
3207 | cid = 0; | |
3208 | ||
3209 | #ifdef SAVE_MI_MATRIX | |
3210 | if (CLASSTYPE_MI_MATRIX (type)) | |
3211 | { | |
3212 | mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type); | |
3213 | mi_matrix = CLASSTYPE_MI_MATRIX (type); | |
3214 | mi_type = type; | |
3215 | dfs_walk (binfo, dfs_number, unnumberedp); | |
3216 | return; | |
3217 | } | |
3218 | #endif | |
3219 | ||
6467930b MS |
3220 | dfs_walk (binfo, dfs_number, unnumberedp); |
3221 | ||
8d08fdba | 3222 | mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type); |
c73964b2 MS |
3223 | if (mi_size < (cid-1)) |
3224 | mi_size = cid-1; | |
8d08fdba MS |
3225 | mi_matrix = (char *)xmalloc ((mi_size + 1) * (mi_size + 1)); |
3226 | mi_type = type; | |
3227 | bzero (mi_matrix, (mi_size + 1) * (mi_size + 1)); | |
8d08fdba MS |
3228 | dfs_walk (binfo, dfs_record_inheritance, unmarkedp); |
3229 | dfs_walk (binfo, dfs_unmark, markedp); | |
3230 | } | |
3231 | ||
3232 | void | |
3233 | free_mi_matrix () | |
3234 | { | |
3235 | dfs_walk (TYPE_BINFO (mi_type), dfs_unnumber, numberedp); | |
3236 | ||
3237 | #ifdef SAVE_MI_MATRIX | |
3238 | CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix; | |
3239 | #else | |
3240 | free (mi_matrix); | |
3241 | mi_size = 0; | |
3242 | cid = 0; | |
3243 | #endif | |
3244 | } | |
22531e51 | 3245 | #endif |
8d08fdba MS |
3246 | \f |
3247 | /* If we want debug info for a type TYPE, make sure all its base types | |
3248 | are also marked as being potentially interesting. This avoids | |
3249 | the problem of not writing any debug info for intermediate basetypes | |
a292b002 | 3250 | that have abstract virtual functions. Also mark member types. */ |
8d08fdba MS |
3251 | |
3252 | void | |
3253 | note_debug_info_needed (type) | |
3254 | tree type; | |
3255 | { | |
a292b002 | 3256 | tree field; |
9a3b49ac MS |
3257 | |
3258 | if (current_template_parms) | |
3259 | return; | |
8857f91e MM |
3260 | |
3261 | if (TYPE_BEING_DEFINED (type)) | |
3262 | /* We can't go looking for the base types and fields just yet. */ | |
3263 | return; | |
9a3b49ac | 3264 | |
d2e5ee5c MS |
3265 | /* We can't do the TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which |
3266 | does not support name references between translation units. Well, we | |
3267 | could, but that would mean putting global labels in the debug output | |
3268 | before each exported type and each of its functions and static data | |
3269 | members. */ | |
3270 | if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG) | |
3271 | return; | |
3272 | ||
8d08fdba | 3273 | dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp); |
a292b002 MS |
3274 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
3275 | { | |
3276 | tree ttype; | |
3277 | if (TREE_CODE (field) == FIELD_DECL | |
3278 | && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field))) | |
3279 | && dfs_debug_unmarkedp (TYPE_BINFO (ttype))) | |
3280 | note_debug_info_needed (ttype); | |
3281 | } | |
8d08fdba MS |
3282 | } |
3283 | \f | |
3284 | /* Subroutines of push_class_decls (). */ | |
3285 | ||
f30432d7 MS |
3286 | /* Add in a decl to the envelope. */ |
3287 | static void | |
3288 | envelope_add_decl (type, decl, values) | |
3289 | tree type, decl, *values; | |
3290 | { | |
3291 | tree context, *tmp; | |
3292 | tree name = DECL_NAME (decl); | |
3293 | int dont_add = 0; | |
3294 | ||
e92cc029 | 3295 | /* virtual base names are always unique. */ |
f30432d7 MS |
3296 | if (VBASE_NAME_P (name)) |
3297 | *values = NULL_TREE; | |
3298 | ||
3299 | /* Possible ambiguity. If its defining type(s) | |
3300 | is (are all) derived from us, no problem. */ | |
3301 | else if (*values && TREE_CODE (*values) != TREE_LIST) | |
3302 | { | |
3303 | tree value = *values; | |
3304 | /* Only complain if we shadow something we can access. */ | |
3305 | if (warn_shadow && TREE_CODE (decl) == FUNCTION_DECL | |
3306 | && ((DECL_LANG_SPECIFIC (*values) | |
3307 | && DECL_CLASS_CONTEXT (value) == current_class_type) | |
3308 | || ! TREE_PRIVATE (value))) | |
3309 | /* Should figure out access control more accurately. */ | |
3310 | { | |
3311 | cp_warning_at ("member `%#D' is shadowed", value); | |
3312 | cp_warning_at ("by member function `%#D'", decl); | |
3313 | warning ("in this context"); | |
3314 | } | |
3315 | ||
3316 | context = (TREE_CODE (value) == FUNCTION_DECL | |
3317 | && DECL_VIRTUAL_P (value)) | |
3318 | ? DECL_CLASS_CONTEXT (value) | |
3319 | : DECL_CONTEXT (value); | |
3320 | ||
3321 | if (context == type) | |
3322 | { | |
3323 | if (TREE_CODE (value) == TYPE_DECL | |
3324 | && DECL_ARTIFICIAL (value)) | |
3325 | *values = NULL_TREE; | |
3326 | else | |
3327 | dont_add = 1; | |
3328 | } | |
22531e51 JM |
3329 | else if (type == current_class_type |
3330 | #ifdef MI_MATRIX | |
3331 | /* If we don't check CLASSTYPE_CID on CONTEXT right now, | |
3332 | we'll end up subtracting from the address of MI_MATRIX, | |
3333 | putting us off in la la land. */ | |
3334 | || (CLASSTYPE_CID (type) | |
3335 | && TYPE_DERIVES_FROM (context, type)) | |
3336 | #else | |
3337 | || DERIVED_FROM_P (context, type) | |
3338 | #endif | |
3339 | ) | |
f30432d7 MS |
3340 | { |
3341 | /* Don't add in *values to list */ | |
3342 | *values = NULL_TREE; | |
3343 | } | |
3344 | else | |
3345 | *values = build_tree_list (NULL_TREE, value); | |
3346 | } | |
3347 | else | |
3348 | for (tmp = values; *tmp;) | |
3349 | { | |
3350 | tree value = TREE_VALUE (*tmp); | |
3351 | my_friendly_assert (TREE_CODE (value) != TREE_LIST, 999); | |
3352 | context = (TREE_CODE (value) == FUNCTION_DECL | |
3353 | && DECL_VIRTUAL_P (value)) | |
3354 | ? DECL_CLASS_CONTEXT (value) | |
3355 | : DECL_CONTEXT (value); | |
3356 | ||
22531e51 JM |
3357 | if (type == current_class_type |
3358 | #ifdef MI_MATRIX | |
3359 | /* If we don't check CLASSTYPE_CID on CONTEXT right now, | |
3360 | we'll end up subtracting from the address of MI_MATRIX, | |
3361 | putting us off in la la land. */ | |
3362 | || (CLASSTYPE_CID (type) | |
3363 | && TYPE_DERIVES_FROM (context, type)) | |
3364 | #else | |
3365 | || DERIVED_FROM_P (context, type) | |
3366 | #endif | |
3367 | ) | |
f30432d7 MS |
3368 | { |
3369 | /* remove *tmp from list */ | |
3370 | *tmp = TREE_CHAIN (*tmp); | |
3371 | } | |
3372 | else | |
3373 | tmp = &TREE_CHAIN (*tmp); | |
3374 | } | |
3375 | ||
3376 | if (! dont_add) | |
3377 | { | |
3378 | /* Put the new contents in our envelope. */ | |
3379 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
3380 | { | |
3381 | *values = tree_cons (name, decl, *values); | |
3382 | TREE_NONLOCAL_FLAG (*values) = 1; | |
3383 | TREE_TYPE (*values) = unknown_type_node; | |
3384 | } | |
3385 | else | |
3386 | { | |
3387 | if (*values) | |
3388 | { | |
3389 | *values = tree_cons (NULL_TREE, decl, *values); | |
3390 | /* Mark this as a potentially ambiguous member. */ | |
3391 | /* Leaving TREE_TYPE blank is intentional. | |
3392 | We cannot use `error_mark_node' (lookup_name) | |
3393 | or `unknown_type_node' (all member functions use this). */ | |
3394 | TREE_NONLOCAL_FLAG (*values) = 1; | |
3395 | } | |
3396 | else | |
3397 | *values = decl; | |
3398 | } | |
3399 | } | |
3400 | } | |
3401 | ||
8d08fdba | 3402 | /* Add the instance variables which this class contributed to the |
f30432d7 MS |
3403 | current class binding contour. When a redefinition occurs, if the |
3404 | redefinition is strictly within a single inheritance path, we just | |
3405 | overwrite the old declaration with the new. If the fields are not | |
3406 | within a single inheritance path, we must cons them. | |
8d08fdba MS |
3407 | |
3408 | In order to know what decls are new (stemming from the current | |
3409 | invocation of push_class_decls) we enclose them in an "envelope", | |
3410 | which is a TREE_LIST node where the TREE_PURPOSE slot contains the | |
3411 | new decl (or possibly a list of competing ones), the TREE_VALUE slot | |
3412 | points to the old value and the TREE_CHAIN slot chains together all | |
3413 | envelopes which needs to be "opened" in push_class_decls. Opening an | |
3414 | envelope means: push the old value onto the class_shadowed list, | |
3415 | install the new one and if it's a TYPE_DECL do the same to the | |
3416 | IDENTIFIER_TYPE_VALUE. Such an envelope is recognized by seeing that | |
3417 | the TREE_PURPOSE slot is non-null, and that it is not an identifier. | |
3418 | Because if it is, it could be a set of overloaded methods from an | |
3419 | outer scope. */ | |
3420 | ||
3421 | static void | |
3422 | dfs_pushdecls (binfo) | |
3423 | tree binfo; | |
3424 | { | |
3425 | tree type = BINFO_TYPE (binfo); | |
3426 | tree fields, *methods, *end; | |
3427 | tree method_vec; | |
3428 | ||
3429 | for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) | |
3430 | { | |
3431 | /* Unmark so that if we are in a constructor, and then find that | |
3432 | this field was initialized by a base initializer, | |
3433 | we can emit an error message. */ | |
3434 | if (TREE_CODE (fields) == FIELD_DECL) | |
3435 | TREE_USED (fields) = 0; | |
3436 | ||
3437 | /* Recurse into anonymous unions. */ | |
3438 | if (DECL_NAME (fields) == NULL_TREE | |
3439 | && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE) | |
3440 | { | |
3441 | dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields))); | |
3442 | continue; | |
3443 | } | |
3444 | ||
8d08fdba MS |
3445 | if (DECL_NAME (fields)) |
3446 | { | |
f30432d7 MS |
3447 | tree name = DECL_NAME (fields); |
3448 | tree class_value = IDENTIFIER_CLASS_VALUE (name); | |
3449 | ||
3450 | /* If the class value is not an envelope of the kind described in | |
3451 | the comment above, we create a new envelope. */ | |
3452 | if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST | |
3453 | || TREE_PURPOSE (class_value) == NULL_TREE | |
3454 | || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE) | |
8d08fdba MS |
3455 | { |
3456 | /* See comment above for a description of envelopes. */ | |
f30432d7 MS |
3457 | closed_envelopes = tree_cons (NULL_TREE, class_value, |
3458 | closed_envelopes); | |
3459 | IDENTIFIER_CLASS_VALUE (name) = closed_envelopes; | |
3460 | class_value = IDENTIFIER_CLASS_VALUE (name); | |
8d08fdba | 3461 | } |
f30432d7 MS |
3462 | |
3463 | envelope_add_decl (type, fields, &TREE_PURPOSE (class_value)); | |
8d08fdba MS |
3464 | } |
3465 | } | |
3466 | ||
3467 | method_vec = CLASSTYPE_METHOD_VEC (type); | |
fc378698 | 3468 | if (method_vec) |
8d08fdba MS |
3469 | { |
3470 | /* Farm out constructors and destructors. */ | |
fc378698 | 3471 | methods = &TREE_VEC_ELT (method_vec, 2); |
8d08fdba MS |
3472 | end = TREE_VEC_END (method_vec); |
3473 | ||
8d08fdba MS |
3474 | while (methods != end) |
3475 | { | |
3476 | /* This will cause lookup_name to return a pointer | |
f30432d7 | 3477 | to the tree_list of possible methods of this name. */ |
2c73f9f5 | 3478 | tree name = DECL_NAME (OVL_CURRENT (*methods)); |
f30432d7 MS |
3479 | tree class_value = IDENTIFIER_CLASS_VALUE (name); |
3480 | ||
3481 | /* If the class value is not an envelope of the kind described in | |
3482 | the comment above, we create a new envelope. */ | |
3483 | if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST | |
3484 | || TREE_PURPOSE (class_value) == NULL_TREE | |
3485 | || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE) | |
8d08fdba | 3486 | { |
8d08fdba | 3487 | /* See comment above for a description of envelopes. */ |
f30432d7 | 3488 | closed_envelopes = tree_cons (NULL_TREE, class_value, |
8d08fdba | 3489 | closed_envelopes); |
f30432d7 MS |
3490 | IDENTIFIER_CLASS_VALUE (name) = closed_envelopes; |
3491 | class_value = IDENTIFIER_CLASS_VALUE (name); | |
8d08fdba | 3492 | } |
f30432d7 MS |
3493 | |
3494 | /* Here we try to rule out possible ambiguities. | |
3495 | If we can't do that, keep a TREE_LIST with possibly ambiguous | |
3496 | decls in there. */ | |
3497 | maybe_push_cache_obstack (); | |
2c73f9f5 | 3498 | envelope_add_decl (type, OVL_CURRENT (*methods), &TREE_PURPOSE (class_value)); |
f30432d7 | 3499 | pop_obstacks (); |
8d08fdba MS |
3500 | |
3501 | methods++; | |
3502 | } | |
3503 | } | |
3504 | SET_BINFO_MARKED (binfo); | |
3505 | } | |
3506 | ||
3507 | /* Consolidate unique (by name) member functions. */ | |
e92cc029 | 3508 | |
8d08fdba MS |
3509 | static void |
3510 | dfs_compress_decls (binfo) | |
3511 | tree binfo; | |
3512 | { | |
3513 | tree type = BINFO_TYPE (binfo); | |
3514 | tree method_vec = CLASSTYPE_METHOD_VEC (type); | |
3515 | ||
3516 | if (method_vec != 0) | |
3517 | { | |
3518 | /* Farm out constructors and destructors. */ | |
fc378698 | 3519 | tree *methods = &TREE_VEC_ELT (method_vec, 2); |
8d08fdba MS |
3520 | tree *end = TREE_VEC_END (method_vec); |
3521 | ||
3522 | for (; methods != end; methods++) | |
3523 | { | |
3524 | /* This is known to be an envelope of the kind described before | |
3525 | dfs_pushdecls. */ | |
2c73f9f5 ML |
3526 | tree class_value = |
3527 | IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (*methods))); | |
8d08fdba MS |
3528 | tree tmp = TREE_PURPOSE (class_value); |
3529 | ||
3530 | /* This was replaced in scope by somebody else. Just leave it | |
3531 | alone. */ | |
3532 | if (TREE_CODE (tmp) != TREE_LIST) | |
3533 | continue; | |
3534 | ||
3535 | if (TREE_CHAIN (tmp) == NULL_TREE | |
3536 | && TREE_VALUE (tmp) | |
2c73f9f5 | 3537 | && OVL_NEXT (TREE_VALUE (tmp)) == NULL_TREE) |
8d08fdba MS |
3538 | { |
3539 | TREE_PURPOSE (class_value) = TREE_VALUE (tmp); | |
3540 | } | |
3541 | } | |
3542 | } | |
3543 | CLEAR_BINFO_MARKED (binfo); | |
3544 | } | |
3545 | ||
3546 | /* When entering the scope of a class, we cache all of the | |
3547 | fields that that class provides within its inheritance | |
3548 | lattice. Where ambiguities result, we mark them | |
3549 | with `error_mark_node' so that if they are encountered | |
3550 | without explicit qualification, we can emit an error | |
45537677 | 3551 | message. */ |
e92cc029 | 3552 | |
8d08fdba | 3553 | void |
45537677 | 3554 | push_class_decls (type) |
8d08fdba MS |
3555 | tree type; |
3556 | { | |
8d08fdba | 3557 | struct obstack *ambient_obstack = current_obstack; |
8d08fdba MS |
3558 | search_stack = push_search_level (search_stack, &search_obstack); |
3559 | ||
8d08fdba MS |
3560 | /* Push class fields into CLASS_VALUE scope, and mark. */ |
3561 | dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarkedp); | |
3562 | ||
3563 | /* Compress fields which have only a single entry | |
3564 | by a given name, and unmark. */ | |
3565 | dfs_walk (TYPE_BINFO (type), dfs_compress_decls, markedp); | |
3566 | ||
3567 | /* Open up all the closed envelopes and push the contained decls into | |
3568 | class scope. */ | |
3569 | while (closed_envelopes) | |
3570 | { | |
3571 | tree new = TREE_PURPOSE (closed_envelopes); | |
3572 | tree id; | |
3573 | ||
3574 | /* This is messy because the class value may be a *_DECL, or a | |
3575 | TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous | |
3576 | *_DECLs. The name is stored at different places in these three | |
3577 | cases. */ | |
3578 | if (TREE_CODE (new) == TREE_LIST) | |
3579 | { | |
3580 | if (TREE_PURPOSE (new) != NULL_TREE) | |
3581 | id = TREE_PURPOSE (new); | |
3582 | else | |
3583 | { | |
3584 | tree node = TREE_VALUE (new); | |
3585 | ||
013bc8af MS |
3586 | if (TREE_CODE (node) == TYPE_DECL |
3587 | && DECL_ARTIFICIAL (node) | |
3588 | && IS_AGGR_TYPE (TREE_TYPE (node)) | |
3589 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (node))) | |
3590 | { | |
3591 | tree t = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (node)); | |
3592 | tree n = new; | |
3593 | ||
3594 | for (; n; n = TREE_CHAIN (n)) | |
3595 | { | |
3596 | tree d = TREE_VALUE (n); | |
3597 | if (TREE_CODE (d) == TYPE_DECL | |
3598 | && DECL_ARTIFICIAL (node) | |
3599 | && IS_AGGR_TYPE (TREE_TYPE (d)) | |
3600 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d)) | |
3601 | && CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d)) == t) | |
3602 | /* OK */; | |
3603 | else | |
3604 | break; | |
3605 | } | |
3606 | ||
3607 | if (n == NULL_TREE) | |
3608 | new = t; | |
3609 | } | |
3610 | else while (TREE_CODE (node) == TREE_LIST) | |
8d08fdba MS |
3611 | node = TREE_VALUE (node); |
3612 | id = DECL_NAME (node); | |
3613 | } | |
3614 | } | |
3615 | else | |
3616 | id = DECL_NAME (new); | |
3617 | ||
3618 | /* Install the original class value in order to make | |
3619 | pushdecl_class_level work correctly. */ | |
3620 | IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes); | |
45537677 | 3621 | if (TREE_CODE (new) == TREE_LIST) |
8d08fdba MS |
3622 | push_class_level_binding (id, new); |
3623 | else | |
3624 | pushdecl_class_level (new); | |
3625 | closed_envelopes = TREE_CHAIN (closed_envelopes); | |
3626 | } | |
3627 | current_obstack = ambient_obstack; | |
3628 | } | |
3629 | ||
3630 | /* Here's a subroutine we need because C lacks lambdas. */ | |
e92cc029 | 3631 | |
8d08fdba MS |
3632 | static void |
3633 | dfs_unuse_fields (binfo) | |
3634 | tree binfo; | |
3635 | { | |
3636 | tree type = TREE_TYPE (binfo); | |
3637 | tree fields; | |
3638 | ||
3639 | for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) | |
3640 | { | |
3641 | if (TREE_CODE (fields) != FIELD_DECL) | |
3642 | continue; | |
3643 | ||
3644 | TREE_USED (fields) = 0; | |
3645 | if (DECL_NAME (fields) == NULL_TREE | |
3646 | && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE) | |
3647 | unuse_fields (TREE_TYPE (fields)); | |
3648 | } | |
3649 | } | |
3650 | ||
3651 | void | |
3652 | unuse_fields (type) | |
3653 | tree type; | |
3654 | { | |
3655 | dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp); | |
3656 | } | |
3657 | ||
3658 | void | |
5566b478 | 3659 | pop_class_decls () |
8d08fdba MS |
3660 | { |
3661 | /* We haven't pushed a search level when dealing with cached classes, | |
3662 | so we'd better not try to pop it. */ | |
3663 | if (search_stack) | |
3664 | search_stack = pop_search_level (search_stack); | |
3665 | } | |
3666 | ||
8d08fdba MS |
3667 | void |
3668 | print_search_statistics () | |
3669 | { | |
3670 | #ifdef GATHER_STATISTICS | |
3671 | if (flag_memoize_lookups) | |
3672 | { | |
3673 | fprintf (stderr, "%d memoized contexts saved\n", | |
3674 | n_contexts_saved); | |
3675 | fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter); | |
3676 | fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter); | |
3677 | fprintf (stderr, "fields statistics:\n"); | |
3678 | fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n", | |
3679 | memoized_fast_finds[0], memoized_fast_rejects[0], | |
3680 | memoized_fields_searched[0]); | |
3681 | fprintf (stderr, " memoized_adds = %d\n", memoized_adds[0]); | |
3682 | fprintf (stderr, "fnfields statistics:\n"); | |
3683 | fprintf (stderr, " memoized finds = %d; rejects = %d; (searches = %d)\n", | |
3684 | memoized_fast_finds[1], memoized_fast_rejects[1], | |
3685 | memoized_fields_searched[1]); | |
3686 | fprintf (stderr, " memoized_adds = %d\n", memoized_adds[1]); | |
3687 | } | |
3688 | fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n", | |
3689 | n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1); | |
3690 | fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n", | |
3691 | n_outer_fields_searched, n_calls_lookup_fnfields); | |
3692 | fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type); | |
fc378698 | 3693 | #else /* GATHER_STATISTICS */ |
8d08fdba | 3694 | fprintf (stderr, "no search statistics\n"); |
fc378698 | 3695 | #endif /* GATHER_STATISTICS */ |
8d08fdba MS |
3696 | } |
3697 | ||
3698 | void | |
3699 | init_search_processing () | |
3700 | { | |
3701 | gcc_obstack_init (&search_obstack); | |
3702 | gcc_obstack_init (&type_obstack); | |
3703 | gcc_obstack_init (&type_obstack_entries); | |
3704 | ||
3705 | /* This gives us room to build our chains of basetypes, | |
3706 | whether or not we decide to memoize them. */ | |
5566b478 | 3707 | type_stack = push_type_level ((struct stack_level *)0, &type_obstack); |
8d08fdba MS |
3708 | _vptr_name = get_identifier ("_vptr"); |
3709 | } | |
3710 | ||
3711 | void | |
3712 | reinit_search_statistics () | |
3713 | { | |
3714 | my_memoized_entry_counter = 0; | |
3715 | memoized_fast_finds[0] = 0; | |
3716 | memoized_fast_finds[1] = 0; | |
3717 | memoized_adds[0] = 0; | |
3718 | memoized_adds[1] = 0; | |
3719 | memoized_fast_rejects[0] = 0; | |
3720 | memoized_fast_rejects[1] = 0; | |
3721 | memoized_fields_searched[0] = 0; | |
3722 | memoized_fields_searched[1] = 0; | |
5566b478 | 3723 | #ifdef GATHER_STATISTICS |
8d08fdba MS |
3724 | n_fields_searched = 0; |
3725 | n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0; | |
3726 | n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0; | |
3727 | n_calls_get_base_type = 0; | |
3728 | n_outer_fields_searched = 0; | |
3729 | n_contexts_saved = 0; | |
fc378698 | 3730 | #endif /* GATHER_STATISTICS */ |
8d08fdba | 3731 | } |
e1cd6e56 | 3732 | |
e66d884e JM |
3733 | #define scratch_tree_cons expr_tree_cons |
3734 | ||
e1cd6e56 MS |
3735 | static tree conversions; |
3736 | static void | |
3737 | add_conversions (binfo) | |
3738 | tree binfo; | |
3739 | { | |
72b7eeff | 3740 | int i; |
fc378698 | 3741 | tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo)); |
72b7eeff | 3742 | |
fc378698 | 3743 | for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i) |
72b7eeff | 3744 | { |
fc378698 | 3745 | tree tmp = TREE_VEC_ELT (method_vec, i); |
2c73f9f5 | 3746 | if (! IDENTIFIER_TYPENAME_P (DECL_NAME (OVL_CURRENT (tmp)))) |
72b7eeff | 3747 | break; |
e66d884e | 3748 | conversions = scratch_tree_cons (binfo, tmp, conversions); |
72b7eeff | 3749 | } |
c73964b2 | 3750 | SET_BINFO_MARKED (binfo); |
e1cd6e56 MS |
3751 | } |
3752 | ||
3753 | tree | |
3754 | lookup_conversions (type) | |
3755 | tree type; | |
3756 | { | |
3757 | conversions = NULL_TREE; | |
e92cc029 | 3758 | if (TYPE_SIZE (type)) |
c73964b2 MS |
3759 | { |
3760 | dfs_walk (TYPE_BINFO (type), add_conversions, unmarkedp); | |
3761 | dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp); | |
3762 | } | |
e1cd6e56 MS |
3763 | return conversions; |
3764 | } | |
6467930b MS |
3765 | |
3766 | /* Subroutine of get_template_base. */ | |
3767 | ||
3768 | static tree | |
3769 | get_template_base_recursive (binfo, rval, template, via_virtual) | |
3770 | tree binfo, template, rval; | |
3771 | int via_virtual; | |
3772 | { | |
3773 | tree binfos; | |
3774 | int i, n_baselinks; | |
3775 | tree type = BINFO_TYPE (binfo); | |
3776 | ||
3777 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
3778 | && CLASSTYPE_TI_TEMPLATE (type) == template) | |
3779 | { | |
3780 | if (rval == NULL_TREE || rval == type) | |
3781 | return type; | |
3782 | else | |
3783 | return error_mark_node; | |
3784 | } | |
3785 | ||
3786 | binfos = BINFO_BASETYPES (binfo); | |
3787 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
3788 | ||
3789 | /* Process base types. */ | |
3790 | for (i = 0; i < n_baselinks; i++) | |
3791 | { | |
3792 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
3793 | ||
3794 | /* Find any specific instance of a virtual base, when searching with | |
3795 | a binfo... */ | |
3796 | if (BINFO_MARKED (base_binfo) == 0) | |
3797 | { | |
3798 | int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo); | |
3799 | ||
3800 | /* When searching for a non-virtual, we cannot mark | |
3801 | virtually found binfos. */ | |
3802 | if (! this_virtual) | |
3803 | SET_BINFO_MARKED (base_binfo); | |
3804 | ||
3805 | rval = get_template_base_recursive | |
3806 | (base_binfo, rval, template, this_virtual); | |
3807 | if (rval == error_mark_node) | |
3808 | return rval; | |
3809 | } | |
3810 | } | |
3811 | ||
3812 | return rval; | |
3813 | } | |
3814 | ||
3815 | /* Given a class template TEMPLATE and a class type or binfo node BINFO, | |
3816 | find the unique base type in BINFO that is an instance of TEMPLATE. | |
3817 | If there are more than one, return error_mark_node. Used by unify. */ | |
3818 | ||
3819 | tree | |
3820 | get_template_base (template, binfo) | |
3821 | register tree template, binfo; | |
3822 | { | |
a703fb38 | 3823 | tree type = NULL_TREE, rval; |
6467930b MS |
3824 | |
3825 | if (TREE_CODE (binfo) == TREE_VEC) | |
3826 | type = BINFO_TYPE (binfo); | |
3827 | else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo))) | |
3828 | { | |
3829 | type = complete_type (binfo); | |
3830 | binfo = TYPE_BINFO (type); | |
3831 | } | |
3832 | else | |
3833 | my_friendly_abort (92); | |
3834 | ||
3835 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
3836 | && CLASSTYPE_TI_TEMPLATE (type) == template) | |
3837 | return type; | |
3838 | ||
3839 | rval = get_template_base_recursive (binfo, NULL_TREE, template, 0); | |
3840 | dfs_walk (binfo, dfs_unmark, markedp); | |
3841 | ||
3842 | return rval; | |
3843 | } | |
732dcb6f JM |
3844 | |
3845 | /* Check whether the empty class indicated by EMPTY_BINFO is also present | |
3846 | at offset 0 in COMPARE_TYPE, and set found_overlap if so. */ | |
3847 | ||
3848 | static tree compare_type; | |
3849 | static int found_overlap; | |
3850 | static void | |
3851 | dfs_check_overlap (empty_binfo) | |
3852 | tree empty_binfo; | |
3853 | { | |
3854 | tree binfo; | |
3855 | for (binfo = TYPE_BINFO (compare_type); ; binfo = BINFO_BASETYPE (binfo, 0)) | |
3856 | { | |
3857 | if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo)) | |
3858 | { | |
3859 | found_overlap = 1; | |
3860 | break; | |
3861 | } | |
3862 | else if (BINFO_BASETYPES (binfo) == NULL_TREE) | |
3863 | break; | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | /* Trivial function to stop base traversal when we find something. */ | |
3868 | ||
3869 | static int | |
3870 | dfs_no_overlap_yet (t) | |
3871 | tree t; | |
3872 | { | |
3873 | return found_overlap == 0; | |
3874 | } | |
3875 | ||
3876 | /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at | |
3877 | offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */ | |
3878 | ||
3879 | int | |
3880 | types_overlap_p (empty_type, next_type) | |
3881 | tree empty_type, next_type; | |
3882 | { | |
3883 | if (! IS_AGGR_TYPE (next_type)) | |
3884 | return 0; | |
3885 | compare_type = next_type; | |
3886 | found_overlap = 0; | |
3887 | dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap, dfs_no_overlap_yet); | |
3888 | return found_overlap; | |
3889 | } |