blob: e629f0fd35bfcc25842fb42067c00214cd46c0bb [file] [log] [blame]
Anders Carlsson29f006b2009-03-27 05:05:05 +00001//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
Anders Carlsson60d6b0d2009-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 Carlssonc60e8882009-03-27 04:54:36 +000013
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall9c3087b2010-08-26 02:13:20 +000015#include "clang/Sema/DelayedDiagnostic.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
17#include "clang/Sema/Lookup.h"
Anders Carlssonc4f1e872009-03-27 06:03:27 +000018#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000019#include "clang/AST/CXXInheritance.h"
20#include "clang/AST/DeclCXX.h"
John McCalld60e22e2010-03-12 01:19:31 +000021#include "clang/AST/DeclFriend.h"
John McCall0c01d182010-03-24 05:22:00 +000022#include "clang/AST/DependentDiagnostic.h"
John McCallc373d482010-01-27 01:50:18 +000023#include "clang/AST/ExprCXX.h"
24
Anders Carlssonc60e8882009-03-27 04:54:36 +000025using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000026using namespace sema;
Anders Carlssonc60e8882009-03-27 04:54:36 +000027
John McCall161755a2010-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 Carlsson29f006b2009-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 Stump1eb44332009-09-09 15:08:12 +000038bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
Anders Carlssonc60e8882009-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 Stump1eb44332009-09-09 15:08:12 +000046
Anders Carlssonc60e8882009-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 Stump1eb44332009-09-09 15:08:12 +000050 Diag(MemberDecl->getLocation(),
51 diag::err_class_redeclared_with_different_access)
Anders Carlssonc60e8882009-03-27 04:54:36 +000052 << MemberDecl << LexicalAS;
53 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
54 << PrevMemberDecl << PrevMemberDecl->getAccess();
John McCall44e067b2009-12-23 00:37:40 +000055
56 MemberDecl->setAccess(LexicalAS);
Anders Carlssonc60e8882009-03-27 04:54:36 +000057 return true;
58 }
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlssonc60e8882009-03-27 04:54:36 +000060 MemberDecl->setAccess(PrevMemberDecl->getAccess());
61 return false;
62}
Anders Carlsson29f006b2009-03-27 05:05:05 +000063
John McCall161755a2010-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 McCall6b2accb2010-02-10 09:31:12 +000078namespace {
79struct EffectiveContext {
John McCall2cc26752010-03-27 06:55:49 +000080 EffectiveContext() : Inner(0), Dependent(false) {}
Anders Carlssonc4f1e872009-03-27 06:03:27 +000081
John McCall7ad650f2010-03-24 07:46:06 +000082 explicit EffectiveContext(DeclContext *DC)
83 : Inner(DC),
84 Dependent(DC->isDependentContext()) {
John McCall0c01d182010-03-24 05:22:00 +000085
John McCall88b6c712010-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 McCall2cc26752010-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 McCall88b6c712010-03-17 04:58:56 +0000112 }
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000113 }
Sebastian Redl726212f2009-07-18 14:32:15 +0000114
John McCall0c01d182010-03-24 05:22:00 +0000115 bool isDependent() const { return Dependent; }
116
John McCall88b6c712010-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 McCall6b2accb2010-02-10 09:31:12 +0000121 }
122
John McCall7ad650f2010-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 McCall0c01d182010-03-24 05:22:00 +0000127 }
128
129 typedef llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
130
John McCall7ad650f2010-03-24 07:46:06 +0000131 DeclContext *Inner;
John McCall2cc26752010-03-27 06:55:49 +0000132 llvm::SmallVector<FunctionDecl*, 4> Functions;
John McCall88b6c712010-03-17 04:58:56 +0000133 llvm::SmallVector<CXXRecordDecl*, 4> Records;
John McCall0c01d182010-03-24 05:22:00 +0000134 bool Dependent;
John McCall6b2accb2010-02-10 09:31:12 +0000135};
John McCall161755a2010-04-06 21:38:20 +0000136
John McCall9c3087b2010-08-26 02:13:20 +0000137/// Like sema:;AccessedEntity, but kindly lets us scribble all over
John McCall161755a2010-04-06 21:38:20 +0000138/// it.
John McCall9c3087b2010-08-26 02:13:20 +0000139struct AccessTarget : public AccessedEntity {
140 AccessTarget(const AccessedEntity &Entity)
John McCall161755a2010-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 McCallc91cc662010-04-07 00:41:46 +0000174 friend struct AccessTarget;
John McCall161755a2010-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 Carlsson29f006b2009-03-27 05:05:05 +0000225}
John McCall92f88312010-01-23 00:46:32 +0000226
John McCall01ebd9d2010-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 McCall161755a2010-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 McCallc1b621d2010-03-24 09:04:37 +0000252
John McCall161755a2010-04-06 21:38:20 +0000253 if (Derived == Target) return AR_accessible;
John McCallc1b621d2010-03-24 09:04:37 +0000254
John McCall01ebd9d2010-05-04 05:11:27 +0000255 bool CheckDependent = Derived->isDependentContext();
256 if (CheckDependent && MightInstantiateTo(Derived, Target))
257 return AR_dependent;
258
John McCall161755a2010-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 McCall01ebd9d2010-05-04 05:11:27 +0000271 } else if (const InjectedClassNameType *IT
272 = T->getAs<InjectedClassNameType>()) {
273 RD = IT->getDecl();
John McCall161755a2010-04-06 21:38:20 +0000274 } else {
John McCall161755a2010-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 McCall01ebd9d2010-05-04 05:11:27 +0000282 if (CheckDependent && MightInstantiateTo(RD, Target))
283 OnFailure = AR_dependent;
284
John McCall161755a2010-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 McCall6b2accb2010-02-10 09:31:12 +0000295}
296
John McCall161755a2010-04-06 21:38:20 +0000297
John McCall0c01d182010-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 McCall161755a2010-04-06 21:38:20 +0000377static AccessResult MatchesFriend(Sema &S,
378 const EffectiveContext &EC,
379 const CXXRecordDecl *Friend) {
John McCalla742db02010-03-17 20:01:29 +0000380 if (EC.includesClass(Friend))
John McCall161755a2010-04-06 21:38:20 +0000381 return AR_accessible;
John McCalla742db02010-03-17 20:01:29 +0000382
John McCall0c01d182010-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 McCall161755a2010-04-06 21:38:20 +0000392 return AR_dependent;
John McCall0c01d182010-03-24 05:22:00 +0000393 }
394 }
395
John McCall161755a2010-04-06 21:38:20 +0000396 return AR_inaccessible;
John McCalla742db02010-03-17 20:01:29 +0000397}
398
John McCall161755a2010-04-06 21:38:20 +0000399static AccessResult MatchesFriend(Sema &S,
400 const EffectiveContext &EC,
401 CanQualType Friend) {
John McCall0c01d182010-03-24 05:22:00 +0000402 if (const RecordType *RT = Friend->getAs<RecordType>())
403 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCalla742db02010-03-17 20:01:29 +0000404
John McCall0c01d182010-03-24 05:22:00 +0000405 // TODO: we can do better than this
406 if (Friend->isDependentType())
John McCall161755a2010-04-06 21:38:20 +0000407 return AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000408
John McCall161755a2010-04-06 21:38:20 +0000409 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000410}
411
412/// Determines whether the given friend class template matches
413/// anything in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000414static AccessResult MatchesFriend(Sema &S,
415 const EffectiveContext &EC,
416 ClassTemplateDecl *Friend) {
417 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000418
John McCall93ba8572010-03-25 06:39:04 +0000419 // Check whether the friend is the template of a class in the
420 // context chain.
John McCall0c01d182010-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 McCall93ba8572010-03-25 06:39:04 +0000425 // Figure out whether the current class has a template:
John McCall0c01d182010-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 McCall161755a2010-04-06 21:38:20 +0000441 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000442
John McCall93ba8572010-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 McCall0c01d182010-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 McCall161755a2010-04-06 21:38:20 +0000459 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000460 }
461
John McCall0c01d182010-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 McCall161755a2010-04-06 21:38:20 +0000467static AccessResult MatchesFriend(Sema &S,
468 const EffectiveContext &EC,
469 FunctionDecl *Friend) {
470 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000471
John McCall2cc26752010-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 McCall161755a2010-04-06 21:38:20 +0000475 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000476
John McCall2cc26752010-03-27 06:55:49 +0000477 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCall161755a2010-04-06 21:38:20 +0000478 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000479 }
John McCall0c01d182010-03-24 05:22:00 +0000480
John McCall2cc26752010-03-27 06:55:49 +0000481 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000482}
483
484/// Determines whether the given friend function template matches
485/// anything in the effective context.
John McCall161755a2010-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 McCall0c01d182010-03-24 05:22:00 +0000490
John McCall161755a2010-04-06 21:38:20 +0000491 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000492
John McCall2cc26752010-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 McCall0c01d182010-03-24 05:22:00 +0000495
John McCall2cc26752010-03-27 06:55:49 +0000496 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
497 if (!FTD)
498 FTD = (*I)->getDescribedFunctionTemplate();
499 if (!FTD)
500 continue;
John McCall0c01d182010-03-24 05:22:00 +0000501
John McCall2cc26752010-03-27 06:55:49 +0000502 FTD = FTD->getCanonicalDecl();
503
504 if (Friend == FTD)
John McCall161755a2010-04-06 21:38:20 +0000505 return AR_accessible;
John McCall2cc26752010-03-27 06:55:49 +0000506
507 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCall161755a2010-04-06 21:38:20 +0000508 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000509 }
510
511 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000512}
513
514/// Determines whether the given friend declaration matches anything
515/// in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000516static AccessResult MatchesFriend(Sema &S,
517 const EffectiveContext &EC,
518 FriendDecl *FriendD) {
John McCall32f2fb52010-03-25 18:04:51 +0000519 if (TypeSourceInfo *T = FriendD->getFriendType())
520 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCall0c01d182010-03-24 05:22:00 +0000521
522 NamedDecl *Friend
523 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCalla742db02010-03-17 20:01:29 +0000524
525 // FIXME: declarations with dependent or templated scope.
526
John McCall0c01d182010-03-24 05:22:00 +0000527 if (isa<ClassTemplateDecl>(Friend))
528 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000529
John McCall0c01d182010-03-24 05:22:00 +0000530 if (isa<FunctionTemplateDecl>(Friend))
531 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000532
John McCall0c01d182010-03-24 05:22:00 +0000533 if (isa<CXXRecordDecl>(Friend))
534 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000535
John McCall0c01d182010-03-24 05:22:00 +0000536 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
537 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000538}
539
John McCall161755a2010-04-06 21:38:20 +0000540static AccessResult GetFriendKind(Sema &S,
541 const EffectiveContext &EC,
542 const CXXRecordDecl *Class) {
543 AccessResult OnFailure = AR_inaccessible;
John McCall88b6c712010-03-17 04:58:56 +0000544
John McCalld60e22e2010-03-12 01:19:31 +0000545 // 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 McCalla742db02010-03-17 20:01:29 +0000550 switch (MatchesFriend(S, EC, Friend)) {
John McCall161755a2010-04-06 21:38:20 +0000551 case AR_accessible:
552 return AR_accessible;
John McCalld60e22e2010-03-12 01:19:31 +0000553
John McCall161755a2010-04-06 21:38:20 +0000554 case AR_inaccessible:
555 continue;
556
557 case AR_dependent:
558 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000559 break;
John McCalld60e22e2010-03-12 01:19:31 +0000560 }
John McCalld60e22e2010-03-12 01:19:31 +0000561 }
562
563 // That's it, give up.
John McCall88b6c712010-03-17 04:58:56 +0000564 return OnFailure;
John McCall6b2accb2010-02-10 09:31:12 +0000565}
566
John McCall8c77bcb2010-08-28 07:56:00 +0000567namespace {
568
569/// A helper class for checking for a friend which will grant access
570/// to a protected instance member.
571struct 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
John McCall326c8c72010-08-28 08:47:21 +0000589 /// Check classes in the current path for friendship, starting at
590 /// the given index.
591 bool checkFriendshipAlongPath(unsigned I) {
592 assert(I < CurPath.size());
593 for (unsigned E = CurPath.size(); I != E; ++I) {
594 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall8c77bcb2010-08-28 07:56:00 +0000595 case AR_accessible: return true;
596 case AR_inaccessible: continue;
597 case AR_dependent: EverDependent = true; continue;
598 }
599 }
600 return false;
601 }
602
603 /// Perform a search starting at the given class.
John McCall326c8c72010-08-28 08:47:21 +0000604 ///
605 /// PrivateDepth is the index of the last (least derived) class
606 /// along the current path such that a notional public member of
607 /// the final class in the path would have access in that class.
608 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall8c77bcb2010-08-28 07:56:00 +0000609 // If we ever reach the naming class, check the current path for
610 // friendship. We can also stop recursing because we obviously
611 // won't find the naming class there again.
John McCall326c8c72010-08-28 08:47:21 +0000612 if (Cur == NamingClass)
613 return checkFriendshipAlongPath(PrivateDepth);
John McCall8c77bcb2010-08-28 07:56:00 +0000614
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
John McCall326c8c72010-08-28 08:47:21 +0000622 // If this is private inheritance, then a public member of the
623 // base will not have any access in classes derived from Cur.
624 unsigned BasePrivateDepth = PrivateDepth;
625 if (I->getAccessSpecifier() == AS_private)
626 BasePrivateDepth = CurPath.size() - 1;
John McCall8c77bcb2010-08-28 07:56:00 +0000627
628 const CXXRecordDecl *RD;
629
630 QualType T = I->getType();
631 if (const RecordType *RT = T->getAs<RecordType>()) {
632 RD = cast<CXXRecordDecl>(RT->getDecl());
633 } else if (const InjectedClassNameType *IT
634 = T->getAs<InjectedClassNameType>()) {
635 RD = IT->getDecl();
636 } else {
637 assert(T->isDependentType() && "non-dependent base wasn't a record?");
638 EverDependent = true;
639 continue;
640 }
641
642 // Recurse. We don't need to clean up if this returns true.
John McCall326c8c72010-08-28 08:47:21 +0000643 CurPath.push_back(RD);
644 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
645 return true;
646 CurPath.pop_back();
John McCall8c77bcb2010-08-28 07:56:00 +0000647 }
648
John McCall8c77bcb2010-08-28 07:56:00 +0000649 return false;
650 }
John McCall326c8c72010-08-28 08:47:21 +0000651
652 bool findFriendship(const CXXRecordDecl *Cur) {
653 assert(CurPath.empty());
654 CurPath.push_back(Cur);
655 return findFriendship(Cur, 0);
656 }
John McCall8c77bcb2010-08-28 07:56:00 +0000657};
658}
659
660/// Search for a class P that EC is a friend of, under the constraint
661/// InstanceContext <= P <= NamingClass
662/// and with the additional restriction that a protected member of
663/// NamingClass would have some natural access in P.
664///
665/// That second condition isn't actually quite right: the condition in
666/// the standard is whether the target would have some natural access
667/// in P. The difference is that the target might be more accessible
668/// along some path not passing through NamingClass. Allowing that
669/// introduces two problems:
670/// - It breaks encapsulation because you can suddenly access a
671/// forbidden base class's members by subclassing it elsewhere.
672/// - It makes access substantially harder to compute because it
673/// breaks the hill-climbing algorithm: knowing that the target is
674/// accessible in some base class would no longer let you change
675/// the question solely to whether the base class is accessible,
676/// because the original target might have been more accessible
677/// because of crazy subclassing.
678/// So we don't implement that.
679static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
680 const CXXRecordDecl *InstanceContext,
681 const CXXRecordDecl *NamingClass) {
682 assert(InstanceContext->getCanonicalDecl() == InstanceContext);
683 assert(NamingClass->getCanonicalDecl() == NamingClass);
684
685 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
686 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
687 if (PRC.EverDependent) return AR_dependent;
688 return AR_inaccessible;
689}
690
John McCall161755a2010-04-06 21:38:20 +0000691static AccessResult HasAccess(Sema &S,
692 const EffectiveContext &EC,
693 const CXXRecordDecl *NamingClass,
694 AccessSpecifier Access,
695 const AccessTarget &Target) {
John McCalldb73c682010-04-02 00:03:43 +0000696 assert(NamingClass->getCanonicalDecl() == NamingClass &&
697 "declaration should be canonicalized before being passed here");
698
John McCall161755a2010-04-06 21:38:20 +0000699 if (Access == AS_public) return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000700 assert(Access == AS_private || Access == AS_protected);
701
John McCall161755a2010-04-06 21:38:20 +0000702 AccessResult OnFailure = AR_inaccessible;
703
John McCalldb73c682010-04-02 00:03:43 +0000704 for (EffectiveContext::record_iterator
705 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
706 // All the declarations in EC have been canonicalized, so pointer
707 // equality from this point on will work fine.
708 const CXXRecordDecl *ECRecord = *I;
709
710 // [B2] and [M2]
John McCall161755a2010-04-06 21:38:20 +0000711 if (Access == AS_private) {
712 if (ECRecord == NamingClass)
713 return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000714
John McCall01ebd9d2010-05-04 05:11:27 +0000715 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
716 OnFailure = AR_dependent;
717
John McCalldb73c682010-04-02 00:03:43 +0000718 // [B3] and [M3]
John McCall161755a2010-04-06 21:38:20 +0000719 } else {
720 assert(Access == AS_protected);
721 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
722 case AR_accessible: break;
723 case AR_inaccessible: continue;
724 case AR_dependent: OnFailure = AR_dependent; continue;
725 }
726
727 if (!Target.hasInstanceContext())
728 return AR_accessible;
729
730 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
731 if (!InstanceContext) {
732 OnFailure = AR_dependent;
733 continue;
734 }
735
736 // C++ [class.protected]p1:
737 // An additional access check beyond those described earlier in
738 // [class.access] is applied when a non-static data member or
739 // non-static member function is a protected member of its naming
740 // class. As described earlier, access to a protected member is
741 // granted because the reference occurs in a friend or member of
742 // some class C. If the access is to form a pointer to member,
743 // the nested-name-specifier shall name C or a class derived from
744 // C. All other accesses involve a (possibly implicit) object
745 // expression. In this case, the class of the object expression
746 // shall be C or a class derived from C.
747 //
748 // We interpret this as a restriction on [M3]. Most of the
749 // conditions are encoded by not having any instance context.
750 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
751 case AR_accessible: return AR_accessible;
752 case AR_inaccessible: continue;
753 case AR_dependent: OnFailure = AR_dependent; continue;
754 }
755 }
John McCalldb73c682010-04-02 00:03:43 +0000756 }
757
John McCall8c77bcb2010-08-28 07:56:00 +0000758 // [M3] and [B3] say that, if the target is protected in N, we grant
759 // access if the access occurs in a friend or member of some class P
760 // that's a subclass of N and where the target has some natural
761 // access in P. The 'member' aspect is easy to handle because P
762 // would necessarily be one of the effective-context records, and we
763 // address that above. The 'friend' aspect is completely ridiculous
764 // to implement because there are no restrictions at all on P
765 // *unless* the [class.protected] restriction applies. If it does,
766 // however, we should ignore whether the naming class is a friend,
767 // and instead rely on whether any potential P is a friend.
John McCall161755a2010-04-06 21:38:20 +0000768 if (Access == AS_protected && Target.hasInstanceContext()) {
769 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
770 if (!InstanceContext) return AR_dependent;
John McCall8c77bcb2010-08-28 07:56:00 +0000771 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
772 case AR_accessible: return AR_accessible;
John McCall161755a2010-04-06 21:38:20 +0000773 case AR_inaccessible: return OnFailure;
774 case AR_dependent: return AR_dependent;
775 }
John McCall1797a052010-08-28 08:10:32 +0000776 llvm_unreachable("impossible friendship kind");
John McCall161755a2010-04-06 21:38:20 +0000777 }
778
779 switch (GetFriendKind(S, EC, NamingClass)) {
780 case AR_accessible: return AR_accessible;
781 case AR_inaccessible: return OnFailure;
782 case AR_dependent: return AR_dependent;
783 }
784
785 // Silence bogus warnings
786 llvm_unreachable("impossible friendship kind");
787 return OnFailure;
John McCalldb73c682010-04-02 00:03:43 +0000788}
789
John McCall6b2accb2010-02-10 09:31:12 +0000790/// Finds the best path from the naming class to the declaring class,
791/// taking friend declarations into account.
792///
John McCalldb73c682010-04-02 00:03:43 +0000793/// C++0x [class.access.base]p5:
794/// A member m is accessible at the point R when named in class N if
795/// [M1] m as a member of N is public, or
796/// [M2] m as a member of N is private, and R occurs in a member or
797/// friend of class N, or
798/// [M3] m as a member of N is protected, and R occurs in a member or
799/// friend of class N, or in a member or friend of a class P
800/// derived from N, where m as a member of P is public, private,
801/// or protected, or
802/// [M4] there exists a base class B of N that is accessible at R, and
803/// m is accessible at R when named in class B.
804///
805/// C++0x [class.access.base]p4:
806/// A base class B of N is accessible at R, if
807/// [B1] an invented public member of B would be a public member of N, or
808/// [B2] R occurs in a member or friend of class N, and an invented public
809/// member of B would be a private or protected member of N, or
810/// [B3] R occurs in a member or friend of a class P derived from N, and an
811/// invented public member of B would be a private or protected member
812/// of P, or
813/// [B4] there exists a class S such that B is a base class of S accessible
814/// at R and S is a base class of N accessible at R.
815///
816/// Along a single inheritance path we can restate both of these
817/// iteratively:
818///
819/// First, we note that M1-4 are equivalent to B1-4 if the member is
820/// treated as a notional base of its declaring class with inheritance
821/// access equivalent to the member's access. Therefore we need only
822/// ask whether a class B is accessible from a class N in context R.
823///
824/// Let B_1 .. B_n be the inheritance path in question (i.e. where
825/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
826/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
827/// closest accessible base in the path:
828/// Access(a, b) = (* access on the base specifier from a to b *)
829/// Merge(a, forbidden) = forbidden
830/// Merge(a, private) = forbidden
831/// Merge(a, b) = min(a,b)
832/// Accessible(c, forbidden) = false
833/// Accessible(c, private) = (R is c) || IsFriend(c, R)
834/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
835/// Accessible(c, public) = true
836/// ACAB(n) = public
837/// ACAB(i) =
838/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
839/// if Accessible(B_i, AccessToBase) then public else AccessToBase
840///
841/// B is an accessible base of N at R iff ACAB(1) = public.
842///
John McCall161755a2010-04-06 21:38:20 +0000843/// \param FinalAccess the access of the "final step", or AS_public if
John McCall7aceaf82010-03-18 23:49:19 +0000844/// there is no final step.
John McCall6b2accb2010-02-10 09:31:12 +0000845/// \return null if friendship is dependent
846static CXXBasePath *FindBestPath(Sema &S,
847 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +0000848 AccessTarget &Target,
John McCall7aceaf82010-03-18 23:49:19 +0000849 AccessSpecifier FinalAccess,
John McCall6b2accb2010-02-10 09:31:12 +0000850 CXXBasePaths &Paths) {
851 // Derive the paths to the desired base.
John McCall161755a2010-04-06 21:38:20 +0000852 const CXXRecordDecl *Derived = Target.getNamingClass();
853 const CXXRecordDecl *Base = Target.getDeclaringClass();
854
855 // FIXME: fail correctly when there are dependent paths.
856 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
857 Paths);
John McCall6b2accb2010-02-10 09:31:12 +0000858 assert(isDerived && "derived class not actually derived from base");
859 (void) isDerived;
860
861 CXXBasePath *BestPath = 0;
862
John McCall7aceaf82010-03-18 23:49:19 +0000863 assert(FinalAccess != AS_none && "forbidden access after declaring class");
864
John McCall0c01d182010-03-24 05:22:00 +0000865 bool AnyDependent = false;
866
John McCall6b2accb2010-02-10 09:31:12 +0000867 // Derive the friend-modified access along each path.
868 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
869 PI != PE; ++PI) {
John McCall161755a2010-04-06 21:38:20 +0000870 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +0000871
872 // Walk through the path backwards.
John McCall7aceaf82010-03-18 23:49:19 +0000873 AccessSpecifier PathAccess = FinalAccess;
John McCall6b2accb2010-02-10 09:31:12 +0000874 CXXBasePath::iterator I = PI->end(), E = PI->begin();
875 while (I != E) {
876 --I;
877
John McCall7aceaf82010-03-18 23:49:19 +0000878 assert(PathAccess != AS_none);
879
880 // If the declaration is a private member of a base class, there
881 // is no level of friendship in derived classes that can make it
882 // accessible.
883 if (PathAccess == AS_private) {
884 PathAccess = AS_none;
885 break;
886 }
887
John McCall161755a2010-04-06 21:38:20 +0000888 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
889
John McCall6b2accb2010-02-10 09:31:12 +0000890 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalldb73c682010-04-02 00:03:43 +0000891 PathAccess = std::max(PathAccess, BaseAccess);
John McCall161755a2010-04-06 21:38:20 +0000892
893 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
894 case AR_inaccessible: break;
895 case AR_accessible:
896 PathAccess = AS_public;
897
898 // Future tests are not against members and so do not have
899 // instance context.
900 Target.suppressInstanceContext();
901 break;
902 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +0000903 AnyDependent = true;
904 goto Next;
John McCall6b2accb2010-02-10 09:31:12 +0000905 }
John McCall6b2accb2010-02-10 09:31:12 +0000906 }
907
908 // Note that we modify the path's Access field to the
909 // friend-modified access.
910 if (BestPath == 0 || PathAccess < BestPath->Access) {
911 BestPath = &*PI;
912 BestPath->Access = PathAccess;
John McCall0c01d182010-03-24 05:22:00 +0000913
914 // Short-circuit if we found a public path.
915 if (BestPath->Access == AS_public)
916 return BestPath;
John McCall6b2accb2010-02-10 09:31:12 +0000917 }
John McCall0c01d182010-03-24 05:22:00 +0000918
919 Next: ;
John McCall6b2accb2010-02-10 09:31:12 +0000920 }
921
John McCall0c01d182010-03-24 05:22:00 +0000922 assert((!BestPath || BestPath->Access != AS_public) &&
923 "fell out of loop with public path");
924
925 // We didn't find a public path, but at least one path was subject
926 // to dependent friendship, so delay the check.
927 if (AnyDependent)
928 return 0;
929
John McCall6b2accb2010-02-10 09:31:12 +0000930 return BestPath;
931}
932
John McCallfe24e052010-09-03 04:56:05 +0000933/// Given that an entity has protected natural access, check whether
934/// access might be denied because of the protected member access
935/// restriction.
936///
937/// \return true if a note was emitted
938static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
939 AccessTarget &Target) {
940 // Only applies to instance accesses.
941 if (!Target.hasInstanceContext())
942 return false;
943 assert(Target.isMemberAccess());
944 NamedDecl *D = Target.getTargetDecl();
945
946 const CXXRecordDecl *DeclaringClass = Target.getDeclaringClass();
947 DeclaringClass = DeclaringClass->getCanonicalDecl();
948
949 for (EffectiveContext::record_iterator
950 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
951 const CXXRecordDecl *ECRecord = *I;
952 switch (IsDerivedFromInclusive(ECRecord, DeclaringClass)) {
953 case AR_accessible: break;
954 case AR_inaccessible: continue;
955 case AR_dependent: continue;
956 }
957
958 // The effective context is a subclass of the declaring class.
959 // If that class isn't a superclass of the instance context,
960 // then the [class.protected] restriction applies.
961
962 // To get this exactly right, this might need to be checked more
963 // holistically; it's not necessarily the case that gaining
964 // access here would grant us access overall.
965
966 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
967 assert(InstanceContext && "diagnosing dependent access");
968
969 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
970 case AR_accessible: continue;
971 case AR_dependent: continue;
972 case AR_inaccessible:
973 S.Diag(D->getLocation(), diag::note_access_protected_restricted)
974 << (InstanceContext != Target.getNamingClass()->getCanonicalDecl())
975 << S.Context.getTypeDeclType(InstanceContext)
976 << S.Context.getTypeDeclType(ECRecord);
977 return true;
978 }
979 }
980
981 return false;
982}
983
John McCall6b2accb2010-02-10 09:31:12 +0000984/// Diagnose the path which caused the given declaration or base class
985/// to become inaccessible.
986static void DiagnoseAccessPath(Sema &S,
987 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +0000988 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +0000989 AccessSpecifier Access = Entity.getAccess();
John McCall161755a2010-04-06 21:38:20 +0000990 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalldb73c682010-04-02 00:03:43 +0000991 NamingClass = NamingClass->getCanonicalDecl();
992
John McCall161755a2010-04-06 21:38:20 +0000993 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
994 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCalldb73c682010-04-02 00:03:43 +0000995
John McCall92f88312010-01-23 00:46:32 +0000996 // Easy case: the decl's natural access determined its path access.
John McCall6b2accb2010-02-10 09:31:12 +0000997 // We have to check against AS_private here in case Access is AS_none,
998 // indicating a non-public member of a private base class.
John McCall6b2accb2010-02-10 09:31:12 +0000999 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
John McCall161755a2010-04-06 21:38:20 +00001000 switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
1001 case AR_inaccessible: {
John McCallfe24e052010-09-03 04:56:05 +00001002 if (Access == AS_protected &&
1003 TryDiagnoseProtectedAccess(S, EC, Entity))
1004 return;
1005
John McCall6b2accb2010-02-10 09:31:12 +00001006 S.Diag(D->getLocation(), diag::note_access_natural)
1007 << (unsigned) (Access == AS_protected)
1008 << /*FIXME: not implicitly*/ 0;
1009 return;
1010 }
1011
John McCall161755a2010-04-06 21:38:20 +00001012 case AR_accessible: break;
John McCall6b2accb2010-02-10 09:31:12 +00001013
John McCall161755a2010-04-06 21:38:20 +00001014 case AR_dependent:
1015 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001016 return;
1017 }
1018 }
1019
1020 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001021 CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
John McCall6b2accb2010-02-10 09:31:12 +00001022
1023 CXXBasePath::iterator I = Path.end(), E = Path.begin();
1024 while (I != E) {
1025 --I;
1026
1027 const CXXBaseSpecifier *BS = I->Base;
1028 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
1029
1030 // If this is public inheritance, or the derived class is a friend,
1031 // skip this step.
1032 if (BaseAccess == AS_public)
1033 continue;
1034
1035 switch (GetFriendKind(S, EC, I->Class)) {
John McCall161755a2010-04-06 21:38:20 +00001036 case AR_accessible: continue;
1037 case AR_inaccessible: break;
1038 case AR_dependent:
1039 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001040 }
1041
1042 // Check whether this base specifier is the tighest point
1043 // constraining access. We have to check against AS_private for
1044 // the same reasons as above.
1045 if (BaseAccess == AS_private || BaseAccess >= Access) {
1046
1047 // We're constrained by inheritance, but we want to say
1048 // "declared private here" if we're diagnosing a hierarchy
1049 // conversion and this is the final step.
1050 unsigned diagnostic;
1051 if (D) diagnostic = diag::note_access_constrained_by_path;
1052 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
1053 else diagnostic = diag::note_access_constrained_by_path;
1054
1055 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
1056 << BS->getSourceRange()
1057 << (BaseAccess == AS_protected)
1058 << (BS->getAccessSpecifierAsWritten() == AS_none);
Douglas Gregor76ef6582010-05-28 04:34:55 +00001059
1060 if (D)
1061 S.Diag(D->getLocation(), diag::note_field_decl);
1062
John McCall6b2accb2010-02-10 09:31:12 +00001063 return;
1064 }
1065 }
1066
1067 llvm_unreachable("access not apparently constrained by path");
1068}
1069
John McCall58e6f342010-03-16 05:22:47 +00001070static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall6b2accb2010-02-10 09:31:12 +00001071 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +00001072 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001073 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCall161755a2010-04-06 21:38:20 +00001074 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
1075 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
John McCalldb73c682010-04-02 00:03:43 +00001076
1077 S.Diag(Loc, Entity.getDiag())
1078 << (Entity.getAccess() == AS_protected)
1079 << (D ? D->getDeclName() : DeclarationName())
1080 << S.Context.getTypeDeclType(NamingClass)
1081 << S.Context.getTypeDeclType(DeclaringClass);
1082 DiagnoseAccessPath(S, EC, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001083}
1084
John McCalldb73c682010-04-02 00:03:43 +00001085/// Determines whether the accessed entity is accessible. Public members
1086/// have been weeded out by this point.
John McCall161755a2010-04-06 21:38:20 +00001087static AccessResult IsAccessible(Sema &S,
1088 const EffectiveContext &EC,
1089 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001090 // Determine the actual naming class.
1091 CXXRecordDecl *NamingClass = Entity.getNamingClass();
1092 while (NamingClass->isAnonymousStructOrUnion())
1093 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
1094 NamingClass = NamingClass->getCanonicalDecl();
John McCall6b2accb2010-02-10 09:31:12 +00001095
John McCalldb73c682010-04-02 00:03:43 +00001096 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1097 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1098
1099 // Before we try to recalculate access paths, try to white-list
1100 // accesses which just trade in on the final step, i.e. accesses
1101 // which don't require [M4] or [B4]. These are by far the most
John McCall161755a2010-04-06 21:38:20 +00001102 // common forms of privileged access.
John McCalldb73c682010-04-02 00:03:43 +00001103 if (UnprivilegedAccess != AS_none) {
John McCall161755a2010-04-06 21:38:20 +00001104 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1105 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +00001106 // This is actually an interesting policy decision. We don't
1107 // *have* to delay immediately here: we can do the full access
1108 // calculation in the hope that friendship on some intermediate
1109 // class will make the declaration accessible non-dependently.
1110 // But that's not cheap, and odds are very good (note: assertion
1111 // made without data) that the friend declaration will determine
1112 // access.
John McCall161755a2010-04-06 21:38:20 +00001113 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001114
John McCall161755a2010-04-06 21:38:20 +00001115 case AR_accessible: return AR_accessible;
1116 case AR_inaccessible: break;
John McCalldb73c682010-04-02 00:03:43 +00001117 }
1118 }
1119
John McCall161755a2010-04-06 21:38:20 +00001120 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +00001121
John McCalldb73c682010-04-02 00:03:43 +00001122 // We lower member accesses to base accesses by pretending that the
1123 // member is a base class of its declaring class.
1124 AccessSpecifier FinalAccess;
1125
John McCall6b2accb2010-02-10 09:31:12 +00001126 if (Entity.isMemberAccess()) {
John McCalldb73c682010-04-02 00:03:43 +00001127 // Determine if the declaration is accessible from EC when named
1128 // in its declaring class.
John McCall6b2accb2010-02-10 09:31:12 +00001129 NamedDecl *Target = Entity.getTargetDecl();
John McCall161755a2010-04-06 21:38:20 +00001130 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall6b2accb2010-02-10 09:31:12 +00001131
John McCalldb73c682010-04-02 00:03:43 +00001132 FinalAccess = Target->getAccess();
John McCall161755a2010-04-06 21:38:20 +00001133 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1134 case AR_accessible:
1135 FinalAccess = AS_public;
1136 break;
1137 case AR_inaccessible: break;
1138 case AR_dependent: return AR_dependent; // see above
John McCall6b2accb2010-02-10 09:31:12 +00001139 }
1140
John McCalldb73c682010-04-02 00:03:43 +00001141 if (DeclaringClass == NamingClass)
John McCall161755a2010-04-06 21:38:20 +00001142 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
1143
1144 Entity.suppressInstanceContext();
John McCalldb73c682010-04-02 00:03:43 +00001145 } else {
1146 FinalAccess = AS_public;
John McCall6b2accb2010-02-10 09:31:12 +00001147 }
1148
John McCall161755a2010-04-06 21:38:20 +00001149 assert(Entity.getDeclaringClass() != NamingClass);
John McCall6b2accb2010-02-10 09:31:12 +00001150
1151 // Append the declaration's access if applicable.
1152 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001153 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCall0c01d182010-03-24 05:22:00 +00001154 if (!Path)
John McCall161755a2010-04-06 21:38:20 +00001155 return AR_dependent;
John McCall92f88312010-01-23 00:46:32 +00001156
John McCalldb73c682010-04-02 00:03:43 +00001157 assert(Path->Access <= UnprivilegedAccess &&
1158 "access along best path worse than direct?");
1159 if (Path->Access == AS_public)
John McCall161755a2010-04-06 21:38:20 +00001160 return AR_accessible;
1161 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +00001162}
1163
John McCall161755a2010-04-06 21:38:20 +00001164static void DelayDependentAccess(Sema &S,
1165 const EffectiveContext &EC,
1166 SourceLocation Loc,
1167 const AccessTarget &Entity) {
John McCall0c01d182010-03-24 05:22:00 +00001168 assert(EC.isDependent() && "delaying non-dependent access");
John McCall7ad650f2010-03-24 07:46:06 +00001169 DeclContext *DC = EC.getInnerContext();
John McCall0c01d182010-03-24 05:22:00 +00001170 assert(DC->isDependentContext() && "delaying non-dependent access");
1171 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1172 Loc,
1173 Entity.isMemberAccess(),
1174 Entity.getAccess(),
1175 Entity.getTargetDecl(),
1176 Entity.getNamingClass(),
John McCall161755a2010-04-06 21:38:20 +00001177 Entity.getBaseObjectType(),
John McCall0c01d182010-03-24 05:22:00 +00001178 Entity.getDiag());
John McCall92f88312010-01-23 00:46:32 +00001179}
1180
John McCall6b2accb2010-02-10 09:31:12 +00001181/// Checks access to an entity from the given effective context.
John McCall161755a2010-04-06 21:38:20 +00001182static AccessResult CheckEffectiveAccess(Sema &S,
1183 const EffectiveContext &EC,
1184 SourceLocation Loc,
1185 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001186 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall92f88312010-01-23 00:46:32 +00001187
John McCalldb73c682010-04-02 00:03:43 +00001188 switch (IsAccessible(S, EC, Entity)) {
John McCall161755a2010-04-06 21:38:20 +00001189 case AR_dependent:
1190 DelayDependentAccess(S, EC, Loc, Entity);
1191 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001192
John McCall161755a2010-04-06 21:38:20 +00001193 case AR_inaccessible:
John McCalldb73c682010-04-02 00:03:43 +00001194 if (!Entity.isQuiet())
1195 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCall161755a2010-04-06 21:38:20 +00001196 return AR_inaccessible;
John McCalldb73c682010-04-02 00:03:43 +00001197
John McCall161755a2010-04-06 21:38:20 +00001198 case AR_accessible:
1199 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +00001200 }
1201
John McCall161755a2010-04-06 21:38:20 +00001202 // silence unnecessary warning
1203 llvm_unreachable("invalid access result");
1204 return AR_accessible;
John McCall6b2accb2010-02-10 09:31:12 +00001205}
John McCall92f88312010-01-23 00:46:32 +00001206
John McCall6b2accb2010-02-10 09:31:12 +00001207static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCall161755a2010-04-06 21:38:20 +00001208 AccessTarget &Entity) {
John McCall6b2accb2010-02-10 09:31:12 +00001209 // If the access path is public, it's accessible everywhere.
1210 if (Entity.getAccess() == AS_public)
1211 return Sema::AR_accessible;
John McCall92f88312010-01-23 00:46:32 +00001212
Chandler Carruth926c4b42010-06-28 08:39:25 +00001213 if (S.SuppressAccessChecking)
1214 return Sema::AR_accessible;
1215
John McCall6b2accb2010-02-10 09:31:12 +00001216 // If we're currently parsing a top-level declaration, delay
1217 // diagnostics. This is the only case where parsing a declaration
1218 // can actually change our effective context for the purposes of
1219 // access control.
1220 if (S.CurContext->isFileContext() && S.ParsingDeclDepth) {
John McCall6b2accb2010-02-10 09:31:12 +00001221 S.DelayedDiagnostics.push_back(
John McCall9c3087b2010-08-26 02:13:20 +00001222 DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall6b2accb2010-02-10 09:31:12 +00001223 return Sema::AR_delayed;
John McCall92f88312010-01-23 00:46:32 +00001224 }
1225
John McCall161755a2010-04-06 21:38:20 +00001226 EffectiveContext EC(S.CurContext);
1227 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1228 case AR_accessible: return Sema::AR_accessible;
1229 case AR_inaccessible: return Sema::AR_inaccessible;
1230 case AR_dependent: return Sema::AR_dependent;
1231 }
1232 llvm_unreachable("falling off end");
1233 return Sema::AR_accessible;
John McCall92f88312010-01-23 00:46:32 +00001234}
1235
John McCall2f514482010-01-27 03:50:35 +00001236void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) {
John McCall2f514482010-01-27 03:50:35 +00001237 // Pretend we did this from the context of the newly-parsed
Chandler Carruth630eb012010-04-18 08:23:21 +00001238 // declaration. If that declaration itself forms a declaration context,
1239 // include it in the effective context so that parameters and return types of
1240 // befriended functions have that function's access priveledges.
1241 DeclContext *DC = Ctx->getDeclContext();
1242 if (isa<FunctionDecl>(Ctx))
1243 DC = cast<DeclContext>(Ctx);
1244 else if (FunctionTemplateDecl *FnTpl = dyn_cast<FunctionTemplateDecl>(Ctx))
1245 DC = cast<DeclContext>(FnTpl->getTemplatedDecl());
1246 EffectiveContext EC(DC);
John McCall2f514482010-01-27 03:50:35 +00001247
John McCall161755a2010-04-06 21:38:20 +00001248 AccessTarget Target(DD.getAccessData());
1249
1250 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall2f514482010-01-27 03:50:35 +00001251 DD.Triggered = true;
1252}
1253
John McCall0c01d182010-03-24 05:22:00 +00001254void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1255 const MultiLevelTemplateArgumentList &TemplateArgs) {
1256 SourceLocation Loc = DD.getAccessLoc();
1257 AccessSpecifier Access = DD.getAccess();
1258
1259 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1260 TemplateArgs);
1261 if (!NamingD) return;
1262 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1263 TemplateArgs);
1264 if (!TargetD) return;
1265
1266 if (DD.isAccessToMember()) {
John McCall161755a2010-04-06 21:38:20 +00001267 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1268 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1269 QualType BaseObjectType = DD.getAccessBaseObjectType();
1270 if (!BaseObjectType.isNull()) {
1271 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1272 DeclarationName());
1273 if (BaseObjectType.isNull()) return;
1274 }
1275
1276 AccessTarget Entity(Context,
1277 AccessTarget::Member,
1278 NamingClass,
1279 DeclAccessPair::make(TargetDecl, Access),
1280 BaseObjectType);
John McCall0c01d182010-03-24 05:22:00 +00001281 Entity.setDiag(DD.getDiagnostic());
1282 CheckAccess(*this, Loc, Entity);
1283 } else {
John McCall161755a2010-04-06 21:38:20 +00001284 AccessTarget Entity(Context,
1285 AccessTarget::Base,
1286 cast<CXXRecordDecl>(TargetD),
1287 cast<CXXRecordDecl>(NamingD),
1288 Access);
John McCall0c01d182010-03-24 05:22:00 +00001289 Entity.setDiag(DD.getDiagnostic());
1290 CheckAccess(*this, Loc, Entity);
1291 }
1292}
1293
John McCall6b2accb2010-02-10 09:31:12 +00001294Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001295 DeclAccessPair Found) {
John McCall58e6f342010-03-16 05:22:47 +00001296 if (!getLangOptions().AccessControl ||
1297 !E->getNamingClass() ||
John McCall9aa472c2010-03-19 07:35:19 +00001298 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001299 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001300
John McCall161755a2010-04-06 21:38:20 +00001301 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1302 Found, QualType());
John McCall58e6f342010-03-16 05:22:47 +00001303 Entity.setDiag(diag::err_access) << E->getSourceRange();
1304
1305 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001306}
1307
1308/// Perform access-control checking on a previously-unresolved member
1309/// access which has now been resolved to a member.
John McCall6b2accb2010-02-10 09:31:12 +00001310Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001311 DeclAccessPair Found) {
John McCall58e6f342010-03-16 05:22:47 +00001312 if (!getLangOptions().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001313 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001314 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001315
John McCall161755a2010-04-06 21:38:20 +00001316 QualType BaseType = E->getBaseType();
1317 if (E->isArrow())
1318 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1319
1320 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1321 Found, BaseType);
John McCall58e6f342010-03-16 05:22:47 +00001322 Entity.setDiag(diag::err_access) << E->getSourceRange();
1323
1324 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001325}
1326
John McCall6b2accb2010-02-10 09:31:12 +00001327Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall58e6f342010-03-16 05:22:47 +00001328 CXXDestructorDecl *Dtor,
1329 const PartialDiagnostic &PDiag) {
John McCall4f9506a2010-02-02 08:45:54 +00001330 if (!getLangOptions().AccessControl)
John McCall6b2accb2010-02-10 09:31:12 +00001331 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001332
John McCall58e6f342010-03-16 05:22:47 +00001333 // There's never a path involved when checking implicit destructor access.
John McCall4f9506a2010-02-02 08:45:54 +00001334 AccessSpecifier Access = Dtor->getAccess();
1335 if (Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001336 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001337
John McCall58e6f342010-03-16 05:22:47 +00001338 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCall161755a2010-04-06 21:38:20 +00001339 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1340 DeclAccessPair::make(Dtor, Access),
1341 QualType());
John McCall58e6f342010-03-16 05:22:47 +00001342 Entity.setDiag(PDiag); // TODO: avoid copy
1343
1344 return CheckAccess(*this, Loc, Entity);
John McCall4f9506a2010-02-02 08:45:54 +00001345}
1346
John McCallb13b7372010-02-01 03:16:54 +00001347/// Checks access to a constructor.
John McCall6b2accb2010-02-10 09:31:12 +00001348Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00001349 CXXConstructorDecl *Constructor,
1350 const InitializedEntity &Entity,
1351 AccessSpecifier Access,
1352 bool IsCopyBindingRefToTemp) {
John McCall58e6f342010-03-16 05:22:47 +00001353 if (!getLangOptions().AccessControl ||
1354 Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001355 return AR_accessible;
John McCallb13b7372010-02-01 03:16:54 +00001356
John McCall6b2accb2010-02-10 09:31:12 +00001357 CXXRecordDecl *NamingClass = Constructor->getParent();
Anders Carlsson9a68a672010-04-21 18:47:17 +00001358 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1359 DeclAccessPair::make(Constructor, Access),
1360 QualType());
1361 switch (Entity.getKind()) {
1362 default:
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00001363 AccessEntity.setDiag(IsCopyBindingRefToTemp
1364 ? diag::ext_rvalue_to_reference_access_ctor
1365 : diag::err_access_ctor);
Anders Carlsson9a68a672010-04-21 18:47:17 +00001366 break;
John McCall58e6f342010-03-16 05:22:47 +00001367
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001368 case InitializedEntity::EK_Base:
1369 AccessEntity.setDiag(PDiag(diag::err_access_base)
1370 << Entity.isInheritedVirtualBase()
1371 << Entity.getBaseSpecifier()->getType()
1372 << getSpecialMember(Constructor));
Anders Carlsson9a68a672010-04-21 18:47:17 +00001373 break;
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001374
Anders Carlssonb99c6662010-04-21 20:28:29 +00001375 case InitializedEntity::EK_Member: {
1376 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Anders Carlsson0e313bd2010-04-23 03:41:35 +00001377 AccessEntity.setDiag(PDiag(diag::err_access_field)
1378 << Field->getType()
1379 << getSpecialMember(Constructor));
Anders Carlssonb99c6662010-04-21 20:28:29 +00001380 break;
1381 }
Anders Carlsson9a68a672010-04-21 18:47:17 +00001382
Anders Carlsson711f34a2010-04-21 19:52:01 +00001383 }
1384
Anders Carlsson9a68a672010-04-21 18:47:17 +00001385 return CheckAccess(*this, UseLoc, AccessEntity);
John McCallb13b7372010-02-01 03:16:54 +00001386}
1387
John McCallb0207482010-03-16 06:11:48 +00001388/// Checks direct (i.e. non-inherited) access to an arbitrary class
1389/// member.
1390Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
1391 NamedDecl *Target,
1392 const PartialDiagnostic &Diag) {
1393 AccessSpecifier Access = Target->getAccess();
1394 if (!getLangOptions().AccessControl ||
1395 Access == AS_public)
1396 return AR_accessible;
1397
1398 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
John McCall161755a2010-04-06 21:38:20 +00001399 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1400 DeclAccessPair::make(Target, Access),
1401 QualType());
John McCallb0207482010-03-16 06:11:48 +00001402 Entity.setDiag(Diag);
1403 return CheckAccess(*this, UseLoc, Entity);
1404}
1405
1406
John McCall90c8c572010-03-18 08:19:33 +00001407/// Checks access to an overloaded operator new or delete.
1408Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1409 SourceRange PlacementRange,
1410 CXXRecordDecl *NamingClass,
John McCall9aa472c2010-03-19 07:35:19 +00001411 DeclAccessPair Found) {
John McCall90c8c572010-03-18 08:19:33 +00001412 if (!getLangOptions().AccessControl ||
1413 !NamingClass ||
John McCall9aa472c2010-03-19 07:35:19 +00001414 Found.getAccess() == AS_public)
John McCall90c8c572010-03-18 08:19:33 +00001415 return AR_accessible;
1416
John McCall161755a2010-04-06 21:38:20 +00001417 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1418 QualType());
John McCall90c8c572010-03-18 08:19:33 +00001419 Entity.setDiag(diag::err_access)
1420 << PlacementRange;
1421
1422 return CheckAccess(*this, OpLoc, Entity);
1423}
1424
John McCallb13b7372010-02-01 03:16:54 +00001425/// Checks access to an overloaded member operator, including
1426/// conversion operators.
John McCall6b2accb2010-02-10 09:31:12 +00001427Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1428 Expr *ObjectExpr,
John McCall58e6f342010-03-16 05:22:47 +00001429 Expr *ArgExpr,
John McCall9aa472c2010-03-19 07:35:19 +00001430 DeclAccessPair Found) {
John McCall58e6f342010-03-16 05:22:47 +00001431 if (!getLangOptions().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001432 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001433 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001434
1435 const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>();
1436 assert(RT && "found member operator but object expr not of record type");
1437 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1438
John McCall161755a2010-04-06 21:38:20 +00001439 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1440 ObjectExpr->getType());
John McCall58e6f342010-03-16 05:22:47 +00001441 Entity.setDiag(diag::err_access)
1442 << ObjectExpr->getSourceRange()
1443 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1444
1445 return CheckAccess(*this, OpLoc, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001446}
John McCall5357b612010-01-28 01:42:12 +00001447
John McCall6bb80172010-03-30 21:47:33 +00001448Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1449 DeclAccessPair Found) {
1450 if (!getLangOptions().AccessControl ||
John McCalle2f5ba92010-03-30 22:20:00 +00001451 Found.getAccess() == AS_none ||
John McCall6bb80172010-03-30 21:47:33 +00001452 Found.getAccess() == AS_public)
1453 return AR_accessible;
1454
John McCall9c72c602010-08-27 09:08:28 +00001455 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCalle9ee23e2010-04-22 18:44:12 +00001456 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall6bb80172010-03-30 21:47:33 +00001457
John McCall161755a2010-04-06 21:38:20 +00001458 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1459 Context.getTypeDeclType(NamingClass));
John McCall6bb80172010-03-30 21:47:33 +00001460 Entity.setDiag(diag::err_access)
1461 << Ovl->getSourceRange();
1462
1463 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1464}
1465
John McCall6b2accb2010-02-10 09:31:12 +00001466/// Checks access for a hierarchy conversion.
1467///
1468/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
1469/// or a derived-to-base conversion (false)
1470/// \param ForceCheck true if this check should be performed even if access
1471/// control is disabled; some things rely on this for semantics
1472/// \param ForceUnprivileged true if this check should proceed as if the
1473/// context had no special privileges
1474/// \param ADK controls the kind of diagnostics that are used
1475Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall6b2accb2010-02-10 09:31:12 +00001476 QualType Base,
1477 QualType Derived,
1478 const CXXBasePath &Path,
John McCall58e6f342010-03-16 05:22:47 +00001479 unsigned DiagID,
John McCall6b2accb2010-02-10 09:31:12 +00001480 bool ForceCheck,
John McCall58e6f342010-03-16 05:22:47 +00001481 bool ForceUnprivileged) {
John McCall6b2accb2010-02-10 09:31:12 +00001482 if (!ForceCheck && !getLangOptions().AccessControl)
1483 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001484
John McCall6b2accb2010-02-10 09:31:12 +00001485 if (Path.Access == AS_public)
1486 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001487
John McCall6b2accb2010-02-10 09:31:12 +00001488 CXXRecordDecl *BaseD, *DerivedD;
1489 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1490 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall58e6f342010-03-16 05:22:47 +00001491
John McCall161755a2010-04-06 21:38:20 +00001492 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1493 Path.Access);
John McCall58e6f342010-03-16 05:22:47 +00001494 if (DiagID)
1495 Entity.setDiag(DiagID) << Derived << Base;
John McCall6b2accb2010-02-10 09:31:12 +00001496
John McCall161755a2010-04-06 21:38:20 +00001497 if (ForceUnprivileged) {
1498 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1499 AccessLoc, Entity)) {
1500 case ::AR_accessible: return Sema::AR_accessible;
1501 case ::AR_inaccessible: return Sema::AR_inaccessible;
1502 case ::AR_dependent: return Sema::AR_dependent;
1503 }
1504 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1505 }
John McCall58e6f342010-03-16 05:22:47 +00001506 return CheckAccess(*this, AccessLoc, Entity);
John McCall5357b612010-01-28 01:42:12 +00001507}
1508
John McCall92f88312010-01-23 00:46:32 +00001509/// Checks access to all the declarations in the given result set.
John McCall6b2accb2010-02-10 09:31:12 +00001510void Sema::CheckLookupAccess(const LookupResult &R) {
1511 assert(getLangOptions().AccessControl
1512 && "performing access check without access control");
1513 assert(R.getNamingClass() && "performing access check without naming class");
1514
John McCall58e6f342010-03-16 05:22:47 +00001515 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1516 if (I.getAccess() != AS_public) {
John McCall161755a2010-04-06 21:38:20 +00001517 AccessTarget Entity(Context, AccessedEntity::Member,
1518 R.getNamingClass(), I.getPair(),
1519 R.getBaseObjectType());
John McCall58e6f342010-03-16 05:22:47 +00001520 Entity.setDiag(diag::err_access);
1521
1522 CheckAccess(*this, R.getNameLoc(), Entity);
1523 }
1524 }
John McCall92f88312010-01-23 00:46:32 +00001525}
Chandler Carruth926c4b42010-06-28 08:39:25 +00001526
1527void Sema::ActOnStartSuppressingAccessChecks() {
1528 assert(!SuppressAccessChecking &&
1529 "Tried to start access check suppression when already started.");
1530 SuppressAccessChecking = true;
1531}
1532
1533void Sema::ActOnStopSuppressingAccessChecks() {
1534 assert(SuppressAccessChecking &&
1535 "Tried to stop access check suprression when already stopped.");
1536 SuppressAccessChecking = false;
1537}