Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 1 | //===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===// |
Anders Carlsson | 60d6b0d | 2009-03-27 04:43:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ access control semantics. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include "Sema.h" |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
| 19 | #include "clang/AST/DeclCXX.h" |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclFriend.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/DependentDiagnostic.h" |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
| 23 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 26 | /// A copy of Sema's enum without AR_delayed. |
| 27 | enum AccessResult { |
| 28 | AR_accessible, |
| 29 | AR_inaccessible, |
| 30 | AR_dependent |
| 31 | }; |
| 32 | |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 33 | /// SetMemberAccessSpecifier - Set the access specifier of a member. |
| 34 | /// Returns true on error (when the previous member decl access specifier |
| 35 | /// is different from the new member decl access specifier). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl, |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 37 | NamedDecl *PrevMemberDecl, |
| 38 | AccessSpecifier LexicalAS) { |
| 39 | if (!PrevMemberDecl) { |
| 40 | // Use the lexical access specifier. |
| 41 | MemberDecl->setAccess(LexicalAS); |
| 42 | return false; |
| 43 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 45 | // C++ [class.access.spec]p3: When a member is redeclared its access |
| 46 | // specifier must be same as its initial declaration. |
| 47 | if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | Diag(MemberDecl->getLocation(), |
| 49 | diag::err_class_redeclared_with_different_access) |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 50 | << MemberDecl << LexicalAS; |
| 51 | Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration) |
| 52 | << PrevMemberDecl << PrevMemberDecl->getAccess(); |
John McCall | 44e067b | 2009-12-23 00:37:40 +0000 | [diff] [blame] | 53 | |
| 54 | MemberDecl->setAccess(LexicalAS); |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 58 | MemberDecl->setAccess(PrevMemberDecl->getAccess()); |
| 59 | return false; |
| 60 | } |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 61 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 62 | static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) { |
| 63 | DeclContext *DC = D->getDeclContext(); |
| 64 | |
| 65 | // This can only happen at top: enum decls only "publish" their |
| 66 | // immediate members. |
| 67 | if (isa<EnumDecl>(DC)) |
| 68 | DC = cast<EnumDecl>(DC)->getDeclContext(); |
| 69 | |
| 70 | CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(DC); |
| 71 | while (DeclaringClass->isAnonymousStructOrUnion()) |
| 72 | DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext()); |
| 73 | return DeclaringClass; |
| 74 | } |
| 75 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | struct EffectiveContext { |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 78 | EffectiveContext() : Inner(0), Dependent(false) {} |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 79 | |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 80 | explicit EffectiveContext(DeclContext *DC) |
| 81 | : Inner(DC), |
| 82 | Dependent(DC->isDependentContext()) { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 83 | |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 84 | // C++ [class.access.nest]p1: |
| 85 | // A nested class is a member and as such has the same access |
| 86 | // rights as any other member. |
| 87 | // C++ [class.access]p2: |
| 88 | // A member of a class can also access all the names to which |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 89 | // the class has access. A local class of a member function |
| 90 | // may access the same names that the member function itself |
| 91 | // may access. |
| 92 | // This almost implies that the privileges of nesting are transitive. |
| 93 | // Technically it says nothing about the local classes of non-member |
| 94 | // functions (which can gain privileges through friendship), but we |
| 95 | // take that as an oversight. |
| 96 | while (true) { |
| 97 | if (isa<CXXRecordDecl>(DC)) { |
| 98 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC)->getCanonicalDecl(); |
| 99 | Records.push_back(Record); |
| 100 | DC = Record->getDeclContext(); |
| 101 | } else if (isa<FunctionDecl>(DC)) { |
| 102 | FunctionDecl *Function = cast<FunctionDecl>(DC)->getCanonicalDecl(); |
| 103 | Functions.push_back(Function); |
| 104 | DC = Function->getDeclContext(); |
| 105 | } else if (DC->isFileContext()) { |
| 106 | break; |
| 107 | } else { |
| 108 | DC = DC->getParent(); |
| 109 | } |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 110 | } |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 111 | } |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 112 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 113 | bool isDependent() const { return Dependent; } |
| 114 | |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 115 | bool includesClass(const CXXRecordDecl *R) const { |
| 116 | R = R->getCanonicalDecl(); |
| 117 | return std::find(Records.begin(), Records.end(), R) |
| 118 | != Records.end(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 121 | /// Retrieves the innermost "useful" context. Can be null if we're |
| 122 | /// doing access-control without privileges. |
| 123 | DeclContext *getInnerContext() const { |
| 124 | return Inner; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | typedef llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator; |
| 128 | |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 129 | DeclContext *Inner; |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 130 | llvm::SmallVector<FunctionDecl*, 4> Functions; |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 131 | llvm::SmallVector<CXXRecordDecl*, 4> Records; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 132 | bool Dependent; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 133 | }; |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 134 | |
| 135 | /// Like Sema's AccessedEntity, but kindly lets us scribble all over |
| 136 | /// it. |
| 137 | struct AccessTarget : public Sema::AccessedEntity { |
| 138 | AccessTarget(const Sema::AccessedEntity &Entity) |
| 139 | : AccessedEntity(Entity) { |
| 140 | initialize(); |
| 141 | } |
| 142 | |
| 143 | AccessTarget(ASTContext &Context, |
| 144 | MemberNonce _, |
| 145 | CXXRecordDecl *NamingClass, |
| 146 | DeclAccessPair FoundDecl, |
| 147 | QualType BaseObjectType) |
| 148 | : AccessedEntity(Context, Member, NamingClass, FoundDecl, BaseObjectType) { |
| 149 | initialize(); |
| 150 | } |
| 151 | |
| 152 | AccessTarget(ASTContext &Context, |
| 153 | BaseNonce _, |
| 154 | CXXRecordDecl *BaseClass, |
| 155 | CXXRecordDecl *DerivedClass, |
| 156 | AccessSpecifier Access) |
| 157 | : AccessedEntity(Context, Base, BaseClass, DerivedClass, Access) { |
| 158 | initialize(); |
| 159 | } |
| 160 | |
| 161 | bool hasInstanceContext() const { |
| 162 | return HasInstanceContext; |
| 163 | } |
| 164 | |
| 165 | class SavedInstanceContext { |
| 166 | public: |
| 167 | ~SavedInstanceContext() { |
| 168 | Target.HasInstanceContext = Has; |
| 169 | } |
| 170 | |
| 171 | private: |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 172 | friend struct AccessTarget; |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 173 | explicit SavedInstanceContext(AccessTarget &Target) |
| 174 | : Target(Target), Has(Target.HasInstanceContext) {} |
| 175 | AccessTarget &Target; |
| 176 | bool Has; |
| 177 | }; |
| 178 | |
| 179 | SavedInstanceContext saveInstanceContext() { |
| 180 | return SavedInstanceContext(*this); |
| 181 | } |
| 182 | |
| 183 | void suppressInstanceContext() { |
| 184 | HasInstanceContext = false; |
| 185 | } |
| 186 | |
| 187 | const CXXRecordDecl *resolveInstanceContext(Sema &S) const { |
| 188 | assert(HasInstanceContext); |
| 189 | if (CalculatedInstanceContext) |
| 190 | return InstanceContext; |
| 191 | |
| 192 | CalculatedInstanceContext = true; |
| 193 | DeclContext *IC = S.computeDeclContext(getBaseObjectType()); |
| 194 | InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0); |
| 195 | return InstanceContext; |
| 196 | } |
| 197 | |
| 198 | const CXXRecordDecl *getDeclaringClass() const { |
| 199 | return DeclaringClass; |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | void initialize() { |
| 204 | HasInstanceContext = (isMemberAccess() && |
| 205 | !getBaseObjectType().isNull() && |
| 206 | getTargetDecl()->isCXXInstanceMember()); |
| 207 | CalculatedInstanceContext = false; |
| 208 | InstanceContext = 0; |
| 209 | |
| 210 | if (isMemberAccess()) |
| 211 | DeclaringClass = FindDeclaringClass(getTargetDecl()); |
| 212 | else |
| 213 | DeclaringClass = getBaseClass(); |
| 214 | DeclaringClass = DeclaringClass->getCanonicalDecl(); |
| 215 | } |
| 216 | |
| 217 | bool HasInstanceContext : 1; |
| 218 | mutable bool CalculatedInstanceContext : 1; |
| 219 | mutable const CXXRecordDecl *InstanceContext; |
| 220 | const CXXRecordDecl *DeclaringClass; |
| 221 | }; |
| 222 | |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 223 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 224 | |
John McCall | 01ebd9d | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 225 | /// Checks whether one class might instantiate to the other. |
| 226 | static bool MightInstantiateTo(const CXXRecordDecl *From, |
| 227 | const CXXRecordDecl *To) { |
| 228 | // Declaration names are always preserved by instantiation. |
| 229 | if (From->getDeclName() != To->getDeclName()) |
| 230 | return false; |
| 231 | |
| 232 | const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext(); |
| 233 | const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext(); |
| 234 | if (FromDC == ToDC) return true; |
| 235 | if (FromDC->isFileContext() || ToDC->isFileContext()) return false; |
| 236 | |
| 237 | // Be conservative. |
| 238 | return true; |
| 239 | } |
| 240 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 241 | /// Checks whether one class is derived from another, inclusively. |
| 242 | /// Properly indicates when it couldn't be determined due to |
| 243 | /// dependence. |
| 244 | /// |
| 245 | /// This should probably be donated to AST or at least Sema. |
| 246 | static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived, |
| 247 | const CXXRecordDecl *Target) { |
| 248 | assert(Derived->getCanonicalDecl() == Derived); |
| 249 | assert(Target->getCanonicalDecl() == Target); |
John McCall | c1b621d | 2010-03-24 09:04:37 +0000 | [diff] [blame] | 250 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 251 | if (Derived == Target) return AR_accessible; |
John McCall | c1b621d | 2010-03-24 09:04:37 +0000 | [diff] [blame] | 252 | |
John McCall | 01ebd9d | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 253 | bool CheckDependent = Derived->isDependentContext(); |
| 254 | if (CheckDependent && MightInstantiateTo(Derived, Target)) |
| 255 | return AR_dependent; |
| 256 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 257 | AccessResult OnFailure = AR_inaccessible; |
| 258 | llvm::SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack |
| 259 | |
| 260 | while (true) { |
| 261 | for (CXXRecordDecl::base_class_const_iterator |
| 262 | I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) { |
| 263 | |
| 264 | const CXXRecordDecl *RD; |
| 265 | |
| 266 | QualType T = I->getType(); |
| 267 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 268 | RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 01ebd9d | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 269 | } else if (const InjectedClassNameType *IT |
| 270 | = T->getAs<InjectedClassNameType>()) { |
| 271 | RD = IT->getDecl(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 272 | } else { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 273 | assert(T->isDependentType() && "non-dependent base wasn't a record?"); |
| 274 | OnFailure = AR_dependent; |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | RD = RD->getCanonicalDecl(); |
| 279 | if (RD == Target) return AR_accessible; |
John McCall | 01ebd9d | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 280 | if (CheckDependent && MightInstantiateTo(RD, Target)) |
| 281 | OnFailure = AR_dependent; |
| 282 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 283 | Queue.push_back(RD); |
| 284 | } |
| 285 | |
| 286 | if (Queue.empty()) break; |
| 287 | |
| 288 | Derived = Queue.back(); |
| 289 | Queue.pop_back(); |
| 290 | } |
| 291 | |
| 292 | return OnFailure; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 293 | } |
| 294 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 295 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 296 | static bool MightInstantiateTo(Sema &S, DeclContext *Context, |
| 297 | DeclContext *Friend) { |
| 298 | if (Friend == Context) |
| 299 | return true; |
| 300 | |
| 301 | assert(!Friend->isDependentContext() && |
| 302 | "can't handle friends with dependent contexts here"); |
| 303 | |
| 304 | if (!Context->isDependentContext()) |
| 305 | return false; |
| 306 | |
| 307 | if (Friend->isFileContext()) |
| 308 | return false; |
| 309 | |
| 310 | // TODO: this is very conservative |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | // Asks whether the type in 'context' can ever instantiate to the type |
| 315 | // in 'friend'. |
| 316 | static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) { |
| 317 | if (Friend == Context) |
| 318 | return true; |
| 319 | |
| 320 | if (!Friend->isDependentType() && !Context->isDependentType()) |
| 321 | return false; |
| 322 | |
| 323 | // TODO: this is very conservative. |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | static bool MightInstantiateTo(Sema &S, |
| 328 | FunctionDecl *Context, |
| 329 | FunctionDecl *Friend) { |
| 330 | if (Context->getDeclName() != Friend->getDeclName()) |
| 331 | return false; |
| 332 | |
| 333 | if (!MightInstantiateTo(S, |
| 334 | Context->getDeclContext(), |
| 335 | Friend->getDeclContext())) |
| 336 | return false; |
| 337 | |
| 338 | CanQual<FunctionProtoType> FriendTy |
| 339 | = S.Context.getCanonicalType(Friend->getType()) |
| 340 | ->getAs<FunctionProtoType>(); |
| 341 | CanQual<FunctionProtoType> ContextTy |
| 342 | = S.Context.getCanonicalType(Context->getType()) |
| 343 | ->getAs<FunctionProtoType>(); |
| 344 | |
| 345 | // There isn't any way that I know of to add qualifiers |
| 346 | // during instantiation. |
| 347 | if (FriendTy.getQualifiers() != ContextTy.getQualifiers()) |
| 348 | return false; |
| 349 | |
| 350 | if (FriendTy->getNumArgs() != ContextTy->getNumArgs()) |
| 351 | return false; |
| 352 | |
| 353 | if (!MightInstantiateTo(S, |
| 354 | ContextTy->getResultType(), |
| 355 | FriendTy->getResultType())) |
| 356 | return false; |
| 357 | |
| 358 | for (unsigned I = 0, E = FriendTy->getNumArgs(); I != E; ++I) |
| 359 | if (!MightInstantiateTo(S, |
| 360 | ContextTy->getArgType(I), |
| 361 | FriendTy->getArgType(I))) |
| 362 | return false; |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | static bool MightInstantiateTo(Sema &S, |
| 368 | FunctionTemplateDecl *Context, |
| 369 | FunctionTemplateDecl *Friend) { |
| 370 | return MightInstantiateTo(S, |
| 371 | Context->getTemplatedDecl(), |
| 372 | Friend->getTemplatedDecl()); |
| 373 | } |
| 374 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 375 | static AccessResult MatchesFriend(Sema &S, |
| 376 | const EffectiveContext &EC, |
| 377 | const CXXRecordDecl *Friend) { |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 378 | if (EC.includesClass(Friend)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 379 | return AR_accessible; |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 380 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 381 | if (EC.isDependent()) { |
| 382 | CanQualType FriendTy |
| 383 | = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend)); |
| 384 | |
| 385 | for (EffectiveContext::record_iterator |
| 386 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 387 | CanQualType ContextTy |
| 388 | = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I)); |
| 389 | if (MightInstantiateTo(S, ContextTy, FriendTy)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 390 | return AR_dependent; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 394 | return AR_inaccessible; |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 395 | } |
| 396 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 397 | static AccessResult MatchesFriend(Sema &S, |
| 398 | const EffectiveContext &EC, |
| 399 | CanQualType Friend) { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 400 | if (const RecordType *RT = Friend->getAs<RecordType>()) |
| 401 | return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl())); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 402 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 403 | // TODO: we can do better than this |
| 404 | if (Friend->isDependentType()) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 405 | return AR_dependent; |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 406 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 407 | return AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /// Determines whether the given friend class template matches |
| 411 | /// anything in the effective context. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 412 | static AccessResult MatchesFriend(Sema &S, |
| 413 | const EffectiveContext &EC, |
| 414 | ClassTemplateDecl *Friend) { |
| 415 | AccessResult OnFailure = AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 416 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 417 | // Check whether the friend is the template of a class in the |
| 418 | // context chain. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 419 | for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator |
| 420 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 421 | CXXRecordDecl *Record = *I; |
| 422 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 423 | // Figure out whether the current class has a template: |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 424 | ClassTemplateDecl *CTD; |
| 425 | |
| 426 | // A specialization of the template... |
| 427 | if (isa<ClassTemplateSpecializationDecl>(Record)) { |
| 428 | CTD = cast<ClassTemplateSpecializationDecl>(Record) |
| 429 | ->getSpecializedTemplate(); |
| 430 | |
| 431 | // ... or the template pattern itself. |
| 432 | } else { |
| 433 | CTD = Record->getDescribedClassTemplate(); |
| 434 | if (!CTD) continue; |
| 435 | } |
| 436 | |
| 437 | // It's a match. |
| 438 | if (Friend == CTD->getCanonicalDecl()) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 439 | return AR_accessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 440 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 441 | // If the context isn't dependent, it can't be a dependent match. |
| 442 | if (!EC.isDependent()) |
| 443 | continue; |
| 444 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 445 | // If the template names don't match, it can't be a dependent |
| 446 | // match. This isn't true in C++0x because of template aliases. |
| 447 | if (!S.LangOpts.CPlusPlus0x && CTD->getDeclName() != Friend->getDeclName()) |
| 448 | continue; |
| 449 | |
| 450 | // If the class's context can't instantiate to the friend's |
| 451 | // context, it can't be a dependent match. |
| 452 | if (!MightInstantiateTo(S, CTD->getDeclContext(), |
| 453 | Friend->getDeclContext())) |
| 454 | continue; |
| 455 | |
| 456 | // Otherwise, it's a dependent match. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 457 | OnFailure = AR_dependent; |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 458 | } |
| 459 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 460 | return OnFailure; |
| 461 | } |
| 462 | |
| 463 | /// Determines whether the given friend function matches anything in |
| 464 | /// the effective context. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 465 | static AccessResult MatchesFriend(Sema &S, |
| 466 | const EffectiveContext &EC, |
| 467 | FunctionDecl *Friend) { |
| 468 | AccessResult OnFailure = AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 469 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 470 | for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator |
| 471 | I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) { |
| 472 | if (Friend == *I) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 473 | return AR_accessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 474 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 475 | if (EC.isDependent() && MightInstantiateTo(S, *I, Friend)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 476 | OnFailure = AR_dependent; |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 477 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 478 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 479 | return OnFailure; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /// Determines whether the given friend function template matches |
| 483 | /// anything in the effective context. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 484 | static AccessResult MatchesFriend(Sema &S, |
| 485 | const EffectiveContext &EC, |
| 486 | FunctionTemplateDecl *Friend) { |
| 487 | if (EC.Functions.empty()) return AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 488 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 489 | AccessResult OnFailure = AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 490 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 491 | for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator |
| 492 | I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 493 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 494 | FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate(); |
| 495 | if (!FTD) |
| 496 | FTD = (*I)->getDescribedFunctionTemplate(); |
| 497 | if (!FTD) |
| 498 | continue; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 499 | |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 500 | FTD = FTD->getCanonicalDecl(); |
| 501 | |
| 502 | if (Friend == FTD) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 503 | return AR_accessible; |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 504 | |
| 505 | if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 506 | OnFailure = AR_dependent; |
John McCall | 2cc2675 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return OnFailure; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /// Determines whether the given friend declaration matches anything |
| 513 | /// in the effective context. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 514 | static AccessResult MatchesFriend(Sema &S, |
| 515 | const EffectiveContext &EC, |
| 516 | FriendDecl *FriendD) { |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 517 | if (TypeSourceInfo *T = FriendD->getFriendType()) |
| 518 | return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified()); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 519 | |
| 520 | NamedDecl *Friend |
| 521 | = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl()); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 522 | |
| 523 | // FIXME: declarations with dependent or templated scope. |
| 524 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 525 | if (isa<ClassTemplateDecl>(Friend)) |
| 526 | return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend)); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 527 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 528 | if (isa<FunctionTemplateDecl>(Friend)) |
| 529 | return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend)); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 530 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 531 | if (isa<CXXRecordDecl>(Friend)) |
| 532 | return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend)); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 533 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 534 | assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind"); |
| 535 | return MatchesFriend(S, EC, cast<FunctionDecl>(Friend)); |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 536 | } |
| 537 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 538 | static AccessResult GetFriendKind(Sema &S, |
| 539 | const EffectiveContext &EC, |
| 540 | const CXXRecordDecl *Class) { |
| 541 | AccessResult OnFailure = AR_inaccessible; |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 542 | |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 543 | // Okay, check friends. |
| 544 | for (CXXRecordDecl::friend_iterator I = Class->friend_begin(), |
| 545 | E = Class->friend_end(); I != E; ++I) { |
| 546 | FriendDecl *Friend = *I; |
| 547 | |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 548 | switch (MatchesFriend(S, EC, Friend)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 549 | case AR_accessible: |
| 550 | return AR_accessible; |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 551 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 552 | case AR_inaccessible: |
| 553 | continue; |
| 554 | |
| 555 | case AR_dependent: |
| 556 | OnFailure = AR_dependent; |
John McCall | a742db0 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 557 | break; |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 558 | } |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // That's it, give up. |
John McCall | 88b6c71 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 562 | return OnFailure; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 563 | } |
| 564 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 565 | static AccessResult HasAccess(Sema &S, |
| 566 | const EffectiveContext &EC, |
| 567 | const CXXRecordDecl *NamingClass, |
| 568 | AccessSpecifier Access, |
| 569 | const AccessTarget &Target) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 570 | assert(NamingClass->getCanonicalDecl() == NamingClass && |
| 571 | "declaration should be canonicalized before being passed here"); |
| 572 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 573 | if (Access == AS_public) return AR_accessible; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 574 | assert(Access == AS_private || Access == AS_protected); |
| 575 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 576 | AccessResult OnFailure = AR_inaccessible; |
| 577 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 578 | for (EffectiveContext::record_iterator |
| 579 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 580 | // All the declarations in EC have been canonicalized, so pointer |
| 581 | // equality from this point on will work fine. |
| 582 | const CXXRecordDecl *ECRecord = *I; |
| 583 | |
| 584 | // [B2] and [M2] |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 585 | if (Access == AS_private) { |
| 586 | if (ECRecord == NamingClass) |
| 587 | return AR_accessible; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 588 | |
John McCall | 01ebd9d | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 589 | if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass)) |
| 590 | OnFailure = AR_dependent; |
| 591 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 592 | // [B3] and [M3] |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 593 | } else { |
| 594 | assert(Access == AS_protected); |
| 595 | switch (IsDerivedFromInclusive(ECRecord, NamingClass)) { |
| 596 | case AR_accessible: break; |
| 597 | case AR_inaccessible: continue; |
| 598 | case AR_dependent: OnFailure = AR_dependent; continue; |
| 599 | } |
| 600 | |
| 601 | if (!Target.hasInstanceContext()) |
| 602 | return AR_accessible; |
| 603 | |
| 604 | const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S); |
| 605 | if (!InstanceContext) { |
| 606 | OnFailure = AR_dependent; |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | // C++ [class.protected]p1: |
| 611 | // An additional access check beyond those described earlier in |
| 612 | // [class.access] is applied when a non-static data member or |
| 613 | // non-static member function is a protected member of its naming |
| 614 | // class. As described earlier, access to a protected member is |
| 615 | // granted because the reference occurs in a friend or member of |
| 616 | // some class C. If the access is to form a pointer to member, |
| 617 | // the nested-name-specifier shall name C or a class derived from |
| 618 | // C. All other accesses involve a (possibly implicit) object |
| 619 | // expression. In this case, the class of the object expression |
| 620 | // shall be C or a class derived from C. |
| 621 | // |
| 622 | // We interpret this as a restriction on [M3]. Most of the |
| 623 | // conditions are encoded by not having any instance context. |
| 624 | switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) { |
| 625 | case AR_accessible: return AR_accessible; |
| 626 | case AR_inaccessible: continue; |
| 627 | case AR_dependent: OnFailure = AR_dependent; continue; |
| 628 | } |
| 629 | } |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 630 | } |
| 631 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 632 | if (!NamingClass->hasFriends()) |
| 633 | return OnFailure; |
| 634 | |
| 635 | // Don't consider friends if we're under the [class.protected] |
| 636 | // restriction, above. |
| 637 | if (Access == AS_protected && Target.hasInstanceContext()) { |
| 638 | const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S); |
| 639 | if (!InstanceContext) return AR_dependent; |
| 640 | |
| 641 | switch (IsDerivedFromInclusive(InstanceContext, NamingClass)) { |
| 642 | case AR_accessible: break; |
| 643 | case AR_inaccessible: return OnFailure; |
| 644 | case AR_dependent: return AR_dependent; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | switch (GetFriendKind(S, EC, NamingClass)) { |
| 649 | case AR_accessible: return AR_accessible; |
| 650 | case AR_inaccessible: return OnFailure; |
| 651 | case AR_dependent: return AR_dependent; |
| 652 | } |
| 653 | |
| 654 | // Silence bogus warnings |
| 655 | llvm_unreachable("impossible friendship kind"); |
| 656 | return OnFailure; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 657 | } |
| 658 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 659 | /// Finds the best path from the naming class to the declaring class, |
| 660 | /// taking friend declarations into account. |
| 661 | /// |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 662 | /// C++0x [class.access.base]p5: |
| 663 | /// A member m is accessible at the point R when named in class N if |
| 664 | /// [M1] m as a member of N is public, or |
| 665 | /// [M2] m as a member of N is private, and R occurs in a member or |
| 666 | /// friend of class N, or |
| 667 | /// [M3] m as a member of N is protected, and R occurs in a member or |
| 668 | /// friend of class N, or in a member or friend of a class P |
| 669 | /// derived from N, where m as a member of P is public, private, |
| 670 | /// or protected, or |
| 671 | /// [M4] there exists a base class B of N that is accessible at R, and |
| 672 | /// m is accessible at R when named in class B. |
| 673 | /// |
| 674 | /// C++0x [class.access.base]p4: |
| 675 | /// A base class B of N is accessible at R, if |
| 676 | /// [B1] an invented public member of B would be a public member of N, or |
| 677 | /// [B2] R occurs in a member or friend of class N, and an invented public |
| 678 | /// member of B would be a private or protected member of N, or |
| 679 | /// [B3] R occurs in a member or friend of a class P derived from N, and an |
| 680 | /// invented public member of B would be a private or protected member |
| 681 | /// of P, or |
| 682 | /// [B4] there exists a class S such that B is a base class of S accessible |
| 683 | /// at R and S is a base class of N accessible at R. |
| 684 | /// |
| 685 | /// Along a single inheritance path we can restate both of these |
| 686 | /// iteratively: |
| 687 | /// |
| 688 | /// First, we note that M1-4 are equivalent to B1-4 if the member is |
| 689 | /// treated as a notional base of its declaring class with inheritance |
| 690 | /// access equivalent to the member's access. Therefore we need only |
| 691 | /// ask whether a class B is accessible from a class N in context R. |
| 692 | /// |
| 693 | /// Let B_1 .. B_n be the inheritance path in question (i.e. where |
| 694 | /// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of |
| 695 | /// B_i). For i in 1..n, we will calculate ACAB(i), the access to the |
| 696 | /// closest accessible base in the path: |
| 697 | /// Access(a, b) = (* access on the base specifier from a to b *) |
| 698 | /// Merge(a, forbidden) = forbidden |
| 699 | /// Merge(a, private) = forbidden |
| 700 | /// Merge(a, b) = min(a,b) |
| 701 | /// Accessible(c, forbidden) = false |
| 702 | /// Accessible(c, private) = (R is c) || IsFriend(c, R) |
| 703 | /// Accessible(c, protected) = (R derived from c) || IsFriend(c, R) |
| 704 | /// Accessible(c, public) = true |
| 705 | /// ACAB(n) = public |
| 706 | /// ACAB(i) = |
| 707 | /// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in |
| 708 | /// if Accessible(B_i, AccessToBase) then public else AccessToBase |
| 709 | /// |
| 710 | /// B is an accessible base of N at R iff ACAB(1) = public. |
| 711 | /// |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 712 | /// \param FinalAccess the access of the "final step", or AS_public if |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 713 | /// there is no final step. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 714 | /// \return null if friendship is dependent |
| 715 | static CXXBasePath *FindBestPath(Sema &S, |
| 716 | const EffectiveContext &EC, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 717 | AccessTarget &Target, |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 718 | AccessSpecifier FinalAccess, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 719 | CXXBasePaths &Paths) { |
| 720 | // Derive the paths to the desired base. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 721 | const CXXRecordDecl *Derived = Target.getNamingClass(); |
| 722 | const CXXRecordDecl *Base = Target.getDeclaringClass(); |
| 723 | |
| 724 | // FIXME: fail correctly when there are dependent paths. |
| 725 | bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base), |
| 726 | Paths); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 727 | assert(isDerived && "derived class not actually derived from base"); |
| 728 | (void) isDerived; |
| 729 | |
| 730 | CXXBasePath *BestPath = 0; |
| 731 | |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 732 | assert(FinalAccess != AS_none && "forbidden access after declaring class"); |
| 733 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 734 | bool AnyDependent = false; |
| 735 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 736 | // Derive the friend-modified access along each path. |
| 737 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
| 738 | PI != PE; ++PI) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 739 | AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 740 | |
| 741 | // Walk through the path backwards. |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 742 | AccessSpecifier PathAccess = FinalAccess; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 743 | CXXBasePath::iterator I = PI->end(), E = PI->begin(); |
| 744 | while (I != E) { |
| 745 | --I; |
| 746 | |
John McCall | 7aceaf8 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 747 | assert(PathAccess != AS_none); |
| 748 | |
| 749 | // If the declaration is a private member of a base class, there |
| 750 | // is no level of friendship in derived classes that can make it |
| 751 | // accessible. |
| 752 | if (PathAccess == AS_private) { |
| 753 | PathAccess = AS_none; |
| 754 | break; |
| 755 | } |
| 756 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 757 | const CXXRecordDecl *NC = I->Class->getCanonicalDecl(); |
| 758 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 759 | AccessSpecifier BaseAccess = I->Base->getAccessSpecifier(); |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 760 | PathAccess = std::max(PathAccess, BaseAccess); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 761 | |
| 762 | switch (HasAccess(S, EC, NC, PathAccess, Target)) { |
| 763 | case AR_inaccessible: break; |
| 764 | case AR_accessible: |
| 765 | PathAccess = AS_public; |
| 766 | |
| 767 | // Future tests are not against members and so do not have |
| 768 | // instance context. |
| 769 | Target.suppressInstanceContext(); |
| 770 | break; |
| 771 | case AR_dependent: |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 772 | AnyDependent = true; |
| 773 | goto Next; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 774 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // Note that we modify the path's Access field to the |
| 778 | // friend-modified access. |
| 779 | if (BestPath == 0 || PathAccess < BestPath->Access) { |
| 780 | BestPath = &*PI; |
| 781 | BestPath->Access = PathAccess; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 782 | |
| 783 | // Short-circuit if we found a public path. |
| 784 | if (BestPath->Access == AS_public) |
| 785 | return BestPath; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 786 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 787 | |
| 788 | Next: ; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 789 | } |
| 790 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 791 | assert((!BestPath || BestPath->Access != AS_public) && |
| 792 | "fell out of loop with public path"); |
| 793 | |
| 794 | // We didn't find a public path, but at least one path was subject |
| 795 | // to dependent friendship, so delay the check. |
| 796 | if (AnyDependent) |
| 797 | return 0; |
| 798 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 799 | return BestPath; |
| 800 | } |
| 801 | |
| 802 | /// Diagnose the path which caused the given declaration or base class |
| 803 | /// to become inaccessible. |
| 804 | static void DiagnoseAccessPath(Sema &S, |
| 805 | const EffectiveContext &EC, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 806 | AccessTarget &Entity) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 807 | AccessSpecifier Access = Entity.getAccess(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 808 | const CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 809 | NamingClass = NamingClass->getCanonicalDecl(); |
| 810 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 811 | NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0); |
| 812 | const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass(); |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 813 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 814 | // Easy case: the decl's natural access determined its path access. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 815 | // We have to check against AS_private here in case Access is AS_none, |
| 816 | // indicating a non-public member of a private base class. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 817 | if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 818 | switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) { |
| 819 | case AR_inaccessible: { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 820 | S.Diag(D->getLocation(), diag::note_access_natural) |
| 821 | << (unsigned) (Access == AS_protected) |
| 822 | << /*FIXME: not implicitly*/ 0; |
| 823 | return; |
| 824 | } |
| 825 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 826 | case AR_accessible: break; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 827 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 828 | case AR_dependent: |
| 829 | llvm_unreachable("can't diagnose dependent access failures"); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | CXXBasePaths Paths; |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 835 | CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 836 | |
| 837 | CXXBasePath::iterator I = Path.end(), E = Path.begin(); |
| 838 | while (I != E) { |
| 839 | --I; |
| 840 | |
| 841 | const CXXBaseSpecifier *BS = I->Base; |
| 842 | AccessSpecifier BaseAccess = BS->getAccessSpecifier(); |
| 843 | |
| 844 | // If this is public inheritance, or the derived class is a friend, |
| 845 | // skip this step. |
| 846 | if (BaseAccess == AS_public) |
| 847 | continue; |
| 848 | |
| 849 | switch (GetFriendKind(S, EC, I->Class)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 850 | case AR_accessible: continue; |
| 851 | case AR_inaccessible: break; |
| 852 | case AR_dependent: |
| 853 | llvm_unreachable("can't diagnose dependent access failures"); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | // Check whether this base specifier is the tighest point |
| 857 | // constraining access. We have to check against AS_private for |
| 858 | // the same reasons as above. |
| 859 | if (BaseAccess == AS_private || BaseAccess >= Access) { |
| 860 | |
| 861 | // We're constrained by inheritance, but we want to say |
| 862 | // "declared private here" if we're diagnosing a hierarchy |
| 863 | // conversion and this is the final step. |
| 864 | unsigned diagnostic; |
| 865 | if (D) diagnostic = diag::note_access_constrained_by_path; |
| 866 | else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural; |
| 867 | else diagnostic = diag::note_access_constrained_by_path; |
| 868 | |
| 869 | S.Diag(BS->getSourceRange().getBegin(), diagnostic) |
| 870 | << BS->getSourceRange() |
| 871 | << (BaseAccess == AS_protected) |
| 872 | << (BS->getAccessSpecifierAsWritten() == AS_none); |
Douglas Gregor | 76ef658 | 2010-05-28 04:34:55 +0000 | [diff] [blame] | 873 | |
| 874 | if (D) |
| 875 | S.Diag(D->getLocation(), diag::note_field_decl); |
| 876 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | llvm_unreachable("access not apparently constrained by path"); |
| 882 | } |
| 883 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 884 | static void DiagnoseBadAccess(Sema &S, SourceLocation Loc, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 885 | const EffectiveContext &EC, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 886 | AccessTarget &Entity) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 887 | const CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 888 | const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass(); |
| 889 | NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0); |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 890 | |
| 891 | S.Diag(Loc, Entity.getDiag()) |
| 892 | << (Entity.getAccess() == AS_protected) |
| 893 | << (D ? D->getDeclName() : DeclarationName()) |
| 894 | << S.Context.getTypeDeclType(NamingClass) |
| 895 | << S.Context.getTypeDeclType(DeclaringClass); |
| 896 | DiagnoseAccessPath(S, EC, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 897 | } |
| 898 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 899 | /// Determines whether the accessed entity is accessible. Public members |
| 900 | /// have been weeded out by this point. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 901 | static AccessResult IsAccessible(Sema &S, |
| 902 | const EffectiveContext &EC, |
| 903 | AccessTarget &Entity) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 904 | // Determine the actual naming class. |
| 905 | CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
| 906 | while (NamingClass->isAnonymousStructOrUnion()) |
| 907 | NamingClass = cast<CXXRecordDecl>(NamingClass->getParent()); |
| 908 | NamingClass = NamingClass->getCanonicalDecl(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 909 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 910 | AccessSpecifier UnprivilegedAccess = Entity.getAccess(); |
| 911 | assert(UnprivilegedAccess != AS_public && "public access not weeded out"); |
| 912 | |
| 913 | // Before we try to recalculate access paths, try to white-list |
| 914 | // accesses which just trade in on the final step, i.e. accesses |
| 915 | // which don't require [M4] or [B4]. These are by far the most |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 916 | // common forms of privileged access. |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 917 | if (UnprivilegedAccess != AS_none) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 918 | switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) { |
| 919 | case AR_dependent: |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 920 | // This is actually an interesting policy decision. We don't |
| 921 | // *have* to delay immediately here: we can do the full access |
| 922 | // calculation in the hope that friendship on some intermediate |
| 923 | // class will make the declaration accessible non-dependently. |
| 924 | // But that's not cheap, and odds are very good (note: assertion |
| 925 | // made without data) that the friend declaration will determine |
| 926 | // access. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 927 | return AR_dependent; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 928 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 929 | case AR_accessible: return AR_accessible; |
| 930 | case AR_inaccessible: break; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 934 | AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 935 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 936 | // We lower member accesses to base accesses by pretending that the |
| 937 | // member is a base class of its declaring class. |
| 938 | AccessSpecifier FinalAccess; |
| 939 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 940 | if (Entity.isMemberAccess()) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 941 | // Determine if the declaration is accessible from EC when named |
| 942 | // in its declaring class. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 943 | NamedDecl *Target = Entity.getTargetDecl(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 944 | const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 945 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 946 | FinalAccess = Target->getAccess(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 947 | switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) { |
| 948 | case AR_accessible: |
| 949 | FinalAccess = AS_public; |
| 950 | break; |
| 951 | case AR_inaccessible: break; |
| 952 | case AR_dependent: return AR_dependent; // see above |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 953 | } |
| 954 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 955 | if (DeclaringClass == NamingClass) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 956 | return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible); |
| 957 | |
| 958 | Entity.suppressInstanceContext(); |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 959 | } else { |
| 960 | FinalAccess = AS_public; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 961 | } |
| 962 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 963 | assert(Entity.getDeclaringClass() != NamingClass); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 964 | |
| 965 | // Append the declaration's access if applicable. |
| 966 | CXXBasePaths Paths; |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 967 | CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 968 | if (!Path) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 969 | return AR_dependent; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 970 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 971 | assert(Path->Access <= UnprivilegedAccess && |
| 972 | "access along best path worse than direct?"); |
| 973 | if (Path->Access == AS_public) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 974 | return AR_accessible; |
| 975 | return AR_inaccessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 976 | } |
| 977 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 978 | static void DelayDependentAccess(Sema &S, |
| 979 | const EffectiveContext &EC, |
| 980 | SourceLocation Loc, |
| 981 | const AccessTarget &Entity) { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 982 | assert(EC.isDependent() && "delaying non-dependent access"); |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 983 | DeclContext *DC = EC.getInnerContext(); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 984 | assert(DC->isDependentContext() && "delaying non-dependent access"); |
| 985 | DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access, |
| 986 | Loc, |
| 987 | Entity.isMemberAccess(), |
| 988 | Entity.getAccess(), |
| 989 | Entity.getTargetDecl(), |
| 990 | Entity.getNamingClass(), |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 991 | Entity.getBaseObjectType(), |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 992 | Entity.getDiag()); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 993 | } |
| 994 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 995 | /// Checks access to an entity from the given effective context. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 996 | static AccessResult CheckEffectiveAccess(Sema &S, |
| 997 | const EffectiveContext &EC, |
| 998 | SourceLocation Loc, |
| 999 | AccessTarget &Entity) { |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1000 | assert(Entity.getAccess() != AS_public && "called for public access!"); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1001 | |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1002 | switch (IsAccessible(S, EC, Entity)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1003 | case AR_dependent: |
| 1004 | DelayDependentAccess(S, EC, Loc, Entity); |
| 1005 | return AR_dependent; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1006 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1007 | case AR_inaccessible: |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1008 | if (!Entity.isQuiet()) |
| 1009 | DiagnoseBadAccess(S, Loc, EC, Entity); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1010 | return AR_inaccessible; |
John McCall | db73c68 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1011 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1012 | case AR_accessible: |
| 1013 | return AR_accessible; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1016 | // silence unnecessary warning |
| 1017 | llvm_unreachable("invalid access result"); |
| 1018 | return AR_accessible; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1019 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1020 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1021 | static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1022 | AccessTarget &Entity) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1023 | // If the access path is public, it's accessible everywhere. |
| 1024 | if (Entity.getAccess() == AS_public) |
| 1025 | return Sema::AR_accessible; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1026 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1027 | // If we're currently parsing a top-level declaration, delay |
| 1028 | // diagnostics. This is the only case where parsing a declaration |
| 1029 | // can actually change our effective context for the purposes of |
| 1030 | // access control. |
| 1031 | if (S.CurContext->isFileContext() && S.ParsingDeclDepth) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1032 | S.DelayedDiagnostics.push_back( |
| 1033 | Sema::DelayedDiagnostic::makeAccess(Loc, Entity)); |
| 1034 | return Sema::AR_delayed; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1037 | EffectiveContext EC(S.CurContext); |
| 1038 | switch (CheckEffectiveAccess(S, EC, Loc, Entity)) { |
| 1039 | case AR_accessible: return Sema::AR_accessible; |
| 1040 | case AR_inaccessible: return Sema::AR_inaccessible; |
| 1041 | case AR_dependent: return Sema::AR_dependent; |
| 1042 | } |
| 1043 | llvm_unreachable("falling off end"); |
| 1044 | return Sema::AR_accessible; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1047 | void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1048 | // Pretend we did this from the context of the newly-parsed |
Chandler Carruth | 630eb01 | 2010-04-18 08:23:21 +0000 | [diff] [blame] | 1049 | // declaration. If that declaration itself forms a declaration context, |
| 1050 | // include it in the effective context so that parameters and return types of |
| 1051 | // befriended functions have that function's access priveledges. |
| 1052 | DeclContext *DC = Ctx->getDeclContext(); |
| 1053 | if (isa<FunctionDecl>(Ctx)) |
| 1054 | DC = cast<DeclContext>(Ctx); |
| 1055 | else if (FunctionTemplateDecl *FnTpl = dyn_cast<FunctionTemplateDecl>(Ctx)) |
| 1056 | DC = cast<DeclContext>(FnTpl->getTemplatedDecl()); |
| 1057 | EffectiveContext EC(DC); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1058 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1059 | AccessTarget Target(DD.getAccessData()); |
| 1060 | |
| 1061 | if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1062 | DD.Triggered = true; |
| 1063 | } |
| 1064 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1065 | void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD, |
| 1066 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1067 | SourceLocation Loc = DD.getAccessLoc(); |
| 1068 | AccessSpecifier Access = DD.getAccess(); |
| 1069 | |
| 1070 | Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(), |
| 1071 | TemplateArgs); |
| 1072 | if (!NamingD) return; |
| 1073 | Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(), |
| 1074 | TemplateArgs); |
| 1075 | if (!TargetD) return; |
| 1076 | |
| 1077 | if (DD.isAccessToMember()) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1078 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD); |
| 1079 | NamedDecl *TargetDecl = cast<NamedDecl>(TargetD); |
| 1080 | QualType BaseObjectType = DD.getAccessBaseObjectType(); |
| 1081 | if (!BaseObjectType.isNull()) { |
| 1082 | BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc, |
| 1083 | DeclarationName()); |
| 1084 | if (BaseObjectType.isNull()) return; |
| 1085 | } |
| 1086 | |
| 1087 | AccessTarget Entity(Context, |
| 1088 | AccessTarget::Member, |
| 1089 | NamingClass, |
| 1090 | DeclAccessPair::make(TargetDecl, Access), |
| 1091 | BaseObjectType); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1092 | Entity.setDiag(DD.getDiagnostic()); |
| 1093 | CheckAccess(*this, Loc, Entity); |
| 1094 | } else { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1095 | AccessTarget Entity(Context, |
| 1096 | AccessTarget::Base, |
| 1097 | cast<CXXRecordDecl>(TargetD), |
| 1098 | cast<CXXRecordDecl>(NamingD), |
| 1099 | Access); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1100 | Entity.setDiag(DD.getDiagnostic()); |
| 1101 | CheckAccess(*this, Loc, Entity); |
| 1102 | } |
| 1103 | } |
| 1104 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1105 | Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1106 | DeclAccessPair Found) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1107 | if (!getLangOptions().AccessControl || |
| 1108 | !E->getNamingClass() || |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1109 | Found.getAccess() == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1110 | return AR_accessible; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1111 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1112 | AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(), |
| 1113 | Found, QualType()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1114 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 1115 | |
| 1116 | return CheckAccess(*this, E->getNameLoc(), Entity); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /// Perform access-control checking on a previously-unresolved member |
| 1120 | /// access which has now been resolved to a member. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1121 | Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1122 | DeclAccessPair Found) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1123 | if (!getLangOptions().AccessControl || |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1124 | Found.getAccess() == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1125 | return AR_accessible; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1126 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1127 | QualType BaseType = E->getBaseType(); |
| 1128 | if (E->isArrow()) |
| 1129 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 1130 | |
| 1131 | AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(), |
| 1132 | Found, BaseType); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1133 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 1134 | |
| 1135 | return CheckAccess(*this, E->getMemberLoc(), Entity); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1138 | Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1139 | CXXDestructorDecl *Dtor, |
| 1140 | const PartialDiagnostic &PDiag) { |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1141 | if (!getLangOptions().AccessControl) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1142 | return AR_accessible; |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1143 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1144 | // There's never a path involved when checking implicit destructor access. |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1145 | AccessSpecifier Access = Dtor->getAccess(); |
| 1146 | if (Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1147 | return AR_accessible; |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1148 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1149 | CXXRecordDecl *NamingClass = Dtor->getParent(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1150 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, |
| 1151 | DeclAccessPair::make(Dtor, Access), |
| 1152 | QualType()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1153 | Entity.setDiag(PDiag); // TODO: avoid copy |
| 1154 | |
| 1155 | return CheckAccess(*this, Loc, Entity); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1158 | /// Checks access to a constructor. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1159 | Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1160 | CXXConstructorDecl *Constructor, |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1161 | const InitializedEntity &Entity, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1162 | AccessSpecifier Access) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1163 | if (!getLangOptions().AccessControl || |
| 1164 | Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1165 | return AR_accessible; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1166 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1167 | CXXRecordDecl *NamingClass = Constructor->getParent(); |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1168 | AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass, |
| 1169 | DeclAccessPair::make(Constructor, Access), |
| 1170 | QualType()); |
| 1171 | switch (Entity.getKind()) { |
| 1172 | default: |
| 1173 | AccessEntity.setDiag(diag::err_access_ctor); |
| 1174 | break; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1175 | |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 1176 | case InitializedEntity::EK_Base: |
| 1177 | AccessEntity.setDiag(PDiag(diag::err_access_base) |
| 1178 | << Entity.isInheritedVirtualBase() |
| 1179 | << Entity.getBaseSpecifier()->getType() |
| 1180 | << getSpecialMember(Constructor)); |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1181 | break; |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 1182 | |
Anders Carlsson | b99c666 | 2010-04-21 20:28:29 +0000 | [diff] [blame] | 1183 | case InitializedEntity::EK_Member: { |
| 1184 | const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl()); |
Anders Carlsson | 0e313bd | 2010-04-23 03:41:35 +0000 | [diff] [blame] | 1185 | AccessEntity.setDiag(PDiag(diag::err_access_field) |
| 1186 | << Field->getType() |
| 1187 | << getSpecialMember(Constructor)); |
Anders Carlsson | b99c666 | 2010-04-21 20:28:29 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | } |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1190 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1193 | return CheckAccess(*this, UseLoc, AccessEntity); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 1196 | /// Checks direct (i.e. non-inherited) access to an arbitrary class |
| 1197 | /// member. |
| 1198 | Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc, |
| 1199 | NamedDecl *Target, |
| 1200 | const PartialDiagnostic &Diag) { |
| 1201 | AccessSpecifier Access = Target->getAccess(); |
| 1202 | if (!getLangOptions().AccessControl || |
| 1203 | Access == AS_public) |
| 1204 | return AR_accessible; |
| 1205 | |
| 1206 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1207 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, |
| 1208 | DeclAccessPair::make(Target, Access), |
| 1209 | QualType()); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 1210 | Entity.setDiag(Diag); |
| 1211 | return CheckAccess(*this, UseLoc, Entity); |
| 1212 | } |
| 1213 | |
| 1214 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1215 | /// Checks access to an overloaded operator new or delete. |
| 1216 | Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc, |
| 1217 | SourceRange PlacementRange, |
| 1218 | CXXRecordDecl *NamingClass, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1219 | DeclAccessPair Found) { |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1220 | if (!getLangOptions().AccessControl || |
| 1221 | !NamingClass || |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1222 | Found.getAccess() == AS_public) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1223 | return AR_accessible; |
| 1224 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1225 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
| 1226 | QualType()); |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1227 | Entity.setDiag(diag::err_access) |
| 1228 | << PlacementRange; |
| 1229 | |
| 1230 | return CheckAccess(*this, OpLoc, Entity); |
| 1231 | } |
| 1232 | |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1233 | /// Checks access to an overloaded member operator, including |
| 1234 | /// conversion operators. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1235 | Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc, |
| 1236 | Expr *ObjectExpr, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1237 | Expr *ArgExpr, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1238 | DeclAccessPair Found) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1239 | if (!getLangOptions().AccessControl || |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1240 | Found.getAccess() == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1241 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1242 | |
| 1243 | const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>(); |
| 1244 | assert(RT && "found member operator but object expr not of record type"); |
| 1245 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl()); |
| 1246 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1247 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
| 1248 | ObjectExpr->getType()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1249 | Entity.setDiag(diag::err_access) |
| 1250 | << ObjectExpr->getSourceRange() |
| 1251 | << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange()); |
| 1252 | |
| 1253 | return CheckAccess(*this, OpLoc, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1254 | } |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1255 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1256 | Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr, |
| 1257 | DeclAccessPair Found) { |
| 1258 | if (!getLangOptions().AccessControl || |
John McCall | e2f5ba9 | 2010-03-30 22:20:00 +0000 | [diff] [blame] | 1259 | Found.getAccess() == AS_none || |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1260 | Found.getAccess() == AS_public) |
| 1261 | return AR_accessible; |
| 1262 | |
| 1263 | OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).getPointer(); |
John McCall | e9ee23e | 2010-04-22 18:44:12 +0000 | [diff] [blame] | 1264 | CXXRecordDecl *NamingClass = Ovl->getNamingClass(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1265 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1266 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
| 1267 | Context.getTypeDeclType(NamingClass)); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1268 | Entity.setDiag(diag::err_access) |
| 1269 | << Ovl->getSourceRange(); |
| 1270 | |
| 1271 | return CheckAccess(*this, Ovl->getNameLoc(), Entity); |
| 1272 | } |
| 1273 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1274 | /// Checks access for a hierarchy conversion. |
| 1275 | /// |
| 1276 | /// \param IsBaseToDerived whether this is a base-to-derived conversion (true) |
| 1277 | /// or a derived-to-base conversion (false) |
| 1278 | /// \param ForceCheck true if this check should be performed even if access |
| 1279 | /// control is disabled; some things rely on this for semantics |
| 1280 | /// \param ForceUnprivileged true if this check should proceed as if the |
| 1281 | /// context had no special privileges |
| 1282 | /// \param ADK controls the kind of diagnostics that are used |
| 1283 | Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1284 | QualType Base, |
| 1285 | QualType Derived, |
| 1286 | const CXXBasePath &Path, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1287 | unsigned DiagID, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1288 | bool ForceCheck, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1289 | bool ForceUnprivileged) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1290 | if (!ForceCheck && !getLangOptions().AccessControl) |
| 1291 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1292 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1293 | if (Path.Access == AS_public) |
| 1294 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1295 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1296 | CXXRecordDecl *BaseD, *DerivedD; |
| 1297 | BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl()); |
| 1298 | DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1299 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1300 | AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD, |
| 1301 | Path.Access); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1302 | if (DiagID) |
| 1303 | Entity.setDiag(DiagID) << Derived << Base; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1304 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1305 | if (ForceUnprivileged) { |
| 1306 | switch (CheckEffectiveAccess(*this, EffectiveContext(), |
| 1307 | AccessLoc, Entity)) { |
| 1308 | case ::AR_accessible: return Sema::AR_accessible; |
| 1309 | case ::AR_inaccessible: return Sema::AR_inaccessible; |
| 1310 | case ::AR_dependent: return Sema::AR_dependent; |
| 1311 | } |
| 1312 | llvm_unreachable("unexpected result from CheckEffectiveAccess"); |
| 1313 | } |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1314 | return CheckAccess(*this, AccessLoc, Entity); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1317 | /// Checks access to all the declarations in the given result set. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1318 | void Sema::CheckLookupAccess(const LookupResult &R) { |
| 1319 | assert(getLangOptions().AccessControl |
| 1320 | && "performing access check without access control"); |
| 1321 | assert(R.getNamingClass() && "performing access check without naming class"); |
| 1322 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1323 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1324 | if (I.getAccess() != AS_public) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1325 | AccessTarget Entity(Context, AccessedEntity::Member, |
| 1326 | R.getNamingClass(), I.getPair(), |
| 1327 | R.getBaseObjectType()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1328 | Entity.setDiag(diag::err_access); |
| 1329 | |
| 1330 | CheckAccess(*this, R.getNameLoc(), Entity); |
| 1331 | } |
| 1332 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1333 | } |