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