]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/search.c
re PR c++/33212 (Broken diagnostic: 'trait_expr' not supported by dump_expr)
[gcc.git] / gcc / cp / search.c
CommitLineData
8d08fdba
MS
1/* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
fed3cef0 3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
e77f031d 4 1999, 2000, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
8d08fdba
MS
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
f5adbb8d 7This file is part of GCC.
8d08fdba 8
f5adbb8d 9GCC is free software; you can redistribute it and/or modify
8d08fdba 10it under the terms of the GNU General Public License as published by
e77f031d 11the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
12any later version.
13
f5adbb8d 14GCC is distributed in the hope that it will be useful,
8d08fdba
MS
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
e77f031d
NC
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
8d08fdba 22
e92cc029 23/* High-level class interface. */
8d08fdba
MS
24
25#include "config.h"
8d052bc7 26#include "system.h"
4977bab6
ZW
27#include "coretypes.h"
28#include "tm.h"
e7a587ef 29#include "tree.h"
8d08fdba
MS
30#include "cp-tree.h"
31#include "obstack.h"
32#include "flags.h"
43f2999d 33#include "rtl.h"
e8abc66f 34#include "output.h"
54f92bfb 35#include "toplev.h"
8d08fdba 36
f8ad2d21 37static int is_subobject_of_p (tree, tree);
2c2e8978 38static tree dfs_lookup_base (tree, void *);
6936e493
NS
39static tree dfs_dcast_hint_pre (tree, void *);
40static tree dfs_dcast_hint_post (tree, void *);
86ac0575 41static tree dfs_debug_mark (tree, void *);
5d5a519f
NS
42static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
43 tree (*post_fn) (tree, void *), void *data);
44static void dfs_unmark_r (tree);
8f2a734f
NS
45static int check_hidden_convs (tree, int, int, tree, tree, tree);
46static tree split_conversions (tree, tree, tree, tree);
47static int lookup_conversions_r (tree, int, int,
48 tree, tree, tree, tree, tree *, tree *);
86ac0575 49static int look_for_overrides_r (tree, tree);
86ac0575 50static tree lookup_field_r (tree, void *);
6936e493
NS
51static tree dfs_accessible_post (tree, void *);
52static tree dfs_walk_once_accessible_r (tree, bool, bool,
53 tree (*pre_fn) (tree, void *),
54 tree (*post_fn) (tree, void *),
55 void *data);
56static tree dfs_walk_once_accessible (tree, bool,
57 tree (*pre_fn) (tree, void *),
58 tree (*post_fn) (tree, void *),
59 void *data);
86ac0575
NS
60static tree dfs_access_in_type (tree, void *);
61static access_kind access_in_type (tree, tree);
86ac0575
NS
62static int protected_accessible_p (tree, tree, tree);
63static int friend_accessible_p (tree, tree, tree);
86ac0575 64static int template_self_reference_p (tree, tree);
86ac0575 65static tree dfs_get_pure_virtuals (tree, void *);
8d08fdba 66
8d08fdba 67\f
8d08fdba 68/* Variables for gathering statistics. */
5566b478 69#ifdef GATHER_STATISTICS
8d08fdba
MS
70static int n_fields_searched;
71static int n_calls_lookup_field, n_calls_lookup_field_1;
72static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
73static int n_calls_get_base_type;
74static int n_outer_fields_searched;
75static int n_contexts_saved;
fc378698 76#endif /* GATHER_STATISTICS */
8d08fdba 77
8d08fdba 78\f
2c2e8978
NS
79/* Data for lookup_base and its workers. */
80
81struct lookup_base_data_s
338d90b8 82{
03fd3f84 83 tree t; /* type being searched. */
0cbd7506
MS
84 tree base; /* The base type we're looking for. */
85 tree binfo; /* Found binfo. */
86 bool via_virtual; /* Found via a virtual path. */
2c2e8978 87 bool ambiguous; /* Found multiply ambiguous */
0cbd7506 88 bool repeated_base; /* Whether there are repeated bases in the
2c2e8978 89 hierarchy. */
0cbd7506 90 bool want_any; /* Whether we want any matching binfo. */
2c2e8978
NS
91};
92
93/* Worker function for lookup_base. See if we've found the desired
f0ec2b9a 94 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */
338d90b8 95
2c2e8978
NS
96static tree
97dfs_lookup_base (tree binfo, void *data_)
98{
67f5655f 99 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
338d90b8 100
2c2e8978
NS
101 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
102 {
103 if (!data->binfo)
338d90b8 104 {
2c2e8978
NS
105 data->binfo = binfo;
106 data->via_virtual
107 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
c8094d83 108
2c2e8978
NS
109 if (!data->repeated_base)
110 /* If there are no repeated bases, we can stop now. */
111 return binfo;
c8094d83 112
2c2e8978
NS
113 if (data->want_any && !data->via_virtual)
114 /* If this is a non-virtual base, then we can't do
115 better. */
116 return binfo;
c8094d83 117
2c2e8978
NS
118 return dfs_skip_bases;
119 }
120 else
121 {
122 gcc_assert (binfo != data->binfo);
c8094d83 123
2c2e8978
NS
124 /* We've found more than one matching binfo. */
125 if (!data->want_any)
126 {
127 /* This is immediately ambiguous. */
128 data->binfo = NULL_TREE;
129 data->ambiguous = true;
130 return error_mark_node;
131 }
132
133 /* Prefer one via a non-virtual path. */
134 if (!binfo_via_virtual (binfo, data->t))
135 {
136 data->binfo = binfo;
137 data->via_virtual = false;
138 return binfo;
139 }
127b8136 140
2c2e8978
NS
141 /* There must be repeated bases, otherwise we'd have stopped
142 on the first base we found. */
143 return dfs_skip_bases;
338d90b8
NS
144 }
145 }
c8094d83 146
2c2e8978 147 return NULL_TREE;
338d90b8
NS
148}
149
bd16cb25 150/* Returns true if type BASE is accessible in T. (BASE is known to be
18e4be85
NS
151 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is
152 true, consider any special access of the current scope, or access
153 bestowed by friendship. */
bd16cb25
MM
154
155bool
18e4be85 156accessible_base_p (tree t, tree base, bool consider_local_p)
bd16cb25
MM
157{
158 tree decl;
159
160 /* [class.access.base]
161
162 A base class is said to be accessible if an invented public
c8094d83 163 member of the base class is accessible.
26bcf8fc
MM
164
165 If BASE is a non-proper base, this condition is trivially
166 true. */
167 if (same_type_p (t, base))
168 return true;
bd16cb25
MM
169 /* Rather than inventing a public member, we use the implicit
170 public typedef created in the scope of every class. */
171 decl = TYPE_FIELDS (base);
172 while (!DECL_SELF_REFERENCE_P (decl))
173 decl = TREE_CHAIN (decl);
174 while (ANON_AGGR_TYPE_P (t))
175 t = TYPE_CONTEXT (t);
18e4be85 176 return accessible_p (t, decl, consider_local_p);
bd16cb25
MM
177}
178
338d90b8 179/* Lookup BASE in the hierarchy dominated by T. Do access checking as
dbbf88d1
NS
180 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
181 non-NULL, fill with information about what kind of base we
182 discovered.
338d90b8 183
50ad9642
MM
184 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
185 not set in ACCESS, then an error is issued and error_mark_node is
186 returned. If the ba_quiet bit is set, then no error is issued and
187 NULL_TREE is returned. */
338d90b8
NS
188
189tree
86ac0575 190lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
338d90b8 191{
2c2e8978
NS
192 tree binfo;
193 tree t_binfo;
338d90b8 194 base_kind bk;
c8094d83 195
338d90b8
NS
196 if (t == error_mark_node || base == error_mark_node)
197 {
198 if (kind_ptr)
199 *kind_ptr = bk_not_base;
200 return error_mark_node;
201 }
50bc768d 202 gcc_assert (TYPE_P (base));
c8094d83 203
4ba126e4
MM
204 if (!TYPE_P (t))
205 {
206 t_binfo = t;
207 t = BINFO_TYPE (t);
208 }
2c2e8978 209 else
cad7e87b
NS
210 {
211 t = complete_type (TYPE_MAIN_VARIANT (t));
212 t_binfo = TYPE_BINFO (t);
213 }
c8094d83 214
cad7e87b
NS
215 base = complete_type (TYPE_MAIN_VARIANT (base));
216
217 if (t_binfo)
2c2e8978
NS
218 {
219 struct lookup_base_data_s data;
220
221 data.t = t;
222 data.base = base;
223 data.binfo = NULL_TREE;
224 data.ambiguous = data.via_virtual = false;
225 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
226 data.want_any = access == ba_any;
227
228 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
229 binfo = data.binfo;
c8094d83 230
2c2e8978
NS
231 if (!binfo)
232 bk = data.ambiguous ? bk_ambig : bk_not_base;
233 else if (binfo == t_binfo)
234 bk = bk_same_type;
235 else if (data.via_virtual)
236 bk = bk_via_virtual;
237 else
238 bk = bk_proper_base;
239 }
cad7e87b 240 else
2c2e8978
NS
241 {
242 binfo = NULL_TREE;
243 bk = bk_not_base;
244 }
338d90b8 245
e80706c4
MM
246 /* Check that the base is unambiguous and accessible. */
247 if (access != ba_any)
248 switch (bk)
249 {
250 case bk_not_base:
251 break;
252
253 case bk_ambig:
e80706c4
MM
254 if (!(access & ba_quiet))
255 {
a82e1a7d 256 error ("%qT is an ambiguous base of %qT", base, t);
e80706c4
MM
257 binfo = error_mark_node;
258 }
259 break;
260
261 default:
18e4be85 262 if ((access & ba_check_bit)
e80706c4
MM
263 /* If BASE is incomplete, then BASE and TYPE are probably
264 the same, in which case BASE is accessible. If they
265 are not the same, then TYPE is invalid. In that case,
266 there's no need to issue another error here, and
267 there's no implicit typedef to use in the code that
268 follows, so we skip the check. */
bd16cb25 269 && COMPLETE_TYPE_P (base)
18e4be85 270 && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
e80706c4 271 {
bd16cb25 272 if (!(access & ba_quiet))
e80706c4 273 {
a82e1a7d 274 error ("%qT is an inaccessible base of %qT", base, t);
bd16cb25 275 binfo = error_mark_node;
e80706c4 276 }
bd16cb25
MM
277 else
278 binfo = NULL_TREE;
279 bk = bk_inaccessible;
e80706c4
MM
280 }
281 break;
282 }
283
338d90b8
NS
284 if (kind_ptr)
285 *kind_ptr = bk;
c8094d83 286
338d90b8
NS
287 return binfo;
288}
289
6936e493 290/* Data for dcast_base_hint walker. */
4a9e5c67 291
6936e493 292struct dcast_data_s
4a9e5c67 293{
6936e493
NS
294 tree subtype; /* The base type we're looking for. */
295 int virt_depth; /* Number of virtual bases encountered from most
296 derived. */
297 tree offset; /* Best hint offset discovered so far. */
298 bool repeated_base; /* Whether there are repeated bases in the
d740dbe7 299 hierarchy. */
6936e493
NS
300};
301
302/* Worker for dcast_base_hint. Search for the base type being cast
303 from. */
304
305static tree
306dfs_dcast_hint_pre (tree binfo, void *data_)
307{
67f5655f 308 struct dcast_data_s *data = (struct dcast_data_s *) data_;
6936e493
NS
309
310 if (BINFO_VIRTUAL_P (binfo))
311 data->virt_depth++;
c8094d83 312
6936e493 313 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
4a9e5c67 314 {
6936e493
NS
315 if (data->virt_depth)
316 {
317 data->offset = ssize_int (-1);
318 return data->offset;
319 }
320 if (data->offset)
321 data->offset = ssize_int (-3);
4a9e5c67 322 else
6936e493
NS
323 data->offset = BINFO_OFFSET (binfo);
324
325 return data->repeated_base ? dfs_skip_bases : data->offset;
4a9e5c67 326 }
6936e493
NS
327
328 return NULL_TREE;
329}
330
331/* Worker for dcast_base_hint. Track the virtual depth. */
332
333static tree
334dfs_dcast_hint_post (tree binfo, void *data_)
335{
67f5655f 336 struct dcast_data_s *data = (struct dcast_data_s *) data_;
6936e493
NS
337
338 if (BINFO_VIRTUAL_P (binfo))
339 data->virt_depth--;
340
341 return NULL_TREE;
4a9e5c67
NS
342}
343
f08dda39
NS
344/* The dynamic cast runtime needs a hint about how the static SUBTYPE type
345 started from is related to the required TARGET type, in order to optimize
306ef644 346 the inheritance graph search. This information is independent of the
4a9e5c67
NS
347 current context, and ignores private paths, hence get_base_distance is
348 inappropriate. Return a TREE specifying the base offset, BOFF.
349 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
350 and there are no public virtual SUBTYPE bases.
f08dda39
NS
351 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
352 BOFF == -2, SUBTYPE is not a public base.
353 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
4a9e5c67
NS
354
355tree
6936e493 356dcast_base_hint (tree subtype, tree target)
4a9e5c67 357{
6936e493
NS
358 struct dcast_data_s data;
359
360 data.subtype = subtype;
361 data.virt_depth = 0;
362 data.offset = NULL_TREE;
363 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
c8094d83 364
6936e493
NS
365 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
366 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
367 return data.offset ? data.offset : ssize_int (-2);
4a9e5c67
NS
368}
369
c717c5af
MM
370/* Search for a member with name NAME in a multiple inheritance
371 lattice specified by TYPE. If it does not exist, return NULL_TREE.
8d08fdba 372 If the member is ambiguously referenced, return `error_mark_node'.
c717c5af
MM
373 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
374 true, type declarations are preferred. */
8d08fdba
MS
375
376/* Do a 1-level search for NAME as a member of TYPE. The caller must
377 figure out whether it can access this field. (Since it is only one
378 level, this is reasonable.) */
e92cc029 379
75135253 380tree
c717c5af 381lookup_field_1 (tree type, tree name, bool want_type)
8d08fdba 382{
926ce8bd 383 tree field;
f84b4be9
JM
384
385 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11e74ea6
KL
386 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
387 || TREE_CODE (type) == TYPENAME_TYPE)
c8094d83 388 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
11e74ea6 389 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
f84b4be9
JM
390 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
391 the code often worked even when we treated the index as a list
11e74ea6
KL
392 of fields!)
393 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
f84b4be9
JM
394 return NULL_TREE;
395
f90cdf34
MT
396 if (TYPE_NAME (type)
397 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
398 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
399 {
d07605f5
AP
400 tree *fields = &DECL_SORTED_FIELDS (TYPE_NAME (type))->elts[0];
401 int lo = 0, hi = DECL_SORTED_FIELDS (TYPE_NAME (type))->len;
f90cdf34
MT
402 int i;
403
404 while (lo < hi)
405 {
406 i = (lo + hi) / 2;
407
408#ifdef GATHER_STATISTICS
409 n_fields_searched++;
410#endif /* GATHER_STATISTICS */
411
412 if (DECL_NAME (fields[i]) > name)
413 hi = i;
414 else if (DECL_NAME (fields[i]) < name)
415 lo = i + 1;
416 else
bff3ce71 417 {
c717c5af
MM
418 field = NULL_TREE;
419
bff3ce71
JM
420 /* We might have a nested class and a field with the
421 same name; we sorted them appropriately via
de0c0e69
NS
422 field_decl_cmp, so just look for the first or last
423 field with this name. */
424 if (want_type)
c717c5af 425 {
de0c0e69
NS
426 do
427 field = fields[i--];
428 while (i >= lo && DECL_NAME (fields[i]) == name);
429 if (TREE_CODE (field) != TYPE_DECL
430 && !DECL_CLASS_TEMPLATE_P (field))
431 field = NULL_TREE;
432 }
433 else
434 {
435 do
436 field = fields[i++];
437 while (i < hi && DECL_NAME (fields[i]) == name);
c717c5af 438 }
c717c5af 439 return field;
bff3ce71 440 }
f90cdf34
MT
441 }
442 return NULL_TREE;
443 }
444
f84b4be9 445 field = TYPE_FIELDS (type);
8d08fdba
MS
446
447#ifdef GATHER_STATISTICS
448 n_calls_lookup_field_1++;
fc378698 449#endif /* GATHER_STATISTICS */
c717c5af 450 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8d08fdba
MS
451 {
452#ifdef GATHER_STATISTICS
453 n_fields_searched++;
fc378698 454#endif /* GATHER_STATISTICS */
50bc768d 455 gcc_assert (DECL_P (field));
8d08fdba 456 if (DECL_NAME (field) == NULL_TREE
6bdb8141 457 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8d08fdba 458 {
c717c5af 459 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
8d08fdba
MS
460 if (temp)
461 return temp;
462 }
2036a15c 463 if (TREE_CODE (field) == USING_DECL)
90ea9897
MM
464 {
465 /* We generally treat class-scope using-declarations as
466 ARM-style access specifications, because support for the
467 ISO semantics has not been implemented. So, in general,
468 there's no reason to return a USING_DECL, and the rest of
469 the compiler cannot handle that. Once the class is
470 defined, USING_DECLs are purged from TYPE_FIELDS; see
471 handle_using_decl. However, we make special efforts to
023458fa 472 make using-declarations in class templates and class
98ed9dae
NS
473 template partial specializations work correctly. */
474 if (!DECL_DEPENDENT_P (field))
90ea9897
MM
475 continue;
476 }
c717c5af
MM
477
478 if (DECL_NAME (field) == name
c8094d83 479 && (!want_type
c717c5af
MM
480 || TREE_CODE (field) == TYPE_DECL
481 || DECL_CLASS_TEMPLATE_P (field)))
65f36ac8 482 return field;
8d08fdba
MS
483 }
484 /* Not found. */
9cd64686 485 if (name == vptr_identifier)
8d08fdba
MS
486 {
487 /* Give the user what s/he thinks s/he wants. */
4c6b7393 488 if (TYPE_POLYMORPHIC_P (type))
d3a3fb6a 489 return TYPE_VFIELD (type);
8d08fdba
MS
490 }
491 return NULL_TREE;
492}
493
a5201a91 494/* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
c8094d83 495 NAMESPACE_DECL corresponding to the innermost non-block scope. */
a5201a91
MM
496
497tree
6ac1920d 498current_scope (void)
a5201a91
MM
499{
500 /* There are a number of cases we need to be aware of here:
7177d104 501 current_class_type current_function_decl
e92cc029
MS
502 global NULL NULL
503 fn-local NULL SET
504 class-local SET NULL
505 class->fn SET SET
506 fn->class SET SET
7177d104 507
a5201a91
MM
508 Those last two make life interesting. If we're in a function which is
509 itself inside a class, we need decls to go into the fn's decls (our
510 second case below). But if we're in a class and the class itself is
511 inside a function, we need decls to go into the decls for the class. To
512 achieve this last goal, we must see if, when both current_class_ptr and
513 current_function_decl are set, the class was declared inside that
514 function. If so, we know to put the decls into the class's scope. */
515 if (current_function_decl && current_class_type
516 && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
517 && same_type_p (DECL_CONTEXT (current_function_decl),
518 current_class_type))
519 || (DECL_FRIEND_CONTEXT (current_function_decl)
520 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
521 current_class_type))))
8d08fdba 522 return current_function_decl;
a5201a91
MM
523 if (current_class_type)
524 return current_class_type;
525 if (current_function_decl)
8d08fdba 526 return current_function_decl;
a5201a91 527 return current_namespace;
8d08fdba
MS
528}
529
838dfd8a 530/* Returns nonzero if we are currently in a function scope. Note
9188c363
MM
531 that this function returns zero if we are within a local class, but
532 not within a member function body of the local class. */
533
534int
edaf3e03 535at_function_scope_p (void)
9188c363
MM
536{
537 tree cs = current_scope ();
538 return cs && TREE_CODE (cs) == FUNCTION_DECL;
539}
540
5f261ba9
MM
541/* Returns true if the innermost active scope is a class scope. */
542
543bool
edaf3e03 544at_class_scope_p (void)
5f261ba9
MM
545{
546 tree cs = current_scope ();
547 return cs && TYPE_P (cs);
548}
549
afb0918a
MM
550/* Returns true if the innermost active scope is a namespace scope. */
551
552bool
553at_namespace_scope_p (void)
554{
a5201a91
MM
555 tree cs = current_scope ();
556 return cs && TREE_CODE (cs) == NAMESPACE_DECL;
afb0918a
MM
557}
558
d6479fe7 559/* Return the scope of DECL, as appropriate when doing name-lookup. */
8d08fdba 560
55de1b66 561tree
86ac0575 562context_for_name_lookup (tree decl)
d6479fe7
MM
563{
564 /* [class.union]
c8094d83 565
d6479fe7
MM
566 For the purposes of name lookup, after the anonymous union
567 definition, the members of the anonymous union are considered to
834c6dff 568 have been defined in the scope in which the anonymous union is
c8094d83 569 declared. */
55de1b66 570 tree context = DECL_CONTEXT (decl);
d6479fe7 571
55de1b66 572 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
d6479fe7
MM
573 context = TYPE_CONTEXT (context);
574 if (!context)
575 context = global_namespace;
8d08fdba 576
d6479fe7
MM
577 return context;
578}
8d08fdba 579
c35cce41 580/* The accessibility routines use BINFO_ACCESS for scratch space
cd0be382 581 during the computation of the accessibility of some declaration. */
c35cce41
MM
582
583#define BINFO_ACCESS(NODE) \
dbbf88d1 584 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
c35cce41
MM
585
586/* Set the access associated with NODE to ACCESS. */
587
588#define SET_BINFO_ACCESS(NODE, ACCESS) \
dbbf88d1
NS
589 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
590 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
c35cce41 591
d6479fe7
MM
592/* Called from access_in_type via dfs_walk. Calculate the access to
593 DATA (which is really a DECL) in BINFO. */
eae89e04 594
d6479fe7 595static tree
86ac0575 596dfs_access_in_type (tree binfo, void *data)
d6479fe7
MM
597{
598 tree decl = (tree) data;
599 tree type = BINFO_TYPE (binfo);
c35cce41 600 access_kind access = ak_none;
8d08fdba 601
d6479fe7 602 if (context_for_name_lookup (decl) == type)
8d08fdba 603 {
a653d067 604 /* If we have descended to the scope of DECL, just note the
d6479fe7
MM
605 appropriate access. */
606 if (TREE_PRIVATE (decl))
c35cce41 607 access = ak_private;
d6479fe7 608 else if (TREE_PROTECTED (decl))
c35cce41 609 access = ak_protected;
d6479fe7 610 else
c35cce41 611 access = ak_public;
8d08fdba 612 }
c8094d83 613 else
d6479fe7
MM
614 {
615 /* First, check for an access-declaration that gives us more
616 access to the DECL. The CONST_DECL for an enumeration
617 constant will not have DECL_LANG_SPECIFIC, and thus no
618 DECL_ACCESS. */
8e4ce833 619 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
d6479fe7 620 {
c35cce41 621 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
c8094d83 622
c35cce41 623 if (decl_access)
dbbf88d1
NS
624 {
625 decl_access = TREE_VALUE (decl_access);
c8094d83 626
dbbf88d1
NS
627 if (decl_access == access_public_node)
628 access = ak_public;
629 else if (decl_access == access_protected_node)
630 access = ak_protected;
631 else if (decl_access == access_private_node)
632 access = ak_private;
633 else
50bc768d 634 gcc_unreachable ();
dbbf88d1 635 }
d6479fe7
MM
636 }
637
638 if (!access)
639 {
640 int i;
63d1c7b3 641 tree base_binfo;
d4e6fecb 642 VEC(tree,gc) *accesses;
c8094d83 643
d6479fe7
MM
644 /* Otherwise, scan our baseclasses, and pick the most favorable
645 access. */
604a3205 646 accesses = BINFO_BASE_ACCESSES (binfo);
fa743e8c 647 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
d6479fe7 648 {
63d1c7b3 649 tree base_access = VEC_index (tree, accesses, i);
dbbf88d1 650 access_kind base_access_now = BINFO_ACCESS (base_binfo);
d6479fe7 651
dbbf88d1 652 if (base_access_now == ak_none || base_access_now == ak_private)
d6479fe7
MM
653 /* If it was not accessible in the base, or only
654 accessible as a private member, we can't access it
655 all. */
dbbf88d1
NS
656 base_access_now = ak_none;
657 else if (base_access == access_protected_node)
658 /* Public and protected members in the base become
d6479fe7 659 protected here. */
dbbf88d1
NS
660 base_access_now = ak_protected;
661 else if (base_access == access_private_node)
662 /* Public and protected members in the base become
d6479fe7 663 private here. */
dbbf88d1 664 base_access_now = ak_private;
d6479fe7
MM
665
666 /* See if the new access, via this base, gives more
667 access than our previous best access. */
dbbf88d1
NS
668 if (base_access_now != ak_none
669 && (access == ak_none || base_access_now < access))
d6479fe7 670 {
dbbf88d1 671 access = base_access_now;
8d08fdba 672
d6479fe7 673 /* If the new access is public, we can't do better. */
c35cce41 674 if (access == ak_public)
d6479fe7
MM
675 break;
676 }
677 }
678 }
679 }
faae18ab 680
d6479fe7 681 /* Note the access to DECL in TYPE. */
c35cce41 682 SET_BINFO_ACCESS (binfo, access);
02020185 683
d6479fe7
MM
684 return NULL_TREE;
685}
8d08fdba 686
d6479fe7 687/* Return the access to DECL in TYPE. */
8d08fdba 688
c35cce41 689static access_kind
86ac0575 690access_in_type (tree type, tree decl)
d6479fe7
MM
691{
692 tree binfo = TYPE_BINFO (type);
8d08fdba 693
d6479fe7 694 /* We must take into account
8d08fdba 695
d6479fe7 696 [class.paths]
8d08fdba 697
d6479fe7
MM
698 If a name can be reached by several paths through a multiple
699 inheritance graph, the access is that of the path that gives
c8094d83 700 most access.
8d08fdba 701
d6479fe7
MM
702 The algorithm we use is to make a post-order depth-first traversal
703 of the base-class hierarchy. As we come up the tree, we annotate
704 each node with the most lenient access. */
5d5a519f 705 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
8d08fdba 706
c35cce41 707 return BINFO_ACCESS (binfo);
d6479fe7
MM
708}
709
838dfd8a 710/* Returns nonzero if it is OK to access DECL through an object
e80706c4 711 indicated by BINFO in the context of DERIVED. */
6a629cac
MM
712
713static int
86ac0575 714protected_accessible_p (tree decl, tree derived, tree binfo)
6a629cac 715{
c35cce41 716 access_kind access;
6a629cac
MM
717
718 /* We're checking this clause from [class.access.base]
719
720 m as a member of N is protected, and the reference occurs in a
721 member or friend of class N, or in a member or friend of a
722 class P derived from N, where m as a member of P is private or
c8094d83 723 protected.
6a629cac 724
d7cca31e
JM
725 Here DERIVED is a possible P and DECL is m. accessible_p will
726 iterate over various values of N, but the access to m in DERIVED
727 does not change.
728
729 Note that I believe that the passage above is wrong, and should read
730 "...is private or protected or public"; otherwise you get bizarre results
731 whereby a public using-decl can prevent you from accessing a protected
732 member of a base. (jason 2000/02/28) */
733
734 /* If DERIVED isn't derived from m's class, then it can't be a P. */
e185aa16 735 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
6a629cac
MM
736 return 0;
737
738 access = access_in_type (derived, decl);
d7cca31e
JM
739
740 /* If m is inaccessible in DERIVED, then it's not a P. */
c35cce41 741 if (access == ak_none)
6a629cac 742 return 0;
c8094d83 743
6a629cac
MM
744 /* [class.protected]
745
746 When a friend or a member function of a derived class references
747 a protected nonstatic member of a base class, an access check
748 applies in addition to those described earlier in clause
d7cca31e 749 _class.access_) Except when forming a pointer to member
6a629cac
MM
750 (_expr.unary.op_), the access must be through a pointer to,
751 reference to, or object of the derived class itself (or any class
752 derived from that class) (_expr.ref_). If the access is to form
753 a pointer to member, the nested-name-specifier shall name the
754 derived class (or any class derived from that class). */
755 if (DECL_NONSTATIC_MEMBER_P (decl))
756 {
757 /* We can tell through what the reference is occurring by
758 chasing BINFO up to the root. */
759 tree t = binfo;
760 while (BINFO_INHERITANCE_CHAIN (t))
761 t = BINFO_INHERITANCE_CHAIN (t);
c8094d83 762
6a629cac
MM
763 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
764 return 0;
765 }
766
767 return 1;
768}
769
838dfd8a 770/* Returns nonzero if SCOPE is a friend of a type which would be able
d7cca31e 771 to access DECL through the object indicated by BINFO. */
6a629cac
MM
772
773static int
86ac0575 774friend_accessible_p (tree scope, tree decl, tree binfo)
6a629cac
MM
775{
776 tree befriending_classes;
777 tree t;
778
779 if (!scope)
780 return 0;
781
782 if (TREE_CODE (scope) == FUNCTION_DECL
783 || DECL_FUNCTION_TEMPLATE_P (scope))
784 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
785 else if (TYPE_P (scope))
786 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
787 else
788 return 0;
789
790 for (t = befriending_classes; t; t = TREE_CHAIN (t))
d7cca31e 791 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
6a629cac
MM
792 return 1;
793
03b1c206
JM
794 /* Nested classes have the same access as their enclosing types, as
795 per DR 45 (this is a change from the standard). */
445ab443
JM
796 if (TYPE_P (scope))
797 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
bdc3400f 798 if (protected_accessible_p (decl, t, binfo))
445ab443
JM
799 return 1;
800
6a629cac
MM
801 if (TREE_CODE (scope) == FUNCTION_DECL
802 || DECL_FUNCTION_TEMPLATE_P (scope))
803 {
c8094d83
MS
804 /* Perhaps this SCOPE is a member of a class which is a
805 friend. */
18e4be85 806 if (DECL_CLASS_SCOPE_P (scope)
d7cca31e 807 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
6a629cac
MM
808 return 1;
809
810 /* Or an instantiation of something which is a friend. */
811 if (DECL_TEMPLATE_INFO (scope))
e59f7322
KL
812 {
813 int ret;
814 /* Increment processing_template_decl to make sure that
815 dependent_type_p works correctly. */
816 ++processing_template_decl;
817 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
818 --processing_template_decl;
819 return ret;
820 }
6a629cac 821 }
6a629cac
MM
822
823 return 0;
70adf8a9
JM
824}
825
6936e493
NS
826/* Called via dfs_walk_once_accessible from accessible_p */
827
5d5a519f 828static tree
6936e493 829dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
5d5a519f 830{
a5201a91
MM
831 if (BINFO_ACCESS (binfo) != ak_none)
832 {
833 tree scope = current_scope ();
834 if (scope && TREE_CODE (scope) != NAMESPACE_DECL
835 && is_friend (BINFO_TYPE (binfo), scope))
836 return binfo;
837 }
c8094d83 838
6936e493 839 return NULL_TREE;
5d5a519f
NS
840}
841
d6479fe7 842/* DECL is a declaration from a base class of TYPE, which was the
838dfd8a 843 class used to name DECL. Return nonzero if, in the current
d6479fe7 844 context, DECL is accessible. If TYPE is actually a BINFO node,
8084bf81 845 then we can tell in what context the access is occurring by looking
18e4be85
NS
846 at the most derived class along the path indicated by BINFO. If
847 CONSIDER_LOCAL is true, do consider special access the current
03fd3f84 848 scope or friendship thereof we might have. */
d6479fe7 849
c8094d83 850int
18e4be85 851accessible_p (tree type, tree decl, bool consider_local_p)
d6479fe7 852{
d6479fe7 853 tree binfo;
0e8c9b28 854 tree scope;
a653d067 855 access_kind access;
d6479fe7 856
838dfd8a 857 /* Nonzero if it's OK to access DECL if it has protected
d6479fe7
MM
858 accessibility in TYPE. */
859 int protected_ok = 0;
860
d6479fe7
MM
861 /* If this declaration is in a block or namespace scope, there's no
862 access control. */
863 if (!TYPE_P (context_for_name_lookup (decl)))
864 return 1;
865
0e8c9b28
MM
866 /* There is no need to perform access checks inside a thunk. */
867 scope = current_scope ();
868 if (scope && DECL_THUNK_P (scope))
869 return 1;
870
279b8466
MM
871 /* In a template declaration, we cannot be sure whether the
872 particular specialization that is instantiated will be a friend
873 or not. Therefore, all access checks are deferred until
94c813b4
MM
874 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the
875 parameter list for a template (because we may see dependent types
876 in default arguments for template parameters), and access
3db45ab5
MS
877 checking should be performed in the outermost parameter list. */
878 if (processing_template_decl
94c813b4 879 && (!processing_template_parmlist || processing_template_decl > 1))
279b8466
MM
880 return 1;
881
d6479fe7
MM
882 if (!TYPE_P (type))
883 {
884 binfo = type;
885 type = BINFO_TYPE (type);
8d08fdba 886 }
d6479fe7
MM
887 else
888 binfo = TYPE_BINFO (type);
889
890 /* [class.access.base]
891
892 A member m is accessible when named in class N if
893
894 --m as a member of N is public, or
8d08fdba 895
d6479fe7
MM
896 --m as a member of N is private, and the reference occurs in a
897 member or friend of class N, or
8d08fdba 898
d6479fe7
MM
899 --m as a member of N is protected, and the reference occurs in a
900 member or friend of class N, or in a member or friend of a
901 class P derived from N, where m as a member of P is private or
902 protected, or
903
904 --there exists a base class B of N that is accessible at the point
c8094d83 905 of reference, and m is accessible when named in class B.
d6479fe7
MM
906
907 We walk the base class hierarchy, checking these conditions. */
908
18e4be85
NS
909 if (consider_local_p)
910 {
911 /* Figure out where the reference is occurring. Check to see if
912 DECL is private or protected in this scope, since that will
913 determine whether protected access is allowed. */
914 if (current_class_type)
915 protected_ok = protected_accessible_p (decl,
916 current_class_type, binfo);
917
918 /* Now, loop through the classes of which we are a friend. */
919 if (!protected_ok)
920 protected_ok = friend_accessible_p (scope, decl, binfo);
921 }
8d08fdba 922
70adf8a9
JM
923 /* Standardize the binfo that access_in_type will use. We don't
924 need to know what path was chosen from this point onwards. */
d6479fe7
MM
925 binfo = TYPE_BINFO (type);
926
927 /* Compute the accessibility of DECL in the class hierarchy
928 dominated by type. */
a653d067
KL
929 access = access_in_type (type, decl);
930 if (access == ak_public
931 || (access == ak_protected && protected_ok))
932 return 1;
c8094d83 933
18e4be85
NS
934 if (!consider_local_p)
935 return 0;
c8094d83 936
18e4be85
NS
937 /* Walk the hierarchy again, looking for a base class that allows
938 access. */
939 return dfs_walk_once_accessible (binfo, /*friends=*/true,
940 NULL, dfs_accessible_post, NULL)
941 != NULL_TREE;
8d08fdba
MS
942}
943
7d4bdeed 944struct lookup_field_info {
d6479fe7
MM
945 /* The type in which we're looking. */
946 tree type;
7d4bdeed
MM
947 /* The name of the field for which we're looking. */
948 tree name;
949 /* If non-NULL, the current result of the lookup. */
950 tree rval;
951 /* The path to RVAL. */
952 tree rval_binfo;
d6479fe7
MM
953 /* If non-NULL, the lookup was ambiguous, and this is a list of the
954 candidates. */
7d4bdeed 955 tree ambiguous;
838dfd8a 956 /* If nonzero, we are looking for types, not data members. */
7d4bdeed
MM
957 int want_type;
958 /* If something went wrong, a message indicating what. */
d8e178a0 959 const char *errstr;
7d4bdeed
MM
960};
961
9188c363
MM
962/* Within the scope of a template class, you can refer to the to the
963 current specialization with the name of the template itself. For
964 example:
c8094d83 965
8f032717
MM
966 template <typename T> struct S { S* sp; }
967
838dfd8a 968 Returns nonzero if DECL is such a declaration in a class TYPE. */
8f032717
MM
969
970static int
86ac0575 971template_self_reference_p (tree type, tree decl)
8f032717
MM
972{
973 return (CLASSTYPE_USE_TEMPLATE (type)
3fc5037b 974 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
8f032717
MM
975 && TREE_CODE (decl) == TYPE_DECL
976 && DECL_ARTIFICIAL (decl)
977 && DECL_NAME (decl) == constructor_name (type));
978}
979
bd0d5d4a
JM
980/* Nonzero for a class member means that it is shared between all objects
981 of that class.
982
983 [class.member.lookup]:If the resulting set of declarations are not all
984 from sub-objects of the same type, or the set has a nonstatic member
985 and includes members from distinct sub-objects, there is an ambiguity
986 and the program is ill-formed.
987
988 This function checks that T contains no nonstatic members. */
989
821eaf2a 990int
86ac0575 991shared_member_p (tree t)
bd0d5d4a
JM
992{
993 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
994 || TREE_CODE (t) == CONST_DECL)
995 return 1;
996 if (is_overloaded_fn (t))
997 {
998 for (; t; t = OVL_NEXT (t))
999 {
1000 tree fn = OVL_CURRENT (t);
1001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1002 return 0;
1003 }
1004 return 1;
1005 }
1006 return 0;
1007}
1008
f8ad2d21
NS
1009/* Routine to see if the sub-object denoted by the binfo PARENT can be
1010 found as a base class and sub-object of the object denoted by
1011 BINFO. */
1012
1013static int
1014is_subobject_of_p (tree parent, tree binfo)
1015{
1016 tree probe;
c8094d83 1017
f8ad2d21
NS
1018 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1019 {
1020 if (probe == binfo)
1021 return 1;
1022 if (BINFO_VIRTUAL_P (probe))
1023 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1024 != NULL_TREE);
1025 }
1026 return 0;
1027}
1028
7d4bdeed
MM
1029/* DATA is really a struct lookup_field_info. Look for a field with
1030 the name indicated there in BINFO. If this function returns a
1031 non-NULL value it is the result of the lookup. Called from
1032 lookup_field via breadth_first_search. */
1033
1034static tree
86ac0575 1035lookup_field_r (tree binfo, void *data)
7d4bdeed
MM
1036{
1037 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1038 tree type = BINFO_TYPE (binfo);
4bb0968f 1039 tree nval = NULL_TREE;
7d4bdeed 1040
5d5a519f
NS
1041 /* If this is a dependent base, don't look in it. */
1042 if (BINFO_DEPENDENT_BASE_P (binfo))
1043 return NULL_TREE;
c8094d83 1044
5d5a519f
NS
1045 /* If this base class is hidden by the best-known value so far, we
1046 don't need to look. */
1047 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1048 && !BINFO_VIRTUAL_P (binfo))
1049 return dfs_skip_bases;
1050
d6479fe7
MM
1051 /* First, look for a function. There can't be a function and a data
1052 member with the same name, and if there's a function and a type
1053 with the same name, the type is hidden by the function. */
4bb0968f
MM
1054 if (!lfi->want_type)
1055 {
477f6664 1056 int idx = lookup_fnfields_1 (type, lfi->name);
4bb0968f 1057 if (idx >= 0)
aaaa46d2 1058 nval = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
4bb0968f
MM
1059 }
1060
1061 if (!nval)
d6479fe7 1062 /* Look for a data member or type. */
c717c5af 1063 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
d6479fe7
MM
1064
1065 /* If there is no declaration with the indicated name in this type,
1066 then there's nothing to do. */
7d4bdeed 1067 if (!nval)
5d5a519f 1068 goto done;
7d4bdeed 1069
4bb0968f
MM
1070 /* If we're looking up a type (as with an elaborated type specifier)
1071 we ignore all non-types we find. */
8a2b77e7
JM
1072 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1073 && !DECL_CLASS_TEMPLATE_P (nval))
4bb0968f 1074 {
881cae05
JJ
1075 if (lfi->name == TYPE_IDENTIFIER (type))
1076 {
1077 /* If the aggregate has no user defined constructors, we allow
1078 it to have fields with the same name as the enclosing type.
1079 If we are looking for that name, find the corresponding
1080 TYPE_DECL. */
1081 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1082 if (DECL_NAME (nval) == lfi->name
1083 && TREE_CODE (nval) == TYPE_DECL)
1084 break;
1085 }
1086 else
1087 nval = NULL_TREE;
5e0c54e5 1088 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
881cae05 1089 {
0cbd7506
MS
1090 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1091 lfi->name);
5e0c54e5
GDR
1092 if (e != NULL)
1093 nval = TYPE_MAIN_DECL (e->type);
c8094d83 1094 else
5d5a519f 1095 goto done;
881cae05 1096 }
4bb0968f
MM
1097 }
1098
8f032717 1099 /* You must name a template base class with a template-id. */
c8094d83 1100 if (!same_type_p (type, lfi->type)
8f032717 1101 && template_self_reference_p (type, nval))
5d5a519f 1102 goto done;
8f032717 1103
7d4bdeed
MM
1104 /* If the lookup already found a match, and the new value doesn't
1105 hide the old one, we might have an ambiguity. */
f8ad2d21
NS
1106 if (lfi->rval_binfo
1107 && !is_subobject_of_p (lfi->rval_binfo, binfo))
c8094d83 1108
7d4bdeed 1109 {
bd0d5d4a 1110 if (nval == lfi->rval && shared_member_p (nval))
7d4bdeed
MM
1111 /* The two things are really the same. */
1112 ;
f8ad2d21 1113 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
7d4bdeed
MM
1114 /* The previous value hides the new one. */
1115 ;
1116 else
1117 {
1118 /* We have a real ambiguity. We keep a chain of all the
1119 candidates. */
1120 if (!lfi->ambiguous && lfi->rval)
aa65d1a2
MM
1121 {
1122 /* This is the first time we noticed an ambiguity. Add
1123 what we previously thought was a reasonable candidate
1124 to the list. */
e1b3e07d 1125 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
aa65d1a2
MM
1126 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1127 }
1128
7d4bdeed 1129 /* Add the new value. */
e1b3e07d 1130 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
aa65d1a2 1131 TREE_TYPE (lfi->ambiguous) = error_mark_node;
9e637a26 1132 lfi->errstr = "request for member %qD is ambiguous";
7d4bdeed
MM
1133 }
1134 }
1135 else
1136 {
d6479fe7 1137 lfi->rval = nval;
7d4bdeed
MM
1138 lfi->rval_binfo = binfo;
1139 }
1140
5d5a519f
NS
1141 done:
1142 /* Don't look for constructors or destructors in base classes. */
1143 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1144 return dfs_skip_bases;
d6479fe7 1145 return NULL_TREE;
7d4bdeed
MM
1146}
1147
c2a124b2 1148/* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
4ba126e4
MM
1149 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1150 FUNCTIONS, and OPTYPE respectively. */
1151
1152tree
1153build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1154{
1155 tree baselink;
1156
50bc768d
NS
1157 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1158 || TREE_CODE (functions) == TEMPLATE_DECL
1159 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1160 || TREE_CODE (functions) == OVERLOAD);
1161 gcc_assert (!optype || TYPE_P (optype));
1162 gcc_assert (TREE_TYPE (functions));
4ba126e4 1163
5dae1114
MM
1164 baselink = make_node (BASELINK);
1165 TREE_TYPE (baselink) = TREE_TYPE (functions);
4ba126e4
MM
1166 BASELINK_BINFO (baselink) = binfo;
1167 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1168 BASELINK_FUNCTIONS (baselink) = functions;
1169 BASELINK_OPTYPE (baselink) = optype;
1170
1171 return baselink;
1172}
1173
1a03d967 1174/* Look for a member named NAME in an inheritance lattice dominated by
171d2f50
NS
1175 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1176 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1177 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1178 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1179 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1180 TREE_VALUEs are the list of ambiguous candidates.
1181
1182 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1183
1184 If nothing can be found return NULL_TREE and do not issue an error. */
e92cc029 1185
8d08fdba 1186tree
86ac0575 1187lookup_member (tree xbasetype, tree name, int protect, bool want_type)
8d08fdba 1188{
7d4bdeed
MM
1189 tree rval, rval_binfo = NULL_TREE;
1190 tree type = NULL_TREE, basetype_path = NULL_TREE;
1191 struct lookup_field_info lfi;
8d08fdba
MS
1192
1193 /* rval_binfo is the binfo associated with the found member, note,
1194 this can be set with useful information, even when rval is not
1195 set, because it must deal with ALL members, not just non-function
1196 members. It is used for ambiguity checking and the hidden
1197 checks. Whereas rval is only set if a proper (not hidden)
1198 non-function member is found. */
1199
d8e178a0 1200 const char *errstr = 0;
8d08fdba 1201
50bc768d 1202 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
de22184b 1203
95b4aca6 1204 if (TREE_CODE (xbasetype) == TREE_BINFO)
8d08fdba 1205 {
8d08fdba 1206 type = BINFO_TYPE (xbasetype);
39211cd5 1207 basetype_path = xbasetype;
8d08fdba 1208 }
6df5158a 1209 else
39211cd5 1210 {
a82f93ac
SE
1211 if (!IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1212 return NULL_TREE;
238109cd 1213 type = xbasetype;
cad7e87b 1214 xbasetype = NULL_TREE;
6df5158a
NS
1215 }
1216
cad7e87b
NS
1217 type = complete_type (type);
1218 if (!basetype_path)
1219 basetype_path = TYPE_BINFO (type);
1220
1221 if (!basetype_path)
1222 return NULL_TREE;
8d08fdba 1223
8d08fdba
MS
1224#ifdef GATHER_STATISTICS
1225 n_calls_lookup_field++;
fc378698 1226#endif /* GATHER_STATISTICS */
8d08fdba 1227
fad205ff 1228 memset (&lfi, 0, sizeof (lfi));
d6479fe7 1229 lfi.type = type;
7d4bdeed 1230 lfi.name = name;
7d4bdeed 1231 lfi.want_type = want_type;
5d5a519f 1232 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
7d4bdeed
MM
1233 rval = lfi.rval;
1234 rval_binfo = lfi.rval_binfo;
1235 if (rval_binfo)
1236 type = BINFO_TYPE (rval_binfo);
1237 errstr = lfi.errstr;
1238
1239 /* If we are not interested in ambiguities, don't report them;
1240 just return NULL_TREE. */
1241 if (!protect && lfi.ambiguous)
1242 return NULL_TREE;
c8094d83
MS
1243
1244 if (protect == 2)
8f032717
MM
1245 {
1246 if (lfi.ambiguous)
aa65d1a2 1247 return lfi.ambiguous;
8f032717
MM
1248 else
1249 protect = 0;
1250 }
1251
d6479fe7
MM
1252 /* [class.access]
1253
1254 In the case of overloaded function names, access control is
eff3a276
MM
1255 applied to the function selected by overloaded resolution.
1256
1257 We cannot check here, even if RVAL is only a single non-static
1258 member function, since we do not know what the "this" pointer
1259 will be. For:
1260
1261 class A { protected: void f(); };
1262 class B : public A {
1263 void g(A *p) {
1264 f(); // OK
1265 p->f(); // Not OK.
1266 }
1267 };
1268
1269 only the first call to "f" is valid. However, if the function is
1270 static, we can check. */
1271 if (rval && protect
1272 && !really_overloaded_fn (rval)
1273 && !(TREE_CODE (rval) == FUNCTION_DECL
1274 && DECL_NONSTATIC_MEMBER_FUNCTION_P (rval)))
02022f3a 1275 perform_or_defer_access_check (basetype_path, rval, rval);
9e9ff709 1276
8251199e 1277 if (errstr && protect)
8d08fdba 1278 {
33bd39a2 1279 error (errstr, name, type);
7d4bdeed 1280 if (lfi.ambiguous)
0cbd7506 1281 print_candidates (lfi.ambiguous);
8d08fdba
MS
1282 rval = error_mark_node;
1283 }
b3709d9b 1284
c8094d83 1285 if (rval && is_overloaded_fn (rval))
4ba126e4
MM
1286 rval = build_baselink (rval_binfo, basetype_path, rval,
1287 (IDENTIFIER_TYPENAME_P (name)
1288 ? TREE_TYPE (name): NULL_TREE));
d6479fe7
MM
1289 return rval;
1290}
1291
1292/* Like lookup_member, except that if we find a function member we
1293 return NULL_TREE. */
1294
1295tree
86ac0575 1296lookup_field (tree xbasetype, tree name, int protect, bool want_type)
d6479fe7
MM
1297{
1298 tree rval = lookup_member (xbasetype, name, protect, want_type);
c8094d83 1299
c566721f
GB
1300 /* Ignore functions, but propagate the ambiguity list. */
1301 if (!error_operand_p (rval)
1302 && (rval && BASELINK_P (rval)))
d6479fe7
MM
1303 return NULL_TREE;
1304
1305 return rval;
1306}
1307
1308/* Like lookup_member, except that if we find a non-function member we
1309 return NULL_TREE. */
1310
1311tree
86ac0575 1312lookup_fnfields (tree xbasetype, tree name, int protect)
d6479fe7 1313{
86ac0575 1314 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
d6479fe7 1315
c566721f
GB
1316 /* Ignore non-functions, but propagate the ambiguity list. */
1317 if (!error_operand_p (rval)
1318 && (rval && !BASELINK_P (rval)))
d6479fe7
MM
1319 return NULL_TREE;
1320
8d08fdba
MS
1321 return rval;
1322}
1323
ca90f3e1
MM
1324/* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1325 corresponding to "operator TYPE ()", or -1 if there is no such
1326 operator. Only CLASS_TYPE itself is searched; this routine does
1327 not scan the base classes of CLASS_TYPE. */
1328
1329static int
1330lookup_conversion_operator (tree class_type, tree type)
1331{
8f2a734f 1332 int tpl_slot = -1;
ca90f3e1 1333
8f2a734f
NS
1334 if (TYPE_HAS_CONVERSION (class_type))
1335 {
1336 int i;
1337 tree fn;
d4e6fecb 1338 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (class_type);
c8094d83 1339
8f2a734f
NS
1340 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1341 VEC_iterate (tree, methods, i, fn); ++i)
1342 {
1343 /* All the conversion operators come near the beginning of
1344 the class. Therefore, if FN is not a conversion
1345 operator, there is no matching conversion operator in
1346 CLASS_TYPE. */
1347 fn = OVL_CURRENT (fn);
1348 if (!DECL_CONV_FN_P (fn))
1349 break;
c8094d83 1350
8f2a734f
NS
1351 if (TREE_CODE (fn) == TEMPLATE_DECL)
1352 /* All the templated conversion functions are on the same
1353 slot, so remember it. */
1354 tpl_slot = i;
1355 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1356 return i;
1357 }
1358 }
ca90f3e1 1359
8f2a734f 1360 return tpl_slot;
ca90f3e1
MM
1361}
1362
8d08fdba
MS
1363/* TYPE is a class type. Return the index of the fields within
1364 the method vector with name NAME, or -1 is no such field exists. */
e92cc029 1365
03017874 1366int
86ac0575 1367lookup_fnfields_1 (tree type, tree name)
8d08fdba 1368{
d4e6fecb 1369 VEC(tree,gc) *method_vec;
aaaa46d2 1370 tree fn;
ca90f3e1 1371 tree tmp;
aaaa46d2 1372 size_t i;
c8094d83 1373
ca90f3e1
MM
1374 if (!CLASS_TYPE_P (type))
1375 return -1;
8d08fdba 1376
508a1c9c
MM
1377 if (COMPLETE_TYPE_P (type))
1378 {
1379 if ((name == ctor_identifier
1380 || name == base_ctor_identifier
1381 || name == complete_ctor_identifier))
1382 {
1383 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1384 lazily_declare_fn (sfk_constructor, type);
1385 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1386 lazily_declare_fn (sfk_copy_constructor, type);
1387 }
1388 else if (name == ansi_assopname(NOP_EXPR)
fb232476 1389 && CLASSTYPE_LAZY_ASSIGNMENT_OP (type))
508a1c9c 1390 lazily_declare_fn (sfk_assignment_operator, type);
9f4faeae
MM
1391 else if ((name == dtor_identifier
1392 || name == base_dtor_identifier
1393 || name == complete_dtor_identifier
1394 || name == deleting_dtor_identifier)
1395 && CLASSTYPE_LAZY_DESTRUCTOR (type))
1396 lazily_declare_fn (sfk_destructor, type);
508a1c9c 1397 }
ca90f3e1 1398
508a1c9c 1399 method_vec = CLASSTYPE_METHOD_VEC (type);
ca90f3e1
MM
1400 if (!method_vec)
1401 return -1;
1402
8d08fdba 1403#ifdef GATHER_STATISTICS
ca90f3e1 1404 n_calls_lookup_fnfields_1++;
fc378698 1405#endif /* GATHER_STATISTICS */
f90cdf34 1406
ca90f3e1
MM
1407 /* Constructors are first... */
1408 if (name == ctor_identifier)
aaaa46d2
MM
1409 {
1410 fn = CLASSTYPE_CONSTRUCTORS (type);
1411 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1412 }
ca90f3e1
MM
1413 /* and destructors are second. */
1414 if (name == dtor_identifier)
aaaa46d2
MM
1415 {
1416 fn = CLASSTYPE_DESTRUCTORS (type);
1417 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1418 }
ca90f3e1
MM
1419 if (IDENTIFIER_TYPENAME_P (name))
1420 return lookup_conversion_operator (type, TREE_TYPE (name));
1421
1422 /* Skip the conversion operators. */
aaaa46d2 1423 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
9ba5ff0f 1424 VEC_iterate (tree, method_vec, i, fn);
aaaa46d2
MM
1425 ++i)
1426 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1427 break;
ca90f3e1
MM
1428
1429 /* If the type is complete, use binary search. */
1430 if (COMPLETE_TYPE_P (type))
1431 {
aaaa46d2
MM
1432 int lo;
1433 int hi;
1434
aaaa46d2
MM
1435 lo = i;
1436 hi = VEC_length (tree, method_vec);
ca90f3e1
MM
1437 while (lo < hi)
1438 {
1439 i = (lo + hi) / 2;
f90cdf34
MT
1440
1441#ifdef GATHER_STATISTICS
ca90f3e1 1442 n_outer_fields_searched++;
f90cdf34
MT
1443#endif /* GATHER_STATISTICS */
1444
aaaa46d2
MM
1445 tmp = VEC_index (tree, method_vec, i);
1446 tmp = DECL_NAME (OVL_CURRENT (tmp));
1447 if (tmp > name)
ca90f3e1
MM
1448 hi = i;
1449 else if (tmp < name)
1450 lo = i + 1;
1451 else
1452 return i;
8d08fdba 1453 }
8d08fdba 1454 }
ca90f3e1 1455 else
9ba5ff0f 1456 for (; VEC_iterate (tree, method_vec, i, fn); ++i)
ca90f3e1
MM
1457 {
1458#ifdef GATHER_STATISTICS
1459 n_outer_fields_searched++;
1460#endif /* GATHER_STATISTICS */
aaaa46d2 1461 if (DECL_NAME (OVL_CURRENT (fn)) == name)
ca90f3e1
MM
1462 return i;
1463 }
8d08fdba 1464
d6479fe7 1465 return -1;
d23a1bb1 1466}
9e259dd1 1467
c7222c02
MM
1468/* Like lookup_fnfields_1, except that the name is extracted from
1469 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1470
1471int
1472class_method_index_for_fn (tree class_type, tree function)
1473{
1474 gcc_assert (TREE_CODE (function) == FUNCTION_DECL
1475 || DECL_FUNCTION_TEMPLATE_P (function));
1476
1477 return lookup_fnfields_1 (class_type,
1478 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1479 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1480 DECL_NAME (function));
1481}
1482
1483
a723baf1
MM
1484/* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1485 the class or namespace used to qualify the name. CONTEXT_CLASS is
1486 the class corresponding to the object in which DECL will be used.
1487 Return a possibly modified version of DECL that takes into account
1488 the CONTEXT_CLASS.
9e259dd1
MM
1489
1490 In particular, consider an expression like `B::m' in the context of
1491 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1492 then the most derived class indicated by the BASELINK_BINFO will be
1493 `B', not `D'. This function makes that adjustment. */
1494
1495tree
c8094d83 1496adjust_result_of_qualified_name_lookup (tree decl,
a723baf1 1497 tree qualifying_scope,
9e259dd1
MM
1498 tree context_class)
1499{
0616700c 1500 if (context_class && context_class != error_mark_node
9c23e505 1501 && CLASS_TYPE_P (context_class)
0616700c 1502 && CLASS_TYPE_P (qualifying_scope)
a723baf1
MM
1503 && DERIVED_FROM_P (qualifying_scope, context_class)
1504 && BASELINK_P (decl))
9e259dd1
MM
1505 {
1506 tree base;
1507
127b8136
MM
1508 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1509 Because we do not yet know which function will be chosen by
1510 overload resolution, we cannot yet check either accessibility
1511 or ambiguity -- in either case, the choice of a static member
1512 function might make the usage valid. */
a723baf1 1513 base = lookup_base (context_class, qualifying_scope,
18e4be85 1514 ba_unique | ba_quiet, NULL);
9e259dd1
MM
1515 if (base)
1516 {
1517 BASELINK_ACCESS_BINFO (decl) = base;
c8094d83 1518 BASELINK_BINFO (decl)
9e259dd1 1519 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
18e4be85 1520 ba_unique | ba_quiet,
9e259dd1
MM
1521 NULL);
1522 }
1523 }
1524
1525 return decl;
1526}
1527
8d08fdba 1528\f
5cf447db 1529/* Walk the class hierarchy within BINFO, in a depth-first traversal.
5d5a519f
NS
1530 PRE_FN is called in preorder, while POST_FN is called in postorder.
1531 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1532 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1533 that value is immediately returned and the walk is terminated. One
1534 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1535 POST_FN are passed the binfo to examine and the caller's DATA
1536 value. All paths are walked, thus virtual and morally virtual
1537 binfos can be multiply walked. */
d6479fe7 1538
bbd15aac 1539tree
5d5a519f
NS
1540dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1541 tree (*post_fn) (tree, void *), void *data)
d6479fe7 1542{
5d5a519f
NS
1543 tree rval;
1544 unsigned ix;
fa743e8c 1545 tree base_binfo;
c8094d83 1546
d6479fe7 1547 /* Call the pre-order walking function. */
5d5a519f 1548 if (pre_fn)
7d4bdeed 1549 {
5d5a519f
NS
1550 rval = pre_fn (binfo, data);
1551 if (rval)
1552 {
1553 if (rval == dfs_skip_bases)
1554 goto skip_bases;
1555 return rval;
1556 }
1557 }
1558
1559 /* Find the next child binfo to walk. */
1560 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1561 {
1562 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
d6479fe7
MM
1563 if (rval)
1564 return rval;
8d08fdba 1565 }
8d08fdba 1566
5d5a519f
NS
1567 skip_bases:
1568 /* Call the post-order walking function. */
1569 if (post_fn)
5b94d9dd
NS
1570 {
1571 rval = post_fn (binfo, data);
1572 gcc_assert (rval != dfs_skip_bases);
1573 return rval;
1574 }
c8094d83 1575
5d5a519f
NS
1576 return NULL_TREE;
1577}
1578
1579/* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1580 that binfos are walked at most once. */
1581
1582static tree
1583dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1584 tree (*post_fn) (tree, void *), void *data)
1585{
1586 tree rval;
1587 unsigned ix;
1588 tree base_binfo;
c8094d83 1589
5d5a519f
NS
1590 /* Call the pre-order walking function. */
1591 if (pre_fn)
d6479fe7 1592 {
5d5a519f
NS
1593 rval = pre_fn (binfo, data);
1594 if (rval)
d6479fe7 1595 {
5d5a519f
NS
1596 if (rval == dfs_skip_bases)
1597 goto skip_bases;
c8094d83 1598
5d5a519f
NS
1599 return rval;
1600 }
1601 }
1602
1603 /* Find the next child binfo to walk. */
1604 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1605 {
1606 if (BINFO_VIRTUAL_P (base_binfo))
1607 {
1608 if (BINFO_MARKED (base_binfo))
fa743e8c 1609 continue;
5d5a519f 1610 BINFO_MARKED (base_binfo) = 1;
d6479fe7 1611 }
c8094d83 1612
5d5a519f 1613 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
fa743e8c
NS
1614 if (rval)
1615 return rval;
d6479fe7 1616 }
c8094d83 1617
5d5a519f 1618 skip_bases:
d6479fe7 1619 /* Call the post-order walking function. */
5d5a519f 1620 if (post_fn)
5b94d9dd
NS
1621 {
1622 rval = post_fn (binfo, data);
1623 gcc_assert (rval != dfs_skip_bases);
1624 return rval;
1625 }
c8094d83 1626
5d5a519f
NS
1627 return NULL_TREE;
1628}
1629
1630/* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1631 BINFO. */
c8094d83 1632
5d5a519f
NS
1633static void
1634dfs_unmark_r (tree binfo)
1635{
1636 unsigned ix;
1637 tree base_binfo;
c8094d83 1638
5d5a519f
NS
1639 /* Process the basetypes. */
1640 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1641 {
1642 if (BINFO_VIRTUAL_P (base_binfo))
1643 {
1644 if (!BINFO_MARKED (base_binfo))
1645 continue;
1646 BINFO_MARKED (base_binfo) = 0;
1647 }
1648 /* Only walk, if it can contain more virtual bases. */
1649 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1650 dfs_unmark_r (base_binfo);
1651 }
8d08fdba
MS
1652}
1653
5d5a519f
NS
1654/* Like dfs_walk_all, except that binfos are not multiply walked. For
1655 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1656 For diamond shaped hierarchies we must mark the virtual bases, to
1657 avoid multiple walks. */
d6479fe7
MM
1658
1659tree
5d5a519f
NS
1660dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1661 tree (*post_fn) (tree, void *), void *data)
d6479fe7 1662{
12a669d1 1663 static int active = 0; /* We must not be called recursively. */
5d5a519f
NS
1664 tree rval;
1665
1666 gcc_assert (pre_fn || post_fn);
12a669d1
NS
1667 gcc_assert (!active);
1668 active++;
c8094d83 1669
5d5a519f
NS
1670 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1671 /* We are not diamond shaped, and therefore cannot encounter the
1672 same binfo twice. */
1673 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1674 else
1675 {
1676 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1677 if (!BINFO_INHERITANCE_CHAIN (binfo))
1678 {
ee81147e 1679 /* We are at the top of the hierarchy, and can use the
0cbd7506
MS
1680 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1681 bases. */
d4e6fecb 1682 VEC(tree,gc) *vbases;
5d5a519f
NS
1683 unsigned ix;
1684 tree base_binfo;
c8094d83 1685
5d5a519f
NS
1686 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1687 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1688 BINFO_MARKED (base_binfo) = 0;
1689 }
1690 else
1691 dfs_unmark_r (binfo);
1692 }
12a669d1
NS
1693
1694 active--;
c8094d83 1695
5d5a519f 1696 return rval;
d6479fe7
MM
1697}
1698
6936e493
NS
1699/* Worker function for dfs_walk_once_accessible. Behaves like
1700 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1701 access given by the current context should be considered, (b) ONCE
1702 indicates whether bases should be marked during traversal. */
1703
1704static tree
1705dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1706 tree (*pre_fn) (tree, void *),
1707 tree (*post_fn) (tree, void *), void *data)
1708{
1709 tree rval = NULL_TREE;
1710 unsigned ix;
1711 tree base_binfo;
1712
1713 /* Call the pre-order walking function. */
1714 if (pre_fn)
1715 {
1716 rval = pre_fn (binfo, data);
1717 if (rval)
1718 {
1719 if (rval == dfs_skip_bases)
1720 goto skip_bases;
c8094d83 1721
6936e493
NS
1722 return rval;
1723 }
1724 }
1725
1726 /* Find the next child binfo to walk. */
1727 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1728 {
1729 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1730
1731 if (mark && BINFO_MARKED (base_binfo))
1732 continue;
c8094d83 1733
6936e493 1734 /* If the base is inherited via private or protected
0cbd7506
MS
1735 inheritance, then we can't see it, unless we are a friend of
1736 the current binfo. */
a5201a91
MM
1737 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
1738 {
1739 tree scope;
1740 if (!friends_p)
1741 continue;
1742 scope = current_scope ();
c8094d83 1743 if (!scope
a5201a91
MM
1744 || TREE_CODE (scope) == NAMESPACE_DECL
1745 || !is_friend (BINFO_TYPE (binfo), scope))
1746 continue;
1747 }
6936e493
NS
1748
1749 if (mark)
1750 BINFO_MARKED (base_binfo) = 1;
1751
1752 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1753 pre_fn, post_fn, data);
1754 if (rval)
1755 return rval;
1756 }
c8094d83 1757
6936e493
NS
1758 skip_bases:
1759 /* Call the post-order walking function. */
1760 if (post_fn)
5b94d9dd
NS
1761 {
1762 rval = post_fn (binfo, data);
1763 gcc_assert (rval != dfs_skip_bases);
1764 return rval;
1765 }
c8094d83 1766
6936e493
NS
1767 return NULL_TREE;
1768}
1769
1770/* Like dfs_walk_once except that only accessible bases are walked.
1771 FRIENDS_P indicates whether friendship of the local context
1772 should be considered when determining accessibility. */
1773
1774static tree
1775dfs_walk_once_accessible (tree binfo, bool friends_p,
1776 tree (*pre_fn) (tree, void *),
1777 tree (*post_fn) (tree, void *), void *data)
1778{
1779 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1780 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1781 pre_fn, post_fn, data);
c8094d83 1782
6936e493
NS
1783 if (diamond_shaped)
1784 {
1785 if (!BINFO_INHERITANCE_CHAIN (binfo))
1786 {
d740dbe7 1787 /* We are at the top of the hierarchy, and can use the
0cbd7506
MS
1788 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1789 bases. */
d4e6fecb 1790 VEC(tree,gc) *vbases;
6936e493
NS
1791 unsigned ix;
1792 tree base_binfo;
c8094d83 1793
6936e493
NS
1794 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1795 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1796 BINFO_MARKED (base_binfo) = 0;
1797 }
1798 else
1799 dfs_unmark_r (binfo);
1800 }
1801 return rval;
1802}
1803
4cc1d462
NS
1804/* Check that virtual overrider OVERRIDER is acceptable for base function
1805 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1806
af746697 1807static int
86ac0575 1808check_final_overrider (tree overrider, tree basefn)
4cc1d462
NS
1809{
1810 tree over_type = TREE_TYPE (overrider);
1811 tree base_type = TREE_TYPE (basefn);
1812 tree over_return = TREE_TYPE (over_type);
1813 tree base_return = TREE_TYPE (base_type);
1814 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1815 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
4977bab6 1816 int fail = 0;
58ec3cc5
MM
1817
1818 if (DECL_INVALID_OVERRIDER_P (overrider))
1819 return 0;
1820
4cc1d462
NS
1821 if (same_type_p (base_return, over_return))
1822 /* OK */;
4977bab6
ZW
1823 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1824 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1825 && POINTER_TYPE_P (base_return)))
4cc1d462 1826 {
9bcb9aae 1827 /* Potentially covariant. */
4977bab6 1828 unsigned base_quals, over_quals;
c8094d83 1829
4977bab6
ZW
1830 fail = !POINTER_TYPE_P (base_return);
1831 if (!fail)
1832 {
1833 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
c8094d83 1834
4977bab6
ZW
1835 base_return = TREE_TYPE (base_return);
1836 over_return = TREE_TYPE (over_return);
1837 }
1838 base_quals = cp_type_quals (base_return);
1839 over_quals = cp_type_quals (over_return);
1840
1841 if ((base_quals & over_quals) != over_quals)
1842 fail = 1;
c8094d83 1843
4977bab6
ZW
1844 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1845 {
1846 tree binfo = lookup_base (over_return, base_return,
1847 ba_check | ba_quiet, NULL);
4cc1d462 1848
4977bab6
ZW
1849 if (!binfo)
1850 fail = 1;
1851 }
1852 else if (!pedantic
1853 && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type)))
1854 /* GNU extension, allow trivial pointer conversions such as
1855 converting to void *, or qualification conversion. */
4cc1d462 1856 {
4977bab6 1857 /* can_convert will permit user defined conversion from a
9bcb9aae 1858 (reference to) class type. We must reject them. */
ee76b931 1859 over_return = non_reference (TREE_TYPE (over_type));
4977bab6
ZW
1860 if (CLASS_TYPE_P (over_return))
1861 fail = 2;
ae209f28
NS
1862 else
1863 {
dee15844 1864 warning (0, "deprecated covariant return type for %q+#D",
ae209f28 1865 overrider);
dee15844 1866 warning (0, " overriding %q+#D", basefn);
ae209f28 1867 }
4cc1d462 1868 }
4977bab6
ZW
1869 else
1870 fail = 2;
4cc1d462 1871 }
4977bab6
ZW
1872 else
1873 fail = 2;
1874 if (!fail)
1875 /* OK */;
4977bab6 1876 else
4cc1d462 1877 {
4977bab6
ZW
1878 if (fail == 1)
1879 {
dee15844
JM
1880 error ("invalid covariant return type for %q+#D", overrider);
1881 error (" overriding %q+#D", basefn);
4977bab6
ZW
1882 }
1883 else
1884 {
dee15844
JM
1885 error ("conflicting return type specified for %q+#D", overrider);
1886 error (" overriding %q+#D", basefn);
4977bab6 1887 }
58ec3cc5 1888 DECL_INVALID_OVERRIDER_P (overrider) = 1;
4cc1d462
NS
1889 return 0;
1890 }
c8094d83 1891
8152c320 1892 /* Check throw specifier is at least as strict. */
03378143 1893 if (!comp_except_specs (base_throw, over_throw, 0))
4cc1d462 1894 {
dee15844
JM
1895 error ("looser throw specifier for %q+#F", overrider);
1896 error (" overriding %q+#F", basefn);
58ec3cc5 1897 DECL_INVALID_OVERRIDER_P (overrider) = 1;
4cc1d462
NS
1898 return 0;
1899 }
c8094d83 1900
4cc1d462
NS
1901 return 1;
1902}
1903
cbb40945
NS
1904/* Given a class TYPE, and a function decl FNDECL, look for
1905 virtual functions in TYPE's hierarchy which FNDECL overrides.
1906 We do not look in TYPE itself, only its bases.
c8094d83 1907
838dfd8a 1908 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
cbb40945 1909 find that it overrides anything.
c8094d83 1910
cbb40945
NS
1911 We check that every function which is overridden, is correctly
1912 overridden. */
e92cc029 1913
cbb40945 1914int
86ac0575 1915look_for_overrides (tree type, tree fndecl)
8d08fdba 1916{
cbb40945 1917 tree binfo = TYPE_BINFO (type);
fa743e8c 1918 tree base_binfo;
cbb40945
NS
1919 int ix;
1920 int found = 0;
8d08fdba 1921
fa743e8c 1922 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
cbb40945 1923 {
fa743e8c 1924 tree basetype = BINFO_TYPE (base_binfo);
c8094d83 1925
cbb40945 1926 if (TYPE_POLYMORPHIC_P (basetype))
0cbd7506 1927 found += look_for_overrides_r (basetype, fndecl);
cbb40945
NS
1928 }
1929 return found;
1930}
5e795528 1931
548502d3
MM
1932/* Look in TYPE for virtual functions with the same signature as
1933 FNDECL. */
5e795528 1934
d0cd8b44 1935tree
86ac0575 1936look_for_overrides_here (tree type, tree fndecl)
cbb40945
NS
1937{
1938 int ix;
d0cd8b44 1939
508a1c9c
MM
1940 /* If there are no methods in TYPE (meaning that only implicitly
1941 declared methods will ever be provided for TYPE), then there are
1942 no virtual functions. */
1943 if (!CLASSTYPE_METHOD_VEC (type))
1944 return NULL_TREE;
1945
d0cd8b44 1946 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
cbb40945 1947 ix = CLASSTYPE_DESTRUCTOR_SLOT;
8d08fdba 1948 else
3c505507 1949 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
cbb40945 1950 if (ix >= 0)
8d08fdba 1951 {
aaaa46d2 1952 tree fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
c8094d83 1953
cbb40945 1954 for (; fns; fns = OVL_NEXT (fns))
0cbd7506
MS
1955 {
1956 tree fn = OVL_CURRENT (fns);
d0cd8b44 1957
0cbd7506
MS
1958 if (!DECL_VIRTUAL_P (fn))
1959 /* Not a virtual. */;
1960 else if (DECL_CONTEXT (fn) != type)
1961 /* Introduced with a using declaration. */;
d0cd8b44 1962 else if (DECL_STATIC_FUNCTION_P (fndecl))
8d08fdba 1963 {
d0cd8b44
JM
1964 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1965 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
0cbd7506 1966 if (compparms (TREE_CHAIN (btypes), dtypes))
d0cd8b44 1967 return fn;
0cbd7506
MS
1968 }
1969 else if (same_signature_p (fndecl, fn))
d0cd8b44
JM
1970 return fn;
1971 }
1972 }
1973 return NULL_TREE;
1974}
e0fff4b3 1975
d0cd8b44 1976/* Look in TYPE for virtual functions overridden by FNDECL. Check both
c6002625 1977 TYPE itself and its bases. */
d0cd8b44
JM
1978
1979static int
86ac0575 1980look_for_overrides_r (tree type, tree fndecl)
d0cd8b44
JM
1981{
1982 tree fn = look_for_overrides_here (type, fndecl);
1983 if (fn)
1984 {
1985 if (DECL_STATIC_FUNCTION_P (fndecl))
1986 {
1987 /* A static member function cannot match an inherited
1988 virtual member function. */
dee15844
JM
1989 error ("%q+#D cannot be declared", fndecl);
1990 error (" since %q+#D declared in base class", fn);
d0cd8b44
JM
1991 }
1992 else
1993 {
1994 /* It's definitely virtual, even if not explicitly set. */
1995 DECL_VIRTUAL_P (fndecl) = 1;
1996 check_final_overrider (fndecl, fn);
8d08fdba 1997 }
d0cd8b44 1998 return 1;
8d08fdba 1999 }
d0cd8b44 2000
cbb40945
NS
2001 /* We failed to find one declared in this class. Look in its bases. */
2002 return look_for_overrides (type, fndecl);
8d08fdba
MS
2003}
2004
99a6c6f4
MM
2005/* Called via dfs_walk from dfs_get_pure_virtuals. */
2006
2007static tree
86ac0575 2008dfs_get_pure_virtuals (tree binfo, void *data)
99a6c6f4 2009{
174eceea
MM
2010 tree type = (tree) data;
2011
99a6c6f4
MM
2012 /* We're not interested in primary base classes; the derived class
2013 of which they are a primary base will contain the information we
2014 need. */
9965d119 2015 if (!BINFO_PRIMARY_P (binfo))
8926095f 2016 {
07b7a812 2017 tree virtuals;
c8094d83 2018
da3d4dfa 2019 for (virtuals = BINFO_VIRTUALS (binfo);
99a6c6f4
MM
2020 virtuals;
2021 virtuals = TREE_CHAIN (virtuals))
31f8e4f3 2022 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
d4e6fecb 2023 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (type),
585b44d3 2024 BV_FN (virtuals));
99a6c6f4 2025 }
8d08fdba 2026
99a6c6f4 2027 return NULL_TREE;
8926095f
MS
2028}
2029
fee7654e 2030/* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
e92cc029 2031
fee7654e 2032void
86ac0575 2033get_pure_virtuals (tree type)
8926095f 2034{
99a6c6f4
MM
2035 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2036 is going to be overridden. */
585b44d3 2037 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
99a6c6f4
MM
2038 /* Now, run through all the bases which are not primary bases, and
2039 collect the pure virtual functions. We look at the vtable in
2040 each class to determine what pure virtual functions are present.
2041 (A primary base is not interesting because the derived class of
2042 which it is a primary base will contain vtable entries for the
2043 pure virtuals in the base class. */
5d5a519f 2044 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
8d08fdba 2045}
8d08fdba 2046\f
ae673f14
JM
2047/* Debug info for C++ classes can get very large; try to avoid
2048 emitting it everywhere.
2049
50e159f6
JM
2050 Note that this optimization wins even when the target supports
2051 BINCL (if only slightly), and reduces the amount of work for the
2052 linker. */
ae673f14
JM
2053
2054void
86ac0575 2055maybe_suppress_debug_info (tree t)
ae673f14 2056{
f8ca7e49 2057 if (write_symbols == NO_DEBUG)
ae673f14
JM
2058 return;
2059
50e159f6
JM
2060 /* We might have set this earlier in cp_finish_decl. */
2061 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2062
e713adf6
CD
2063 /* Always emit the information for each class every time. */
2064 if (flag_emit_class_debug_always)
2065 return;
2066
ae673f14
JM
2067 /* If we already know how we're handling this class, handle debug info
2068 the same way. */
3ae18eaf
JM
2069 if (CLASSTYPE_INTERFACE_KNOWN (t))
2070 {
2071 if (CLASSTYPE_INTERFACE_ONLY (t))
2072 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2073 /* else don't set it. */
2074 }
bbd15aac
MM
2075 /* If the class has a vtable, write out the debug info along with
2076 the vtable. */
2077 else if (TYPE_CONTAINS_VPTR_P (t))
ae673f14
JM
2078 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2079
2080 /* Otherwise, just emit the debug info normally. */
2081}
2082
6db20143
JM
2083/* Note that we want debugging information for a base class of a class
2084 whose vtable is being emitted. Normally, this would happen because
2085 calling the constructor for a derived class implies calling the
2086 constructors for all bases, which involve initializing the
2087 appropriate vptr with the vtable for the base class; but in the
2088 presence of optimization, this initialization may be optimized
2089 away, so we tell finish_vtable_vardecl that we want the debugging
2090 information anyway. */
2091
2092static tree
86ac0575 2093dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
6db20143
JM
2094{
2095 tree t = BINFO_TYPE (binfo);
2096
5d5a519f
NS
2097 if (CLASSTYPE_DEBUG_REQUESTED (t))
2098 return dfs_skip_bases;
2099
6db20143
JM
2100 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2101
2102 return NULL_TREE;
2103}
2104
6db20143
JM
2105/* Write out the debugging information for TYPE, whose vtable is being
2106 emitted. Also walk through our bases and note that we want to
2107 write out information for them. This avoids the problem of not
2108 writing any debug info for intermediate basetypes whose
2109 constructors, and thus the references to their vtables, and thus
2110 the vtables themselves, were optimized away. */
8d08fdba
MS
2111
2112void
86ac0575 2113note_debug_info_needed (tree type)
8d08fdba 2114{
15f1a795
JM
2115 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2116 {
2117 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2118 rest_of_type_compilation (type, toplevel_bindings_p ());
2119 }
d2e5ee5c 2120
5d5a519f 2121 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
8d08fdba
MS
2122}
2123\f
8d08fdba 2124void
edaf3e03 2125print_search_statistics (void)
8d08fdba
MS
2126{
2127#ifdef GATHER_STATISTICS
8d08fdba
MS
2128 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2129 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2130 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2131 n_outer_fields_searched, n_calls_lookup_fnfields);
2132 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
fc378698 2133#else /* GATHER_STATISTICS */
8d08fdba 2134 fprintf (stderr, "no search statistics\n");
fc378698 2135#endif /* GATHER_STATISTICS */
8d08fdba
MS
2136}
2137
8d08fdba 2138void
edaf3e03 2139reinit_search_statistics (void)
8d08fdba 2140{
5566b478 2141#ifdef GATHER_STATISTICS
8d08fdba
MS
2142 n_fields_searched = 0;
2143 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2144 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2145 n_calls_get_base_type = 0;
2146 n_outer_fields_searched = 0;
2147 n_contexts_saved = 0;
fc378698 2148#endif /* GATHER_STATISTICS */
8d08fdba 2149}
e1cd6e56 2150
8f2a734f 2151/* Helper for lookup_conversions_r. TO_TYPE is the type converted to
9c763d19
KH
2152 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2153 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
8f2a734f
NS
2154 bases have been encountered already in the tree walk. PARENT_CONVS
2155 is the list of lists of conversion functions that could hide CONV
2156 and OTHER_CONVS is the list of lists of conversion functions that
2157 could hide or be hidden by CONV, should virtualness be involved in
2158 the hierarchy. Merely checking the conversion op's name is not
2159 enough because two conversion operators to the same type can have
9c763d19 2160 different names. Return nonzero if we are visible. */
8f2a734f
NS
2161
2162static int
2163check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2164 tree to_type, tree parent_convs, tree other_convs)
2165{
2166 tree level, probe;
2167
2168 /* See if we are hidden by a parent conversion. */
2169 for (level = parent_convs; level; level = TREE_CHAIN (level))
2170 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2171 if (same_type_p (to_type, TREE_TYPE (probe)))
2172 return 0;
2173
2174 if (virtual_depth || virtualness)
2175 {
2176 /* In a virtual hierarchy, we could be hidden, or could hide a
0cbd7506 2177 conversion function on the other_convs list. */
8f2a734f
NS
2178 for (level = other_convs; level; level = TREE_CHAIN (level))
2179 {
2180 int we_hide_them;
2181 int they_hide_us;
2182 tree *prev, other;
c8094d83 2183
8f2a734f 2184 if (!(virtual_depth || TREE_STATIC (level)))
03fd3f84 2185 /* Neither is morally virtual, so cannot hide each other. */
8f2a734f 2186 continue;
c8094d83 2187
8f2a734f
NS
2188 if (!TREE_VALUE (level))
2189 /* They evaporated away already. */
2190 continue;
2191
2192 they_hide_us = (virtual_depth
2193 && original_binfo (binfo, TREE_PURPOSE (level)));
2194 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2195 && original_binfo (TREE_PURPOSE (level), binfo));
2196
2197 if (!(we_hide_them || they_hide_us))
2198 /* Neither is within the other, so no hiding can occur. */
2199 continue;
c8094d83 2200
8f2a734f
NS
2201 for (prev = &TREE_VALUE (level), other = *prev; other;)
2202 {
2203 if (same_type_p (to_type, TREE_TYPE (other)))
2204 {
2205 if (they_hide_us)
03fd3f84 2206 /* We are hidden. */
8f2a734f
NS
2207 return 0;
2208
2209 if (we_hide_them)
2210 {
2211 /* We hide the other one. */
2212 other = TREE_CHAIN (other);
2213 *prev = other;
2214 continue;
2215 }
2216 }
2217 prev = &TREE_CHAIN (other);
2218 other = *prev;
2219 }
2220 }
2221 }
2222 return 1;
2223}
2224
2225/* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2226 of conversion functions, the first slot will be for the current
2227 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
77880ae4
KH
2228 of conversion functions from children of the current binfo,
2229 concatenated with conversions from elsewhere in the hierarchy --
8f2a734f
NS
2230 that list begins with OTHER_CONVS. Return a single list of lists
2231 containing only conversions from the current binfo and its
2232 children. */
2233
72c4a2a6 2234static tree
8f2a734f
NS
2235split_conversions (tree my_convs, tree parent_convs,
2236 tree child_convs, tree other_convs)
e1cd6e56 2237{
8f2a734f
NS
2238 tree t;
2239 tree prev;
c8094d83 2240
8f2a734f
NS
2241 /* Remove the original other_convs portion from child_convs. */
2242 for (prev = NULL, t = child_convs;
2243 t != other_convs; prev = t, t = TREE_CHAIN (t))
2244 continue;
c8094d83 2245
8f2a734f
NS
2246 if (prev)
2247 TREE_CHAIN (prev) = NULL_TREE;
2248 else
2249 child_convs = NULL_TREE;
72b7eeff 2250
8f2a734f
NS
2251 /* Attach the child convs to any we had at this level. */
2252 if (my_convs)
2253 {
2254 my_convs = parent_convs;
2255 TREE_CHAIN (my_convs) = child_convs;
2256 }
2257 else
2258 my_convs = child_convs;
c8094d83 2259
8f2a734f
NS
2260 return my_convs;
2261}
2262
2263/* Worker for lookup_conversions. Lookup conversion functions in
9c763d19
KH
2264 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2265 a morally virtual base, and VIRTUALNESS is nonzero, if we've
8f2a734f
NS
2266 encountered virtual bases already in the tree walk. PARENT_CONVS &
2267 PARENT_TPL_CONVS are lists of list of conversions within parent
2268 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2269 elsewhere in the tree. Return the conversions found within this
9c763d19 2270 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
8f2a734f
NS
2271 encountered virtualness. We keep template and non-template
2272 conversions separate, to avoid unnecessary type comparisons.
2273
2274 The located conversion functions are held in lists of lists. The
2275 TREE_VALUE of the outer list is the list of conversion functions
2276 found in a particular binfo. The TREE_PURPOSE of both the outer
2277 and inner lists is the binfo at which those conversions were
2278 found. TREE_STATIC is set for those lists within of morally
2279 virtual binfos. The TREE_VALUE of the inner list is the conversion
2280 function or overload itself. The TREE_TYPE of each inner list node
2281 is the converted-to type. */
2282
2283static int
2284lookup_conversions_r (tree binfo,
2285 int virtual_depth, int virtualness,
2286 tree parent_convs, tree parent_tpl_convs,
2287 tree other_convs, tree other_tpl_convs,
2288 tree *convs, tree *tpl_convs)
2289{
2290 int my_virtualness = 0;
2291 tree my_convs = NULL_TREE;
2292 tree my_tpl_convs = NULL_TREE;
2293 tree child_convs = NULL_TREE;
2294 tree child_tpl_convs = NULL_TREE;
2295 unsigned i;
2296 tree base_binfo;
d4e6fecb 2297 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
8f2a734f 2298 tree conv;
a7a64a77 2299
8f2a734f
NS
2300 /* If we have no conversion operators, then don't look. */
2301 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2302 {
2303 *convs = *tpl_convs = NULL_TREE;
c8094d83 2304
8f2a734f
NS
2305 return 0;
2306 }
c8094d83 2307
8f2a734f
NS
2308 if (BINFO_VIRTUAL_P (binfo))
2309 virtual_depth++;
c8094d83 2310
8f2a734f 2311 /* First, locate the unhidden ones at this level. */
c8094d83 2312 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
8f2a734f 2313 VEC_iterate (tree, method_vec, i, conv);
aaaa46d2 2314 ++i)
72b7eeff 2315 {
8f2a734f 2316 tree cur = OVL_CURRENT (conv);
61a127b3 2317
8f2a734f 2318 if (!DECL_CONV_FN_P (cur))
72b7eeff 2319 break;
72c4a2a6 2320
8f2a734f 2321 if (TREE_CODE (cur) == TEMPLATE_DECL)
72c4a2a6 2322 {
8f2a734f
NS
2323 /* Only template conversions can be overloaded, and we must
2324 flatten them out and check each one individually. */
2325 tree tpls;
20d65560 2326
8f2a734f 2327 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
20d65560 2328 {
8f2a734f
NS
2329 tree tpl = OVL_CURRENT (tpls);
2330 tree type = DECL_CONV_FN_TYPE (tpl);
c8094d83 2331
8f2a734f
NS
2332 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2333 type, parent_tpl_convs, other_tpl_convs))
2334 {
2335 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2336 TREE_TYPE (my_tpl_convs) = type;
2337 if (virtual_depth)
2338 {
2339 TREE_STATIC (my_tpl_convs) = 1;
2340 my_virtualness = 1;
2341 }
2342 }
20d65560 2343 }
8f2a734f
NS
2344 }
2345 else
2346 {
2347 tree name = DECL_NAME (cur);
2348
2349 if (!IDENTIFIER_MARKED (name))
20d65560 2350 {
8f2a734f 2351 tree type = DECL_CONV_FN_TYPE (cur);
c8094d83 2352
8f2a734f
NS
2353 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2354 type, parent_convs, other_convs))
2355 {
2356 my_convs = tree_cons (binfo, conv, my_convs);
2357 TREE_TYPE (my_convs) = type;
2358 if (virtual_depth)
2359 {
2360 TREE_STATIC (my_convs) = 1;
2361 my_virtualness = 1;
2362 }
2363 IDENTIFIER_MARKED (name) = 1;
2364 }
20d65560 2365 }
72c4a2a6 2366 }
72b7eeff 2367 }
8f2a734f
NS
2368
2369 if (my_convs)
2370 {
2371 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2372 if (virtual_depth)
2373 TREE_STATIC (parent_convs) = 1;
2374 }
c8094d83 2375
8f2a734f
NS
2376 if (my_tpl_convs)
2377 {
2378 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2379 if (virtual_depth)
db2acc36 2380 TREE_STATIC (parent_tpl_convs) = 1;
8f2a734f
NS
2381 }
2382
2383 child_convs = other_convs;
2384 child_tpl_convs = other_tpl_convs;
c8094d83 2385
8f2a734f
NS
2386 /* Now iterate over each base, looking for more conversions. */
2387 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2388 {
2389 tree base_convs, base_tpl_convs;
2390 unsigned base_virtualness;
2391
2392 base_virtualness = lookup_conversions_r (base_binfo,
2393 virtual_depth, virtualness,
2394 parent_convs, parent_tpl_convs,
2395 child_convs, child_tpl_convs,
2396 &base_convs, &base_tpl_convs);
2397 if (base_virtualness)
2398 my_virtualness = virtualness = 1;
2399 child_convs = chainon (base_convs, child_convs);
2400 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2401 }
2402
2403 /* Unmark the conversions found at this level */
2404 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2405 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2406
2407 *convs = split_conversions (my_convs, parent_convs,
2408 child_convs, other_convs);
2409 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2410 child_tpl_convs, other_tpl_convs);
c8094d83 2411
8f2a734f 2412 return my_virtualness;
e1cd6e56
MS
2413}
2414
27b8d0cd
MM
2415/* Return a TREE_LIST containing all the non-hidden user-defined
2416 conversion functions for TYPE (and its base-classes). The
8f2a734f
NS
2417 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2418 function. The TREE_PURPOSE is the BINFO from which the conversion
2419 functions in this node were selected. This function is effectively
2420 performing a set of member lookups as lookup_fnfield does, but
2421 using the type being converted to as the unique key, rather than the
2422 field name. */
27b8d0cd 2423
e1cd6e56 2424tree
86ac0575 2425lookup_conversions (tree type)
e1cd6e56 2426{
8f2a734f
NS
2427 tree convs, tpl_convs;
2428 tree list = NULL_TREE;
c8094d83 2429
0171b21c 2430 complete_type (type);
8f2a734f
NS
2431 if (!TYPE_BINFO (type))
2432 return NULL_TREE;
c8094d83 2433
8f2a734f
NS
2434 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2435 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2436 &convs, &tpl_convs);
c8094d83 2437
8f2a734f
NS
2438 /* Flatten the list-of-lists */
2439 for (; convs; convs = TREE_CHAIN (convs))
2440 {
2441 tree probe, next;
2442
2443 for (probe = TREE_VALUE (convs); probe; probe = next)
2444 {
2445 next = TREE_CHAIN (probe);
2446
2447 TREE_CHAIN (probe) = list;
2448 list = probe;
2449 }
2450 }
c8094d83 2451
8f2a734f
NS
2452 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2453 {
2454 tree probe, next;
72c4a2a6 2455
8f2a734f
NS
2456 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2457 {
2458 next = TREE_CHAIN (probe);
72c4a2a6 2459
8f2a734f
NS
2460 TREE_CHAIN (probe) = list;
2461 list = probe;
2462 }
2463 }
c8094d83 2464
8f2a734f 2465 return list;
e1cd6e56 2466}
6467930b 2467
9965d119
NS
2468/* Returns the binfo of the first direct or indirect virtual base derived
2469 from BINFO, or NULL if binfo is not via virtual. */
6ad07332 2470
f9825168 2471tree
86ac0575 2472binfo_from_vbase (tree binfo)
6ad07332
JM
2473{
2474 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2475 {
809e3e7f 2476 if (BINFO_VIRTUAL_P (binfo))
f9825168 2477 return binfo;
6ad07332 2478 }
f9825168 2479 return NULL_TREE;
6ad07332 2480}
a55583e9 2481
9965d119
NS
2482/* Returns the binfo of the first direct or indirect virtual base derived
2483 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2484 via virtual. */
2485
2486tree
86ac0575 2487binfo_via_virtual (tree binfo, tree limit)
9965d119 2488{
2c2e8978
NS
2489 if (limit && !CLASSTYPE_VBASECLASSES (limit))
2490 /* LIMIT has no virtual bases, so BINFO cannot be via one. */
2491 return NULL_TREE;
c8094d83 2492
539ed333 2493 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
9965d119
NS
2494 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2495 {
809e3e7f 2496 if (BINFO_VIRTUAL_P (binfo))
9965d119
NS
2497 return binfo;
2498 }
2499 return NULL_TREE;
2500}
2501
dbbf88d1
NS
2502/* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2503 Find the equivalent binfo within whatever graph HERE is located.
9bcb9aae 2504 This is the inverse of original_binfo. */
a55583e9
MM
2505
2506tree
dbbf88d1 2507copied_binfo (tree binfo, tree here)
a55583e9 2508{
dbbf88d1 2509 tree result = NULL_TREE;
c8094d83 2510
809e3e7f 2511 if (BINFO_VIRTUAL_P (binfo))
dbbf88d1
NS
2512 {
2513 tree t;
a55583e9 2514
dbbf88d1
NS
2515 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2516 t = BINFO_INHERITANCE_CHAIN (t))
2517 continue;
58c42dc2
NS
2518
2519 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
dbbf88d1
NS
2520 }
2521 else if (BINFO_INHERITANCE_CHAIN (binfo))
2522 {
fa743e8c
NS
2523 tree cbinfo;
2524 tree base_binfo;
2525 int ix;
c8094d83 2526
fa743e8c
NS
2527 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2528 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
539ed333 2529 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
fa743e8c
NS
2530 {
2531 result = base_binfo;
2532 break;
2533 }
dbbf88d1
NS
2534 }
2535 else
2536 {
539ed333 2537 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
dbbf88d1
NS
2538 result = here;
2539 }
2540
50bc768d 2541 gcc_assert (result);
dbbf88d1 2542 return result;
a55583e9 2543}
dbbf88d1 2544
58c42dc2
NS
2545tree
2546binfo_for_vbase (tree base, tree t)
2547{
2548 unsigned ix;
2549 tree binfo;
d4e6fecb 2550 VEC(tree,gc) *vbases;
c8094d83 2551
9ba5ff0f
NS
2552 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2553 VEC_iterate (tree, vbases, ix, binfo); ix++)
539ed333 2554 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
58c42dc2
NS
2555 return binfo;
2556 return NULL;
2557}
2558
dbbf88d1 2559/* BINFO is some base binfo of HERE, within some other
34cd5ae7 2560 hierarchy. Return the equivalent binfo, but in the hierarchy
dbbf88d1 2561 dominated by HERE. This is the inverse of copied_binfo. If BINFO
9bcb9aae 2562 is not a base binfo of HERE, returns NULL_TREE. */
dbbf88d1
NS
2563
2564tree
2565original_binfo (tree binfo, tree here)
2566{
2567 tree result = NULL;
c8094d83 2568
539ed333 2569 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
dbbf88d1 2570 result = here;
809e3e7f 2571 else if (BINFO_VIRTUAL_P (binfo))
58c42dc2
NS
2572 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2573 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2574 : NULL_TREE);
dbbf88d1
NS
2575 else if (BINFO_INHERITANCE_CHAIN (binfo))
2576 {
2577 tree base_binfos;
c8094d83 2578
dbbf88d1
NS
2579 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2580 if (base_binfos)
2581 {
fa743e8c
NS
2582 int ix;
2583 tree base_binfo;
c8094d83 2584
fa743e8c 2585 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
539ed333
NS
2586 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2587 BINFO_TYPE (binfo)))
fa743e8c
NS
2588 {
2589 result = base_binfo;
2590 break;
2591 }
dbbf88d1
NS
2592 }
2593 }
c8094d83 2594
dbbf88d1
NS
2595 return result;
2596}
2597
This page took 2.676265 seconds and 5 git commands to generate.