blob: c3a1e7521050bb1d38cbf98c8aa7eac831705fac [file] [log] [blame]
Anders Carlsson4742a9c2009-03-27 05:05:05 +00001//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
Anders Carlsson8ed6f362009-03-27 04:43:36 +00002//
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 Carlsson17941122009-03-27 04:54:36 +000013
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCallb45a1e72010-08-26 02:13:20 +000015#include "clang/Sema/DelayedDiagnostic.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
17#include "clang/Sema/Lookup.h"
Anders Carlsson733d77f2009-03-27 06:03:27 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000019#include "clang/AST/CXXInheritance.h"
20#include "clang/AST/DeclCXX.h"
John McCall16927f62010-03-12 01:19:31 +000021#include "clang/AST/DeclFriend.h"
John McCallc62bb642010-03-24 05:22:00 +000022#include "clang/AST/DependentDiagnostic.h"
John McCall58cc69d2010-01-27 01:50:18 +000023#include "clang/AST/ExprCXX.h"
24
Anders Carlsson17941122009-03-27 04:54:36 +000025using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000026using namespace sema;
Anders Carlsson17941122009-03-27 04:54:36 +000027
John McCalla8ae2222010-04-06 21:38:20 +000028/// A copy of Sema's enum without AR_delayed.
29enum AccessResult {
30 AR_accessible,
31 AR_inaccessible,
32 AR_dependent
33};
34
Anders Carlsson4742a9c2009-03-27 05:05:05 +000035/// 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 Stump11289f42009-09-09 15:08:12 +000038bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
Anders Carlsson17941122009-03-27 04:54:36 +000039 NamedDecl *PrevMemberDecl,
40 AccessSpecifier LexicalAS) {
41 if (!PrevMemberDecl) {
42 // Use the lexical access specifier.
43 MemberDecl->setAccess(LexicalAS);
44 return false;
45 }
Mike Stump11289f42009-09-09 15:08:12 +000046
Anders Carlsson17941122009-03-27 04:54:36 +000047 // 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 Stump11289f42009-09-09 15:08:12 +000050 Diag(MemberDecl->getLocation(),
51 diag::err_class_redeclared_with_different_access)
Anders Carlsson17941122009-03-27 04:54:36 +000052 << MemberDecl << LexicalAS;
53 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
54 << PrevMemberDecl << PrevMemberDecl->getAccess();
John McCall0a4bb262009-12-23 00:37:40 +000055
56 MemberDecl->setAccess(LexicalAS);
Anders Carlsson17941122009-03-27 04:54:36 +000057 return true;
58 }
Mike Stump11289f42009-09-09 15:08:12 +000059
Anders Carlsson17941122009-03-27 04:54:36 +000060 MemberDecl->setAccess(PrevMemberDecl->getAccess());
61 return false;
62}
Anders Carlsson4742a9c2009-03-27 05:05:05 +000063
John McCalla8ae2222010-04-06 21:38:20 +000064static 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 McCall5b0829a2010-02-10 09:31:12 +000078namespace {
79struct EffectiveContext {
John McCall3dc81f72010-03-27 06:55:49 +000080 EffectiveContext() : Inner(0), Dependent(false) {}
Anders Carlsson733d77f2009-03-27 06:03:27 +000081
John McCall816d75b2010-03-24 07:46:06 +000082 explicit EffectiveContext(DeclContext *DC)
83 : Inner(DC),
84 Dependent(DC->isDependentContext()) {
John McCallc62bb642010-03-24 05:22:00 +000085
John McCallfb803d72010-03-17 04:58:56 +000086 // 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 McCall3dc81f72010-03-27 06:55:49 +000091 // 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 McCallfb803d72010-03-17 04:58:56 +0000112 }
Anders Carlsson733d77f2009-03-27 06:03:27 +0000113 }
Sebastian Redle644e192009-07-18 14:32:15 +0000114
John McCallc62bb642010-03-24 05:22:00 +0000115 bool isDependent() const { return Dependent; }
116
John McCallfb803d72010-03-17 04:58:56 +0000117 bool includesClass(const CXXRecordDecl *R) const {
118 R = R->getCanonicalDecl();
119 return std::find(Records.begin(), Records.end(), R)
120 != Records.end();
John McCall5b0829a2010-02-10 09:31:12 +0000121 }
122
John McCall816d75b2010-03-24 07:46:06 +0000123 /// 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 McCallc62bb642010-03-24 05:22:00 +0000127 }
128
129 typedef llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
130
John McCall816d75b2010-03-24 07:46:06 +0000131 DeclContext *Inner;
John McCall3dc81f72010-03-27 06:55:49 +0000132 llvm::SmallVector<FunctionDecl*, 4> Functions;
John McCallfb803d72010-03-17 04:58:56 +0000133 llvm::SmallVector<CXXRecordDecl*, 4> Records;
John McCallc62bb642010-03-24 05:22:00 +0000134 bool Dependent;
John McCall5b0829a2010-02-10 09:31:12 +0000135};
John McCalla8ae2222010-04-06 21:38:20 +0000136
John McCallb45a1e72010-08-26 02:13:20 +0000137/// Like sema:;AccessedEntity, but kindly lets us scribble all over
John McCalla8ae2222010-04-06 21:38:20 +0000138/// it.
John McCallb45a1e72010-08-26 02:13:20 +0000139struct AccessTarget : public AccessedEntity {
140 AccessTarget(const AccessedEntity &Entity)
John McCalla8ae2222010-04-06 21:38:20 +0000141 : 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 McCall8e36d532010-04-07 00:41:46 +0000174 friend struct AccessTarget;
John McCalla8ae2222010-04-06 21:38:20 +0000175 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
204private:
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 Carlsson4742a9c2009-03-27 05:05:05 +0000225}
John McCall553c0792010-01-23 00:46:32 +0000226
John McCall97205142010-05-04 05:11:27 +0000227/// Checks whether one class might instantiate to the other.
228static 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 McCalla8ae2222010-04-06 21:38:20 +0000243/// 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.
248static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
249 const CXXRecordDecl *Target) {
250 assert(Derived->getCanonicalDecl() == Derived);
251 assert(Target->getCanonicalDecl() == Target);
John McCall69f75862010-03-24 09:04:37 +0000252
John McCalla8ae2222010-04-06 21:38:20 +0000253 if (Derived == Target) return AR_accessible;
John McCall69f75862010-03-24 09:04:37 +0000254
John McCall97205142010-05-04 05:11:27 +0000255 bool CheckDependent = Derived->isDependentContext();
256 if (CheckDependent && MightInstantiateTo(Derived, Target))
257 return AR_dependent;
258
John McCalla8ae2222010-04-06 21:38:20 +0000259 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 McCall97205142010-05-04 05:11:27 +0000271 } else if (const InjectedClassNameType *IT
272 = T->getAs<InjectedClassNameType>()) {
273 RD = IT->getDecl();
John McCalla8ae2222010-04-06 21:38:20 +0000274 } else {
John McCalla8ae2222010-04-06 21:38:20 +0000275 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 McCall97205142010-05-04 05:11:27 +0000282 if (CheckDependent && MightInstantiateTo(RD, Target))
283 OnFailure = AR_dependent;
284
John McCalla8ae2222010-04-06 21:38:20 +0000285 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 McCall5b0829a2010-02-10 09:31:12 +0000295}
296
John McCalla8ae2222010-04-06 21:38:20 +0000297
John McCallc62bb642010-03-24 05:22:00 +0000298static 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'.
318static 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
329static 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
369static bool MightInstantiateTo(Sema &S,
370 FunctionTemplateDecl *Context,
371 FunctionTemplateDecl *Friend) {
372 return MightInstantiateTo(S,
373 Context->getTemplatedDecl(),
374 Friend->getTemplatedDecl());
375}
376
John McCalla8ae2222010-04-06 21:38:20 +0000377static AccessResult MatchesFriend(Sema &S,
378 const EffectiveContext &EC,
379 const CXXRecordDecl *Friend) {
John McCall39e82882010-03-17 20:01:29 +0000380 if (EC.includesClass(Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000381 return AR_accessible;
John McCall39e82882010-03-17 20:01:29 +0000382
John McCallc62bb642010-03-24 05:22:00 +0000383 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 McCalla8ae2222010-04-06 21:38:20 +0000392 return AR_dependent;
John McCallc62bb642010-03-24 05:22:00 +0000393 }
394 }
395
John McCalla8ae2222010-04-06 21:38:20 +0000396 return AR_inaccessible;
John McCall39e82882010-03-17 20:01:29 +0000397}
398
John McCalla8ae2222010-04-06 21:38:20 +0000399static AccessResult MatchesFriend(Sema &S,
400 const EffectiveContext &EC,
401 CanQualType Friend) {
John McCallc62bb642010-03-24 05:22:00 +0000402 if (const RecordType *RT = Friend->getAs<RecordType>())
403 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCall39e82882010-03-17 20:01:29 +0000404
John McCallc62bb642010-03-24 05:22:00 +0000405 // TODO: we can do better than this
406 if (Friend->isDependentType())
John McCalla8ae2222010-04-06 21:38:20 +0000407 return AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000408
John McCalla8ae2222010-04-06 21:38:20 +0000409 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000410}
411
412/// Determines whether the given friend class template matches
413/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000414static AccessResult MatchesFriend(Sema &S,
415 const EffectiveContext &EC,
416 ClassTemplateDecl *Friend) {
417 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000418
John McCall598b4402010-03-25 06:39:04 +0000419 // Check whether the friend is the template of a class in the
420 // context chain.
John McCallc62bb642010-03-24 05:22:00 +0000421 for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator
422 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
423 CXXRecordDecl *Record = *I;
424
John McCall598b4402010-03-25 06:39:04 +0000425 // Figure out whether the current class has a template:
John McCallc62bb642010-03-24 05:22:00 +0000426 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 McCalla8ae2222010-04-06 21:38:20 +0000441 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000442
John McCall598b4402010-03-25 06:39:04 +0000443 // If the context isn't dependent, it can't be a dependent match.
444 if (!EC.isDependent())
445 continue;
446
John McCallc62bb642010-03-24 05:22:00 +0000447 // 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 McCalla8ae2222010-04-06 21:38:20 +0000459 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000460 }
461
John McCallc62bb642010-03-24 05:22:00 +0000462 return OnFailure;
463}
464
465/// Determines whether the given friend function matches anything in
466/// the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000467static AccessResult MatchesFriend(Sema &S,
468 const EffectiveContext &EC,
469 FunctionDecl *Friend) {
470 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000471
John McCall3dc81f72010-03-27 06:55:49 +0000472 for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
473 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
474 if (Friend == *I)
John McCalla8ae2222010-04-06 21:38:20 +0000475 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000476
John McCall3dc81f72010-03-27 06:55:49 +0000477 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000478 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000479 }
John McCallc62bb642010-03-24 05:22:00 +0000480
John McCall3dc81f72010-03-27 06:55:49 +0000481 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000482}
483
484/// Determines whether the given friend function template matches
485/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000486static AccessResult MatchesFriend(Sema &S,
487 const EffectiveContext &EC,
488 FunctionTemplateDecl *Friend) {
489 if (EC.Functions.empty()) return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000490
John McCalla8ae2222010-04-06 21:38:20 +0000491 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000492
John McCall3dc81f72010-03-27 06:55:49 +0000493 for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
494 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCallc62bb642010-03-24 05:22:00 +0000495
John McCall3dc81f72010-03-27 06:55:49 +0000496 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
497 if (!FTD)
498 FTD = (*I)->getDescribedFunctionTemplate();
499 if (!FTD)
500 continue;
John McCallc62bb642010-03-24 05:22:00 +0000501
John McCall3dc81f72010-03-27 06:55:49 +0000502 FTD = FTD->getCanonicalDecl();
503
504 if (Friend == FTD)
John McCalla8ae2222010-04-06 21:38:20 +0000505 return AR_accessible;
John McCall3dc81f72010-03-27 06:55:49 +0000506
507 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000508 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000509 }
510
511 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000512}
513
514/// Determines whether the given friend declaration matches anything
515/// in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000516static AccessResult MatchesFriend(Sema &S,
517 const EffectiveContext &EC,
518 FriendDecl *FriendD) {
John McCall2c2eb122010-10-16 06:59:13 +0000519 // Whitelist accesses if there's an invalid or unsupported friend
520 // declaration.
521 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCallde3fd222010-10-12 23:13:28 +0000522 return AR_accessible;
523
John McCall15ad0962010-03-25 18:04:51 +0000524 if (TypeSourceInfo *T = FriendD->getFriendType())
525 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCallc62bb642010-03-24 05:22:00 +0000526
527 NamedDecl *Friend
528 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCall39e82882010-03-17 20:01:29 +0000529
530 // FIXME: declarations with dependent or templated scope.
531
John McCallc62bb642010-03-24 05:22:00 +0000532 if (isa<ClassTemplateDecl>(Friend))
533 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000534
John McCallc62bb642010-03-24 05:22:00 +0000535 if (isa<FunctionTemplateDecl>(Friend))
536 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000537
John McCallc62bb642010-03-24 05:22:00 +0000538 if (isa<CXXRecordDecl>(Friend))
539 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000540
John McCallc62bb642010-03-24 05:22:00 +0000541 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
542 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000543}
544
John McCalla8ae2222010-04-06 21:38:20 +0000545static AccessResult GetFriendKind(Sema &S,
546 const EffectiveContext &EC,
547 const CXXRecordDecl *Class) {
548 AccessResult OnFailure = AR_inaccessible;
John McCallfb803d72010-03-17 04:58:56 +0000549
John McCall16927f62010-03-12 01:19:31 +0000550 // Okay, check friends.
551 for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
552 E = Class->friend_end(); I != E; ++I) {
553 FriendDecl *Friend = *I;
554
John McCall39e82882010-03-17 20:01:29 +0000555 switch (MatchesFriend(S, EC, Friend)) {
John McCalla8ae2222010-04-06 21:38:20 +0000556 case AR_accessible:
557 return AR_accessible;
John McCall16927f62010-03-12 01:19:31 +0000558
John McCalla8ae2222010-04-06 21:38:20 +0000559 case AR_inaccessible:
560 continue;
561
562 case AR_dependent:
563 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000564 break;
John McCall16927f62010-03-12 01:19:31 +0000565 }
John McCall16927f62010-03-12 01:19:31 +0000566 }
567
568 // That's it, give up.
John McCallfb803d72010-03-17 04:58:56 +0000569 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000570}
571
John McCall96329672010-08-28 07:56:00 +0000572namespace {
573
574/// A helper class for checking for a friend which will grant access
575/// to a protected instance member.
576struct ProtectedFriendContext {
577 Sema &S;
578 const EffectiveContext &EC;
579 const CXXRecordDecl *NamingClass;
580 bool CheckDependent;
581 bool EverDependent;
582
583 /// The path down to the current base class.
584 llvm::SmallVector<const CXXRecordDecl*, 20> CurPath;
585
586 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
587 const CXXRecordDecl *InstanceContext,
588 const CXXRecordDecl *NamingClass)
589 : S(S), EC(EC), NamingClass(NamingClass),
590 CheckDependent(InstanceContext->isDependentContext() ||
591 NamingClass->isDependentContext()),
592 EverDependent(false) {}
593
John McCall1177ff12010-08-28 08:47:21 +0000594 /// Check classes in the current path for friendship, starting at
595 /// the given index.
596 bool checkFriendshipAlongPath(unsigned I) {
597 assert(I < CurPath.size());
598 for (unsigned E = CurPath.size(); I != E; ++I) {
599 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall96329672010-08-28 07:56:00 +0000600 case AR_accessible: return true;
601 case AR_inaccessible: continue;
602 case AR_dependent: EverDependent = true; continue;
603 }
604 }
605 return false;
606 }
607
608 /// Perform a search starting at the given class.
John McCall1177ff12010-08-28 08:47:21 +0000609 ///
610 /// PrivateDepth is the index of the last (least derived) class
611 /// along the current path such that a notional public member of
612 /// the final class in the path would have access in that class.
613 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall96329672010-08-28 07:56:00 +0000614 // If we ever reach the naming class, check the current path for
615 // friendship. We can also stop recursing because we obviously
616 // won't find the naming class there again.
John McCall1177ff12010-08-28 08:47:21 +0000617 if (Cur == NamingClass)
618 return checkFriendshipAlongPath(PrivateDepth);
John McCall96329672010-08-28 07:56:00 +0000619
620 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
621 EverDependent = true;
622
623 // Recurse into the base classes.
624 for (CXXRecordDecl::base_class_const_iterator
625 I = Cur->bases_begin(), E = Cur->bases_end(); I != E; ++I) {
626
John McCall1177ff12010-08-28 08:47:21 +0000627 // If this is private inheritance, then a public member of the
628 // base will not have any access in classes derived from Cur.
629 unsigned BasePrivateDepth = PrivateDepth;
630 if (I->getAccessSpecifier() == AS_private)
631 BasePrivateDepth = CurPath.size() - 1;
John McCall96329672010-08-28 07:56:00 +0000632
633 const CXXRecordDecl *RD;
634
635 QualType T = I->getType();
636 if (const RecordType *RT = T->getAs<RecordType>()) {
637 RD = cast<CXXRecordDecl>(RT->getDecl());
638 } else if (const InjectedClassNameType *IT
639 = T->getAs<InjectedClassNameType>()) {
640 RD = IT->getDecl();
641 } else {
642 assert(T->isDependentType() && "non-dependent base wasn't a record?");
643 EverDependent = true;
644 continue;
645 }
646
647 // Recurse. We don't need to clean up if this returns true.
John McCall1177ff12010-08-28 08:47:21 +0000648 CurPath.push_back(RD);
649 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
650 return true;
651 CurPath.pop_back();
John McCall96329672010-08-28 07:56:00 +0000652 }
653
John McCall96329672010-08-28 07:56:00 +0000654 return false;
655 }
John McCall1177ff12010-08-28 08:47:21 +0000656
657 bool findFriendship(const CXXRecordDecl *Cur) {
658 assert(CurPath.empty());
659 CurPath.push_back(Cur);
660 return findFriendship(Cur, 0);
661 }
John McCall96329672010-08-28 07:56:00 +0000662};
663}
664
665/// Search for a class P that EC is a friend of, under the constraint
666/// InstanceContext <= P <= NamingClass
667/// and with the additional restriction that a protected member of
668/// NamingClass would have some natural access in P.
669///
670/// That second condition isn't actually quite right: the condition in
671/// the standard is whether the target would have some natural access
672/// in P. The difference is that the target might be more accessible
673/// along some path not passing through NamingClass. Allowing that
674/// introduces two problems:
675/// - It breaks encapsulation because you can suddenly access a
676/// forbidden base class's members by subclassing it elsewhere.
677/// - It makes access substantially harder to compute because it
678/// breaks the hill-climbing algorithm: knowing that the target is
679/// accessible in some base class would no longer let you change
680/// the question solely to whether the base class is accessible,
681/// because the original target might have been more accessible
682/// because of crazy subclassing.
683/// So we don't implement that.
684static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
685 const CXXRecordDecl *InstanceContext,
686 const CXXRecordDecl *NamingClass) {
687 assert(InstanceContext->getCanonicalDecl() == InstanceContext);
688 assert(NamingClass->getCanonicalDecl() == NamingClass);
689
690 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
691 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
692 if (PRC.EverDependent) return AR_dependent;
693 return AR_inaccessible;
694}
695
John McCalla8ae2222010-04-06 21:38:20 +0000696static AccessResult HasAccess(Sema &S,
697 const EffectiveContext &EC,
698 const CXXRecordDecl *NamingClass,
699 AccessSpecifier Access,
700 const AccessTarget &Target) {
John McCalld79b4d82010-04-02 00:03:43 +0000701 assert(NamingClass->getCanonicalDecl() == NamingClass &&
702 "declaration should be canonicalized before being passed here");
703
John McCalla8ae2222010-04-06 21:38:20 +0000704 if (Access == AS_public) return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000705 assert(Access == AS_private || Access == AS_protected);
706
John McCalla8ae2222010-04-06 21:38:20 +0000707 AccessResult OnFailure = AR_inaccessible;
708
John McCalld79b4d82010-04-02 00:03:43 +0000709 for (EffectiveContext::record_iterator
710 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
711 // All the declarations in EC have been canonicalized, so pointer
712 // equality from this point on will work fine.
713 const CXXRecordDecl *ECRecord = *I;
714
715 // [B2] and [M2]
John McCalla8ae2222010-04-06 21:38:20 +0000716 if (Access == AS_private) {
717 if (ECRecord == NamingClass)
718 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000719
John McCall97205142010-05-04 05:11:27 +0000720 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
721 OnFailure = AR_dependent;
722
John McCalld79b4d82010-04-02 00:03:43 +0000723 // [B3] and [M3]
John McCalla8ae2222010-04-06 21:38:20 +0000724 } else {
725 assert(Access == AS_protected);
726 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
727 case AR_accessible: break;
728 case AR_inaccessible: continue;
729 case AR_dependent: OnFailure = AR_dependent; continue;
730 }
731
732 if (!Target.hasInstanceContext())
733 return AR_accessible;
734
735 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
736 if (!InstanceContext) {
737 OnFailure = AR_dependent;
738 continue;
739 }
740
741 // C++ [class.protected]p1:
742 // An additional access check beyond those described earlier in
743 // [class.access] is applied when a non-static data member or
744 // non-static member function is a protected member of its naming
745 // class. As described earlier, access to a protected member is
746 // granted because the reference occurs in a friend or member of
747 // some class C. If the access is to form a pointer to member,
748 // the nested-name-specifier shall name C or a class derived from
749 // C. All other accesses involve a (possibly implicit) object
750 // expression. In this case, the class of the object expression
751 // shall be C or a class derived from C.
752 //
753 // We interpret this as a restriction on [M3]. Most of the
754 // conditions are encoded by not having any instance context.
755 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
756 case AR_accessible: return AR_accessible;
757 case AR_inaccessible: continue;
758 case AR_dependent: OnFailure = AR_dependent; continue;
759 }
760 }
John McCalld79b4d82010-04-02 00:03:43 +0000761 }
762
John McCall96329672010-08-28 07:56:00 +0000763 // [M3] and [B3] say that, if the target is protected in N, we grant
764 // access if the access occurs in a friend or member of some class P
765 // that's a subclass of N and where the target has some natural
766 // access in P. The 'member' aspect is easy to handle because P
767 // would necessarily be one of the effective-context records, and we
768 // address that above. The 'friend' aspect is completely ridiculous
769 // to implement because there are no restrictions at all on P
770 // *unless* the [class.protected] restriction applies. If it does,
771 // however, we should ignore whether the naming class is a friend,
772 // and instead rely on whether any potential P is a friend.
John McCalla8ae2222010-04-06 21:38:20 +0000773 if (Access == AS_protected && Target.hasInstanceContext()) {
774 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
775 if (!InstanceContext) return AR_dependent;
John McCall96329672010-08-28 07:56:00 +0000776 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
777 case AR_accessible: return AR_accessible;
John McCalla8ae2222010-04-06 21:38:20 +0000778 case AR_inaccessible: return OnFailure;
779 case AR_dependent: return AR_dependent;
780 }
John McCallc6af8f42010-08-28 08:10:32 +0000781 llvm_unreachable("impossible friendship kind");
John McCalla8ae2222010-04-06 21:38:20 +0000782 }
783
784 switch (GetFriendKind(S, EC, NamingClass)) {
785 case AR_accessible: return AR_accessible;
786 case AR_inaccessible: return OnFailure;
787 case AR_dependent: return AR_dependent;
788 }
789
790 // Silence bogus warnings
791 llvm_unreachable("impossible friendship kind");
792 return OnFailure;
John McCalld79b4d82010-04-02 00:03:43 +0000793}
794
John McCall5b0829a2010-02-10 09:31:12 +0000795/// Finds the best path from the naming class to the declaring class,
796/// taking friend declarations into account.
797///
John McCalld79b4d82010-04-02 00:03:43 +0000798/// C++0x [class.access.base]p5:
799/// A member m is accessible at the point R when named in class N if
800/// [M1] m as a member of N is public, or
801/// [M2] m as a member of N is private, and R occurs in a member or
802/// friend of class N, or
803/// [M3] m as a member of N is protected, and R occurs in a member or
804/// friend of class N, or in a member or friend of a class P
805/// derived from N, where m as a member of P is public, private,
806/// or protected, or
807/// [M4] there exists a base class B of N that is accessible at R, and
808/// m is accessible at R when named in class B.
809///
810/// C++0x [class.access.base]p4:
811/// A base class B of N is accessible at R, if
812/// [B1] an invented public member of B would be a public member of N, or
813/// [B2] R occurs in a member or friend of class N, and an invented public
814/// member of B would be a private or protected member of N, or
815/// [B3] R occurs in a member or friend of a class P derived from N, and an
816/// invented public member of B would be a private or protected member
817/// of P, or
818/// [B4] there exists a class S such that B is a base class of S accessible
819/// at R and S is a base class of N accessible at R.
820///
821/// Along a single inheritance path we can restate both of these
822/// iteratively:
823///
824/// First, we note that M1-4 are equivalent to B1-4 if the member is
825/// treated as a notional base of its declaring class with inheritance
826/// access equivalent to the member's access. Therefore we need only
827/// ask whether a class B is accessible from a class N in context R.
828///
829/// Let B_1 .. B_n be the inheritance path in question (i.e. where
830/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
831/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
832/// closest accessible base in the path:
833/// Access(a, b) = (* access on the base specifier from a to b *)
834/// Merge(a, forbidden) = forbidden
835/// Merge(a, private) = forbidden
836/// Merge(a, b) = min(a,b)
837/// Accessible(c, forbidden) = false
838/// Accessible(c, private) = (R is c) || IsFriend(c, R)
839/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
840/// Accessible(c, public) = true
841/// ACAB(n) = public
842/// ACAB(i) =
843/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
844/// if Accessible(B_i, AccessToBase) then public else AccessToBase
845///
846/// B is an accessible base of N at R iff ACAB(1) = public.
847///
John McCalla8ae2222010-04-06 21:38:20 +0000848/// \param FinalAccess the access of the "final step", or AS_public if
John McCalla332b952010-03-18 23:49:19 +0000849/// there is no final step.
John McCall5b0829a2010-02-10 09:31:12 +0000850/// \return null if friendship is dependent
851static CXXBasePath *FindBestPath(Sema &S,
852 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000853 AccessTarget &Target,
John McCalla332b952010-03-18 23:49:19 +0000854 AccessSpecifier FinalAccess,
John McCall5b0829a2010-02-10 09:31:12 +0000855 CXXBasePaths &Paths) {
856 // Derive the paths to the desired base.
John McCalla8ae2222010-04-06 21:38:20 +0000857 const CXXRecordDecl *Derived = Target.getNamingClass();
858 const CXXRecordDecl *Base = Target.getDeclaringClass();
859
860 // FIXME: fail correctly when there are dependent paths.
861 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
862 Paths);
John McCall5b0829a2010-02-10 09:31:12 +0000863 assert(isDerived && "derived class not actually derived from base");
864 (void) isDerived;
865
866 CXXBasePath *BestPath = 0;
867
John McCalla332b952010-03-18 23:49:19 +0000868 assert(FinalAccess != AS_none && "forbidden access after declaring class");
869
John McCallc62bb642010-03-24 05:22:00 +0000870 bool AnyDependent = false;
871
John McCall5b0829a2010-02-10 09:31:12 +0000872 // Derive the friend-modified access along each path.
873 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
874 PI != PE; ++PI) {
John McCalla8ae2222010-04-06 21:38:20 +0000875 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +0000876
877 // Walk through the path backwards.
John McCalla332b952010-03-18 23:49:19 +0000878 AccessSpecifier PathAccess = FinalAccess;
John McCall5b0829a2010-02-10 09:31:12 +0000879 CXXBasePath::iterator I = PI->end(), E = PI->begin();
880 while (I != E) {
881 --I;
882
John McCalla332b952010-03-18 23:49:19 +0000883 assert(PathAccess != AS_none);
884
885 // If the declaration is a private member of a base class, there
886 // is no level of friendship in derived classes that can make it
887 // accessible.
888 if (PathAccess == AS_private) {
889 PathAccess = AS_none;
890 break;
891 }
892
John McCalla8ae2222010-04-06 21:38:20 +0000893 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
894
John McCall5b0829a2010-02-10 09:31:12 +0000895 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalld79b4d82010-04-02 00:03:43 +0000896 PathAccess = std::max(PathAccess, BaseAccess);
John McCalla8ae2222010-04-06 21:38:20 +0000897
898 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
899 case AR_inaccessible: break;
900 case AR_accessible:
901 PathAccess = AS_public;
902
903 // Future tests are not against members and so do not have
904 // instance context.
905 Target.suppressInstanceContext();
906 break;
907 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +0000908 AnyDependent = true;
909 goto Next;
John McCall5b0829a2010-02-10 09:31:12 +0000910 }
John McCall5b0829a2010-02-10 09:31:12 +0000911 }
912
913 // Note that we modify the path's Access field to the
914 // friend-modified access.
915 if (BestPath == 0 || PathAccess < BestPath->Access) {
916 BestPath = &*PI;
917 BestPath->Access = PathAccess;
John McCallc62bb642010-03-24 05:22:00 +0000918
919 // Short-circuit if we found a public path.
920 if (BestPath->Access == AS_public)
921 return BestPath;
John McCall5b0829a2010-02-10 09:31:12 +0000922 }
John McCallc62bb642010-03-24 05:22:00 +0000923
924 Next: ;
John McCall5b0829a2010-02-10 09:31:12 +0000925 }
926
John McCallc62bb642010-03-24 05:22:00 +0000927 assert((!BestPath || BestPath->Access != AS_public) &&
928 "fell out of loop with public path");
929
930 // We didn't find a public path, but at least one path was subject
931 // to dependent friendship, so delay the check.
932 if (AnyDependent)
933 return 0;
934
John McCall5b0829a2010-02-10 09:31:12 +0000935 return BestPath;
936}
937
John McCall417e7442010-09-03 04:56:05 +0000938/// Given that an entity has protected natural access, check whether
939/// access might be denied because of the protected member access
940/// restriction.
941///
942/// \return true if a note was emitted
943static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
944 AccessTarget &Target) {
945 // Only applies to instance accesses.
946 if (!Target.hasInstanceContext())
947 return false;
948 assert(Target.isMemberAccess());
949 NamedDecl *D = Target.getTargetDecl();
950
951 const CXXRecordDecl *DeclaringClass = Target.getDeclaringClass();
952 DeclaringClass = DeclaringClass->getCanonicalDecl();
953
954 for (EffectiveContext::record_iterator
955 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
956 const CXXRecordDecl *ECRecord = *I;
957 switch (IsDerivedFromInclusive(ECRecord, DeclaringClass)) {
958 case AR_accessible: break;
959 case AR_inaccessible: continue;
960 case AR_dependent: continue;
961 }
962
963 // The effective context is a subclass of the declaring class.
964 // If that class isn't a superclass of the instance context,
965 // then the [class.protected] restriction applies.
966
967 // To get this exactly right, this might need to be checked more
968 // holistically; it's not necessarily the case that gaining
969 // access here would grant us access overall.
970
971 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
972 assert(InstanceContext && "diagnosing dependent access");
973
974 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
975 case AR_accessible: continue;
976 case AR_dependent: continue;
977 case AR_inaccessible:
978 S.Diag(D->getLocation(), diag::note_access_protected_restricted)
979 << (InstanceContext != Target.getNamingClass()->getCanonicalDecl())
980 << S.Context.getTypeDeclType(InstanceContext)
981 << S.Context.getTypeDeclType(ECRecord);
982 return true;
983 }
984 }
985
986 return false;
987}
988
John McCall5b0829a2010-02-10 09:31:12 +0000989/// Diagnose the path which caused the given declaration or base class
990/// to become inaccessible.
991static void DiagnoseAccessPath(Sema &S,
992 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000993 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +0000994 AccessSpecifier Access = Entity.getAccess();
John McCalla8ae2222010-04-06 21:38:20 +0000995 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalld79b4d82010-04-02 00:03:43 +0000996 NamingClass = NamingClass->getCanonicalDecl();
997
John McCalla8ae2222010-04-06 21:38:20 +0000998 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
999 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCalld79b4d82010-04-02 00:03:43 +00001000
John McCall553c0792010-01-23 00:46:32 +00001001 // Easy case: the decl's natural access determined its path access.
John McCall5b0829a2010-02-10 09:31:12 +00001002 // We have to check against AS_private here in case Access is AS_none,
1003 // indicating a non-public member of a private base class.
John McCall5b0829a2010-02-10 09:31:12 +00001004 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
John McCalla8ae2222010-04-06 21:38:20 +00001005 switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
1006 case AR_inaccessible: {
John McCall417e7442010-09-03 04:56:05 +00001007 if (Access == AS_protected &&
1008 TryDiagnoseProtectedAccess(S, EC, Entity))
1009 return;
1010
John McCall5b0829a2010-02-10 09:31:12 +00001011 S.Diag(D->getLocation(), diag::note_access_natural)
1012 << (unsigned) (Access == AS_protected)
1013 << /*FIXME: not implicitly*/ 0;
1014 return;
1015 }
1016
John McCalla8ae2222010-04-06 21:38:20 +00001017 case AR_accessible: break;
John McCall5b0829a2010-02-10 09:31:12 +00001018
John McCalla8ae2222010-04-06 21:38:20 +00001019 case AR_dependent:
1020 llvm_unreachable("can't diagnose dependent access failures");
John McCall5b0829a2010-02-10 09:31:12 +00001021 return;
1022 }
1023 }
1024
1025 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001026 CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
John McCall5b0829a2010-02-10 09:31:12 +00001027
1028 CXXBasePath::iterator I = Path.end(), E = Path.begin();
1029 while (I != E) {
1030 --I;
1031
1032 const CXXBaseSpecifier *BS = I->Base;
1033 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
1034
1035 // If this is public inheritance, or the derived class is a friend,
1036 // skip this step.
1037 if (BaseAccess == AS_public)
1038 continue;
1039
1040 switch (GetFriendKind(S, EC, I->Class)) {
John McCalla8ae2222010-04-06 21:38:20 +00001041 case AR_accessible: continue;
1042 case AR_inaccessible: break;
1043 case AR_dependent:
1044 llvm_unreachable("can't diagnose dependent access failures");
John McCall5b0829a2010-02-10 09:31:12 +00001045 }
1046
1047 // Check whether this base specifier is the tighest point
1048 // constraining access. We have to check against AS_private for
1049 // the same reasons as above.
1050 if (BaseAccess == AS_private || BaseAccess >= Access) {
1051
1052 // We're constrained by inheritance, but we want to say
1053 // "declared private here" if we're diagnosing a hierarchy
1054 // conversion and this is the final step.
1055 unsigned diagnostic;
1056 if (D) diagnostic = diag::note_access_constrained_by_path;
1057 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
1058 else diagnostic = diag::note_access_constrained_by_path;
1059
1060 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
1061 << BS->getSourceRange()
1062 << (BaseAccess == AS_protected)
1063 << (BS->getAccessSpecifierAsWritten() == AS_none);
Douglas Gregored2540d2010-05-28 04:34:55 +00001064
1065 if (D)
1066 S.Diag(D->getLocation(), diag::note_field_decl);
1067
John McCall5b0829a2010-02-10 09:31:12 +00001068 return;
1069 }
1070 }
1071
1072 llvm_unreachable("access not apparently constrained by path");
1073}
1074
John McCall1064d7e2010-03-16 05:22:47 +00001075static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall5b0829a2010-02-10 09:31:12 +00001076 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +00001077 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001078 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalla8ae2222010-04-06 21:38:20 +00001079 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
1080 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
John McCalld79b4d82010-04-02 00:03:43 +00001081
1082 S.Diag(Loc, Entity.getDiag())
1083 << (Entity.getAccess() == AS_protected)
1084 << (D ? D->getDeclName() : DeclarationName())
1085 << S.Context.getTypeDeclType(NamingClass)
1086 << S.Context.getTypeDeclType(DeclaringClass);
1087 DiagnoseAccessPath(S, EC, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001088}
1089
John McCalld79b4d82010-04-02 00:03:43 +00001090/// Determines whether the accessed entity is accessible. Public members
1091/// have been weeded out by this point.
John McCalla8ae2222010-04-06 21:38:20 +00001092static AccessResult IsAccessible(Sema &S,
1093 const EffectiveContext &EC,
1094 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001095 // Determine the actual naming class.
1096 CXXRecordDecl *NamingClass = Entity.getNamingClass();
1097 while (NamingClass->isAnonymousStructOrUnion())
1098 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
1099 NamingClass = NamingClass->getCanonicalDecl();
John McCall5b0829a2010-02-10 09:31:12 +00001100
John McCalld79b4d82010-04-02 00:03:43 +00001101 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1102 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1103
1104 // Before we try to recalculate access paths, try to white-list
1105 // accesses which just trade in on the final step, i.e. accesses
1106 // which don't require [M4] or [B4]. These are by far the most
John McCalla8ae2222010-04-06 21:38:20 +00001107 // common forms of privileged access.
John McCalld79b4d82010-04-02 00:03:43 +00001108 if (UnprivilegedAccess != AS_none) {
John McCalla8ae2222010-04-06 21:38:20 +00001109 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1110 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +00001111 // This is actually an interesting policy decision. We don't
1112 // *have* to delay immediately here: we can do the full access
1113 // calculation in the hope that friendship on some intermediate
1114 // class will make the declaration accessible non-dependently.
1115 // But that's not cheap, and odds are very good (note: assertion
1116 // made without data) that the friend declaration will determine
1117 // access.
John McCalla8ae2222010-04-06 21:38:20 +00001118 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001119
John McCalla8ae2222010-04-06 21:38:20 +00001120 case AR_accessible: return AR_accessible;
1121 case AR_inaccessible: break;
John McCalld79b4d82010-04-02 00:03:43 +00001122 }
1123 }
1124
John McCalla8ae2222010-04-06 21:38:20 +00001125 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +00001126
John McCalld79b4d82010-04-02 00:03:43 +00001127 // We lower member accesses to base accesses by pretending that the
1128 // member is a base class of its declaring class.
1129 AccessSpecifier FinalAccess;
1130
John McCall5b0829a2010-02-10 09:31:12 +00001131 if (Entity.isMemberAccess()) {
John McCalld79b4d82010-04-02 00:03:43 +00001132 // Determine if the declaration is accessible from EC when named
1133 // in its declaring class.
John McCall5b0829a2010-02-10 09:31:12 +00001134 NamedDecl *Target = Entity.getTargetDecl();
John McCalla8ae2222010-04-06 21:38:20 +00001135 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall5b0829a2010-02-10 09:31:12 +00001136
John McCalld79b4d82010-04-02 00:03:43 +00001137 FinalAccess = Target->getAccess();
John McCalla8ae2222010-04-06 21:38:20 +00001138 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1139 case AR_accessible:
1140 FinalAccess = AS_public;
1141 break;
1142 case AR_inaccessible: break;
1143 case AR_dependent: return AR_dependent; // see above
John McCall5b0829a2010-02-10 09:31:12 +00001144 }
1145
John McCalld79b4d82010-04-02 00:03:43 +00001146 if (DeclaringClass == NamingClass)
John McCalla8ae2222010-04-06 21:38:20 +00001147 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
1148
1149 Entity.suppressInstanceContext();
John McCalld79b4d82010-04-02 00:03:43 +00001150 } else {
1151 FinalAccess = AS_public;
John McCall5b0829a2010-02-10 09:31:12 +00001152 }
1153
John McCalla8ae2222010-04-06 21:38:20 +00001154 assert(Entity.getDeclaringClass() != NamingClass);
John McCall5b0829a2010-02-10 09:31:12 +00001155
1156 // Append the declaration's access if applicable.
1157 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001158 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCallc62bb642010-03-24 05:22:00 +00001159 if (!Path)
John McCalla8ae2222010-04-06 21:38:20 +00001160 return AR_dependent;
John McCall553c0792010-01-23 00:46:32 +00001161
John McCalld79b4d82010-04-02 00:03:43 +00001162 assert(Path->Access <= UnprivilegedAccess &&
1163 "access along best path worse than direct?");
1164 if (Path->Access == AS_public)
John McCalla8ae2222010-04-06 21:38:20 +00001165 return AR_accessible;
1166 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +00001167}
1168
John McCalla8ae2222010-04-06 21:38:20 +00001169static void DelayDependentAccess(Sema &S,
1170 const EffectiveContext &EC,
1171 SourceLocation Loc,
1172 const AccessTarget &Entity) {
John McCallc62bb642010-03-24 05:22:00 +00001173 assert(EC.isDependent() && "delaying non-dependent access");
John McCall816d75b2010-03-24 07:46:06 +00001174 DeclContext *DC = EC.getInnerContext();
John McCallc62bb642010-03-24 05:22:00 +00001175 assert(DC->isDependentContext() && "delaying non-dependent access");
1176 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1177 Loc,
1178 Entity.isMemberAccess(),
1179 Entity.getAccess(),
1180 Entity.getTargetDecl(),
1181 Entity.getNamingClass(),
John McCalla8ae2222010-04-06 21:38:20 +00001182 Entity.getBaseObjectType(),
John McCallc62bb642010-03-24 05:22:00 +00001183 Entity.getDiag());
John McCall553c0792010-01-23 00:46:32 +00001184}
1185
John McCall5b0829a2010-02-10 09:31:12 +00001186/// Checks access to an entity from the given effective context.
John McCalla8ae2222010-04-06 21:38:20 +00001187static AccessResult CheckEffectiveAccess(Sema &S,
1188 const EffectiveContext &EC,
1189 SourceLocation Loc,
1190 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001191 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall553c0792010-01-23 00:46:32 +00001192
John McCalld79b4d82010-04-02 00:03:43 +00001193 switch (IsAccessible(S, EC, Entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001194 case AR_dependent:
1195 DelayDependentAccess(S, EC, Loc, Entity);
1196 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001197
John McCalla8ae2222010-04-06 21:38:20 +00001198 case AR_inaccessible:
John McCalld79b4d82010-04-02 00:03:43 +00001199 if (!Entity.isQuiet())
1200 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCalla8ae2222010-04-06 21:38:20 +00001201 return AR_inaccessible;
John McCalld79b4d82010-04-02 00:03:43 +00001202
John McCalla8ae2222010-04-06 21:38:20 +00001203 case AR_accessible:
1204 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +00001205 }
1206
John McCalla8ae2222010-04-06 21:38:20 +00001207 // silence unnecessary warning
1208 llvm_unreachable("invalid access result");
1209 return AR_accessible;
John McCall5b0829a2010-02-10 09:31:12 +00001210}
John McCall553c0792010-01-23 00:46:32 +00001211
John McCall5b0829a2010-02-10 09:31:12 +00001212static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCalla8ae2222010-04-06 21:38:20 +00001213 AccessTarget &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +00001214 // If the access path is public, it's accessible everywhere.
1215 if (Entity.getAccess() == AS_public)
1216 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001217
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001218 if (S.SuppressAccessChecking)
1219 return Sema::AR_accessible;
1220
John McCall5b0829a2010-02-10 09:31:12 +00001221 // If we're currently parsing a top-level declaration, delay
1222 // diagnostics. This is the only case where parsing a declaration
1223 // can actually change our effective context for the purposes of
1224 // access control.
1225 if (S.CurContext->isFileContext() && S.ParsingDeclDepth) {
John McCall5b0829a2010-02-10 09:31:12 +00001226 S.DelayedDiagnostics.push_back(
John McCallb45a1e72010-08-26 02:13:20 +00001227 DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall5b0829a2010-02-10 09:31:12 +00001228 return Sema::AR_delayed;
John McCall553c0792010-01-23 00:46:32 +00001229 }
1230
John McCalla8ae2222010-04-06 21:38:20 +00001231 EffectiveContext EC(S.CurContext);
1232 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1233 case AR_accessible: return Sema::AR_accessible;
1234 case AR_inaccessible: return Sema::AR_inaccessible;
1235 case AR_dependent: return Sema::AR_dependent;
1236 }
1237 llvm_unreachable("falling off end");
1238 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001239}
1240
John McCall86121512010-01-27 03:50:35 +00001241void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00001242 // Pretend we did this from the context of the newly-parsed
Chandler Carruthaad30072010-04-18 08:23:21 +00001243 // declaration. If that declaration itself forms a declaration context,
1244 // include it in the effective context so that parameters and return types of
1245 // befriended functions have that function's access priveledges.
1246 DeclContext *DC = Ctx->getDeclContext();
1247 if (isa<FunctionDecl>(Ctx))
1248 DC = cast<DeclContext>(Ctx);
1249 else if (FunctionTemplateDecl *FnTpl = dyn_cast<FunctionTemplateDecl>(Ctx))
1250 DC = cast<DeclContext>(FnTpl->getTemplatedDecl());
1251 EffectiveContext EC(DC);
John McCall86121512010-01-27 03:50:35 +00001252
John McCalla8ae2222010-04-06 21:38:20 +00001253 AccessTarget Target(DD.getAccessData());
1254
1255 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall86121512010-01-27 03:50:35 +00001256 DD.Triggered = true;
1257}
1258
John McCallc62bb642010-03-24 05:22:00 +00001259void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1260 const MultiLevelTemplateArgumentList &TemplateArgs) {
1261 SourceLocation Loc = DD.getAccessLoc();
1262 AccessSpecifier Access = DD.getAccess();
1263
1264 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1265 TemplateArgs);
1266 if (!NamingD) return;
1267 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1268 TemplateArgs);
1269 if (!TargetD) return;
1270
1271 if (DD.isAccessToMember()) {
John McCalla8ae2222010-04-06 21:38:20 +00001272 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1273 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1274 QualType BaseObjectType = DD.getAccessBaseObjectType();
1275 if (!BaseObjectType.isNull()) {
1276 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1277 DeclarationName());
1278 if (BaseObjectType.isNull()) return;
1279 }
1280
1281 AccessTarget Entity(Context,
1282 AccessTarget::Member,
1283 NamingClass,
1284 DeclAccessPair::make(TargetDecl, Access),
1285 BaseObjectType);
John McCallc62bb642010-03-24 05:22:00 +00001286 Entity.setDiag(DD.getDiagnostic());
1287 CheckAccess(*this, Loc, Entity);
1288 } else {
John McCalla8ae2222010-04-06 21:38:20 +00001289 AccessTarget Entity(Context,
1290 AccessTarget::Base,
1291 cast<CXXRecordDecl>(TargetD),
1292 cast<CXXRecordDecl>(NamingD),
1293 Access);
John McCallc62bb642010-03-24 05:22:00 +00001294 Entity.setDiag(DD.getDiagnostic());
1295 CheckAccess(*this, Loc, Entity);
1296 }
1297}
1298
John McCall5b0829a2010-02-10 09:31:12 +00001299Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001300 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001301 if (!getLangOptions().AccessControl ||
1302 !E->getNamingClass() ||
John McCalla0296f72010-03-19 07:35:19 +00001303 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001304 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001305
John McCalla8ae2222010-04-06 21:38:20 +00001306 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1307 Found, QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001308 Entity.setDiag(diag::err_access) << E->getSourceRange();
1309
1310 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001311}
1312
1313/// Perform access-control checking on a previously-unresolved member
1314/// access which has now been resolved to a member.
John McCall5b0829a2010-02-10 09:31:12 +00001315Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001316 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001317 if (!getLangOptions().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001318 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001319 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001320
John McCalla8ae2222010-04-06 21:38:20 +00001321 QualType BaseType = E->getBaseType();
1322 if (E->isArrow())
1323 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1324
1325 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1326 Found, BaseType);
John McCall1064d7e2010-03-16 05:22:47 +00001327 Entity.setDiag(diag::err_access) << E->getSourceRange();
1328
1329 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001330}
1331
John McCall5b0829a2010-02-10 09:31:12 +00001332Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +00001333 CXXDestructorDecl *Dtor,
1334 const PartialDiagnostic &PDiag) {
John McCall6781b052010-02-02 08:45:54 +00001335 if (!getLangOptions().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +00001336 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001337
John McCall1064d7e2010-03-16 05:22:47 +00001338 // There's never a path involved when checking implicit destructor access.
John McCall6781b052010-02-02 08:45:54 +00001339 AccessSpecifier Access = Dtor->getAccess();
1340 if (Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001341 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001342
John McCall1064d7e2010-03-16 05:22:47 +00001343 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCalla8ae2222010-04-06 21:38:20 +00001344 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1345 DeclAccessPair::make(Dtor, Access),
1346 QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001347 Entity.setDiag(PDiag); // TODO: avoid copy
1348
1349 return CheckAccess(*this, Loc, Entity);
John McCall6781b052010-02-02 08:45:54 +00001350}
1351
John McCall760af172010-02-01 03:16:54 +00001352/// Checks access to a constructor.
John McCall5b0829a2010-02-10 09:31:12 +00001353Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001354 CXXConstructorDecl *Constructor,
1355 const InitializedEntity &Entity,
1356 AccessSpecifier Access,
1357 bool IsCopyBindingRefToTemp) {
John McCall1064d7e2010-03-16 05:22:47 +00001358 if (!getLangOptions().AccessControl ||
1359 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001360 return AR_accessible;
John McCall760af172010-02-01 03:16:54 +00001361
John McCall5b0829a2010-02-10 09:31:12 +00001362 CXXRecordDecl *NamingClass = Constructor->getParent();
Anders Carlssona01874b2010-04-21 18:47:17 +00001363 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1364 DeclAccessPair::make(Constructor, Access),
1365 QualType());
1366 switch (Entity.getKind()) {
1367 default:
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001368 AccessEntity.setDiag(IsCopyBindingRefToTemp
1369 ? diag::ext_rvalue_to_reference_access_ctor
1370 : diag::err_access_ctor);
Anders Carlssona01874b2010-04-21 18:47:17 +00001371 break;
John McCall1064d7e2010-03-16 05:22:47 +00001372
Anders Carlsson05bf0092010-04-22 05:40:53 +00001373 case InitializedEntity::EK_Base:
1374 AccessEntity.setDiag(PDiag(diag::err_access_base)
1375 << Entity.isInheritedVirtualBase()
1376 << Entity.getBaseSpecifier()->getType()
1377 << getSpecialMember(Constructor));
Anders Carlssona01874b2010-04-21 18:47:17 +00001378 break;
Anders Carlsson05bf0092010-04-22 05:40:53 +00001379
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001380 case InitializedEntity::EK_Member: {
1381 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Anders Carlsson11373b52010-04-23 03:41:35 +00001382 AccessEntity.setDiag(PDiag(diag::err_access_field)
1383 << Field->getType()
1384 << getSpecialMember(Constructor));
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001385 break;
1386 }
Anders Carlssona01874b2010-04-21 18:47:17 +00001387
Anders Carlsson43c64af2010-04-21 19:52:01 +00001388 }
1389
Anders Carlssona01874b2010-04-21 18:47:17 +00001390 return CheckAccess(*this, UseLoc, AccessEntity);
John McCall760af172010-02-01 03:16:54 +00001391}
1392
John McCallab8c2732010-03-16 06:11:48 +00001393/// Checks direct (i.e. non-inherited) access to an arbitrary class
1394/// member.
1395Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
1396 NamedDecl *Target,
1397 const PartialDiagnostic &Diag) {
1398 AccessSpecifier Access = Target->getAccess();
1399 if (!getLangOptions().AccessControl ||
1400 Access == AS_public)
1401 return AR_accessible;
1402
1403 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
John McCalla8ae2222010-04-06 21:38:20 +00001404 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1405 DeclAccessPair::make(Target, Access),
1406 QualType());
John McCallab8c2732010-03-16 06:11:48 +00001407 Entity.setDiag(Diag);
1408 return CheckAccess(*this, UseLoc, Entity);
1409}
1410
1411
John McCallfb6f5262010-03-18 08:19:33 +00001412/// Checks access to an overloaded operator new or delete.
1413Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1414 SourceRange PlacementRange,
1415 CXXRecordDecl *NamingClass,
John McCalla0296f72010-03-19 07:35:19 +00001416 DeclAccessPair Found) {
John McCallfb6f5262010-03-18 08:19:33 +00001417 if (!getLangOptions().AccessControl ||
1418 !NamingClass ||
John McCalla0296f72010-03-19 07:35:19 +00001419 Found.getAccess() == AS_public)
John McCallfb6f5262010-03-18 08:19:33 +00001420 return AR_accessible;
1421
John McCalla8ae2222010-04-06 21:38:20 +00001422 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1423 QualType());
John McCallfb6f5262010-03-18 08:19:33 +00001424 Entity.setDiag(diag::err_access)
1425 << PlacementRange;
1426
1427 return CheckAccess(*this, OpLoc, Entity);
1428}
1429
John McCall760af172010-02-01 03:16:54 +00001430/// Checks access to an overloaded member operator, including
1431/// conversion operators.
John McCall5b0829a2010-02-10 09:31:12 +00001432Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1433 Expr *ObjectExpr,
John McCall1064d7e2010-03-16 05:22:47 +00001434 Expr *ArgExpr,
John McCalla0296f72010-03-19 07:35:19 +00001435 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001436 if (!getLangOptions().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001437 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001438 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001439
1440 const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>();
1441 assert(RT && "found member operator but object expr not of record type");
1442 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1443
John McCalla8ae2222010-04-06 21:38:20 +00001444 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1445 ObjectExpr->getType());
John McCall1064d7e2010-03-16 05:22:47 +00001446 Entity.setDiag(diag::err_access)
1447 << ObjectExpr->getSourceRange()
1448 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1449
1450 return CheckAccess(*this, OpLoc, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001451}
John McCallb3a44002010-01-28 01:42:12 +00001452
John McCall16df1e52010-03-30 21:47:33 +00001453Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1454 DeclAccessPair Found) {
1455 if (!getLangOptions().AccessControl ||
John McCallb493d532010-03-30 22:20:00 +00001456 Found.getAccess() == AS_none ||
John McCall16df1e52010-03-30 21:47:33 +00001457 Found.getAccess() == AS_public)
1458 return AR_accessible;
1459
John McCall8d08b9b2010-08-27 09:08:28 +00001460 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCall8c12dc42010-04-22 18:44:12 +00001461 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall16df1e52010-03-30 21:47:33 +00001462
John McCalla8ae2222010-04-06 21:38:20 +00001463 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1464 Context.getTypeDeclType(NamingClass));
John McCall16df1e52010-03-30 21:47:33 +00001465 Entity.setDiag(diag::err_access)
1466 << Ovl->getSourceRange();
1467
1468 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1469}
1470
John McCall5b0829a2010-02-10 09:31:12 +00001471/// Checks access for a hierarchy conversion.
1472///
1473/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
1474/// or a derived-to-base conversion (false)
1475/// \param ForceCheck true if this check should be performed even if access
1476/// control is disabled; some things rely on this for semantics
1477/// \param ForceUnprivileged true if this check should proceed as if the
1478/// context had no special privileges
1479/// \param ADK controls the kind of diagnostics that are used
1480Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall5b0829a2010-02-10 09:31:12 +00001481 QualType Base,
1482 QualType Derived,
1483 const CXXBasePath &Path,
John McCall1064d7e2010-03-16 05:22:47 +00001484 unsigned DiagID,
John McCall5b0829a2010-02-10 09:31:12 +00001485 bool ForceCheck,
John McCall1064d7e2010-03-16 05:22:47 +00001486 bool ForceUnprivileged) {
John McCall5b0829a2010-02-10 09:31:12 +00001487 if (!ForceCheck && !getLangOptions().AccessControl)
1488 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001489
John McCall5b0829a2010-02-10 09:31:12 +00001490 if (Path.Access == AS_public)
1491 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001492
John McCall5b0829a2010-02-10 09:31:12 +00001493 CXXRecordDecl *BaseD, *DerivedD;
1494 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1495 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall1064d7e2010-03-16 05:22:47 +00001496
John McCalla8ae2222010-04-06 21:38:20 +00001497 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1498 Path.Access);
John McCall1064d7e2010-03-16 05:22:47 +00001499 if (DiagID)
1500 Entity.setDiag(DiagID) << Derived << Base;
John McCall5b0829a2010-02-10 09:31:12 +00001501
John McCalla8ae2222010-04-06 21:38:20 +00001502 if (ForceUnprivileged) {
1503 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1504 AccessLoc, Entity)) {
1505 case ::AR_accessible: return Sema::AR_accessible;
1506 case ::AR_inaccessible: return Sema::AR_inaccessible;
1507 case ::AR_dependent: return Sema::AR_dependent;
1508 }
1509 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1510 }
John McCall1064d7e2010-03-16 05:22:47 +00001511 return CheckAccess(*this, AccessLoc, Entity);
John McCallb3a44002010-01-28 01:42:12 +00001512}
1513
John McCall553c0792010-01-23 00:46:32 +00001514/// Checks access to all the declarations in the given result set.
John McCall5b0829a2010-02-10 09:31:12 +00001515void Sema::CheckLookupAccess(const LookupResult &R) {
1516 assert(getLangOptions().AccessControl
1517 && "performing access check without access control");
1518 assert(R.getNamingClass() && "performing access check without naming class");
1519
John McCall1064d7e2010-03-16 05:22:47 +00001520 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1521 if (I.getAccess() != AS_public) {
John McCalla8ae2222010-04-06 21:38:20 +00001522 AccessTarget Entity(Context, AccessedEntity::Member,
1523 R.getNamingClass(), I.getPair(),
1524 R.getBaseObjectType());
John McCall1064d7e2010-03-16 05:22:47 +00001525 Entity.setDiag(diag::err_access);
1526
1527 CheckAccess(*this, R.getNameLoc(), Entity);
1528 }
1529 }
John McCall553c0792010-01-23 00:46:32 +00001530}
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001531
1532void Sema::ActOnStartSuppressingAccessChecks() {
1533 assert(!SuppressAccessChecking &&
1534 "Tried to start access check suppression when already started.");
1535 SuppressAccessChecking = true;
1536}
1537
1538void Sema::ActOnStopSuppressingAccessChecks() {
1539 assert(SuppressAccessChecking &&
1540 "Tried to stop access check suprression when already stopped.");
1541 SuppressAccessChecking = false;
1542}