2009-03-27 13:05:05 +08:00
|
|
|
//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
|
2009-03-27 12:43:36 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides Sema routines for C++ access control semantics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-03-27 12:54:36 +08:00
|
|
|
|
|
|
|
#include "Sema.h"
|
2010-01-23 08:46:32 +08:00
|
|
|
#include "Lookup.h"
|
2009-03-27 14:03:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-10-07 01:59:45 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2010-03-12 09:19:31 +08:00
|
|
|
#include "clang/AST/DeclFriend.h"
|
2010-03-24 13:22:00 +08:00
|
|
|
#include "clang/AST/DependentDiagnostic.h"
|
2010-01-27 09:50:18 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
|
2009-03-27 12:54:36 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
/// A copy of Sema's enum without AR_delayed.
|
|
|
|
enum AccessResult {
|
|
|
|
AR_accessible,
|
|
|
|
AR_inaccessible,
|
|
|
|
AR_dependent
|
|
|
|
};
|
|
|
|
|
2009-03-27 13:05:05 +08:00
|
|
|
/// SetMemberAccessSpecifier - Set the access specifier of a member.
|
|
|
|
/// Returns true on error (when the previous member decl access specifier
|
|
|
|
/// is different from the new member decl access specifier).
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
|
2009-03-27 12:54:36 +08:00
|
|
|
NamedDecl *PrevMemberDecl,
|
|
|
|
AccessSpecifier LexicalAS) {
|
|
|
|
if (!PrevMemberDecl) {
|
|
|
|
// Use the lexical access specifier.
|
|
|
|
MemberDecl->setAccess(LexicalAS);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-27 12:54:36 +08:00
|
|
|
// C++ [class.access.spec]p3: When a member is redeclared its access
|
|
|
|
// specifier must be same as its initial declaration.
|
|
|
|
if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(MemberDecl->getLocation(),
|
|
|
|
diag::err_class_redeclared_with_different_access)
|
2009-03-27 12:54:36 +08:00
|
|
|
<< MemberDecl << LexicalAS;
|
|
|
|
Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
|
|
|
|
<< PrevMemberDecl << PrevMemberDecl->getAccess();
|
2009-12-23 08:37:40 +08:00
|
|
|
|
|
|
|
MemberDecl->setAccess(LexicalAS);
|
2009-03-27 12:54:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-27 12:54:36 +08:00
|
|
|
MemberDecl->setAccess(PrevMemberDecl->getAccess());
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-27 13:05:05 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) {
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
|
|
|
|
// This can only happen at top: enum decls only "publish" their
|
|
|
|
// immediate members.
|
|
|
|
if (isa<EnumDecl>(DC))
|
|
|
|
DC = cast<EnumDecl>(DC)->getDeclContext();
|
|
|
|
|
|
|
|
CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(DC);
|
|
|
|
while (DeclaringClass->isAnonymousStructOrUnion())
|
|
|
|
DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext());
|
|
|
|
return DeclaringClass;
|
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
namespace {
|
|
|
|
struct EffectiveContext {
|
2010-03-27 14:55:49 +08:00
|
|
|
EffectiveContext() : Inner(0), Dependent(false) {}
|
2010-02-10 17:31:12 +08:00
|
|
|
|
2010-03-24 15:46:06 +08:00
|
|
|
explicit EffectiveContext(DeclContext *DC)
|
|
|
|
: Inner(DC),
|
|
|
|
Dependent(DC->isDependentContext()) {
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-03-17 12:58:56 +08:00
|
|
|
// C++ [class.access.nest]p1:
|
|
|
|
// A nested class is a member and as such has the same access
|
|
|
|
// rights as any other member.
|
|
|
|
// C++ [class.access]p2:
|
|
|
|
// A member of a class can also access all the names to which
|
2010-03-27 14:55:49 +08:00
|
|
|
// the class has access. A local class of a member function
|
|
|
|
// may access the same names that the member function itself
|
|
|
|
// may access.
|
|
|
|
// This almost implies that the privileges of nesting are transitive.
|
|
|
|
// Technically it says nothing about the local classes of non-member
|
|
|
|
// functions (which can gain privileges through friendship), but we
|
|
|
|
// take that as an oversight.
|
|
|
|
while (true) {
|
|
|
|
if (isa<CXXRecordDecl>(DC)) {
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
|
|
|
|
Records.push_back(Record);
|
|
|
|
DC = Record->getDeclContext();
|
|
|
|
} else if (isa<FunctionDecl>(DC)) {
|
|
|
|
FunctionDecl *Function = cast<FunctionDecl>(DC)->getCanonicalDecl();
|
|
|
|
Functions.push_back(Function);
|
|
|
|
DC = Function->getDeclContext();
|
|
|
|
} else if (DC->isFileContext()) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
DC = DC->getParent();
|
|
|
|
}
|
2010-03-17 12:58:56 +08:00
|
|
|
}
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
bool isDependent() const { return Dependent; }
|
|
|
|
|
2010-03-17 12:58:56 +08:00
|
|
|
bool includesClass(const CXXRecordDecl *R) const {
|
|
|
|
R = R->getCanonicalDecl();
|
|
|
|
return std::find(Records.begin(), Records.end(), R)
|
|
|
|
!= Records.end();
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 15:46:06 +08:00
|
|
|
/// Retrieves the innermost "useful" context. Can be null if we're
|
|
|
|
/// doing access-control without privileges.
|
|
|
|
DeclContext *getInnerContext() const {
|
|
|
|
return Inner;
|
2010-03-24 13:22:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
|
|
|
|
|
2010-03-24 15:46:06 +08:00
|
|
|
DeclContext *Inner;
|
2010-03-27 14:55:49 +08:00
|
|
|
llvm::SmallVector<FunctionDecl*, 4> Functions;
|
2010-03-17 12:58:56 +08:00
|
|
|
llvm::SmallVector<CXXRecordDecl*, 4> Records;
|
2010-03-24 13:22:00 +08:00
|
|
|
bool Dependent;
|
2010-02-10 17:31:12 +08:00
|
|
|
};
|
2010-04-07 05:38:20 +08:00
|
|
|
|
|
|
|
/// Like Sema's AccessedEntity, but kindly lets us scribble all over
|
|
|
|
/// it.
|
|
|
|
struct AccessTarget : public Sema::AccessedEntity {
|
|
|
|
AccessTarget(const Sema::AccessedEntity &Entity)
|
|
|
|
: AccessedEntity(Entity) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessTarget(ASTContext &Context,
|
|
|
|
MemberNonce _,
|
|
|
|
CXXRecordDecl *NamingClass,
|
|
|
|
DeclAccessPair FoundDecl,
|
|
|
|
QualType BaseObjectType)
|
|
|
|
: AccessedEntity(Context, Member, NamingClass, FoundDecl, BaseObjectType) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessTarget(ASTContext &Context,
|
|
|
|
BaseNonce _,
|
|
|
|
CXXRecordDecl *BaseClass,
|
|
|
|
CXXRecordDecl *DerivedClass,
|
|
|
|
AccessSpecifier Access)
|
|
|
|
: AccessedEntity(Context, Base, BaseClass, DerivedClass, Access) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasInstanceContext() const {
|
|
|
|
return HasInstanceContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SavedInstanceContext {
|
|
|
|
public:
|
|
|
|
~SavedInstanceContext() {
|
|
|
|
Target.HasInstanceContext = Has;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-04-07 08:41:46 +08:00
|
|
|
friend struct AccessTarget;
|
2010-04-07 05:38:20 +08:00
|
|
|
explicit SavedInstanceContext(AccessTarget &Target)
|
|
|
|
: Target(Target), Has(Target.HasInstanceContext) {}
|
|
|
|
AccessTarget &Target;
|
|
|
|
bool Has;
|
|
|
|
};
|
|
|
|
|
|
|
|
SavedInstanceContext saveInstanceContext() {
|
|
|
|
return SavedInstanceContext(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void suppressInstanceContext() {
|
|
|
|
HasInstanceContext = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
|
|
|
|
assert(HasInstanceContext);
|
|
|
|
if (CalculatedInstanceContext)
|
|
|
|
return InstanceContext;
|
|
|
|
|
|
|
|
CalculatedInstanceContext = true;
|
|
|
|
DeclContext *IC = S.computeDeclContext(getBaseObjectType());
|
|
|
|
InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0);
|
|
|
|
return InstanceContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *getDeclaringClass() const {
|
|
|
|
return DeclaringClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void initialize() {
|
|
|
|
HasInstanceContext = (isMemberAccess() &&
|
|
|
|
!getBaseObjectType().isNull() &&
|
|
|
|
getTargetDecl()->isCXXInstanceMember());
|
|
|
|
CalculatedInstanceContext = false;
|
|
|
|
InstanceContext = 0;
|
|
|
|
|
|
|
|
if (isMemberAccess())
|
|
|
|
DeclaringClass = FindDeclaringClass(getTargetDecl());
|
|
|
|
else
|
|
|
|
DeclaringClass = getBaseClass();
|
|
|
|
DeclaringClass = DeclaringClass->getCanonicalDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasInstanceContext : 1;
|
|
|
|
mutable bool CalculatedInstanceContext : 1;
|
|
|
|
mutable const CXXRecordDecl *InstanceContext;
|
|
|
|
const CXXRecordDecl *DeclaringClass;
|
|
|
|
};
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
/// Checks whether one class is derived from another, inclusively.
|
|
|
|
/// Properly indicates when it couldn't be determined due to
|
|
|
|
/// dependence.
|
|
|
|
///
|
|
|
|
/// This should probably be donated to AST or at least Sema.
|
|
|
|
static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
|
|
|
|
const CXXRecordDecl *Target) {
|
|
|
|
assert(Derived->getCanonicalDecl() == Derived);
|
|
|
|
assert(Target->getCanonicalDecl() == Target);
|
2010-03-24 17:04:37 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
if (Derived == Target) return AR_accessible;
|
2010-03-24 17:04:37 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
|
|
|
llvm::SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator
|
|
|
|
I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD;
|
|
|
|
|
|
|
|
QualType T = I->getType();
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
} else {
|
|
|
|
// It's possible for a base class to be the current
|
|
|
|
// instantiation of some enclosing template, but I'm guessing
|
|
|
|
// nobody will ever care that we just dependently delay here.
|
|
|
|
assert(T->isDependentType() && "non-dependent base wasn't a record?");
|
|
|
|
OnFailure = AR_dependent;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RD = RD->getCanonicalDecl();
|
|
|
|
if (RD == Target) return AR_accessible;
|
|
|
|
Queue.push_back(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Queue.empty()) break;
|
|
|
|
|
|
|
|
Derived = Queue.back();
|
|
|
|
Queue.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return OnFailure;
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
static bool MightInstantiateTo(Sema &S, DeclContext *Context,
|
|
|
|
DeclContext *Friend) {
|
|
|
|
if (Friend == Context)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(!Friend->isDependentContext() &&
|
|
|
|
"can't handle friends with dependent contexts here");
|
|
|
|
|
|
|
|
if (!Context->isDependentContext())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Friend->isFileContext())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: this is very conservative
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Asks whether the type in 'context' can ever instantiate to the type
|
|
|
|
// in 'friend'.
|
|
|
|
static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
|
|
|
|
if (Friend == Context)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Friend->isDependentType() && !Context->isDependentType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: this is very conservative.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MightInstantiateTo(Sema &S,
|
|
|
|
FunctionDecl *Context,
|
|
|
|
FunctionDecl *Friend) {
|
|
|
|
if (Context->getDeclName() != Friend->getDeclName())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!MightInstantiateTo(S,
|
|
|
|
Context->getDeclContext(),
|
|
|
|
Friend->getDeclContext()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CanQual<FunctionProtoType> FriendTy
|
|
|
|
= S.Context.getCanonicalType(Friend->getType())
|
|
|
|
->getAs<FunctionProtoType>();
|
|
|
|
CanQual<FunctionProtoType> ContextTy
|
|
|
|
= S.Context.getCanonicalType(Context->getType())
|
|
|
|
->getAs<FunctionProtoType>();
|
|
|
|
|
|
|
|
// There isn't any way that I know of to add qualifiers
|
|
|
|
// during instantiation.
|
|
|
|
if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (FriendTy->getNumArgs() != ContextTy->getNumArgs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!MightInstantiateTo(S,
|
|
|
|
ContextTy->getResultType(),
|
|
|
|
FriendTy->getResultType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = FriendTy->getNumArgs(); I != E; ++I)
|
|
|
|
if (!MightInstantiateTo(S,
|
|
|
|
ContextTy->getArgType(I),
|
|
|
|
FriendTy->getArgType(I)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MightInstantiateTo(Sema &S,
|
|
|
|
FunctionTemplateDecl *Context,
|
|
|
|
FunctionTemplateDecl *Friend) {
|
|
|
|
return MightInstantiateTo(S,
|
|
|
|
Context->getTemplatedDecl(),
|
|
|
|
Friend->getTemplatedDecl());
|
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
const CXXRecordDecl *Friend) {
|
2010-03-18 04:01:29 +08:00
|
|
|
if (EC.includesClass(Friend))
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_accessible;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
if (EC.isDependent()) {
|
|
|
|
CanQualType FriendTy
|
|
|
|
= S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
|
|
|
|
|
|
|
|
for (EffectiveContext::record_iterator
|
|
|
|
I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
|
|
|
|
CanQualType ContextTy
|
|
|
|
= S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
|
|
|
|
if (MightInstantiateTo(S, ContextTy, FriendTy))
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_dependent;
|
2010-03-24 13:22:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_inaccessible;
|
2010-03-18 04:01:29 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
CanQualType Friend) {
|
2010-03-24 13:22:00 +08:00
|
|
|
if (const RecordType *RT = Friend->getAs<RecordType>())
|
|
|
|
return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
// TODO: we can do better than this
|
|
|
|
if (Friend->isDependentType())
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_dependent;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_inaccessible;
|
2010-03-24 13:22:00 +08:00
|
|
|
}
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
/// Determines whether the given friend class template matches
|
|
|
|
/// anything in the effective context.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
ClassTemplateDecl *Friend) {
|
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-25 14:39:04 +08:00
|
|
|
// Check whether the friend is the template of a class in the
|
|
|
|
// context chain.
|
2010-03-24 13:22:00 +08:00
|
|
|
for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator
|
|
|
|
I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
|
|
|
|
CXXRecordDecl *Record = *I;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-25 14:39:04 +08:00
|
|
|
// Figure out whether the current class has a template:
|
2010-03-24 13:22:00 +08:00
|
|
|
ClassTemplateDecl *CTD;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
// A specialization of the template...
|
|
|
|
if (isa<ClassTemplateSpecializationDecl>(Record)) {
|
|
|
|
CTD = cast<ClassTemplateSpecializationDecl>(Record)
|
|
|
|
->getSpecializedTemplate();
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
// ... or the template pattern itself.
|
|
|
|
} else {
|
|
|
|
CTD = Record->getDescribedClassTemplate();
|
|
|
|
if (!CTD) continue;
|
2010-03-18 04:01:29 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
// It's a match.
|
|
|
|
if (Friend == CTD->getCanonicalDecl())
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_accessible;
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-03-25 14:39:04 +08:00
|
|
|
// If the context isn't dependent, it can't be a dependent match.
|
|
|
|
if (!EC.isDependent())
|
|
|
|
continue;
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
// If the template names don't match, it can't be a dependent
|
|
|
|
// match. This isn't true in C++0x because of template aliases.
|
|
|
|
if (!S.LangOpts.CPlusPlus0x && CTD->getDeclName() != Friend->getDeclName())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the class's context can't instantiate to the friend's
|
|
|
|
// context, it can't be a dependent match.
|
|
|
|
if (!MightInstantiateTo(S, CTD->getDeclContext(),
|
|
|
|
Friend->getDeclContext()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise, it's a dependent match.
|
2010-04-07 05:38:20 +08:00
|
|
|
OnFailure = AR_dependent;
|
2010-03-18 04:01:29 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
return OnFailure;
|
|
|
|
}
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
/// Determines whether the given friend function matches anything in
|
|
|
|
/// the effective context.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
FunctionDecl *Friend) {
|
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
|
|
|
|
I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
|
|
|
|
if (Friend == *I)
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_accessible;
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
|
2010-04-07 05:38:20 +08:00
|
|
|
OnFailure = AR_dependent;
|
2010-03-27 14:55:49 +08:00
|
|
|
}
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
return OnFailure;
|
2010-03-24 13:22:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines whether the given friend function template matches
|
|
|
|
/// anything in the effective context.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
FunctionTemplateDecl *Friend) {
|
|
|
|
if (EC.Functions.empty()) return AR_inaccessible;
|
2010-03-27 14:55:49 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
|
|
|
|
I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
|
|
|
|
if (!FTD)
|
|
|
|
FTD = (*I)->getDescribedFunctionTemplate();
|
|
|
|
if (!FTD)
|
|
|
|
continue;
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
FTD = FTD->getCanonicalDecl();
|
2010-03-18 04:01:29 +08:00
|
|
|
|
2010-03-27 14:55:49 +08:00
|
|
|
if (Friend == FTD)
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_accessible;
|
2010-03-27 14:55:49 +08:00
|
|
|
|
|
|
|
if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
|
2010-04-07 05:38:20 +08:00
|
|
|
OnFailure = AR_dependent;
|
2010-03-27 14:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return OnFailure;
|
2010-03-18 04:01:29 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
/// Determines whether the given friend declaration matches anything
|
|
|
|
/// in the effective context.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult MatchesFriend(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
FriendDecl *FriendD) {
|
2010-03-26 02:04:51 +08:00
|
|
|
if (TypeSourceInfo *T = FriendD->getFriendType())
|
|
|
|
return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
|
2010-03-24 13:22:00 +08:00
|
|
|
|
|
|
|
NamedDecl *Friend
|
|
|
|
= cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
|
|
|
|
|
|
|
|
// FIXME: declarations with dependent or templated scope.
|
|
|
|
|
|
|
|
if (isa<ClassTemplateDecl>(Friend))
|
|
|
|
return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
|
|
|
|
|
|
|
|
if (isa<FunctionTemplateDecl>(Friend))
|
|
|
|
return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
|
|
|
|
|
|
|
|
if (isa<CXXRecordDecl>(Friend))
|
|
|
|
return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
|
|
|
|
|
|
|
|
assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
|
|
|
|
return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
|
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult GetFriendKind(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
const CXXRecordDecl *Class) {
|
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
2010-03-17 12:58:56 +08:00
|
|
|
|
2010-03-12 09:19:31 +08:00
|
|
|
// Okay, check friends.
|
|
|
|
for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
|
|
|
|
E = Class->friend_end(); I != E; ++I) {
|
|
|
|
FriendDecl *Friend = *I;
|
|
|
|
|
2010-03-18 04:01:29 +08:00
|
|
|
switch (MatchesFriend(S, EC, Friend)) {
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_accessible:
|
|
|
|
return AR_accessible;
|
2010-03-17 12:58:56 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_inaccessible:
|
|
|
|
continue;
|
2010-03-12 09:19:31 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_dependent:
|
|
|
|
OnFailure = AR_dependent;
|
2010-03-18 04:01:29 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-03-12 09:19:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// That's it, give up.
|
2010-03-17 12:58:56 +08:00
|
|
|
return OnFailure;
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult HasAccess(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
const CXXRecordDecl *NamingClass,
|
|
|
|
AccessSpecifier Access,
|
|
|
|
const AccessTarget &Target) {
|
2010-04-02 08:03:43 +08:00
|
|
|
assert(NamingClass->getCanonicalDecl() == NamingClass &&
|
|
|
|
"declaration should be canonicalized before being passed here");
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
if (Access == AS_public) return AR_accessible;
|
2010-04-02 08:03:43 +08:00
|
|
|
assert(Access == AS_private || Access == AS_protected);
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessResult OnFailure = AR_inaccessible;
|
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
for (EffectiveContext::record_iterator
|
|
|
|
I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
|
|
|
|
// All the declarations in EC have been canonicalized, so pointer
|
|
|
|
// equality from this point on will work fine.
|
|
|
|
const CXXRecordDecl *ECRecord = *I;
|
|
|
|
|
|
|
|
// [B2] and [M2]
|
2010-04-07 05:38:20 +08:00
|
|
|
if (Access == AS_private) {
|
|
|
|
if (ECRecord == NamingClass)
|
|
|
|
return AR_accessible;
|
2010-04-02 08:03:43 +08:00
|
|
|
|
|
|
|
// [B3] and [M3]
|
2010-04-07 05:38:20 +08:00
|
|
|
} else {
|
|
|
|
assert(Access == AS_protected);
|
|
|
|
switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
|
|
|
|
case AR_accessible: break;
|
|
|
|
case AR_inaccessible: continue;
|
|
|
|
case AR_dependent: OnFailure = AR_dependent; continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Target.hasInstanceContext())
|
|
|
|
return AR_accessible;
|
|
|
|
|
|
|
|
const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
|
|
|
|
if (!InstanceContext) {
|
|
|
|
OnFailure = AR_dependent;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [class.protected]p1:
|
|
|
|
// An additional access check beyond those described earlier in
|
|
|
|
// [class.access] is applied when a non-static data member or
|
|
|
|
// non-static member function is a protected member of its naming
|
|
|
|
// class. As described earlier, access to a protected member is
|
|
|
|
// granted because the reference occurs in a friend or member of
|
|
|
|
// some class C. If the access is to form a pointer to member,
|
|
|
|
// the nested-name-specifier shall name C or a class derived from
|
|
|
|
// C. All other accesses involve a (possibly implicit) object
|
|
|
|
// expression. In this case, the class of the object expression
|
|
|
|
// shall be C or a class derived from C.
|
|
|
|
//
|
|
|
|
// We interpret this as a restriction on [M3]. Most of the
|
|
|
|
// conditions are encoded by not having any instance context.
|
|
|
|
switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
|
|
|
|
case AR_accessible: return AR_accessible;
|
|
|
|
case AR_inaccessible: continue;
|
|
|
|
case AR_dependent: OnFailure = AR_dependent; continue;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 08:03:43 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
if (!NamingClass->hasFriends())
|
|
|
|
return OnFailure;
|
|
|
|
|
|
|
|
// Don't consider friends if we're under the [class.protected]
|
|
|
|
// restriction, above.
|
|
|
|
if (Access == AS_protected && Target.hasInstanceContext()) {
|
|
|
|
const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
|
|
|
|
if (!InstanceContext) return AR_dependent;
|
|
|
|
|
|
|
|
switch (IsDerivedFromInclusive(InstanceContext, NamingClass)) {
|
|
|
|
case AR_accessible: break;
|
|
|
|
case AR_inaccessible: return OnFailure;
|
|
|
|
case AR_dependent: return AR_dependent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (GetFriendKind(S, EC, NamingClass)) {
|
|
|
|
case AR_accessible: return AR_accessible;
|
|
|
|
case AR_inaccessible: return OnFailure;
|
|
|
|
case AR_dependent: return AR_dependent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silence bogus warnings
|
|
|
|
llvm_unreachable("impossible friendship kind");
|
|
|
|
return OnFailure;
|
2010-04-02 08:03:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
/// Finds the best path from the naming class to the declaring class,
|
|
|
|
/// taking friend declarations into account.
|
|
|
|
///
|
2010-04-02 08:03:43 +08:00
|
|
|
/// C++0x [class.access.base]p5:
|
|
|
|
/// A member m is accessible at the point R when named in class N if
|
|
|
|
/// [M1] m as a member of N is public, or
|
|
|
|
/// [M2] m as a member of N is private, and R occurs in a member or
|
|
|
|
/// friend of class N, or
|
|
|
|
/// [M3] m as a member of N is protected, and R 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 public, private,
|
|
|
|
/// or protected, or
|
|
|
|
/// [M4] there exists a base class B of N that is accessible at R, and
|
|
|
|
/// m is accessible at R when named in class B.
|
|
|
|
///
|
|
|
|
/// C++0x [class.access.base]p4:
|
|
|
|
/// A base class B of N is accessible at R, if
|
|
|
|
/// [B1] an invented public member of B would be a public member of N, or
|
|
|
|
/// [B2] R occurs in a member or friend of class N, and an invented public
|
|
|
|
/// member of B would be a private or protected member of N, or
|
|
|
|
/// [B3] R occurs in a member or friend of a class P derived from N, and an
|
|
|
|
/// invented public member of B would be a private or protected member
|
|
|
|
/// of P, or
|
|
|
|
/// [B4] there exists a class S such that B is a base class of S accessible
|
|
|
|
/// at R and S is a base class of N accessible at R.
|
|
|
|
///
|
|
|
|
/// Along a single inheritance path we can restate both of these
|
|
|
|
/// iteratively:
|
|
|
|
///
|
|
|
|
/// First, we note that M1-4 are equivalent to B1-4 if the member is
|
|
|
|
/// treated as a notional base of its declaring class with inheritance
|
|
|
|
/// access equivalent to the member's access. Therefore we need only
|
|
|
|
/// ask whether a class B is accessible from a class N in context R.
|
|
|
|
///
|
|
|
|
/// Let B_1 .. B_n be the inheritance path in question (i.e. where
|
|
|
|
/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
|
|
|
|
/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
|
|
|
|
/// closest accessible base in the path:
|
|
|
|
/// Access(a, b) = (* access on the base specifier from a to b *)
|
|
|
|
/// Merge(a, forbidden) = forbidden
|
|
|
|
/// Merge(a, private) = forbidden
|
|
|
|
/// Merge(a, b) = min(a,b)
|
|
|
|
/// Accessible(c, forbidden) = false
|
|
|
|
/// Accessible(c, private) = (R is c) || IsFriend(c, R)
|
|
|
|
/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
|
|
|
|
/// Accessible(c, public) = true
|
|
|
|
/// ACAB(n) = public
|
|
|
|
/// ACAB(i) =
|
|
|
|
/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
|
|
|
|
/// if Accessible(B_i, AccessToBase) then public else AccessToBase
|
|
|
|
///
|
|
|
|
/// B is an accessible base of N at R iff ACAB(1) = public.
|
|
|
|
///
|
2010-04-07 05:38:20 +08:00
|
|
|
/// \param FinalAccess the access of the "final step", or AS_public if
|
2010-03-19 07:49:19 +08:00
|
|
|
/// there is no final step.
|
2010-02-10 17:31:12 +08:00
|
|
|
/// \return null if friendship is dependent
|
|
|
|
static CXXBasePath *FindBestPath(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget &Target,
|
2010-03-19 07:49:19 +08:00
|
|
|
AccessSpecifier FinalAccess,
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXBasePaths &Paths) {
|
|
|
|
// Derive the paths to the desired base.
|
2010-04-07 05:38:20 +08:00
|
|
|
const CXXRecordDecl *Derived = Target.getNamingClass();
|
|
|
|
const CXXRecordDecl *Base = Target.getDeclaringClass();
|
|
|
|
|
|
|
|
// FIXME: fail correctly when there are dependent paths.
|
|
|
|
bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
|
|
|
|
Paths);
|
2010-02-10 17:31:12 +08:00
|
|
|
assert(isDerived && "derived class not actually derived from base");
|
|
|
|
(void) isDerived;
|
|
|
|
|
|
|
|
CXXBasePath *BestPath = 0;
|
|
|
|
|
2010-03-19 07:49:19 +08:00
|
|
|
assert(FinalAccess != AS_none && "forbidden access after declaring class");
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
bool AnyDependent = false;
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
// Derive the friend-modified access along each path.
|
|
|
|
for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
|
|
|
|
PI != PE; ++PI) {
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
|
2010-02-10 17:31:12 +08:00
|
|
|
|
|
|
|
// Walk through the path backwards.
|
2010-03-19 07:49:19 +08:00
|
|
|
AccessSpecifier PathAccess = FinalAccess;
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXBasePath::iterator I = PI->end(), E = PI->begin();
|
|
|
|
while (I != E) {
|
|
|
|
--I;
|
|
|
|
|
2010-03-19 07:49:19 +08:00
|
|
|
assert(PathAccess != AS_none);
|
|
|
|
|
|
|
|
// If the declaration is a private member of a base class, there
|
|
|
|
// is no level of friendship in derived classes that can make it
|
|
|
|
// accessible.
|
|
|
|
if (PathAccess == AS_private) {
|
|
|
|
PathAccess = AS_none;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
|
2010-04-02 08:03:43 +08:00
|
|
|
PathAccess = std::max(PathAccess, BaseAccess);
|
2010-04-07 05:38:20 +08:00
|
|
|
|
|
|
|
switch (HasAccess(S, EC, NC, PathAccess, Target)) {
|
|
|
|
case AR_inaccessible: break;
|
|
|
|
case AR_accessible:
|
|
|
|
PathAccess = AS_public;
|
|
|
|
|
|
|
|
// Future tests are not against members and so do not have
|
|
|
|
// instance context.
|
|
|
|
Target.suppressInstanceContext();
|
|
|
|
break;
|
|
|
|
case AR_dependent:
|
2010-04-02 08:03:43 +08:00
|
|
|
AnyDependent = true;
|
|
|
|
goto Next;
|
2009-03-27 14:03:27 +08:00
|
|
|
}
|
|
|
|
}
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
// Note that we modify the path's Access field to the
|
|
|
|
// friend-modified access.
|
|
|
|
if (BestPath == 0 || PathAccess < BestPath->Access) {
|
|
|
|
BestPath = &*PI;
|
|
|
|
BestPath->Access = PathAccess;
|
2010-03-24 13:22:00 +08:00
|
|
|
|
|
|
|
// Short-circuit if we found a public path.
|
|
|
|
if (BestPath->Access == AS_public)
|
|
|
|
return BestPath;
|
2009-03-27 14:03:27 +08:00
|
|
|
}
|
2010-03-24 13:22:00 +08:00
|
|
|
|
|
|
|
Next: ;
|
2009-03-27 14:03:27 +08:00
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
assert((!BestPath || BestPath->Access != AS_public) &&
|
|
|
|
"fell out of loop with public path");
|
|
|
|
|
|
|
|
// We didn't find a public path, but at least one path was subject
|
|
|
|
// to dependent friendship, so delay the check.
|
|
|
|
if (AnyDependent)
|
|
|
|
return 0;
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
return BestPath;
|
2009-07-18 22:32:15 +08:00
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
/// Diagnose the path which caused the given declaration or base class
|
|
|
|
/// to become inaccessible.
|
|
|
|
static void DiagnoseAccessPath(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget &Entity) {
|
2010-04-02 08:03:43 +08:00
|
|
|
AccessSpecifier Access = Entity.getAccess();
|
2010-04-07 05:38:20 +08:00
|
|
|
const CXXRecordDecl *NamingClass = Entity.getNamingClass();
|
2010-04-02 08:03:43 +08:00
|
|
|
NamingClass = NamingClass->getCanonicalDecl();
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
|
|
|
|
const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
|
2010-04-02 08:03:43 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
// Easy case: the decl's natural access determined its path access.
|
|
|
|
// We have to check against AS_private here in case Access is AS_none,
|
|
|
|
// indicating a non-public member of a private base class.
|
|
|
|
if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
|
2010-04-07 05:38:20 +08:00
|
|
|
switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
|
|
|
|
case AR_inaccessible: {
|
2010-02-10 17:31:12 +08:00
|
|
|
S.Diag(D->getLocation(), diag::note_access_natural)
|
|
|
|
<< (unsigned) (Access == AS_protected)
|
|
|
|
<< /*FIXME: not implicitly*/ 0;
|
|
|
|
return;
|
|
|
|
}
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_accessible: break;
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_dependent:
|
|
|
|
llvm_unreachable("can't diagnose dependent access failures");
|
2010-02-10 17:31:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-03-27 14:03:27 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXBasePaths Paths;
|
2010-04-07 05:38:20 +08:00
|
|
|
CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXBasePath::iterator I = Path.end(), E = Path.begin();
|
|
|
|
while (I != E) {
|
|
|
|
--I;
|
2009-03-27 14:03:27 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
const CXXBaseSpecifier *BS = I->Base;
|
|
|
|
AccessSpecifier BaseAccess = BS->getAccessSpecifier();
|
|
|
|
|
|
|
|
// If this is public inheritance, or the derived class is a friend,
|
|
|
|
// skip this step.
|
|
|
|
if (BaseAccess == AS_public)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (GetFriendKind(S, EC, I->Class)) {
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_accessible: continue;
|
|
|
|
case AR_inaccessible: break;
|
|
|
|
case AR_dependent:
|
|
|
|
llvm_unreachable("can't diagnose dependent access failures");
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether this base specifier is the tighest point
|
|
|
|
// constraining access. We have to check against AS_private for
|
|
|
|
// the same reasons as above.
|
|
|
|
if (BaseAccess == AS_private || BaseAccess >= Access) {
|
|
|
|
|
|
|
|
// We're constrained by inheritance, but we want to say
|
|
|
|
// "declared private here" if we're diagnosing a hierarchy
|
|
|
|
// conversion and this is the final step.
|
|
|
|
unsigned diagnostic;
|
|
|
|
if (D) diagnostic = diag::note_access_constrained_by_path;
|
|
|
|
else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
|
|
|
|
else diagnostic = diag::note_access_constrained_by_path;
|
|
|
|
|
|
|
|
S.Diag(BS->getSourceRange().getBegin(), diagnostic)
|
|
|
|
<< BS->getSourceRange()
|
|
|
|
<< (BaseAccess == AS_protected)
|
|
|
|
<< (BS->getAccessSpecifierAsWritten() == AS_none);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-27 14:03:27 +08:00
|
|
|
}
|
2009-07-18 22:32:15 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
llvm_unreachable("access not apparently constrained by path");
|
2009-03-27 13:05:05 +08:00
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
|
|
|
|
const EffectiveContext &EC,
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget &Entity) {
|
2010-04-02 08:03:43 +08:00
|
|
|
const CXXRecordDecl *NamingClass = Entity.getNamingClass();
|
2010-04-07 05:38:20 +08:00
|
|
|
const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
|
|
|
|
NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
|
2010-02-10 17:31:12 +08:00
|
|
|
|
2010-03-16 13:22:47 +08:00
|
|
|
S.Diag(Loc, Entity.getDiag())
|
2010-04-02 08:03:43 +08:00
|
|
|
<< (Entity.getAccess() == AS_protected)
|
|
|
|
<< (D ? D->getDeclName() : DeclarationName())
|
2010-03-16 13:22:47 +08:00
|
|
|
<< S.Context.getTypeDeclType(NamingClass)
|
|
|
|
<< S.Context.getTypeDeclType(DeclaringClass);
|
2010-04-02 08:03:43 +08:00
|
|
|
DiagnoseAccessPath(S, EC, Entity);
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
/// Determines whether the accessed entity is accessible. Public members
|
|
|
|
/// have been weeded out by this point.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult IsAccessible(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
AccessTarget &Entity) {
|
2010-04-02 08:03:43 +08:00
|
|
|
// Determine the actual naming class.
|
|
|
|
CXXRecordDecl *NamingClass = Entity.getNamingClass();
|
|
|
|
while (NamingClass->isAnonymousStructOrUnion())
|
|
|
|
NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
|
|
|
|
NamingClass = NamingClass->getCanonicalDecl();
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
AccessSpecifier UnprivilegedAccess = Entity.getAccess();
|
|
|
|
assert(UnprivilegedAccess != AS_public && "public access not weeded out");
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
// Before we try to recalculate access paths, try to white-list
|
|
|
|
// accesses which just trade in on the final step, i.e. accesses
|
|
|
|
// which don't require [M4] or [B4]. These are by far the most
|
2010-04-07 05:38:20 +08:00
|
|
|
// common forms of privileged access.
|
2010-04-02 08:03:43 +08:00
|
|
|
if (UnprivilegedAccess != AS_none) {
|
2010-04-07 05:38:20 +08:00
|
|
|
switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
|
|
|
|
case AR_dependent:
|
2010-04-02 08:03:43 +08:00
|
|
|
// This is actually an interesting policy decision. We don't
|
|
|
|
// *have* to delay immediately here: we can do the full access
|
|
|
|
// calculation in the hope that friendship on some intermediate
|
|
|
|
// class will make the declaration accessible non-dependently.
|
|
|
|
// But that's not cheap, and odds are very good (note: assertion
|
|
|
|
// made without data) that the friend declaration will determine
|
|
|
|
// access.
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_dependent;
|
2010-04-02 08:03:43 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_accessible: return AR_accessible;
|
|
|
|
case AR_inaccessible: break;
|
2010-04-02 08:03:43 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
|
2010-04-02 08:03:43 +08:00
|
|
|
|
|
|
|
// We lower member accesses to base accesses by pretending that the
|
|
|
|
// member is a base class of its declaring class.
|
|
|
|
AccessSpecifier FinalAccess;
|
2010-02-10 17:31:12 +08:00
|
|
|
|
|
|
|
if (Entity.isMemberAccess()) {
|
2010-04-02 08:03:43 +08:00
|
|
|
// Determine if the declaration is accessible from EC when named
|
|
|
|
// in its declaring class.
|
2010-02-10 17:31:12 +08:00
|
|
|
NamedDecl *Target = Entity.getTargetDecl();
|
2010-04-07 05:38:20 +08:00
|
|
|
const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
|
2010-02-10 17:31:12 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
FinalAccess = Target->getAccess();
|
2010-04-07 05:38:20 +08:00
|
|
|
switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
|
|
|
|
case AR_accessible:
|
|
|
|
FinalAccess = AS_public;
|
|
|
|
break;
|
|
|
|
case AR_inaccessible: break;
|
|
|
|
case AR_dependent: return AR_dependent; // see above
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2010-01-27 11:50:35 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
if (DeclaringClass == NamingClass)
|
2010-04-07 05:38:20 +08:00
|
|
|
return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
|
|
|
|
|
|
|
|
Entity.suppressInstanceContext();
|
2010-04-02 08:03:43 +08:00
|
|
|
} else {
|
|
|
|
FinalAccess = AS_public;
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2010-01-27 11:50:35 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
assert(Entity.getDeclaringClass() != NamingClass);
|
2010-02-10 17:31:12 +08:00
|
|
|
|
|
|
|
// Append the declaration's access if applicable.
|
|
|
|
CXXBasePaths Paths;
|
2010-04-07 05:38:20 +08:00
|
|
|
CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
|
2010-03-24 13:22:00 +08:00
|
|
|
if (!Path)
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_dependent;
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
assert(Path->Access <= UnprivilegedAccess &&
|
|
|
|
"access along best path worse than direct?");
|
|
|
|
if (Path->Access == AS_public)
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_accessible;
|
|
|
|
return AR_inaccessible;
|
2010-03-24 13:22:00 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
static void DelayDependentAccess(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
SourceLocation Loc,
|
|
|
|
const AccessTarget &Entity) {
|
2010-03-24 13:22:00 +08:00
|
|
|
assert(EC.isDependent() && "delaying non-dependent access");
|
2010-03-24 15:46:06 +08:00
|
|
|
DeclContext *DC = EC.getInnerContext();
|
2010-03-24 13:22:00 +08:00
|
|
|
assert(DC->isDependentContext() && "delaying non-dependent access");
|
|
|
|
DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
|
|
|
|
Loc,
|
|
|
|
Entity.isMemberAccess(),
|
|
|
|
Entity.getAccess(),
|
|
|
|
Entity.getTargetDecl(),
|
|
|
|
Entity.getNamingClass(),
|
2010-04-07 05:38:20 +08:00
|
|
|
Entity.getBaseObjectType(),
|
2010-03-24 13:22:00 +08:00
|
|
|
Entity.getDiag());
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks access to an entity from the given effective context.
|
2010-04-07 05:38:20 +08:00
|
|
|
static AccessResult CheckEffectiveAccess(Sema &S,
|
|
|
|
const EffectiveContext &EC,
|
|
|
|
SourceLocation Loc,
|
|
|
|
AccessTarget &Entity) {
|
2010-04-02 08:03:43 +08:00
|
|
|
assert(Entity.getAccess() != AS_public && "called for public access!");
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-02 08:03:43 +08:00
|
|
|
switch (IsAccessible(S, EC, Entity)) {
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_dependent:
|
|
|
|
DelayDependentAccess(S, EC, Loc, Entity);
|
|
|
|
return AR_dependent;
|
2010-03-24 13:22:00 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_inaccessible:
|
2010-04-02 08:03:43 +08:00
|
|
|
if (!Entity.isQuiet())
|
|
|
|
DiagnoseBadAccess(S, Loc, EC, Entity);
|
2010-04-07 05:38:20 +08:00
|
|
|
return AR_inaccessible;
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
case AR_accessible:
|
|
|
|
return AR_accessible;
|
2010-04-02 08:03:43 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
// silence unnecessary warning
|
|
|
|
llvm_unreachable("invalid access result");
|
|
|
|
return AR_accessible;
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget &Entity) {
|
2010-02-10 17:31:12 +08:00
|
|
|
// If the access path is public, it's accessible everywhere.
|
|
|
|
if (Entity.getAccess() == AS_public)
|
|
|
|
return Sema::AR_accessible;
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
// If we're currently parsing a top-level declaration, delay
|
|
|
|
// diagnostics. This is the only case where parsing a declaration
|
|
|
|
// can actually change our effective context for the purposes of
|
|
|
|
// access control.
|
|
|
|
if (S.CurContext->isFileContext() && S.ParsingDeclDepth) {
|
|
|
|
S.DelayedDiagnostics.push_back(
|
|
|
|
Sema::DelayedDiagnostic::makeAccess(Loc, Entity));
|
|
|
|
return Sema::AR_delayed;
|
2010-01-23 08:46:32 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
EffectiveContext EC(S.CurContext);
|
|
|
|
switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
|
|
|
|
case AR_accessible: return Sema::AR_accessible;
|
|
|
|
case AR_inaccessible: return Sema::AR_inaccessible;
|
|
|
|
case AR_dependent: return Sema::AR_dependent;
|
|
|
|
}
|
|
|
|
llvm_unreachable("falling off end");
|
|
|
|
return Sema::AR_accessible;
|
2010-01-23 08:46:32 +08:00
|
|
|
}
|
|
|
|
|
2010-01-27 11:50:35 +08:00
|
|
|
void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) {
|
|
|
|
// Pretend we did this from the context of the newly-parsed
|
|
|
|
// declaration.
|
2010-02-10 17:31:12 +08:00
|
|
|
EffectiveContext EC(Ctx->getDeclContext());
|
2010-01-27 11:50:35 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Target(DD.getAccessData());
|
|
|
|
|
|
|
|
if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
|
2010-01-27 11:50:35 +08:00
|
|
|
DD.Triggered = true;
|
|
|
|
}
|
|
|
|
|
2010-03-24 13:22:00 +08:00
|
|
|
void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
|
|
|
SourceLocation Loc = DD.getAccessLoc();
|
|
|
|
AccessSpecifier Access = DD.getAccess();
|
|
|
|
|
|
|
|
Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!NamingD) return;
|
|
|
|
Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!TargetD) return;
|
|
|
|
|
|
|
|
if (DD.isAccessToMember()) {
|
2010-04-07 05:38:20 +08:00
|
|
|
CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
|
|
|
|
NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
|
|
|
|
QualType BaseObjectType = DD.getAccessBaseObjectType();
|
|
|
|
if (!BaseObjectType.isNull()) {
|
|
|
|
BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
|
|
|
|
DeclarationName());
|
|
|
|
if (BaseObjectType.isNull()) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessTarget Entity(Context,
|
|
|
|
AccessTarget::Member,
|
|
|
|
NamingClass,
|
|
|
|
DeclAccessPair::make(TargetDecl, Access),
|
|
|
|
BaseObjectType);
|
2010-03-24 13:22:00 +08:00
|
|
|
Entity.setDiag(DD.getDiagnostic());
|
|
|
|
CheckAccess(*this, Loc, Entity);
|
|
|
|
} else {
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context,
|
|
|
|
AccessTarget::Base,
|
|
|
|
cast<CXXRecordDecl>(TargetD),
|
|
|
|
cast<CXXRecordDecl>(NamingD),
|
|
|
|
Access);
|
2010-03-24 13:22:00 +08:00
|
|
|
Entity.setDiag(DD.getDiagnostic());
|
|
|
|
CheckAccess(*this, Loc, Entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
|
2010-03-19 15:35:19 +08:00
|
|
|
DeclAccessPair Found) {
|
2010-03-16 13:22:47 +08:00
|
|
|
if (!getLangOptions().AccessControl ||
|
|
|
|
!E->getNamingClass() ||
|
2010-03-19 15:35:19 +08:00
|
|
|
Found.getAccess() == AS_public)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-01-27 09:50:18 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
|
|
|
|
Found, QualType());
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(diag::err_access) << E->getSourceRange();
|
|
|
|
|
|
|
|
return CheckAccess(*this, E->getNameLoc(), Entity);
|
2010-01-27 09:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform access-control checking on a previously-unresolved member
|
|
|
|
/// access which has now been resolved to a member.
|
2010-02-10 17:31:12 +08:00
|
|
|
Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
|
2010-03-19 15:35:19 +08:00
|
|
|
DeclAccessPair Found) {
|
2010-03-16 13:22:47 +08:00
|
|
|
if (!getLangOptions().AccessControl ||
|
2010-03-19 15:35:19 +08:00
|
|
|
Found.getAccess() == AS_public)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-01-27 09:50:18 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
QualType BaseType = E->getBaseType();
|
|
|
|
if (E->isArrow())
|
|
|
|
BaseType = BaseType->getAs<PointerType>()->getPointeeType();
|
|
|
|
|
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
|
|
|
|
Found, BaseType);
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(diag::err_access) << E->getSourceRange();
|
|
|
|
|
|
|
|
return CheckAccess(*this, E->getMemberLoc(), Entity);
|
2010-01-27 09:50:18 +08:00
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
|
2010-03-16 13:22:47 +08:00
|
|
|
CXXDestructorDecl *Dtor,
|
|
|
|
const PartialDiagnostic &PDiag) {
|
2010-02-02 16:45:54 +08:00
|
|
|
if (!getLangOptions().AccessControl)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-02-02 16:45:54 +08:00
|
|
|
|
2010-03-16 13:22:47 +08:00
|
|
|
// There's never a path involved when checking implicit destructor access.
|
2010-02-02 16:45:54 +08:00
|
|
|
AccessSpecifier Access = Dtor->getAccess();
|
|
|
|
if (Access == AS_public)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-02-02 16:45:54 +08:00
|
|
|
|
2010-03-16 13:22:47 +08:00
|
|
|
CXXRecordDecl *NamingClass = Dtor->getParent();
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
|
|
|
|
DeclAccessPair::make(Dtor, Access),
|
|
|
|
QualType());
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(PDiag); // TODO: avoid copy
|
|
|
|
|
|
|
|
return CheckAccess(*this, Loc, Entity);
|
2010-02-02 16:45:54 +08:00
|
|
|
}
|
|
|
|
|
2010-02-01 11:16:54 +08:00
|
|
|
/// Checks access to a constructor.
|
2010-02-10 17:31:12 +08:00
|
|
|
Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
|
2010-02-01 11:16:54 +08:00
|
|
|
CXXConstructorDecl *Constructor,
|
|
|
|
AccessSpecifier Access) {
|
2010-03-16 13:22:47 +08:00
|
|
|
if (!getLangOptions().AccessControl ||
|
|
|
|
Access == AS_public)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-02-01 11:16:54 +08:00
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXRecordDecl *NamingClass = Constructor->getParent();
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
|
|
|
|
DeclAccessPair::make(Constructor, Access),
|
|
|
|
QualType());
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(diag::err_access_ctor);
|
|
|
|
|
|
|
|
return CheckAccess(*this, UseLoc, Entity);
|
2010-02-01 11:16:54 +08:00
|
|
|
}
|
|
|
|
|
2010-03-16 14:11:48 +08:00
|
|
|
/// Checks direct (i.e. non-inherited) access to an arbitrary class
|
|
|
|
/// member.
|
|
|
|
Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
|
|
|
|
NamedDecl *Target,
|
|
|
|
const PartialDiagnostic &Diag) {
|
|
|
|
AccessSpecifier Access = Target->getAccess();
|
|
|
|
if (!getLangOptions().AccessControl ||
|
|
|
|
Access == AS_public)
|
|
|
|
return AR_accessible;
|
|
|
|
|
|
|
|
CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
|
|
|
|
DeclAccessPair::make(Target, Access),
|
|
|
|
QualType());
|
2010-03-16 14:11:48 +08:00
|
|
|
Entity.setDiag(Diag);
|
|
|
|
return CheckAccess(*this, UseLoc, Entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-18 16:19:33 +08:00
|
|
|
/// Checks access to an overloaded operator new or delete.
|
|
|
|
Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
|
|
|
|
SourceRange PlacementRange,
|
|
|
|
CXXRecordDecl *NamingClass,
|
2010-03-19 15:35:19 +08:00
|
|
|
DeclAccessPair Found) {
|
2010-03-18 16:19:33 +08:00
|
|
|
if (!getLangOptions().AccessControl ||
|
|
|
|
!NamingClass ||
|
2010-03-19 15:35:19 +08:00
|
|
|
Found.getAccess() == AS_public)
|
2010-03-18 16:19:33 +08:00
|
|
|
return AR_accessible;
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
|
|
|
|
QualType());
|
2010-03-18 16:19:33 +08:00
|
|
|
Entity.setDiag(diag::err_access)
|
|
|
|
<< PlacementRange;
|
|
|
|
|
|
|
|
return CheckAccess(*this, OpLoc, Entity);
|
|
|
|
}
|
|
|
|
|
2010-02-01 11:16:54 +08:00
|
|
|
/// Checks access to an overloaded member operator, including
|
|
|
|
/// conversion operators.
|
2010-02-10 17:31:12 +08:00
|
|
|
Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
|
|
|
|
Expr *ObjectExpr,
|
2010-03-16 13:22:47 +08:00
|
|
|
Expr *ArgExpr,
|
2010-03-19 15:35:19 +08:00
|
|
|
DeclAccessPair Found) {
|
2010-03-16 13:22:47 +08:00
|
|
|
if (!getLangOptions().AccessControl ||
|
2010-03-19 15:35:19 +08:00
|
|
|
Found.getAccess() == AS_public)
|
2010-02-10 17:31:12 +08:00
|
|
|
return AR_accessible;
|
2010-01-28 09:42:12 +08:00
|
|
|
|
|
|
|
const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>();
|
|
|
|
assert(RT && "found member operator but object expr not of record type");
|
|
|
|
CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
|
|
|
|
ObjectExpr->getType());
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(diag::err_access)
|
|
|
|
<< ObjectExpr->getSourceRange()
|
|
|
|
<< (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
|
|
|
|
|
|
|
|
return CheckAccess(*this, OpLoc, Entity);
|
2010-02-10 17:31:12 +08:00
|
|
|
}
|
2010-01-28 09:42:12 +08:00
|
|
|
|
2010-03-31 05:47:33 +08:00
|
|
|
Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
|
|
|
|
DeclAccessPair Found) {
|
|
|
|
if (!getLangOptions().AccessControl ||
|
2010-03-31 06:20:00 +08:00
|
|
|
Found.getAccess() == AS_none ||
|
2010-03-31 05:47:33 +08:00
|
|
|
Found.getAccess() == AS_public)
|
|
|
|
return AR_accessible;
|
|
|
|
|
|
|
|
OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).getPointer();
|
|
|
|
NestedNameSpecifier *Qualifier = Ovl->getQualifier();
|
|
|
|
assert(Qualifier && "address of overloaded member without qualifier");
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.setScopeRep(Qualifier);
|
|
|
|
SS.setRange(Ovl->getQualifierRange());
|
|
|
|
DeclContext *DC = computeDeclContext(SS);
|
|
|
|
assert(DC && DC->isRecord() && "scope did not resolve to record");
|
|
|
|
CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(DC);
|
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
|
|
|
|
Context.getTypeDeclType(NamingClass));
|
2010-03-31 05:47:33 +08:00
|
|
|
Entity.setDiag(diag::err_access)
|
|
|
|
<< Ovl->getSourceRange();
|
|
|
|
|
|
|
|
return CheckAccess(*this, Ovl->getNameLoc(), Entity);
|
|
|
|
}
|
|
|
|
|
2010-02-10 17:31:12 +08:00
|
|
|
/// Checks access for a hierarchy conversion.
|
|
|
|
///
|
|
|
|
/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
|
|
|
|
/// or a derived-to-base conversion (false)
|
|
|
|
/// \param ForceCheck true if this check should be performed even if access
|
|
|
|
/// control is disabled; some things rely on this for semantics
|
|
|
|
/// \param ForceUnprivileged true if this check should proceed as if the
|
|
|
|
/// context had no special privileges
|
|
|
|
/// \param ADK controls the kind of diagnostics that are used
|
|
|
|
Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
|
|
|
|
QualType Base,
|
|
|
|
QualType Derived,
|
|
|
|
const CXXBasePath &Path,
|
2010-03-16 13:22:47 +08:00
|
|
|
unsigned DiagID,
|
2010-02-10 17:31:12 +08:00
|
|
|
bool ForceCheck,
|
2010-03-16 13:22:47 +08:00
|
|
|
bool ForceUnprivileged) {
|
2010-02-10 17:31:12 +08:00
|
|
|
if (!ForceCheck && !getLangOptions().AccessControl)
|
|
|
|
return AR_accessible;
|
|
|
|
|
|
|
|
if (Path.Access == AS_public)
|
|
|
|
return AR_accessible;
|
|
|
|
|
|
|
|
CXXRecordDecl *BaseD, *DerivedD;
|
|
|
|
BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
|
|
|
|
DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
|
2010-03-16 13:22:47 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
|
|
|
|
Path.Access);
|
2010-03-16 13:22:47 +08:00
|
|
|
if (DiagID)
|
|
|
|
Entity.setDiag(DiagID) << Derived << Base;
|
2010-02-10 17:31:12 +08:00
|
|
|
|
2010-04-07 05:38:20 +08:00
|
|
|
if (ForceUnprivileged) {
|
|
|
|
switch (CheckEffectiveAccess(*this, EffectiveContext(),
|
|
|
|
AccessLoc, Entity)) {
|
|
|
|
case ::AR_accessible: return Sema::AR_accessible;
|
|
|
|
case ::AR_inaccessible: return Sema::AR_inaccessible;
|
|
|
|
case ::AR_dependent: return Sema::AR_dependent;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unexpected result from CheckEffectiveAccess");
|
|
|
|
}
|
2010-03-16 13:22:47 +08:00
|
|
|
return CheckAccess(*this, AccessLoc, Entity);
|
2010-01-28 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
2010-01-23 08:46:32 +08:00
|
|
|
/// Checks access to all the declarations in the given result set.
|
2010-02-10 17:31:12 +08:00
|
|
|
void Sema::CheckLookupAccess(const LookupResult &R) {
|
|
|
|
assert(getLangOptions().AccessControl
|
|
|
|
&& "performing access check without access control");
|
|
|
|
assert(R.getNamingClass() && "performing access check without naming class");
|
|
|
|
|
2010-03-16 13:22:47 +08:00
|
|
|
for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
|
|
|
|
if (I.getAccess() != AS_public) {
|
2010-04-07 05:38:20 +08:00
|
|
|
AccessTarget Entity(Context, AccessedEntity::Member,
|
|
|
|
R.getNamingClass(), I.getPair(),
|
|
|
|
R.getBaseObjectType());
|
2010-03-16 13:22:47 +08:00
|
|
|
Entity.setDiag(diag::err_access);
|
|
|
|
|
|
|
|
CheckAccess(*this, R.getNameLoc(), Entity);
|
|
|
|
}
|
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
}
|