blob: add0c5d6590fef2362688ab46d9ac81652a7e94d [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
Nico Weber20c9f1d2010-11-28 22:53:37 +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,
Francois Pichetefb1af92011-05-23 03:43:44 +0000149 QualType BaseObjectType,
150 bool IsUsingDecl = false)
151 : AccessedEntity(Context, Member, NamingClass, FoundDecl, BaseObjectType),
152 IsUsingDeclaration(IsUsingDecl) {
John McCalla8ae2222010-04-06 21:38:20 +0000153 initialize();
154 }
155
156 AccessTarget(ASTContext &Context,
157 BaseNonce _,
158 CXXRecordDecl *BaseClass,
159 CXXRecordDecl *DerivedClass,
160 AccessSpecifier Access)
161 : AccessedEntity(Context, Base, BaseClass, DerivedClass, Access) {
162 initialize();
163 }
164
165 bool hasInstanceContext() const {
166 return HasInstanceContext;
167 }
168
169 class SavedInstanceContext {
170 public:
171 ~SavedInstanceContext() {
172 Target.HasInstanceContext = Has;
173 }
174
175 private:
John McCall8e36d532010-04-07 00:41:46 +0000176 friend struct AccessTarget;
John McCalla8ae2222010-04-06 21:38:20 +0000177 explicit SavedInstanceContext(AccessTarget &Target)
178 : Target(Target), Has(Target.HasInstanceContext) {}
179 AccessTarget &Target;
180 bool Has;
181 };
182
183 SavedInstanceContext saveInstanceContext() {
184 return SavedInstanceContext(*this);
185 }
186
187 void suppressInstanceContext() {
188 HasInstanceContext = false;
189 }
190
191 const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
192 assert(HasInstanceContext);
193 if (CalculatedInstanceContext)
194 return InstanceContext;
195
196 CalculatedInstanceContext = true;
197 DeclContext *IC = S.computeDeclContext(getBaseObjectType());
198 InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0);
199 return InstanceContext;
200 }
201
202 const CXXRecordDecl *getDeclaringClass() const {
203 return DeclaringClass;
204 }
205
206private:
207 void initialize() {
208 HasInstanceContext = (isMemberAccess() &&
209 !getBaseObjectType().isNull() &&
210 getTargetDecl()->isCXXInstanceMember());
211 CalculatedInstanceContext = false;
212 InstanceContext = 0;
213
214 if (isMemberAccess())
215 DeclaringClass = FindDeclaringClass(getTargetDecl());
216 else
217 DeclaringClass = getBaseClass();
218 DeclaringClass = DeclaringClass->getCanonicalDecl();
219 }
220
Francois Pichetefb1af92011-05-23 03:43:44 +0000221 bool IsUsingDeclaration : 1;
John McCalla8ae2222010-04-06 21:38:20 +0000222 bool HasInstanceContext : 1;
223 mutable bool CalculatedInstanceContext : 1;
224 mutable const CXXRecordDecl *InstanceContext;
225 const CXXRecordDecl *DeclaringClass;
226};
227
Anders Carlsson4742a9c2009-03-27 05:05:05 +0000228}
John McCall553c0792010-01-23 00:46:32 +0000229
John McCall97205142010-05-04 05:11:27 +0000230/// Checks whether one class might instantiate to the other.
231static bool MightInstantiateTo(const CXXRecordDecl *From,
232 const CXXRecordDecl *To) {
233 // Declaration names are always preserved by instantiation.
234 if (From->getDeclName() != To->getDeclName())
235 return false;
236
237 const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext();
238 const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext();
239 if (FromDC == ToDC) return true;
240 if (FromDC->isFileContext() || ToDC->isFileContext()) return false;
241
242 // Be conservative.
243 return true;
244}
245
John McCalla8ae2222010-04-06 21:38:20 +0000246/// Checks whether one class is derived from another, inclusively.
247/// Properly indicates when it couldn't be determined due to
248/// dependence.
249///
250/// This should probably be donated to AST or at least Sema.
251static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
252 const CXXRecordDecl *Target) {
253 assert(Derived->getCanonicalDecl() == Derived);
254 assert(Target->getCanonicalDecl() == Target);
John McCall69f75862010-03-24 09:04:37 +0000255
John McCalla8ae2222010-04-06 21:38:20 +0000256 if (Derived == Target) return AR_accessible;
John McCall69f75862010-03-24 09:04:37 +0000257
John McCall97205142010-05-04 05:11:27 +0000258 bool CheckDependent = Derived->isDependentContext();
259 if (CheckDependent && MightInstantiateTo(Derived, Target))
260 return AR_dependent;
261
John McCalla8ae2222010-04-06 21:38:20 +0000262 AccessResult OnFailure = AR_inaccessible;
263 llvm::SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
264
265 while (true) {
266 for (CXXRecordDecl::base_class_const_iterator
267 I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) {
268
269 const CXXRecordDecl *RD;
270
271 QualType T = I->getType();
272 if (const RecordType *RT = T->getAs<RecordType>()) {
273 RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall97205142010-05-04 05:11:27 +0000274 } else if (const InjectedClassNameType *IT
275 = T->getAs<InjectedClassNameType>()) {
276 RD = IT->getDecl();
John McCalla8ae2222010-04-06 21:38:20 +0000277 } else {
John McCalla8ae2222010-04-06 21:38:20 +0000278 assert(T->isDependentType() && "non-dependent base wasn't a record?");
279 OnFailure = AR_dependent;
280 continue;
281 }
282
283 RD = RD->getCanonicalDecl();
284 if (RD == Target) return AR_accessible;
John McCall97205142010-05-04 05:11:27 +0000285 if (CheckDependent && MightInstantiateTo(RD, Target))
286 OnFailure = AR_dependent;
287
John McCalla8ae2222010-04-06 21:38:20 +0000288 Queue.push_back(RD);
289 }
290
291 if (Queue.empty()) break;
292
293 Derived = Queue.back();
294 Queue.pop_back();
295 }
296
297 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000298}
299
John McCalla8ae2222010-04-06 21:38:20 +0000300
John McCallc62bb642010-03-24 05:22:00 +0000301static bool MightInstantiateTo(Sema &S, DeclContext *Context,
302 DeclContext *Friend) {
303 if (Friend == Context)
304 return true;
305
306 assert(!Friend->isDependentContext() &&
307 "can't handle friends with dependent contexts here");
308
309 if (!Context->isDependentContext())
310 return false;
311
312 if (Friend->isFileContext())
313 return false;
314
315 // TODO: this is very conservative
316 return true;
317}
318
319// Asks whether the type in 'context' can ever instantiate to the type
320// in 'friend'.
321static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
322 if (Friend == Context)
323 return true;
324
325 if (!Friend->isDependentType() && !Context->isDependentType())
326 return false;
327
328 // TODO: this is very conservative.
329 return true;
330}
331
332static bool MightInstantiateTo(Sema &S,
333 FunctionDecl *Context,
334 FunctionDecl *Friend) {
335 if (Context->getDeclName() != Friend->getDeclName())
336 return false;
337
338 if (!MightInstantiateTo(S,
339 Context->getDeclContext(),
340 Friend->getDeclContext()))
341 return false;
342
343 CanQual<FunctionProtoType> FriendTy
344 = S.Context.getCanonicalType(Friend->getType())
345 ->getAs<FunctionProtoType>();
346 CanQual<FunctionProtoType> ContextTy
347 = S.Context.getCanonicalType(Context->getType())
348 ->getAs<FunctionProtoType>();
349
350 // There isn't any way that I know of to add qualifiers
351 // during instantiation.
352 if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
353 return false;
354
355 if (FriendTy->getNumArgs() != ContextTy->getNumArgs())
356 return false;
357
358 if (!MightInstantiateTo(S,
359 ContextTy->getResultType(),
360 FriendTy->getResultType()))
361 return false;
362
363 for (unsigned I = 0, E = FriendTy->getNumArgs(); I != E; ++I)
364 if (!MightInstantiateTo(S,
365 ContextTy->getArgType(I),
366 FriendTy->getArgType(I)))
367 return false;
368
369 return true;
370}
371
372static bool MightInstantiateTo(Sema &S,
373 FunctionTemplateDecl *Context,
374 FunctionTemplateDecl *Friend) {
375 return MightInstantiateTo(S,
376 Context->getTemplatedDecl(),
377 Friend->getTemplatedDecl());
378}
379
John McCalla8ae2222010-04-06 21:38:20 +0000380static AccessResult MatchesFriend(Sema &S,
381 const EffectiveContext &EC,
382 const CXXRecordDecl *Friend) {
John McCall39e82882010-03-17 20:01:29 +0000383 if (EC.includesClass(Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000384 return AR_accessible;
John McCall39e82882010-03-17 20:01:29 +0000385
John McCallc62bb642010-03-24 05:22:00 +0000386 if (EC.isDependent()) {
387 CanQualType FriendTy
388 = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
389
390 for (EffectiveContext::record_iterator
391 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
392 CanQualType ContextTy
393 = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
394 if (MightInstantiateTo(S, ContextTy, FriendTy))
John McCalla8ae2222010-04-06 21:38:20 +0000395 return AR_dependent;
John McCallc62bb642010-03-24 05:22:00 +0000396 }
397 }
398
John McCalla8ae2222010-04-06 21:38:20 +0000399 return AR_inaccessible;
John McCall39e82882010-03-17 20:01:29 +0000400}
401
John McCalla8ae2222010-04-06 21:38:20 +0000402static AccessResult MatchesFriend(Sema &S,
403 const EffectiveContext &EC,
404 CanQualType Friend) {
John McCallc62bb642010-03-24 05:22:00 +0000405 if (const RecordType *RT = Friend->getAs<RecordType>())
406 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCall39e82882010-03-17 20:01:29 +0000407
John McCallc62bb642010-03-24 05:22:00 +0000408 // TODO: we can do better than this
409 if (Friend->isDependentType())
John McCalla8ae2222010-04-06 21:38:20 +0000410 return AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000411
John McCalla8ae2222010-04-06 21:38:20 +0000412 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000413}
414
415/// Determines whether the given friend class template matches
416/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000417static AccessResult MatchesFriend(Sema &S,
418 const EffectiveContext &EC,
419 ClassTemplateDecl *Friend) {
420 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000421
John McCall598b4402010-03-25 06:39:04 +0000422 // Check whether the friend is the template of a class in the
423 // context chain.
John McCallc62bb642010-03-24 05:22:00 +0000424 for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator
425 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
426 CXXRecordDecl *Record = *I;
427
John McCall598b4402010-03-25 06:39:04 +0000428 // Figure out whether the current class has a template:
John McCallc62bb642010-03-24 05:22:00 +0000429 ClassTemplateDecl *CTD;
430
431 // A specialization of the template...
432 if (isa<ClassTemplateSpecializationDecl>(Record)) {
433 CTD = cast<ClassTemplateSpecializationDecl>(Record)
434 ->getSpecializedTemplate();
435
436 // ... or the template pattern itself.
437 } else {
438 CTD = Record->getDescribedClassTemplate();
439 if (!CTD) continue;
440 }
441
442 // It's a match.
443 if (Friend == CTD->getCanonicalDecl())
John McCalla8ae2222010-04-06 21:38:20 +0000444 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000445
John McCall598b4402010-03-25 06:39:04 +0000446 // If the context isn't dependent, it can't be a dependent match.
447 if (!EC.isDependent())
448 continue;
449
John McCallc62bb642010-03-24 05:22:00 +0000450 // If the template names don't match, it can't be a dependent
Richard Smith3f1b5d02011-05-05 21:57:07 +0000451 // match.
452 if (CTD->getDeclName() != Friend->getDeclName())
John McCallc62bb642010-03-24 05:22:00 +0000453 continue;
454
455 // If the class's context can't instantiate to the friend's
456 // context, it can't be a dependent match.
457 if (!MightInstantiateTo(S, CTD->getDeclContext(),
458 Friend->getDeclContext()))
459 continue;
460
461 // Otherwise, it's a dependent match.
John McCalla8ae2222010-04-06 21:38:20 +0000462 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000463 }
464
John McCallc62bb642010-03-24 05:22:00 +0000465 return OnFailure;
466}
467
468/// Determines whether the given friend function matches anything in
469/// the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000470static AccessResult MatchesFriend(Sema &S,
471 const EffectiveContext &EC,
472 FunctionDecl *Friend) {
473 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000474
John McCall3dc81f72010-03-27 06:55:49 +0000475 for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
476 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
477 if (Friend == *I)
John McCalla8ae2222010-04-06 21:38:20 +0000478 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000479
John McCall3dc81f72010-03-27 06:55:49 +0000480 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000481 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000482 }
John McCallc62bb642010-03-24 05:22:00 +0000483
John McCall3dc81f72010-03-27 06:55:49 +0000484 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000485}
486
487/// Determines whether the given friend function template matches
488/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000489static AccessResult MatchesFriend(Sema &S,
490 const EffectiveContext &EC,
491 FunctionTemplateDecl *Friend) {
492 if (EC.Functions.empty()) return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000493
John McCalla8ae2222010-04-06 21:38:20 +0000494 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000495
John McCall3dc81f72010-03-27 06:55:49 +0000496 for (llvm::SmallVectorImpl<FunctionDecl*>::const_iterator
497 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCallc62bb642010-03-24 05:22:00 +0000498
John McCall3dc81f72010-03-27 06:55:49 +0000499 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
500 if (!FTD)
501 FTD = (*I)->getDescribedFunctionTemplate();
502 if (!FTD)
503 continue;
John McCallc62bb642010-03-24 05:22:00 +0000504
John McCall3dc81f72010-03-27 06:55:49 +0000505 FTD = FTD->getCanonicalDecl();
506
507 if (Friend == FTD)
John McCalla8ae2222010-04-06 21:38:20 +0000508 return AR_accessible;
John McCall3dc81f72010-03-27 06:55:49 +0000509
510 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000511 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000512 }
513
514 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000515}
516
517/// Determines whether the given friend declaration matches anything
518/// in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000519static AccessResult MatchesFriend(Sema &S,
520 const EffectiveContext &EC,
521 FriendDecl *FriendD) {
John McCall2c2eb122010-10-16 06:59:13 +0000522 // Whitelist accesses if there's an invalid or unsupported friend
523 // declaration.
524 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCallde3fd222010-10-12 23:13:28 +0000525 return AR_accessible;
526
John McCall15ad0962010-03-25 18:04:51 +0000527 if (TypeSourceInfo *T = FriendD->getFriendType())
528 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCallc62bb642010-03-24 05:22:00 +0000529
530 NamedDecl *Friend
531 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCall39e82882010-03-17 20:01:29 +0000532
533 // FIXME: declarations with dependent or templated scope.
534
John McCallc62bb642010-03-24 05:22:00 +0000535 if (isa<ClassTemplateDecl>(Friend))
536 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000537
John McCallc62bb642010-03-24 05:22:00 +0000538 if (isa<FunctionTemplateDecl>(Friend))
539 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000540
John McCallc62bb642010-03-24 05:22:00 +0000541 if (isa<CXXRecordDecl>(Friend))
542 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000543
John McCallc62bb642010-03-24 05:22:00 +0000544 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
545 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000546}
547
John McCalla8ae2222010-04-06 21:38:20 +0000548static AccessResult GetFriendKind(Sema &S,
549 const EffectiveContext &EC,
550 const CXXRecordDecl *Class) {
551 AccessResult OnFailure = AR_inaccessible;
John McCallfb803d72010-03-17 04:58:56 +0000552
John McCall16927f62010-03-12 01:19:31 +0000553 // Okay, check friends.
554 for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
555 E = Class->friend_end(); I != E; ++I) {
556 FriendDecl *Friend = *I;
557
John McCall39e82882010-03-17 20:01:29 +0000558 switch (MatchesFriend(S, EC, Friend)) {
John McCalla8ae2222010-04-06 21:38:20 +0000559 case AR_accessible:
560 return AR_accessible;
John McCall16927f62010-03-12 01:19:31 +0000561
John McCalla8ae2222010-04-06 21:38:20 +0000562 case AR_inaccessible:
563 continue;
564
565 case AR_dependent:
566 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000567 break;
John McCall16927f62010-03-12 01:19:31 +0000568 }
John McCall16927f62010-03-12 01:19:31 +0000569 }
570
571 // That's it, give up.
John McCallfb803d72010-03-17 04:58:56 +0000572 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000573}
574
John McCall96329672010-08-28 07:56:00 +0000575namespace {
576
577/// A helper class for checking for a friend which will grant access
578/// to a protected instance member.
579struct ProtectedFriendContext {
580 Sema &S;
581 const EffectiveContext &EC;
582 const CXXRecordDecl *NamingClass;
583 bool CheckDependent;
584 bool EverDependent;
585
586 /// The path down to the current base class.
587 llvm::SmallVector<const CXXRecordDecl*, 20> CurPath;
588
589 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
590 const CXXRecordDecl *InstanceContext,
591 const CXXRecordDecl *NamingClass)
592 : S(S), EC(EC), NamingClass(NamingClass),
593 CheckDependent(InstanceContext->isDependentContext() ||
594 NamingClass->isDependentContext()),
595 EverDependent(false) {}
596
John McCall1177ff12010-08-28 08:47:21 +0000597 /// Check classes in the current path for friendship, starting at
598 /// the given index.
599 bool checkFriendshipAlongPath(unsigned I) {
600 assert(I < CurPath.size());
601 for (unsigned E = CurPath.size(); I != E; ++I) {
602 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall96329672010-08-28 07:56:00 +0000603 case AR_accessible: return true;
604 case AR_inaccessible: continue;
605 case AR_dependent: EverDependent = true; continue;
606 }
607 }
608 return false;
609 }
610
611 /// Perform a search starting at the given class.
John McCall1177ff12010-08-28 08:47:21 +0000612 ///
613 /// PrivateDepth is the index of the last (least derived) class
614 /// along the current path such that a notional public member of
615 /// the final class in the path would have access in that class.
616 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall96329672010-08-28 07:56:00 +0000617 // If we ever reach the naming class, check the current path for
618 // friendship. We can also stop recursing because we obviously
619 // won't find the naming class there again.
John McCall1177ff12010-08-28 08:47:21 +0000620 if (Cur == NamingClass)
621 return checkFriendshipAlongPath(PrivateDepth);
John McCall96329672010-08-28 07:56:00 +0000622
623 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
624 EverDependent = true;
625
626 // Recurse into the base classes.
627 for (CXXRecordDecl::base_class_const_iterator
628 I = Cur->bases_begin(), E = Cur->bases_end(); I != E; ++I) {
629
John McCall1177ff12010-08-28 08:47:21 +0000630 // If this is private inheritance, then a public member of the
631 // base will not have any access in classes derived from Cur.
632 unsigned BasePrivateDepth = PrivateDepth;
633 if (I->getAccessSpecifier() == AS_private)
634 BasePrivateDepth = CurPath.size() - 1;
John McCall96329672010-08-28 07:56:00 +0000635
636 const CXXRecordDecl *RD;
637
638 QualType T = I->getType();
639 if (const RecordType *RT = T->getAs<RecordType>()) {
640 RD = cast<CXXRecordDecl>(RT->getDecl());
641 } else if (const InjectedClassNameType *IT
642 = T->getAs<InjectedClassNameType>()) {
643 RD = IT->getDecl();
644 } else {
645 assert(T->isDependentType() && "non-dependent base wasn't a record?");
646 EverDependent = true;
647 continue;
648 }
649
650 // Recurse. We don't need to clean up if this returns true.
John McCall1177ff12010-08-28 08:47:21 +0000651 CurPath.push_back(RD);
652 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
653 return true;
654 CurPath.pop_back();
John McCall96329672010-08-28 07:56:00 +0000655 }
656
John McCall96329672010-08-28 07:56:00 +0000657 return false;
658 }
John McCall1177ff12010-08-28 08:47:21 +0000659
660 bool findFriendship(const CXXRecordDecl *Cur) {
661 assert(CurPath.empty());
662 CurPath.push_back(Cur);
663 return findFriendship(Cur, 0);
664 }
John McCall96329672010-08-28 07:56:00 +0000665};
666}
667
668/// Search for a class P that EC is a friend of, under the constraint
669/// InstanceContext <= P <= NamingClass
670/// and with the additional restriction that a protected member of
671/// NamingClass would have some natural access in P.
672///
673/// That second condition isn't actually quite right: the condition in
674/// the standard is whether the target would have some natural access
675/// in P. The difference is that the target might be more accessible
676/// along some path not passing through NamingClass. Allowing that
677/// introduces two problems:
678/// - It breaks encapsulation because you can suddenly access a
679/// forbidden base class's members by subclassing it elsewhere.
680/// - It makes access substantially harder to compute because it
681/// breaks the hill-climbing algorithm: knowing that the target is
682/// accessible in some base class would no longer let you change
683/// the question solely to whether the base class is accessible,
684/// because the original target might have been more accessible
685/// because of crazy subclassing.
686/// So we don't implement that.
687static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
688 const CXXRecordDecl *InstanceContext,
689 const CXXRecordDecl *NamingClass) {
690 assert(InstanceContext->getCanonicalDecl() == InstanceContext);
691 assert(NamingClass->getCanonicalDecl() == NamingClass);
692
693 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
694 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
695 if (PRC.EverDependent) return AR_dependent;
696 return AR_inaccessible;
697}
698
John McCalla8ae2222010-04-06 21:38:20 +0000699static AccessResult HasAccess(Sema &S,
700 const EffectiveContext &EC,
701 const CXXRecordDecl *NamingClass,
702 AccessSpecifier Access,
703 const AccessTarget &Target) {
John McCalld79b4d82010-04-02 00:03:43 +0000704 assert(NamingClass->getCanonicalDecl() == NamingClass &&
705 "declaration should be canonicalized before being passed here");
706
John McCalla8ae2222010-04-06 21:38:20 +0000707 if (Access == AS_public) return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000708 assert(Access == AS_private || Access == AS_protected);
709
John McCalla8ae2222010-04-06 21:38:20 +0000710 AccessResult OnFailure = AR_inaccessible;
711
John McCalld79b4d82010-04-02 00:03:43 +0000712 for (EffectiveContext::record_iterator
713 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
714 // All the declarations in EC have been canonicalized, so pointer
715 // equality from this point on will work fine.
716 const CXXRecordDecl *ECRecord = *I;
717
718 // [B2] and [M2]
John McCalla8ae2222010-04-06 21:38:20 +0000719 if (Access == AS_private) {
720 if (ECRecord == NamingClass)
721 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000722
John McCall97205142010-05-04 05:11:27 +0000723 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
724 OnFailure = AR_dependent;
725
John McCalld79b4d82010-04-02 00:03:43 +0000726 // [B3] and [M3]
John McCalla8ae2222010-04-06 21:38:20 +0000727 } else {
728 assert(Access == AS_protected);
729 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
730 case AR_accessible: break;
731 case AR_inaccessible: continue;
732 case AR_dependent: OnFailure = AR_dependent; continue;
733 }
734
735 if (!Target.hasInstanceContext())
736 return AR_accessible;
737
738 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
739 if (!InstanceContext) {
740 OnFailure = AR_dependent;
741 continue;
742 }
743
744 // C++ [class.protected]p1:
745 // An additional access check beyond those described earlier in
746 // [class.access] is applied when a non-static data member or
747 // non-static member function is a protected member of its naming
748 // class. As described earlier, access to a protected member is
749 // granted because the reference occurs in a friend or member of
750 // some class C. If the access is to form a pointer to member,
751 // the nested-name-specifier shall name C or a class derived from
752 // C. All other accesses involve a (possibly implicit) object
753 // expression. In this case, the class of the object expression
754 // shall be C or a class derived from C.
755 //
756 // We interpret this as a restriction on [M3]. Most of the
757 // conditions are encoded by not having any instance context.
758 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
759 case AR_accessible: return AR_accessible;
760 case AR_inaccessible: continue;
761 case AR_dependent: OnFailure = AR_dependent; continue;
762 }
763 }
John McCalld79b4d82010-04-02 00:03:43 +0000764 }
765
John McCall96329672010-08-28 07:56:00 +0000766 // [M3] and [B3] say that, if the target is protected in N, we grant
767 // access if the access occurs in a friend or member of some class P
768 // that's a subclass of N and where the target has some natural
769 // access in P. The 'member' aspect is easy to handle because P
770 // would necessarily be one of the effective-context records, and we
771 // address that above. The 'friend' aspect is completely ridiculous
772 // to implement because there are no restrictions at all on P
773 // *unless* the [class.protected] restriction applies. If it does,
774 // however, we should ignore whether the naming class is a friend,
775 // and instead rely on whether any potential P is a friend.
John McCalla8ae2222010-04-06 21:38:20 +0000776 if (Access == AS_protected && Target.hasInstanceContext()) {
777 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
778 if (!InstanceContext) return AR_dependent;
John McCall96329672010-08-28 07:56:00 +0000779 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
780 case AR_accessible: return AR_accessible;
John McCalla8ae2222010-04-06 21:38:20 +0000781 case AR_inaccessible: return OnFailure;
782 case AR_dependent: return AR_dependent;
783 }
John McCallc6af8f42010-08-28 08:10:32 +0000784 llvm_unreachable("impossible friendship kind");
John McCalla8ae2222010-04-06 21:38:20 +0000785 }
786
787 switch (GetFriendKind(S, EC, NamingClass)) {
788 case AR_accessible: return AR_accessible;
789 case AR_inaccessible: return OnFailure;
790 case AR_dependent: return AR_dependent;
791 }
792
793 // Silence bogus warnings
794 llvm_unreachable("impossible friendship kind");
795 return OnFailure;
John McCalld79b4d82010-04-02 00:03:43 +0000796}
797
John McCall5b0829a2010-02-10 09:31:12 +0000798/// Finds the best path from the naming class to the declaring class,
799/// taking friend declarations into account.
800///
John McCalld79b4d82010-04-02 00:03:43 +0000801/// C++0x [class.access.base]p5:
802/// A member m is accessible at the point R when named in class N if
803/// [M1] m as a member of N is public, or
804/// [M2] m as a member of N is private, and R occurs in a member or
805/// friend of class N, or
806/// [M3] m as a member of N is protected, and R occurs in a member or
807/// friend of class N, or in a member or friend of a class P
808/// derived from N, where m as a member of P is public, private,
809/// or protected, or
810/// [M4] there exists a base class B of N that is accessible at R, and
811/// m is accessible at R when named in class B.
812///
813/// C++0x [class.access.base]p4:
814/// A base class B of N is accessible at R, if
815/// [B1] an invented public member of B would be a public member of N, or
816/// [B2] R occurs in a member or friend of class N, and an invented public
817/// member of B would be a private or protected member of N, or
818/// [B3] R occurs in a member or friend of a class P derived from N, and an
819/// invented public member of B would be a private or protected member
820/// of P, or
821/// [B4] there exists a class S such that B is a base class of S accessible
822/// at R and S is a base class of N accessible at R.
823///
824/// Along a single inheritance path we can restate both of these
825/// iteratively:
826///
827/// First, we note that M1-4 are equivalent to B1-4 if the member is
828/// treated as a notional base of its declaring class with inheritance
829/// access equivalent to the member's access. Therefore we need only
830/// ask whether a class B is accessible from a class N in context R.
831///
832/// Let B_1 .. B_n be the inheritance path in question (i.e. where
833/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
834/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
835/// closest accessible base in the path:
836/// Access(a, b) = (* access on the base specifier from a to b *)
837/// Merge(a, forbidden) = forbidden
838/// Merge(a, private) = forbidden
839/// Merge(a, b) = min(a,b)
840/// Accessible(c, forbidden) = false
841/// Accessible(c, private) = (R is c) || IsFriend(c, R)
842/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
843/// Accessible(c, public) = true
844/// ACAB(n) = public
845/// ACAB(i) =
846/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
847/// if Accessible(B_i, AccessToBase) then public else AccessToBase
848///
849/// B is an accessible base of N at R iff ACAB(1) = public.
850///
John McCalla8ae2222010-04-06 21:38:20 +0000851/// \param FinalAccess the access of the "final step", or AS_public if
John McCalla332b952010-03-18 23:49:19 +0000852/// there is no final step.
John McCall5b0829a2010-02-10 09:31:12 +0000853/// \return null if friendship is dependent
854static CXXBasePath *FindBestPath(Sema &S,
855 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000856 AccessTarget &Target,
John McCalla332b952010-03-18 23:49:19 +0000857 AccessSpecifier FinalAccess,
John McCall5b0829a2010-02-10 09:31:12 +0000858 CXXBasePaths &Paths) {
859 // Derive the paths to the desired base.
John McCalla8ae2222010-04-06 21:38:20 +0000860 const CXXRecordDecl *Derived = Target.getNamingClass();
861 const CXXRecordDecl *Base = Target.getDeclaringClass();
862
863 // FIXME: fail correctly when there are dependent paths.
864 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
865 Paths);
John McCall5b0829a2010-02-10 09:31:12 +0000866 assert(isDerived && "derived class not actually derived from base");
867 (void) isDerived;
868
869 CXXBasePath *BestPath = 0;
870
John McCalla332b952010-03-18 23:49:19 +0000871 assert(FinalAccess != AS_none && "forbidden access after declaring class");
872
John McCallc62bb642010-03-24 05:22:00 +0000873 bool AnyDependent = false;
874
John McCall5b0829a2010-02-10 09:31:12 +0000875 // Derive the friend-modified access along each path.
876 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
877 PI != PE; ++PI) {
John McCalla8ae2222010-04-06 21:38:20 +0000878 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +0000879
880 // Walk through the path backwards.
John McCalla332b952010-03-18 23:49:19 +0000881 AccessSpecifier PathAccess = FinalAccess;
John McCall5b0829a2010-02-10 09:31:12 +0000882 CXXBasePath::iterator I = PI->end(), E = PI->begin();
883 while (I != E) {
884 --I;
885
John McCalla332b952010-03-18 23:49:19 +0000886 assert(PathAccess != AS_none);
887
888 // If the declaration is a private member of a base class, there
889 // is no level of friendship in derived classes that can make it
890 // accessible.
891 if (PathAccess == AS_private) {
892 PathAccess = AS_none;
893 break;
894 }
895
John McCalla8ae2222010-04-06 21:38:20 +0000896 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
897
John McCall5b0829a2010-02-10 09:31:12 +0000898 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalld79b4d82010-04-02 00:03:43 +0000899 PathAccess = std::max(PathAccess, BaseAccess);
John McCalla8ae2222010-04-06 21:38:20 +0000900
901 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
902 case AR_inaccessible: break;
903 case AR_accessible:
904 PathAccess = AS_public;
905
906 // Future tests are not against members and so do not have
907 // instance context.
908 Target.suppressInstanceContext();
909 break;
910 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +0000911 AnyDependent = true;
912 goto Next;
John McCall5b0829a2010-02-10 09:31:12 +0000913 }
John McCall5b0829a2010-02-10 09:31:12 +0000914 }
915
916 // Note that we modify the path's Access field to the
917 // friend-modified access.
918 if (BestPath == 0 || PathAccess < BestPath->Access) {
919 BestPath = &*PI;
920 BestPath->Access = PathAccess;
John McCallc62bb642010-03-24 05:22:00 +0000921
922 // Short-circuit if we found a public path.
923 if (BestPath->Access == AS_public)
924 return BestPath;
John McCall5b0829a2010-02-10 09:31:12 +0000925 }
John McCallc62bb642010-03-24 05:22:00 +0000926
927 Next: ;
John McCall5b0829a2010-02-10 09:31:12 +0000928 }
929
John McCallc62bb642010-03-24 05:22:00 +0000930 assert((!BestPath || BestPath->Access != AS_public) &&
931 "fell out of loop with public path");
932
933 // We didn't find a public path, but at least one path was subject
934 // to dependent friendship, so delay the check.
935 if (AnyDependent)
936 return 0;
937
John McCall5b0829a2010-02-10 09:31:12 +0000938 return BestPath;
939}
940
John McCall417e7442010-09-03 04:56:05 +0000941/// Given that an entity has protected natural access, check whether
942/// access might be denied because of the protected member access
943/// restriction.
944///
945/// \return true if a note was emitted
946static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
947 AccessTarget &Target) {
948 // Only applies to instance accesses.
949 if (!Target.hasInstanceContext())
950 return false;
951 assert(Target.isMemberAccess());
952 NamedDecl *D = Target.getTargetDecl();
953
954 const CXXRecordDecl *DeclaringClass = Target.getDeclaringClass();
955 DeclaringClass = DeclaringClass->getCanonicalDecl();
956
957 for (EffectiveContext::record_iterator
958 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
959 const CXXRecordDecl *ECRecord = *I;
960 switch (IsDerivedFromInclusive(ECRecord, DeclaringClass)) {
961 case AR_accessible: break;
962 case AR_inaccessible: continue;
963 case AR_dependent: continue;
964 }
965
966 // The effective context is a subclass of the declaring class.
967 // If that class isn't a superclass of the instance context,
968 // then the [class.protected] restriction applies.
969
970 // To get this exactly right, this might need to be checked more
971 // holistically; it's not necessarily the case that gaining
972 // access here would grant us access overall.
973
974 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
975 assert(InstanceContext && "diagnosing dependent access");
976
977 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
978 case AR_accessible: continue;
979 case AR_dependent: continue;
980 case AR_inaccessible:
981 S.Diag(D->getLocation(), diag::note_access_protected_restricted)
982 << (InstanceContext != Target.getNamingClass()->getCanonicalDecl())
983 << S.Context.getTypeDeclType(InstanceContext)
984 << S.Context.getTypeDeclType(ECRecord);
985 return true;
986 }
987 }
988
989 return false;
990}
991
John McCall5b0829a2010-02-10 09:31:12 +0000992/// Diagnose the path which caused the given declaration or base class
993/// to become inaccessible.
994static void DiagnoseAccessPath(Sema &S,
995 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000996 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +0000997 AccessSpecifier Access = Entity.getAccess();
John McCalla8ae2222010-04-06 21:38:20 +0000998 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalld79b4d82010-04-02 00:03:43 +0000999 NamingClass = NamingClass->getCanonicalDecl();
1000
John McCalla8ae2222010-04-06 21:38:20 +00001001 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
1002 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCalld79b4d82010-04-02 00:03:43 +00001003
John McCall553c0792010-01-23 00:46:32 +00001004 // Easy case: the decl's natural access determined its path access.
John McCall5b0829a2010-02-10 09:31:12 +00001005 // We have to check against AS_private here in case Access is AS_none,
1006 // indicating a non-public member of a private base class.
John McCall5b0829a2010-02-10 09:31:12 +00001007 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
John McCalla8ae2222010-04-06 21:38:20 +00001008 switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
1009 case AR_inaccessible: {
John McCall417e7442010-09-03 04:56:05 +00001010 if (Access == AS_protected &&
1011 TryDiagnoseProtectedAccess(S, EC, Entity))
1012 return;
1013
John McCallf551aca2010-10-20 08:15:06 +00001014 // Find an original declaration.
1015 while (D->isOutOfLine()) {
1016 NamedDecl *PrevDecl = 0;
Richard Smithdda56e42011-04-15 14:24:37 +00001017 if (VarDecl *VD = dyn_cast<VarDecl>(D))
1018 PrevDecl = VD->getPreviousDeclaration();
1019 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1020 PrevDecl = FD->getPreviousDeclaration();
1021 else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
1022 PrevDecl = TND->getPreviousDeclaration();
1023 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
John McCallf551aca2010-10-20 08:15:06 +00001024 if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1025 break;
Richard Smithdda56e42011-04-15 14:24:37 +00001026 PrevDecl = TD->getPreviousDeclaration();
John McCallf551aca2010-10-20 08:15:06 +00001027 }
1028 if (!PrevDecl) break;
1029 D = PrevDecl;
1030 }
1031
1032 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
1033 Decl *ImmediateChild;
1034 if (D->getDeclContext() == DeclaringClass)
1035 ImmediateChild = D;
1036 else {
1037 DeclContext *DC = D->getDeclContext();
1038 while (DC->getParent() != DeclaringClass)
1039 DC = DC->getParent();
1040 ImmediateChild = cast<Decl>(DC);
1041 }
1042
1043 // Check whether there's an AccessSpecDecl preceding this in the
1044 // chain of the DeclContext.
1045 bool Implicit = true;
1046 for (CXXRecordDecl::decl_iterator
1047 I = DeclaringClass->decls_begin(), E = DeclaringClass->decls_end();
1048 I != E; ++I) {
1049 if (*I == ImmediateChild) break;
1050 if (isa<AccessSpecDecl>(*I)) {
1051 Implicit = false;
1052 break;
1053 }
1054 }
1055
John McCall5b0829a2010-02-10 09:31:12 +00001056 S.Diag(D->getLocation(), diag::note_access_natural)
1057 << (unsigned) (Access == AS_protected)
John McCallf551aca2010-10-20 08:15:06 +00001058 << Implicit;
John McCall5b0829a2010-02-10 09:31:12 +00001059 return;
1060 }
1061
John McCalla8ae2222010-04-06 21:38:20 +00001062 case AR_accessible: break;
John McCall5b0829a2010-02-10 09:31:12 +00001063
John McCalla8ae2222010-04-06 21:38:20 +00001064 case AR_dependent:
1065 llvm_unreachable("can't diagnose dependent access failures");
John McCall5b0829a2010-02-10 09:31:12 +00001066 return;
1067 }
1068 }
1069
1070 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001071 CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
John McCall5b0829a2010-02-10 09:31:12 +00001072
1073 CXXBasePath::iterator I = Path.end(), E = Path.begin();
1074 while (I != E) {
1075 --I;
1076
1077 const CXXBaseSpecifier *BS = I->Base;
1078 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
1079
1080 // If this is public inheritance, or the derived class is a friend,
1081 // skip this step.
1082 if (BaseAccess == AS_public)
1083 continue;
1084
1085 switch (GetFriendKind(S, EC, I->Class)) {
John McCalla8ae2222010-04-06 21:38:20 +00001086 case AR_accessible: continue;
1087 case AR_inaccessible: break;
1088 case AR_dependent:
1089 llvm_unreachable("can't diagnose dependent access failures");
John McCall5b0829a2010-02-10 09:31:12 +00001090 }
1091
1092 // Check whether this base specifier is the tighest point
1093 // constraining access. We have to check against AS_private for
1094 // the same reasons as above.
1095 if (BaseAccess == AS_private || BaseAccess >= Access) {
1096
1097 // We're constrained by inheritance, but we want to say
1098 // "declared private here" if we're diagnosing a hierarchy
1099 // conversion and this is the final step.
1100 unsigned diagnostic;
1101 if (D) diagnostic = diag::note_access_constrained_by_path;
1102 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
1103 else diagnostic = diag::note_access_constrained_by_path;
1104
1105 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
1106 << BS->getSourceRange()
1107 << (BaseAccess == AS_protected)
1108 << (BS->getAccessSpecifierAsWritten() == AS_none);
Douglas Gregored2540d2010-05-28 04:34:55 +00001109
1110 if (D)
1111 S.Diag(D->getLocation(), diag::note_field_decl);
1112
John McCall5b0829a2010-02-10 09:31:12 +00001113 return;
1114 }
1115 }
1116
1117 llvm_unreachable("access not apparently constrained by path");
1118}
1119
John McCall1064d7e2010-03-16 05:22:47 +00001120static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall5b0829a2010-02-10 09:31:12 +00001121 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +00001122 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001123 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalla8ae2222010-04-06 21:38:20 +00001124 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
1125 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
John McCalld79b4d82010-04-02 00:03:43 +00001126
1127 S.Diag(Loc, Entity.getDiag())
1128 << (Entity.getAccess() == AS_protected)
1129 << (D ? D->getDeclName() : DeclarationName())
1130 << S.Context.getTypeDeclType(NamingClass)
1131 << S.Context.getTypeDeclType(DeclaringClass);
1132 DiagnoseAccessPath(S, EC, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001133}
1134
Francois Pichetefb1af92011-05-23 03:43:44 +00001135/// MSVC has a bug where if during an using declaration name lookup,
1136/// the declaration found is unaccessible (private) and that declaration
1137/// was bring into scope via another using declaration whose target
1138/// declaration is accessible (public) then no error is generated.
1139/// Example:
1140/// class A {
1141/// public:
1142/// int f();
1143/// };
1144/// class B : public A {
1145/// private:
1146/// using A::f;
1147/// };
1148/// class C : public B {
1149/// private:
1150/// using B::f;
1151/// };
1152///
1153/// Here, B::f is private so this should fail in Standard C++, but
1154/// because B::f refers to A::f which is public MSVC accepts it.
1155static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S,
1156 SourceLocation AccessLoc,
1157 AccessTarget &Entity) {
1158 if (UsingShadowDecl *Shadow =
1159 dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) {
1160 const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl();
1161 if (Entity.getTargetDecl()->getAccess() == AS_private &&
1162 (OrigDecl->getAccess() == AS_public ||
1163 OrigDecl->getAccess() == AS_protected)) {
1164 S.Diag(AccessLoc, diag::war_ms_using_declaration_inaccessible)
1165 << Shadow->getUsingDecl()->getQualifiedNameAsString()
1166 << OrigDecl->getQualifiedNameAsString();
1167 return true;
1168 }
1169 }
1170 return false;
1171}
1172
John McCalld79b4d82010-04-02 00:03:43 +00001173/// Determines whether the accessed entity is accessible. Public members
1174/// have been weeded out by this point.
John McCalla8ae2222010-04-06 21:38:20 +00001175static AccessResult IsAccessible(Sema &S,
1176 const EffectiveContext &EC,
1177 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001178 // Determine the actual naming class.
1179 CXXRecordDecl *NamingClass = Entity.getNamingClass();
1180 while (NamingClass->isAnonymousStructOrUnion())
1181 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
1182 NamingClass = NamingClass->getCanonicalDecl();
John McCall5b0829a2010-02-10 09:31:12 +00001183
John McCalld79b4d82010-04-02 00:03:43 +00001184 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1185 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1186
1187 // Before we try to recalculate access paths, try to white-list
1188 // accesses which just trade in on the final step, i.e. accesses
1189 // which don't require [M4] or [B4]. These are by far the most
John McCalla8ae2222010-04-06 21:38:20 +00001190 // common forms of privileged access.
John McCalld79b4d82010-04-02 00:03:43 +00001191 if (UnprivilegedAccess != AS_none) {
John McCalla8ae2222010-04-06 21:38:20 +00001192 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1193 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +00001194 // This is actually an interesting policy decision. We don't
1195 // *have* to delay immediately here: we can do the full access
1196 // calculation in the hope that friendship on some intermediate
1197 // class will make the declaration accessible non-dependently.
1198 // But that's not cheap, and odds are very good (note: assertion
1199 // made without data) that the friend declaration will determine
1200 // access.
John McCalla8ae2222010-04-06 21:38:20 +00001201 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001202
John McCalla8ae2222010-04-06 21:38:20 +00001203 case AR_accessible: return AR_accessible;
1204 case AR_inaccessible: break;
John McCalld79b4d82010-04-02 00:03:43 +00001205 }
1206 }
1207
John McCalla8ae2222010-04-06 21:38:20 +00001208 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +00001209
John McCalld79b4d82010-04-02 00:03:43 +00001210 // We lower member accesses to base accesses by pretending that the
1211 // member is a base class of its declaring class.
1212 AccessSpecifier FinalAccess;
1213
John McCall5b0829a2010-02-10 09:31:12 +00001214 if (Entity.isMemberAccess()) {
John McCalld79b4d82010-04-02 00:03:43 +00001215 // Determine if the declaration is accessible from EC when named
1216 // in its declaring class.
John McCall5b0829a2010-02-10 09:31:12 +00001217 NamedDecl *Target = Entity.getTargetDecl();
John McCalla8ae2222010-04-06 21:38:20 +00001218 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall5b0829a2010-02-10 09:31:12 +00001219
John McCalld79b4d82010-04-02 00:03:43 +00001220 FinalAccess = Target->getAccess();
John McCalla8ae2222010-04-06 21:38:20 +00001221 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1222 case AR_accessible:
1223 FinalAccess = AS_public;
1224 break;
1225 case AR_inaccessible: break;
1226 case AR_dependent: return AR_dependent; // see above
John McCall5b0829a2010-02-10 09:31:12 +00001227 }
1228
John McCalld79b4d82010-04-02 00:03:43 +00001229 if (DeclaringClass == NamingClass)
John McCalla8ae2222010-04-06 21:38:20 +00001230 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
1231
1232 Entity.suppressInstanceContext();
John McCalld79b4d82010-04-02 00:03:43 +00001233 } else {
1234 FinalAccess = AS_public;
John McCall5b0829a2010-02-10 09:31:12 +00001235 }
1236
John McCalla8ae2222010-04-06 21:38:20 +00001237 assert(Entity.getDeclaringClass() != NamingClass);
John McCall5b0829a2010-02-10 09:31:12 +00001238
1239 // Append the declaration's access if applicable.
1240 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001241 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCallc62bb642010-03-24 05:22:00 +00001242 if (!Path)
John McCalla8ae2222010-04-06 21:38:20 +00001243 return AR_dependent;
John McCall553c0792010-01-23 00:46:32 +00001244
John McCalld79b4d82010-04-02 00:03:43 +00001245 assert(Path->Access <= UnprivilegedAccess &&
1246 "access along best path worse than direct?");
1247 if (Path->Access == AS_public)
John McCalla8ae2222010-04-06 21:38:20 +00001248 return AR_accessible;
1249 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +00001250}
1251
John McCalla8ae2222010-04-06 21:38:20 +00001252static void DelayDependentAccess(Sema &S,
1253 const EffectiveContext &EC,
1254 SourceLocation Loc,
1255 const AccessTarget &Entity) {
John McCallc62bb642010-03-24 05:22:00 +00001256 assert(EC.isDependent() && "delaying non-dependent access");
John McCall816d75b2010-03-24 07:46:06 +00001257 DeclContext *DC = EC.getInnerContext();
John McCallc62bb642010-03-24 05:22:00 +00001258 assert(DC->isDependentContext() && "delaying non-dependent access");
1259 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1260 Loc,
1261 Entity.isMemberAccess(),
1262 Entity.getAccess(),
1263 Entity.getTargetDecl(),
1264 Entity.getNamingClass(),
John McCalla8ae2222010-04-06 21:38:20 +00001265 Entity.getBaseObjectType(),
John McCallc62bb642010-03-24 05:22:00 +00001266 Entity.getDiag());
John McCall553c0792010-01-23 00:46:32 +00001267}
1268
John McCall5b0829a2010-02-10 09:31:12 +00001269/// Checks access to an entity from the given effective context.
John McCalla8ae2222010-04-06 21:38:20 +00001270static AccessResult CheckEffectiveAccess(Sema &S,
1271 const EffectiveContext &EC,
1272 SourceLocation Loc,
1273 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001274 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall553c0792010-01-23 00:46:32 +00001275
Francois Pichetefb1af92011-05-23 03:43:44 +00001276 if (S.getLangOptions().Microsoft &&
1277 IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity))
1278 return AR_accessible;
1279
John McCalld79b4d82010-04-02 00:03:43 +00001280 switch (IsAccessible(S, EC, Entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001281 case AR_dependent:
1282 DelayDependentAccess(S, EC, Loc, Entity);
1283 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001284
John McCalla8ae2222010-04-06 21:38:20 +00001285 case AR_inaccessible:
John McCalld79b4d82010-04-02 00:03:43 +00001286 if (!Entity.isQuiet())
1287 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCalla8ae2222010-04-06 21:38:20 +00001288 return AR_inaccessible;
John McCalld79b4d82010-04-02 00:03:43 +00001289
John McCalla8ae2222010-04-06 21:38:20 +00001290 case AR_accessible:
1291 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +00001292 }
1293
John McCalla8ae2222010-04-06 21:38:20 +00001294 // silence unnecessary warning
1295 llvm_unreachable("invalid access result");
1296 return AR_accessible;
John McCall5b0829a2010-02-10 09:31:12 +00001297}
John McCall553c0792010-01-23 00:46:32 +00001298
John McCall5b0829a2010-02-10 09:31:12 +00001299static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCalla8ae2222010-04-06 21:38:20 +00001300 AccessTarget &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +00001301 // If the access path is public, it's accessible everywhere.
1302 if (Entity.getAccess() == AS_public)
1303 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001304
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001305 if (S.SuppressAccessChecking)
1306 return Sema::AR_accessible;
1307
John McCallc1465822011-02-14 07:13:47 +00001308 // If we're currently parsing a declaration, we may need to delay
1309 // access control checking, because our effective context might be
1310 // different based on what the declaration comes out as.
1311 //
1312 // For example, we might be parsing a declaration with a scope
1313 // specifier, like this:
1314 // A::private_type A::foo() { ... }
1315 //
1316 // Or we might be parsing something that will turn out to be a friend:
1317 // void foo(A::private_type);
1318 // void B::foo(A::private_type);
1319 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1320 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall5b0829a2010-02-10 09:31:12 +00001321 return Sema::AR_delayed;
John McCall553c0792010-01-23 00:46:32 +00001322 }
1323
John McCalla8ae2222010-04-06 21:38:20 +00001324 EffectiveContext EC(S.CurContext);
1325 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1326 case AR_accessible: return Sema::AR_accessible;
1327 case AR_inaccessible: return Sema::AR_inaccessible;
1328 case AR_dependent: return Sema::AR_dependent;
1329 }
1330 llvm_unreachable("falling off end");
1331 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001332}
1333
John McCall9743e8d2011-02-15 22:51:53 +00001334void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *decl) {
1335 // Access control for names used in the declarations of functions
1336 // and function templates should normally be evaluated in the context
1337 // of the declaration, just in case it's a friend of something.
1338 // However, this does not apply to local extern declarations.
1339
1340 DeclContext *DC = decl->getDeclContext();
1341 if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
1342 if (!DC->isFunctionOrMethod()) DC = fn;
1343 } else if (FunctionTemplateDecl *fnt = dyn_cast<FunctionTemplateDecl>(decl)) {
1344 // Never a local declaration.
1345 DC = fnt->getTemplatedDecl();
1346 }
1347
Chandler Carruthaad30072010-04-18 08:23:21 +00001348 EffectiveContext EC(DC);
John McCall86121512010-01-27 03:50:35 +00001349
John McCalla8ae2222010-04-06 21:38:20 +00001350 AccessTarget Target(DD.getAccessData());
1351
1352 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall86121512010-01-27 03:50:35 +00001353 DD.Triggered = true;
1354}
1355
John McCallc62bb642010-03-24 05:22:00 +00001356void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1357 const MultiLevelTemplateArgumentList &TemplateArgs) {
1358 SourceLocation Loc = DD.getAccessLoc();
1359 AccessSpecifier Access = DD.getAccess();
1360
1361 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1362 TemplateArgs);
1363 if (!NamingD) return;
1364 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1365 TemplateArgs);
1366 if (!TargetD) return;
1367
1368 if (DD.isAccessToMember()) {
John McCalla8ae2222010-04-06 21:38:20 +00001369 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1370 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1371 QualType BaseObjectType = DD.getAccessBaseObjectType();
1372 if (!BaseObjectType.isNull()) {
1373 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1374 DeclarationName());
1375 if (BaseObjectType.isNull()) return;
1376 }
1377
1378 AccessTarget Entity(Context,
1379 AccessTarget::Member,
1380 NamingClass,
1381 DeclAccessPair::make(TargetDecl, Access),
1382 BaseObjectType);
John McCallc62bb642010-03-24 05:22:00 +00001383 Entity.setDiag(DD.getDiagnostic());
1384 CheckAccess(*this, Loc, Entity);
1385 } else {
John McCalla8ae2222010-04-06 21:38:20 +00001386 AccessTarget Entity(Context,
1387 AccessTarget::Base,
1388 cast<CXXRecordDecl>(TargetD),
1389 cast<CXXRecordDecl>(NamingD),
1390 Access);
John McCallc62bb642010-03-24 05:22:00 +00001391 Entity.setDiag(DD.getDiagnostic());
1392 CheckAccess(*this, Loc, Entity);
1393 }
1394}
1395
John McCall5b0829a2010-02-10 09:31:12 +00001396Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001397 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001398 if (!getLangOptions().AccessControl ||
1399 !E->getNamingClass() ||
John McCalla0296f72010-03-19 07:35:19 +00001400 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001401 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001402
John McCalla8ae2222010-04-06 21:38:20 +00001403 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1404 Found, QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001405 Entity.setDiag(diag::err_access) << E->getSourceRange();
1406
1407 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001408}
1409
1410/// Perform access-control checking on a previously-unresolved member
1411/// access which has now been resolved to a member.
John McCall5b0829a2010-02-10 09:31:12 +00001412Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001413 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001414 if (!getLangOptions().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001415 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001416 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001417
John McCalla8ae2222010-04-06 21:38:20 +00001418 QualType BaseType = E->getBaseType();
1419 if (E->isArrow())
1420 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1421
1422 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1423 Found, BaseType);
John McCall1064d7e2010-03-16 05:22:47 +00001424 Entity.setDiag(diag::err_access) << E->getSourceRange();
1425
1426 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001427}
1428
John McCall5b0829a2010-02-10 09:31:12 +00001429Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +00001430 CXXDestructorDecl *Dtor,
1431 const PartialDiagnostic &PDiag) {
John McCall6781b052010-02-02 08:45:54 +00001432 if (!getLangOptions().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +00001433 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001434
John McCall1064d7e2010-03-16 05:22:47 +00001435 // There's never a path involved when checking implicit destructor access.
John McCall6781b052010-02-02 08:45:54 +00001436 AccessSpecifier Access = Dtor->getAccess();
1437 if (Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001438 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001439
John McCall1064d7e2010-03-16 05:22:47 +00001440 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCalla8ae2222010-04-06 21:38:20 +00001441 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1442 DeclAccessPair::make(Dtor, Access),
1443 QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001444 Entity.setDiag(PDiag); // TODO: avoid copy
1445
1446 return CheckAccess(*this, Loc, Entity);
John McCall6781b052010-02-02 08:45:54 +00001447}
1448
John McCall760af172010-02-01 03:16:54 +00001449/// Checks access to a constructor.
John McCall5b0829a2010-02-10 09:31:12 +00001450Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001451 CXXConstructorDecl *Constructor,
1452 const InitializedEntity &Entity,
1453 AccessSpecifier Access,
1454 bool IsCopyBindingRefToTemp) {
John McCall1064d7e2010-03-16 05:22:47 +00001455 if (!getLangOptions().AccessControl ||
1456 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001457 return AR_accessible;
John McCall760af172010-02-01 03:16:54 +00001458
John McCall5b0829a2010-02-10 09:31:12 +00001459 CXXRecordDecl *NamingClass = Constructor->getParent();
Anders Carlssona01874b2010-04-21 18:47:17 +00001460 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1461 DeclAccessPair::make(Constructor, Access),
1462 QualType());
1463 switch (Entity.getKind()) {
1464 default:
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001465 AccessEntity.setDiag(IsCopyBindingRefToTemp
1466 ? diag::ext_rvalue_to_reference_access_ctor
1467 : diag::err_access_ctor);
Anders Carlssona01874b2010-04-21 18:47:17 +00001468 break;
John McCall1064d7e2010-03-16 05:22:47 +00001469
Anders Carlsson05bf0092010-04-22 05:40:53 +00001470 case InitializedEntity::EK_Base:
Alexis Hunt80f00ff2011-05-10 19:08:14 +00001471 AccessEntity.setDiag(PDiag(diag::err_access_base_ctor)
Anders Carlsson05bf0092010-04-22 05:40:53 +00001472 << Entity.isInheritedVirtualBase()
Matt Beaumont-Gay6c307ae2011-05-19 23:44:42 +00001473 << Entity.getBaseSpecifier()->getType()
Alexis Hunt119c10e2011-05-25 23:16:36 +00001474 << getSpecialMember(Constructor));
Anders Carlssona01874b2010-04-21 18:47:17 +00001475 break;
Anders Carlsson05bf0092010-04-22 05:40:53 +00001476
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001477 case InitializedEntity::EK_Member: {
1478 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Alexis Hunt80f00ff2011-05-10 19:08:14 +00001479 AccessEntity.setDiag(PDiag(diag::err_access_field_ctor)
Matt Beaumont-Gay6c307ae2011-05-19 23:44:42 +00001480 << Field->getType()
Alexis Hunt119c10e2011-05-25 23:16:36 +00001481 << getSpecialMember(Constructor));
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001482 break;
1483 }
Anders Carlssona01874b2010-04-21 18:47:17 +00001484
Anders Carlsson43c64af2010-04-21 19:52:01 +00001485 }
1486
Anders Carlssona01874b2010-04-21 18:47:17 +00001487 return CheckAccess(*this, UseLoc, AccessEntity);
John McCall760af172010-02-01 03:16:54 +00001488}
1489
John McCallab8c2732010-03-16 06:11:48 +00001490/// Checks direct (i.e. non-inherited) access to an arbitrary class
1491/// member.
1492Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
1493 NamedDecl *Target,
1494 const PartialDiagnostic &Diag) {
1495 AccessSpecifier Access = Target->getAccess();
1496 if (!getLangOptions().AccessControl ||
1497 Access == AS_public)
1498 return AR_accessible;
1499
1500 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
John McCalla8ae2222010-04-06 21:38:20 +00001501 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1502 DeclAccessPair::make(Target, Access),
1503 QualType());
John McCallab8c2732010-03-16 06:11:48 +00001504 Entity.setDiag(Diag);
1505 return CheckAccess(*this, UseLoc, Entity);
1506}
1507
1508
John McCallfb6f5262010-03-18 08:19:33 +00001509/// Checks access to an overloaded operator new or delete.
1510Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1511 SourceRange PlacementRange,
1512 CXXRecordDecl *NamingClass,
Alexis Huntf91729462011-05-12 22:46:25 +00001513 DeclAccessPair Found,
1514 bool Diagnose) {
John McCallfb6f5262010-03-18 08:19:33 +00001515 if (!getLangOptions().AccessControl ||
1516 !NamingClass ||
John McCalla0296f72010-03-19 07:35:19 +00001517 Found.getAccess() == AS_public)
John McCallfb6f5262010-03-18 08:19:33 +00001518 return AR_accessible;
1519
John McCalla8ae2222010-04-06 21:38:20 +00001520 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1521 QualType());
Alexis Huntf91729462011-05-12 22:46:25 +00001522 if (Diagnose)
1523 Entity.setDiag(diag::err_access)
1524 << PlacementRange;
John McCallfb6f5262010-03-18 08:19:33 +00001525
1526 return CheckAccess(*this, OpLoc, Entity);
1527}
1528
John McCall760af172010-02-01 03:16:54 +00001529/// Checks access to an overloaded member operator, including
1530/// conversion operators.
John McCall5b0829a2010-02-10 09:31:12 +00001531Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1532 Expr *ObjectExpr,
John McCall1064d7e2010-03-16 05:22:47 +00001533 Expr *ArgExpr,
John McCalla0296f72010-03-19 07:35:19 +00001534 DeclAccessPair Found) {
John McCall1064d7e2010-03-16 05:22:47 +00001535 if (!getLangOptions().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001536 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001537 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001538
1539 const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>();
1540 assert(RT && "found member operator but object expr not of record type");
1541 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1542
John McCalla8ae2222010-04-06 21:38:20 +00001543 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1544 ObjectExpr->getType());
John McCall1064d7e2010-03-16 05:22:47 +00001545 Entity.setDiag(diag::err_access)
1546 << ObjectExpr->getSourceRange()
1547 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1548
1549 return CheckAccess(*this, OpLoc, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001550}
John McCallb3a44002010-01-28 01:42:12 +00001551
John McCall16df1e52010-03-30 21:47:33 +00001552Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1553 DeclAccessPair Found) {
1554 if (!getLangOptions().AccessControl ||
John McCallb493d532010-03-30 22:20:00 +00001555 Found.getAccess() == AS_none ||
John McCall16df1e52010-03-30 21:47:33 +00001556 Found.getAccess() == AS_public)
1557 return AR_accessible;
1558
John McCall8d08b9b2010-08-27 09:08:28 +00001559 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCall8c12dc42010-04-22 18:44:12 +00001560 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall16df1e52010-03-30 21:47:33 +00001561
John McCalla8ae2222010-04-06 21:38:20 +00001562 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1563 Context.getTypeDeclType(NamingClass));
John McCall16df1e52010-03-30 21:47:33 +00001564 Entity.setDiag(diag::err_access)
1565 << Ovl->getSourceRange();
1566
1567 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1568}
1569
John McCall5b0829a2010-02-10 09:31:12 +00001570/// Checks access for a hierarchy conversion.
1571///
1572/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
1573/// or a derived-to-base conversion (false)
1574/// \param ForceCheck true if this check should be performed even if access
1575/// control is disabled; some things rely on this for semantics
1576/// \param ForceUnprivileged true if this check should proceed as if the
1577/// context had no special privileges
1578/// \param ADK controls the kind of diagnostics that are used
1579Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall5b0829a2010-02-10 09:31:12 +00001580 QualType Base,
1581 QualType Derived,
1582 const CXXBasePath &Path,
John McCall1064d7e2010-03-16 05:22:47 +00001583 unsigned DiagID,
John McCall5b0829a2010-02-10 09:31:12 +00001584 bool ForceCheck,
John McCall1064d7e2010-03-16 05:22:47 +00001585 bool ForceUnprivileged) {
John McCall5b0829a2010-02-10 09:31:12 +00001586 if (!ForceCheck && !getLangOptions().AccessControl)
1587 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001588
John McCall5b0829a2010-02-10 09:31:12 +00001589 if (Path.Access == AS_public)
1590 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001591
John McCall5b0829a2010-02-10 09:31:12 +00001592 CXXRecordDecl *BaseD, *DerivedD;
1593 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1594 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall1064d7e2010-03-16 05:22:47 +00001595
John McCalla8ae2222010-04-06 21:38:20 +00001596 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1597 Path.Access);
John McCall1064d7e2010-03-16 05:22:47 +00001598 if (DiagID)
1599 Entity.setDiag(DiagID) << Derived << Base;
John McCall5b0829a2010-02-10 09:31:12 +00001600
John McCalla8ae2222010-04-06 21:38:20 +00001601 if (ForceUnprivileged) {
1602 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1603 AccessLoc, Entity)) {
1604 case ::AR_accessible: return Sema::AR_accessible;
1605 case ::AR_inaccessible: return Sema::AR_inaccessible;
1606 case ::AR_dependent: return Sema::AR_dependent;
1607 }
1608 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1609 }
John McCall1064d7e2010-03-16 05:22:47 +00001610 return CheckAccess(*this, AccessLoc, Entity);
John McCallb3a44002010-01-28 01:42:12 +00001611}
1612
John McCall553c0792010-01-23 00:46:32 +00001613/// Checks access to all the declarations in the given result set.
John McCall5b0829a2010-02-10 09:31:12 +00001614void Sema::CheckLookupAccess(const LookupResult &R) {
1615 assert(getLangOptions().AccessControl
1616 && "performing access check without access control");
1617 assert(R.getNamingClass() && "performing access check without naming class");
1618
John McCall1064d7e2010-03-16 05:22:47 +00001619 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1620 if (I.getAccess() != AS_public) {
John McCalla8ae2222010-04-06 21:38:20 +00001621 AccessTarget Entity(Context, AccessedEntity::Member,
1622 R.getNamingClass(), I.getPair(),
Francois Pichetefb1af92011-05-23 03:43:44 +00001623 R.getBaseObjectType(), R.isUsingDeclaration());
John McCall1064d7e2010-03-16 05:22:47 +00001624 Entity.setDiag(diag::err_access);
John McCall1064d7e2010-03-16 05:22:47 +00001625 CheckAccess(*this, R.getNameLoc(), Entity);
1626 }
1627 }
John McCall553c0792010-01-23 00:46:32 +00001628}
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001629
1630void Sema::ActOnStartSuppressingAccessChecks() {
1631 assert(!SuppressAccessChecking &&
1632 "Tried to start access check suppression when already started.");
1633 SuppressAccessChecking = true;
1634}
1635
1636void Sema::ActOnStopSuppressingAccessChecks() {
1637 assert(SuppressAccessChecking &&
1638 "Tried to stop access check suprression when already stopped.");
1639 SuppressAccessChecking = false;
1640}