📄 search.c
字号:
if (DECL_NONSTATIC_MEMBER_P (decl)) { /* We can tell through what the reference is occurring by chasing BINFO up to the root. */ tree t = binfo; while (BINFO_INHERITANCE_CHAIN (t)) t = BINFO_INHERITANCE_CHAIN (t); if (!DERIVED_FROM_P (derived, BINFO_TYPE (t))) return 0; } return 1;}/* Returns non-zero if SCOPE is a friend of a type which would be able to acces DECL, named in TYPE, through the object indicated by BINFO. */static intfriend_accessible_p (scope, type, decl, binfo) tree scope; tree type; tree decl; tree binfo;{ tree befriending_classes; tree t; if (!scope) return 0; if (TREE_CODE (scope) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (scope)) befriending_classes = DECL_BEFRIENDING_CLASSES (scope); else if (TYPE_P (scope)) befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope); else return 0; for (t = befriending_classes; t; t = TREE_CHAIN (t)) if (protected_accessible_p (type, decl, TREE_VALUE (t), binfo)) return 1; if (TREE_CODE (scope) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (scope)) { /* Perhaps this SCOPE is a member of a class which is a friend. */ if (friend_accessible_p (DECL_CLASS_CONTEXT (scope), type, decl, binfo)) return 1; /* Or an instantiation of something which is a friend. */ if (DECL_TEMPLATE_INFO (scope)) return friend_accessible_p (DECL_TI_TEMPLATE (scope), type, decl, binfo); } else if (CLASSTYPE_TEMPLATE_INFO (scope)) return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), type, decl, binfo); return 0;} /* DECL is a declaration from a base class of TYPE, which was the classs used to name DECL. Return non-zero if, in the current context, DECL is accessible. If TYPE is actually a BINFO node, then we can tell in what context the access is occurring by looking at the most derived class along the path indicated by BINFO. */int accessible_p (type, decl) tree type; tree decl; { tree binfo; tree t; /* Non-zero if it's OK to access DECL if it has protected accessibility in TYPE. */ int protected_ok = 0; /* If we're not checking access, everything is accessible. */ if (!flag_access_control) return 1; /* If this declaration is in a block or namespace scope, there's no access control. */ if (!TYPE_P (context_for_name_lookup (decl))) return 1; /* We don't do access control for types yet. */ if (TREE_CODE (decl) == TYPE_DECL) return 1; if (!TYPE_P (type)) { binfo = type; type = BINFO_TYPE (type); } else binfo = TYPE_BINFO (type); /* [class.access.base] A member m is accessible when named in class N if --m as a member of N is public, or --m as a member of N is private, and the reference occurs in a member or friend of class N, or --m as a member of N is protected, and the reference occurs in a member or friend of class N, or in a member or friend of a class P derived from N, where m as a member of P is private or protected, or --there exists a base class B of N that is accessible at the point of reference, and m is accessible when named in class B. We walk the base class hierarchy, checking these conditions. */ /* Figure out where the reference is occurring. Check to see if DECL is private or protected in this scope, since that will determine whether protected access in TYPE allowed. */ if (current_class_type) protected_ok = protected_accessible_p (type, decl, current_class_type, binfo); /* Now, loop through the classes of which we are a friend. */ if (!protected_ok) protected_ok = friend_accessible_p (current_scope (), type, decl, binfo); /* Standardize on the same that will access_in_type will use. We don't need to know what path was chosen from this point onwards. */ binfo = TYPE_BINFO (type); /* Compute the accessibility of DECL in the class hierarchy dominated by type. */ access_in_type (type, decl); /* Walk the hierarchy again, looking for a base class that allows access. */ t = dfs_walk (binfo, dfs_accessible_p, dfs_accessible_queue_p, protected_ok ? &protected_ok : 0); /* Clear any mark bits. Note that we have to walk the whole tree here, since we have aborted the previous walk from some point deep in the tree. */ dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0); assert_canonical_unmarked (binfo); return t != NULL_TREE;}/* Routine to see if the sub-object denoted by the binfo PARENT can be found as a base class and sub-object of the object denoted by BINFO. This routine relies upon binfos not being shared, except for binfos for virtual bases. */static intis_subobject_of_p (parent, binfo) tree parent, binfo;{ tree binfos; int i, n_baselinks; /* We want to canonicalize for comparison purposes. But, when we iterate through basetypes later, we want the binfos from the original hierarchy. That's why we have to calculate BINFOS first, and then canonicalize. */ binfos = BINFO_BASETYPES (binfo); parent = canonical_binfo (parent); binfo = canonical_binfo (binfo); if (parent == binfo) return 1; n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; /* Process and/or queue base types. */ for (i = 0; i < n_baselinks; i++) { tree base_binfo = TREE_VEC_ELT (binfos, i); if (!CLASS_TYPE_P (TREE_TYPE (base_binfo))) /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base class there's no way to descend into it. */ continue; if (is_subobject_of_p (parent, base_binfo)) return 1; } return 0;}/* See if a one FIELD_DECL hides another. This routine is meant to correspond to ANSI working paper Sept 17, 1992 10p4. The two binfos given are the binfos corresponding to the particular places the FIELD_DECLs are found. This routine relies upon binfos not being shared, except for virtual bases. */static inthides (hider_binfo, hidee_binfo) tree hider_binfo, hidee_binfo;{ /* hider hides hidee, if hider has hidee as a base class and the instance of hidee is a sub-object of hider. The first part is always true is the second part is true. When hider and hidee are the same (two ways to get to the exact same member) we consider either one as hiding the other. */ return is_subobject_of_p (hidee_binfo, hider_binfo);}/* Very similar to lookup_fnfields_1 but it ensures that at least one function was declared inside the class given by TYPE. It really should only return functions that match the given TYPE. */static intlookup_fnfields_here (type, name) tree type, name;{ int idx = lookup_fnfields_1 (type, name); tree fndecls; /* ctors and dtors are always only in the right class. */ if (idx <= 1) return idx; fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); while (fndecls) { if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (OVL_CURRENT (fndecls))) == TYPE_MAIN_VARIANT (type)) return idx; fndecls = OVL_CHAIN (fndecls); } return -1;}struct lookup_field_info { /* The type in which we're looking. */ tree type; /* The name of the field for which we're looking. */ tree name; /* If non-NULL, the current result of the lookup. */ tree rval; /* The path to RVAL. */ tree rval_binfo; /* If non-NULL, the lookup was ambiguous, and this is a list of the candidates. */ tree ambiguous; /* If non-zero, we are looking for types, not data members. */ int want_type; /* If non-zero, RVAL was found by looking through a dependent base. */ int from_dep_base_p; /* If something went wrong, a message indicating what. */ const char *errstr;};/* Returns non-zero if BINFO is not hidden by the value found by the lookup so far. If BINFO is hidden, then there's no need to look in it. DATA is really a struct lookup_field_info. Called from lookup_field via breadth_first_search. */static treelookup_field_queue_p (binfo, data) tree binfo; void *data;{ struct lookup_field_info *lfi = (struct lookup_field_info *) data; /* Don't look for constructors or destructors in base classes. */ if (lfi->name == ctor_identifier || lfi->name == dtor_identifier) return NULL_TREE; /* If this base class is hidden by the best-known value so far, we don't need to look. */ if (!lfi->from_dep_base_p && lfi->rval_binfo && hides (lfi->rval_binfo, binfo)) return NULL_TREE; if (TREE_VIA_VIRTUAL (binfo)) return binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (lfi->type)); else return binfo;}/* Within the scope of a template class, you can refer to the particular to the current specialization with the name of the template itself. For example: template <typename T> struct S { S* sp; } Returns non-zero if DECL is such a declaration in a class TYPE. */static inttemplate_self_reference_p (type, decl) tree type; tree decl;{ return (CLASSTYPE_USE_TEMPLATE (type) && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) && TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == constructor_name (type));}/* DATA is really a struct lookup_field_info. Look for a field with the name indicated there in BINFO. If this function returns a non-NULL value it is the result of the lookup. Called from lookup_field via breadth_first_search. */static treelookup_field_r (binfo, data) tree binfo; void *data;{ struct lookup_field_info *lfi = (struct lookup_field_info *) data; tree type = BINFO_TYPE (binfo); tree nval = NULL_TREE; int from_dep_base_p; /* First, look for a function. There can't be a function and a data member with the same name, and if there's a function and a type with the same name, the type is hidden by the function. */ if (!lfi->want_type) { int idx = lookup_fnfields_here (type, lfi->name); if (idx >= 0) nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx); } if (!nval) /* Look for a data member or type. */ nval = lookup_field_1 (type, lfi->name); /* If there is no declaration with the indicated name in this type, then there's nothing to do. */ if (!nval) return NULL_TREE; /* If we're looking up a type (as with an elaborated type specifier) we ignore all non-types we find. */ if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL) { nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type)); if (nval) nval = TYPE_MAIN_DECL (TREE_VALUE (nval)); else return NULL_TREE; } /* You must name a template base class with a template-id. */ if (!same_type_p (type, lfi->type) && template_self_reference_p (type, nval)) return NULL_TREE; from_dep_base_p = dependent_base_p (binfo); if (lfi->from_dep_base_p && !from_dep_base_p) { /* If the new declaration is not found via a dependent base, and the old one was, then we must prefer the new one. We weren't really supposed to be able to find the old one, so we don't want to be affected by a specialization. Consider: struct B { typedef int I; }; template <typename T> struct D1 : virtual public B {}; template <typename T> struct D : public D1, virtual pubic B { I i; }; The `I' in `D<T>' is unambigousuly `B::I', regardless of how D1 is specialized. */ lfi->from_dep_base_p = 0; lfi->rval = NULL_TREE; lfi->rval_binfo = NULL_TREE; lfi->ambiguous = NULL_TREE; lfi->errstr = 0; } else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p) /* Similarly, if the old declaration was not found via a dependent base, and the new one is, ignore the new one. */ return NULL_TREE; /* If the lookup already found a match, and the new value doesn't hide the old one, we might have an ambiguity. */ if (lfi->rval_binfo && !hides (binfo, lfi->rval_binfo)) { if (nval == lfi->rval && SHARED_MEMBER_P (nval)) /* The two things are really the same. */ ; else if (hides (lfi->rval_binfo, binfo)) /* The previous value hides the new one. */ ; else { /* We have a real ambiguity. We keep a chain of all the candidates. */ if (!lfi->ambiguous && lfi->rval) { /* This is the first time we noticed an ambiguity. Add what we previously thought was a reasonable candidate to the list. */ lfi->ambiguous = scratch_tree_cons (NULL_TREE, lfi->rval, NULL_TREE); TREE_TYPE (lfi->ambiguous) = error_mark_node; } /* Add the new value. */ lfi->ambiguous = scratch_tree_cons (NULL_TREE, nval, lfi->ambiguous); TREE_TYPE (lfi->ambiguous) = error_mark_node; lfi->errstr = "request for member `%D' is ambiguous"; } } else { /* If the thing we're looking for is a virtual base class, then we know we've got what we want at this point; there's no way to get an ambiguity. */ if (VBASE_NAME_P (lfi->name)) { lfi->rval = nval; return nval; } if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL /* We need to return a member template class so we can define partial specializations. Is there a better way? */ && !DECL_CLASS_TEMPLATE_P (nval)) /* The thing we're looking for isn't a type, so the implicit typename extension doesn't apply, so we just pretend we didn't find anything. */ return NULL_TREE; lfi->rval = nval; lfi->from_dep_base_p = from_dep_base_p; lfi->rval_binfo = binfo; } return NULL_TREE;}/* Look for a memer named NAME in an inheritance lattice dominated by XBASETYPE. PROTECT is 0 or two, we do not check access. If it is 1, we enforce accessibility. If PROTECT is zero, then, for an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -