blob: 0e973cc5ebaae6fd9975b8bed5b8c0422678ccf8 [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"
Anders Carlsson733d77f2009-03-27 06:03:27 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
17#include "clang/AST/DeclCXX.h"
John McCall16927f62010-03-12 01:19:31 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregor21ceb182011-11-03 19:00:24 +000019#include "clang/AST/DeclObjC.h"
John McCallc62bb642010-03-24 05:22:00 +000020#include "clang/AST/DependentDiagnostic.h"
John McCall58cc69d2010-01-27 01:50:18 +000021#include "clang/AST/ExprCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/DelayedDiagnostic.h"
23#include "clang/Sema/Initialization.h"
24#include "clang/Sema/Lookup.h"
John McCall58cc69d2010-01-27 01:50:18 +000025
Anders Carlsson17941122009-03-27 04:54:36 +000026using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000027using namespace sema;
Anders Carlsson17941122009-03-27 04:54:36 +000028
John McCalla8ae2222010-04-06 21:38:20 +000029/// A copy of Sema's enum without AR_delayed.
30enum AccessResult {
31 AR_accessible,
32 AR_inaccessible,
33 AR_dependent
34};
35
Anders Carlsson4742a9c2009-03-27 05:05:05 +000036/// SetMemberAccessSpecifier - Set the access specifier of a member.
37/// Returns true on error (when the previous member decl access specifier
38/// is different from the new member decl access specifier).
Mike Stump11289f42009-09-09 15:08:12 +000039bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
Anders Carlsson17941122009-03-27 04:54:36 +000040 NamedDecl *PrevMemberDecl,
41 AccessSpecifier LexicalAS) {
42 if (!PrevMemberDecl) {
43 // Use the lexical access specifier.
44 MemberDecl->setAccess(LexicalAS);
45 return false;
46 }
Mike Stump11289f42009-09-09 15:08:12 +000047
Anders Carlsson17941122009-03-27 04:54:36 +000048 // C++ [class.access.spec]p3: When a member is redeclared its access
49 // specifier must be same as its initial declaration.
50 if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) {
Mike Stump11289f42009-09-09 15:08:12 +000051 Diag(MemberDecl->getLocation(),
52 diag::err_class_redeclared_with_different_access)
Anders Carlsson17941122009-03-27 04:54:36 +000053 << MemberDecl << LexicalAS;
54 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
55 << PrevMemberDecl << PrevMemberDecl->getAccess();
John McCall0a4bb262009-12-23 00:37:40 +000056
57 MemberDecl->setAccess(LexicalAS);
Anders Carlsson17941122009-03-27 04:54:36 +000058 return true;
59 }
Mike Stump11289f42009-09-09 15:08:12 +000060
Anders Carlsson17941122009-03-27 04:54:36 +000061 MemberDecl->setAccess(PrevMemberDecl->getAccess());
62 return false;
63}
Anders Carlsson4742a9c2009-03-27 05:05:05 +000064
John McCalla8ae2222010-04-06 21:38:20 +000065static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) {
66 DeclContext *DC = D->getDeclContext();
67
68 // This can only happen at top: enum decls only "publish" their
69 // immediate members.
70 if (isa<EnumDecl>(DC))
71 DC = cast<EnumDecl>(DC)->getDeclContext();
72
73 CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(DC);
74 while (DeclaringClass->isAnonymousStructOrUnion())
75 DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext());
76 return DeclaringClass;
77}
78
John McCall5b0829a2010-02-10 09:31:12 +000079namespace {
80struct EffectiveContext {
Craig Topperc3ec1492014-05-26 06:22:03 +000081 EffectiveContext() : Inner(nullptr), Dependent(false) {}
Anders Carlsson733d77f2009-03-27 06:03:27 +000082
John McCall816d75b2010-03-24 07:46:06 +000083 explicit EffectiveContext(DeclContext *DC)
84 : Inner(DC),
85 Dependent(DC->isDependentContext()) {
John McCallc62bb642010-03-24 05:22:00 +000086
Richard Smithad5c1ca2013-04-29 10:13:55 +000087 // C++11 [class.access.nest]p1:
John McCallfb803d72010-03-17 04:58:56 +000088 // A nested class is a member and as such has the same access
89 // rights as any other member.
Richard Smithad5c1ca2013-04-29 10:13:55 +000090 // C++11 [class.access]p2:
John McCallfb803d72010-03-17 04:58:56 +000091 // A member of a class can also access all the names to which
John McCall3dc81f72010-03-27 06:55:49 +000092 // the class has access. A local class of a member function
93 // may access the same names that the member function itself
94 // may access.
95 // This almost implies that the privileges of nesting are transitive.
96 // Technically it says nothing about the local classes of non-member
97 // functions (which can gain privileges through friendship), but we
98 // take that as an oversight.
99 while (true) {
John McCalle91aec72012-08-24 22:54:02 +0000100 // We want to add canonical declarations to the EC lists for
101 // simplicity of checking, but we need to walk up through the
102 // actual current DC chain. Otherwise, something like a local
103 // extern or friend which happens to be the canonical
104 // declaration will really mess us up.
105
John McCall3dc81f72010-03-27 06:55:49 +0000106 if (isa<CXXRecordDecl>(DC)) {
John McCalle91aec72012-08-24 22:54:02 +0000107 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
108 Records.push_back(Record->getCanonicalDecl());
John McCall3dc81f72010-03-27 06:55:49 +0000109 DC = Record->getDeclContext();
110 } else if (isa<FunctionDecl>(DC)) {
John McCalle91aec72012-08-24 22:54:02 +0000111 FunctionDecl *Function = cast<FunctionDecl>(DC);
112 Functions.push_back(Function->getCanonicalDecl());
Douglas Gregor56636582011-10-09 22:38:36 +0000113 if (Function->getFriendObjectKind())
114 DC = Function->getLexicalDeclContext();
115 else
116 DC = Function->getDeclContext();
John McCall3dc81f72010-03-27 06:55:49 +0000117 } else if (DC->isFileContext()) {
118 break;
119 } else {
120 DC = DC->getParent();
121 }
John McCallfb803d72010-03-17 04:58:56 +0000122 }
Anders Carlsson733d77f2009-03-27 06:03:27 +0000123 }
Sebastian Redle644e192009-07-18 14:32:15 +0000124
John McCallc62bb642010-03-24 05:22:00 +0000125 bool isDependent() const { return Dependent; }
126
John McCallfb803d72010-03-17 04:58:56 +0000127 bool includesClass(const CXXRecordDecl *R) const {
128 R = R->getCanonicalDecl();
129 return std::find(Records.begin(), Records.end(), R)
130 != Records.end();
John McCall5b0829a2010-02-10 09:31:12 +0000131 }
132
John McCall816d75b2010-03-24 07:46:06 +0000133 /// Retrieves the innermost "useful" context. Can be null if we're
134 /// doing access-control without privileges.
135 DeclContext *getInnerContext() const {
136 return Inner;
John McCallc62bb642010-03-24 05:22:00 +0000137 }
138
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000139 typedef SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
John McCallc62bb642010-03-24 05:22:00 +0000140
John McCall816d75b2010-03-24 07:46:06 +0000141 DeclContext *Inner;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 SmallVector<FunctionDecl*, 4> Functions;
143 SmallVector<CXXRecordDecl*, 4> Records;
John McCallc62bb642010-03-24 05:22:00 +0000144 bool Dependent;
John McCall5b0829a2010-02-10 09:31:12 +0000145};
John McCalla8ae2222010-04-06 21:38:20 +0000146
Nico Weber20c9f1d2010-11-28 22:53:37 +0000147/// Like sema::AccessedEntity, but kindly lets us scribble all over
John McCalla8ae2222010-04-06 21:38:20 +0000148/// it.
John McCallb45a1e72010-08-26 02:13:20 +0000149struct AccessTarget : public AccessedEntity {
150 AccessTarget(const AccessedEntity &Entity)
John McCalla8ae2222010-04-06 21:38:20 +0000151 : AccessedEntity(Entity) {
152 initialize();
153 }
154
155 AccessTarget(ASTContext &Context,
156 MemberNonce _,
157 CXXRecordDecl *NamingClass,
158 DeclAccessPair FoundDecl,
Erik Verbruggen631dfc62011-09-19 15:10:40 +0000159 QualType BaseObjectType)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000160 : AccessedEntity(Context.getDiagAllocator(), Member, NamingClass,
161 FoundDecl, BaseObjectType) {
John McCalla8ae2222010-04-06 21:38:20 +0000162 initialize();
163 }
164
165 AccessTarget(ASTContext &Context,
166 BaseNonce _,
167 CXXRecordDecl *BaseClass,
168 CXXRecordDecl *DerivedClass,
169 AccessSpecifier Access)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000170 : AccessedEntity(Context.getDiagAllocator(), Base, BaseClass, DerivedClass,
171 Access) {
John McCalla8ae2222010-04-06 21:38:20 +0000172 initialize();
173 }
174
John McCall5dadb652012-04-07 03:04:20 +0000175 bool isInstanceMember() const {
176 return (isMemberAccess() && getTargetDecl()->isCXXInstanceMember());
177 }
178
John McCalla8ae2222010-04-06 21:38:20 +0000179 bool hasInstanceContext() const {
180 return HasInstanceContext;
181 }
182
183 class SavedInstanceContext {
184 public:
185 ~SavedInstanceContext() {
186 Target.HasInstanceContext = Has;
187 }
188
189 private:
John McCall8e36d532010-04-07 00:41:46 +0000190 friend struct AccessTarget;
John McCalla8ae2222010-04-06 21:38:20 +0000191 explicit SavedInstanceContext(AccessTarget &Target)
192 : Target(Target), Has(Target.HasInstanceContext) {}
193 AccessTarget &Target;
194 bool Has;
195 };
196
197 SavedInstanceContext saveInstanceContext() {
198 return SavedInstanceContext(*this);
199 }
200
201 void suppressInstanceContext() {
202 HasInstanceContext = false;
203 }
204
205 const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
206 assert(HasInstanceContext);
207 if (CalculatedInstanceContext)
208 return InstanceContext;
209
210 CalculatedInstanceContext = true;
211 DeclContext *IC = S.computeDeclContext(getBaseObjectType());
Craig Topperc3ec1492014-05-26 06:22:03 +0000212 InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl()
213 : nullptr);
John McCalla8ae2222010-04-06 21:38:20 +0000214 return InstanceContext;
215 }
216
217 const CXXRecordDecl *getDeclaringClass() const {
218 return DeclaringClass;
219 }
220
John McCalld010ac92013-02-27 00:08:19 +0000221 /// The "effective" naming class is the canonical non-anonymous
222 /// class containing the actual naming class.
223 const CXXRecordDecl *getEffectiveNamingClass() const {
224 const CXXRecordDecl *namingClass = getNamingClass();
225 while (namingClass->isAnonymousStructOrUnion())
226 namingClass = cast<CXXRecordDecl>(namingClass->getParent());
227 return namingClass->getCanonicalDecl();
228 }
229
John McCalla8ae2222010-04-06 21:38:20 +0000230private:
231 void initialize() {
232 HasInstanceContext = (isMemberAccess() &&
233 !getBaseObjectType().isNull() &&
234 getTargetDecl()->isCXXInstanceMember());
235 CalculatedInstanceContext = false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000236 InstanceContext = nullptr;
John McCalla8ae2222010-04-06 21:38:20 +0000237
238 if (isMemberAccess())
239 DeclaringClass = FindDeclaringClass(getTargetDecl());
240 else
241 DeclaringClass = getBaseClass();
242 DeclaringClass = DeclaringClass->getCanonicalDecl();
243 }
244
245 bool HasInstanceContext : 1;
246 mutable bool CalculatedInstanceContext : 1;
247 mutable const CXXRecordDecl *InstanceContext;
248 const CXXRecordDecl *DeclaringClass;
249};
250
Anders Carlsson4742a9c2009-03-27 05:05:05 +0000251}
John McCall553c0792010-01-23 00:46:32 +0000252
John McCall97205142010-05-04 05:11:27 +0000253/// Checks whether one class might instantiate to the other.
254static bool MightInstantiateTo(const CXXRecordDecl *From,
255 const CXXRecordDecl *To) {
256 // Declaration names are always preserved by instantiation.
257 if (From->getDeclName() != To->getDeclName())
258 return false;
259
260 const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext();
261 const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext();
262 if (FromDC == ToDC) return true;
263 if (FromDC->isFileContext() || ToDC->isFileContext()) return false;
264
265 // Be conservative.
266 return true;
267}
268
John McCalla8ae2222010-04-06 21:38:20 +0000269/// Checks whether one class is derived from another, inclusively.
270/// Properly indicates when it couldn't be determined due to
271/// dependence.
272///
273/// This should probably be donated to AST or at least Sema.
274static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
275 const CXXRecordDecl *Target) {
276 assert(Derived->getCanonicalDecl() == Derived);
277 assert(Target->getCanonicalDecl() == Target);
John McCall69f75862010-03-24 09:04:37 +0000278
John McCalla8ae2222010-04-06 21:38:20 +0000279 if (Derived == Target) return AR_accessible;
John McCall69f75862010-03-24 09:04:37 +0000280
John McCall97205142010-05-04 05:11:27 +0000281 bool CheckDependent = Derived->isDependentContext();
282 if (CheckDependent && MightInstantiateTo(Derived, Target))
283 return AR_dependent;
284
John McCalla8ae2222010-04-06 21:38:20 +0000285 AccessResult OnFailure = AR_inaccessible;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000286 SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
John McCalla8ae2222010-04-06 21:38:20 +0000287
288 while (true) {
Douglas Gregor0201a4c2011-11-14 23:00:43 +0000289 if (Derived->isDependentContext() && !Derived->hasDefinition())
290 return AR_dependent;
291
Aaron Ballman574705e2014-03-13 15:41:46 +0000292 for (const auto &I : Derived->bases()) {
John McCalla8ae2222010-04-06 21:38:20 +0000293 const CXXRecordDecl *RD;
294
Aaron Ballman574705e2014-03-13 15:41:46 +0000295 QualType T = I.getType();
John McCalla8ae2222010-04-06 21:38:20 +0000296 if (const RecordType *RT = T->getAs<RecordType>()) {
297 RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall97205142010-05-04 05:11:27 +0000298 } else if (const InjectedClassNameType *IT
299 = T->getAs<InjectedClassNameType>()) {
300 RD = IT->getDecl();
John McCalla8ae2222010-04-06 21:38:20 +0000301 } else {
John McCalla8ae2222010-04-06 21:38:20 +0000302 assert(T->isDependentType() && "non-dependent base wasn't a record?");
303 OnFailure = AR_dependent;
304 continue;
305 }
306
307 RD = RD->getCanonicalDecl();
308 if (RD == Target) return AR_accessible;
John McCall97205142010-05-04 05:11:27 +0000309 if (CheckDependent && MightInstantiateTo(RD, Target))
310 OnFailure = AR_dependent;
311
John McCalla8ae2222010-04-06 21:38:20 +0000312 Queue.push_back(RD);
313 }
314
315 if (Queue.empty()) break;
316
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000317 Derived = Queue.pop_back_val();
John McCalla8ae2222010-04-06 21:38:20 +0000318 }
319
320 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000321}
322
John McCalla8ae2222010-04-06 21:38:20 +0000323
John McCallc62bb642010-03-24 05:22:00 +0000324static bool MightInstantiateTo(Sema &S, DeclContext *Context,
325 DeclContext *Friend) {
326 if (Friend == Context)
327 return true;
328
329 assert(!Friend->isDependentContext() &&
330 "can't handle friends with dependent contexts here");
331
332 if (!Context->isDependentContext())
333 return false;
334
335 if (Friend->isFileContext())
336 return false;
337
338 // TODO: this is very conservative
339 return true;
340}
341
342// Asks whether the type in 'context' can ever instantiate to the type
343// in 'friend'.
344static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
345 if (Friend == Context)
346 return true;
347
348 if (!Friend->isDependentType() && !Context->isDependentType())
349 return false;
350
351 // TODO: this is very conservative.
352 return true;
353}
354
355static bool MightInstantiateTo(Sema &S,
356 FunctionDecl *Context,
357 FunctionDecl *Friend) {
358 if (Context->getDeclName() != Friend->getDeclName())
359 return false;
360
361 if (!MightInstantiateTo(S,
362 Context->getDeclContext(),
363 Friend->getDeclContext()))
364 return false;
365
366 CanQual<FunctionProtoType> FriendTy
367 = S.Context.getCanonicalType(Friend->getType())
368 ->getAs<FunctionProtoType>();
369 CanQual<FunctionProtoType> ContextTy
370 = S.Context.getCanonicalType(Context->getType())
371 ->getAs<FunctionProtoType>();
372
373 // There isn't any way that I know of to add qualifiers
374 // during instantiation.
375 if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
376 return false;
377
Alp Toker9cacbab2014-01-20 20:26:09 +0000378 if (FriendTy->getNumParams() != ContextTy->getNumParams())
John McCallc62bb642010-03-24 05:22:00 +0000379 return false;
380
Alp Toker314cc812014-01-25 16:55:45 +0000381 if (!MightInstantiateTo(S, ContextTy->getReturnType(),
382 FriendTy->getReturnType()))
John McCallc62bb642010-03-24 05:22:00 +0000383 return false;
384
Alp Toker9cacbab2014-01-20 20:26:09 +0000385 for (unsigned I = 0, E = FriendTy->getNumParams(); I != E; ++I)
386 if (!MightInstantiateTo(S, ContextTy->getParamType(I),
387 FriendTy->getParamType(I)))
John McCallc62bb642010-03-24 05:22:00 +0000388 return false;
389
390 return true;
391}
392
393static bool MightInstantiateTo(Sema &S,
394 FunctionTemplateDecl *Context,
395 FunctionTemplateDecl *Friend) {
396 return MightInstantiateTo(S,
397 Context->getTemplatedDecl(),
398 Friend->getTemplatedDecl());
399}
400
John McCalla8ae2222010-04-06 21:38:20 +0000401static AccessResult MatchesFriend(Sema &S,
402 const EffectiveContext &EC,
403 const CXXRecordDecl *Friend) {
John McCall39e82882010-03-17 20:01:29 +0000404 if (EC.includesClass(Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000405 return AR_accessible;
John McCall39e82882010-03-17 20:01:29 +0000406
John McCallc62bb642010-03-24 05:22:00 +0000407 if (EC.isDependent()) {
408 CanQualType FriendTy
409 = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
410
411 for (EffectiveContext::record_iterator
412 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
413 CanQualType ContextTy
414 = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
415 if (MightInstantiateTo(S, ContextTy, FriendTy))
John McCalla8ae2222010-04-06 21:38:20 +0000416 return AR_dependent;
John McCallc62bb642010-03-24 05:22:00 +0000417 }
418 }
419
John McCalla8ae2222010-04-06 21:38:20 +0000420 return AR_inaccessible;
John McCall39e82882010-03-17 20:01:29 +0000421}
422
John McCalla8ae2222010-04-06 21:38:20 +0000423static AccessResult MatchesFriend(Sema &S,
424 const EffectiveContext &EC,
425 CanQualType Friend) {
John McCallc62bb642010-03-24 05:22:00 +0000426 if (const RecordType *RT = Friend->getAs<RecordType>())
427 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCall39e82882010-03-17 20:01:29 +0000428
John McCallc62bb642010-03-24 05:22:00 +0000429 // TODO: we can do better than this
430 if (Friend->isDependentType())
John McCalla8ae2222010-04-06 21:38:20 +0000431 return AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000432
John McCalla8ae2222010-04-06 21:38:20 +0000433 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000434}
435
436/// Determines whether the given friend class template matches
437/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000438static AccessResult MatchesFriend(Sema &S,
439 const EffectiveContext &EC,
440 ClassTemplateDecl *Friend) {
441 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000442
John McCall598b4402010-03-25 06:39:04 +0000443 // Check whether the friend is the template of a class in the
444 // context chain.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000445 for (SmallVectorImpl<CXXRecordDecl*>::const_iterator
John McCallc62bb642010-03-24 05:22:00 +0000446 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
447 CXXRecordDecl *Record = *I;
448
John McCall598b4402010-03-25 06:39:04 +0000449 // Figure out whether the current class has a template:
John McCallc62bb642010-03-24 05:22:00 +0000450 ClassTemplateDecl *CTD;
451
452 // A specialization of the template...
453 if (isa<ClassTemplateSpecializationDecl>(Record)) {
454 CTD = cast<ClassTemplateSpecializationDecl>(Record)
455 ->getSpecializedTemplate();
456
457 // ... or the template pattern itself.
458 } else {
459 CTD = Record->getDescribedClassTemplate();
460 if (!CTD) continue;
461 }
462
463 // It's a match.
464 if (Friend == CTD->getCanonicalDecl())
John McCalla8ae2222010-04-06 21:38:20 +0000465 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000466
John McCall598b4402010-03-25 06:39:04 +0000467 // If the context isn't dependent, it can't be a dependent match.
468 if (!EC.isDependent())
469 continue;
470
John McCallc62bb642010-03-24 05:22:00 +0000471 // If the template names don't match, it can't be a dependent
Richard Smith3f1b5d02011-05-05 21:57:07 +0000472 // match.
473 if (CTD->getDeclName() != Friend->getDeclName())
John McCallc62bb642010-03-24 05:22:00 +0000474 continue;
475
476 // If the class's context can't instantiate to the friend's
477 // context, it can't be a dependent match.
478 if (!MightInstantiateTo(S, CTD->getDeclContext(),
479 Friend->getDeclContext()))
480 continue;
481
482 // Otherwise, it's a dependent match.
John McCalla8ae2222010-04-06 21:38:20 +0000483 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000484 }
485
John McCallc62bb642010-03-24 05:22:00 +0000486 return OnFailure;
487}
488
489/// Determines whether the given friend function matches anything in
490/// the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000491static AccessResult MatchesFriend(Sema &S,
492 const EffectiveContext &EC,
493 FunctionDecl *Friend) {
494 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000495
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000496 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall3dc81f72010-03-27 06:55:49 +0000497 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
498 if (Friend == *I)
John McCalla8ae2222010-04-06 21:38:20 +0000499 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000500
John McCall3dc81f72010-03-27 06:55:49 +0000501 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000502 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000503 }
John McCallc62bb642010-03-24 05:22:00 +0000504
John McCall3dc81f72010-03-27 06:55:49 +0000505 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000506}
507
508/// Determines whether the given friend function template matches
509/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000510static AccessResult MatchesFriend(Sema &S,
511 const EffectiveContext &EC,
512 FunctionTemplateDecl *Friend) {
513 if (EC.Functions.empty()) return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000514
John McCalla8ae2222010-04-06 21:38:20 +0000515 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000516
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000517 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall3dc81f72010-03-27 06:55:49 +0000518 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCallc62bb642010-03-24 05:22:00 +0000519
John McCall3dc81f72010-03-27 06:55:49 +0000520 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
521 if (!FTD)
522 FTD = (*I)->getDescribedFunctionTemplate();
523 if (!FTD)
524 continue;
John McCallc62bb642010-03-24 05:22:00 +0000525
John McCall3dc81f72010-03-27 06:55:49 +0000526 FTD = FTD->getCanonicalDecl();
527
528 if (Friend == FTD)
John McCalla8ae2222010-04-06 21:38:20 +0000529 return AR_accessible;
John McCall3dc81f72010-03-27 06:55:49 +0000530
531 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000532 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000533 }
534
535 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000536}
537
538/// Determines whether the given friend declaration matches anything
539/// in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000540static AccessResult MatchesFriend(Sema &S,
541 const EffectiveContext &EC,
542 FriendDecl *FriendD) {
John McCall2c2eb122010-10-16 06:59:13 +0000543 // Whitelist accesses if there's an invalid or unsupported friend
544 // declaration.
545 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCallde3fd222010-10-12 23:13:28 +0000546 return AR_accessible;
547
John McCall15ad0962010-03-25 18:04:51 +0000548 if (TypeSourceInfo *T = FriendD->getFriendType())
549 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCallc62bb642010-03-24 05:22:00 +0000550
551 NamedDecl *Friend
552 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCall39e82882010-03-17 20:01:29 +0000553
554 // FIXME: declarations with dependent or templated scope.
555
John McCallc62bb642010-03-24 05:22:00 +0000556 if (isa<ClassTemplateDecl>(Friend))
557 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000558
John McCallc62bb642010-03-24 05:22:00 +0000559 if (isa<FunctionTemplateDecl>(Friend))
560 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000561
John McCallc62bb642010-03-24 05:22:00 +0000562 if (isa<CXXRecordDecl>(Friend))
563 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000564
John McCallc62bb642010-03-24 05:22:00 +0000565 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
566 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000567}
568
John McCalla8ae2222010-04-06 21:38:20 +0000569static AccessResult GetFriendKind(Sema &S,
570 const EffectiveContext &EC,
571 const CXXRecordDecl *Class) {
572 AccessResult OnFailure = AR_inaccessible;
John McCallfb803d72010-03-17 04:58:56 +0000573
John McCall16927f62010-03-12 01:19:31 +0000574 // Okay, check friends.
Aaron Ballman522a6dd2014-03-13 17:00:06 +0000575 for (auto *Friend : Class->friends()) {
John McCall39e82882010-03-17 20:01:29 +0000576 switch (MatchesFriend(S, EC, Friend)) {
John McCalla8ae2222010-04-06 21:38:20 +0000577 case AR_accessible:
578 return AR_accessible;
John McCall16927f62010-03-12 01:19:31 +0000579
John McCalla8ae2222010-04-06 21:38:20 +0000580 case AR_inaccessible:
581 continue;
582
583 case AR_dependent:
584 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000585 break;
John McCall16927f62010-03-12 01:19:31 +0000586 }
John McCall16927f62010-03-12 01:19:31 +0000587 }
588
589 // That's it, give up.
John McCallfb803d72010-03-17 04:58:56 +0000590 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000591}
592
John McCall96329672010-08-28 07:56:00 +0000593namespace {
594
595/// A helper class for checking for a friend which will grant access
596/// to a protected instance member.
597struct ProtectedFriendContext {
598 Sema &S;
599 const EffectiveContext &EC;
600 const CXXRecordDecl *NamingClass;
601 bool CheckDependent;
602 bool EverDependent;
603
604 /// The path down to the current base class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000605 SmallVector<const CXXRecordDecl*, 20> CurPath;
John McCall96329672010-08-28 07:56:00 +0000606
607 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
608 const CXXRecordDecl *InstanceContext,
609 const CXXRecordDecl *NamingClass)
610 : S(S), EC(EC), NamingClass(NamingClass),
611 CheckDependent(InstanceContext->isDependentContext() ||
612 NamingClass->isDependentContext()),
613 EverDependent(false) {}
614
John McCall1177ff12010-08-28 08:47:21 +0000615 /// Check classes in the current path for friendship, starting at
616 /// the given index.
617 bool checkFriendshipAlongPath(unsigned I) {
618 assert(I < CurPath.size());
619 for (unsigned E = CurPath.size(); I != E; ++I) {
620 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall96329672010-08-28 07:56:00 +0000621 case AR_accessible: return true;
622 case AR_inaccessible: continue;
623 case AR_dependent: EverDependent = true; continue;
624 }
625 }
626 return false;
627 }
628
629 /// Perform a search starting at the given class.
John McCall1177ff12010-08-28 08:47:21 +0000630 ///
631 /// PrivateDepth is the index of the last (least derived) class
632 /// along the current path such that a notional public member of
633 /// the final class in the path would have access in that class.
634 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall96329672010-08-28 07:56:00 +0000635 // If we ever reach the naming class, check the current path for
636 // friendship. We can also stop recursing because we obviously
637 // won't find the naming class there again.
John McCall1177ff12010-08-28 08:47:21 +0000638 if (Cur == NamingClass)
639 return checkFriendshipAlongPath(PrivateDepth);
John McCall96329672010-08-28 07:56:00 +0000640
641 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
642 EverDependent = true;
643
644 // Recurse into the base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +0000645 for (const auto &I : Cur->bases()) {
John McCall1177ff12010-08-28 08:47:21 +0000646 // If this is private inheritance, then a public member of the
647 // base will not have any access in classes derived from Cur.
648 unsigned BasePrivateDepth = PrivateDepth;
Aaron Ballman574705e2014-03-13 15:41:46 +0000649 if (I.getAccessSpecifier() == AS_private)
John McCall1177ff12010-08-28 08:47:21 +0000650 BasePrivateDepth = CurPath.size() - 1;
John McCall96329672010-08-28 07:56:00 +0000651
652 const CXXRecordDecl *RD;
653
Aaron Ballman574705e2014-03-13 15:41:46 +0000654 QualType T = I.getType();
John McCall96329672010-08-28 07:56:00 +0000655 if (const RecordType *RT = T->getAs<RecordType>()) {
656 RD = cast<CXXRecordDecl>(RT->getDecl());
657 } else if (const InjectedClassNameType *IT
658 = T->getAs<InjectedClassNameType>()) {
659 RD = IT->getDecl();
660 } else {
661 assert(T->isDependentType() && "non-dependent base wasn't a record?");
662 EverDependent = true;
663 continue;
664 }
665
666 // Recurse. We don't need to clean up if this returns true.
John McCall1177ff12010-08-28 08:47:21 +0000667 CurPath.push_back(RD);
668 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
669 return true;
670 CurPath.pop_back();
John McCall96329672010-08-28 07:56:00 +0000671 }
672
John McCall96329672010-08-28 07:56:00 +0000673 return false;
674 }
John McCall1177ff12010-08-28 08:47:21 +0000675
676 bool findFriendship(const CXXRecordDecl *Cur) {
677 assert(CurPath.empty());
678 CurPath.push_back(Cur);
679 return findFriendship(Cur, 0);
680 }
John McCall96329672010-08-28 07:56:00 +0000681};
682}
683
684/// Search for a class P that EC is a friend of, under the constraint
John McCall5dadb652012-04-07 03:04:20 +0000685/// InstanceContext <= P
686/// if InstanceContext exists, or else
687/// NamingClass <= P
John McCall96329672010-08-28 07:56:00 +0000688/// and with the additional restriction that a protected member of
John McCall5dadb652012-04-07 03:04:20 +0000689/// NamingClass would have some natural access in P, which implicitly
690/// imposes the constraint that P <= NamingClass.
John McCall96329672010-08-28 07:56:00 +0000691///
John McCall5dadb652012-04-07 03:04:20 +0000692/// This isn't quite the condition laid out in the standard.
693/// Instead of saying that a notional protected member of NamingClass
694/// would have to have some natural access in P, it says the actual
695/// target has to have some natural access in P, which opens up the
696/// possibility that the target (which is not necessarily a member
697/// of NamingClass) might be more accessible along some path not
698/// passing through it. That's really a bad idea, though, because it
John McCall96329672010-08-28 07:56:00 +0000699/// introduces two problems:
John McCall5dadb652012-04-07 03:04:20 +0000700/// - Most importantly, it breaks encapsulation because you can
701/// access a forbidden base class's members by directly subclassing
702/// it elsewhere.
703/// - It also makes access substantially harder to compute because it
John McCall96329672010-08-28 07:56:00 +0000704/// breaks the hill-climbing algorithm: knowing that the target is
705/// accessible in some base class would no longer let you change
706/// the question solely to whether the base class is accessible,
707/// because the original target might have been more accessible
708/// because of crazy subclassing.
709/// So we don't implement that.
710static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
711 const CXXRecordDecl *InstanceContext,
712 const CXXRecordDecl *NamingClass) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000713 assert(InstanceContext == nullptr ||
John McCall5dadb652012-04-07 03:04:20 +0000714 InstanceContext->getCanonicalDecl() == InstanceContext);
John McCall96329672010-08-28 07:56:00 +0000715 assert(NamingClass->getCanonicalDecl() == NamingClass);
716
John McCall5dadb652012-04-07 03:04:20 +0000717 // If we don't have an instance context, our constraints give us
718 // that NamingClass <= P <= NamingClass, i.e. P == NamingClass.
719 // This is just the usual friendship check.
720 if (!InstanceContext) return GetFriendKind(S, EC, NamingClass);
721
John McCall96329672010-08-28 07:56:00 +0000722 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
723 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
724 if (PRC.EverDependent) return AR_dependent;
725 return AR_inaccessible;
726}
727
John McCalla8ae2222010-04-06 21:38:20 +0000728static AccessResult HasAccess(Sema &S,
729 const EffectiveContext &EC,
730 const CXXRecordDecl *NamingClass,
731 AccessSpecifier Access,
732 const AccessTarget &Target) {
John McCalld79b4d82010-04-02 00:03:43 +0000733 assert(NamingClass->getCanonicalDecl() == NamingClass &&
734 "declaration should be canonicalized before being passed here");
735
John McCalla8ae2222010-04-06 21:38:20 +0000736 if (Access == AS_public) return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000737 assert(Access == AS_private || Access == AS_protected);
738
John McCalla8ae2222010-04-06 21:38:20 +0000739 AccessResult OnFailure = AR_inaccessible;
740
John McCalld79b4d82010-04-02 00:03:43 +0000741 for (EffectiveContext::record_iterator
742 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
743 // All the declarations in EC have been canonicalized, so pointer
744 // equality from this point on will work fine.
745 const CXXRecordDecl *ECRecord = *I;
746
747 // [B2] and [M2]
John McCalla8ae2222010-04-06 21:38:20 +0000748 if (Access == AS_private) {
749 if (ECRecord == NamingClass)
750 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000751
John McCall97205142010-05-04 05:11:27 +0000752 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
753 OnFailure = AR_dependent;
754
John McCalld79b4d82010-04-02 00:03:43 +0000755 // [B3] and [M3]
John McCalla8ae2222010-04-06 21:38:20 +0000756 } else {
757 assert(Access == AS_protected);
758 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
759 case AR_accessible: break;
760 case AR_inaccessible: continue;
761 case AR_dependent: OnFailure = AR_dependent; continue;
762 }
763
John McCalla8ae2222010-04-06 21:38:20 +0000764 // C++ [class.protected]p1:
765 // An additional access check beyond those described earlier in
766 // [class.access] is applied when a non-static data member or
767 // non-static member function is a protected member of its naming
768 // class. As described earlier, access to a protected member is
769 // granted because the reference occurs in a friend or member of
770 // some class C. If the access is to form a pointer to member,
771 // the nested-name-specifier shall name C or a class derived from
772 // C. All other accesses involve a (possibly implicit) object
773 // expression. In this case, the class of the object expression
774 // shall be C or a class derived from C.
775 //
John McCall5dadb652012-04-07 03:04:20 +0000776 // We interpret this as a restriction on [M3].
777
778 // In this part of the code, 'C' is just our context class ECRecord.
779
780 // These rules are different if we don't have an instance context.
781 if (!Target.hasInstanceContext()) {
782 // If it's not an instance member, these restrictions don't apply.
783 if (!Target.isInstanceMember()) return AR_accessible;
784
785 // If it's an instance member, use the pointer-to-member rule
786 // that the naming class has to be derived from the effective
787 // context.
788
Francois Picheta39371c2012-04-17 12:35:05 +0000789 // Emulate a MSVC bug where the creation of pointer-to-member
790 // to protected member of base class is allowed but only from
Francois Pichet52d38982012-04-19 07:48:57 +0000791 // static member functions.
Alp Tokerbfa39342014-01-14 12:51:41 +0000792 if (S.getLangOpts().MSVCCompat && !EC.Functions.empty())
Francois Pichet5b061f02012-04-18 03:24:38 +0000793 if (CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(EC.Functions.front()))
794 if (MD->isStatic()) return AR_accessible;
Francois Picheta39371c2012-04-17 12:35:05 +0000795
John McCall5dadb652012-04-07 03:04:20 +0000796 // Despite the standard's confident wording, there is a case
797 // where you can have an instance member that's neither in a
798 // pointer-to-member expression nor in a member access: when
799 // it names a field in an unevaluated context that can't be an
800 // implicit member. Pending clarification, we just apply the
801 // same naming-class restriction here.
802 // FIXME: we're probably not correctly adding the
803 // protected-member restriction when we retroactively convert
804 // an expression to being evaluated.
805
806 // We know that ECRecord derives from NamingClass. The
807 // restriction says to check whether NamingClass derives from
808 // ECRecord, but that's not really necessary: two distinct
809 // classes can't be recursively derived from each other. So
810 // along this path, we just need to check whether the classes
811 // are equal.
812 if (NamingClass == ECRecord) return AR_accessible;
813
814 // Otherwise, this context class tells us nothing; on to the next.
815 continue;
816 }
817
818 assert(Target.isInstanceMember());
819
820 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
821 if (!InstanceContext) {
822 OnFailure = AR_dependent;
823 continue;
824 }
825
John McCalla8ae2222010-04-06 21:38:20 +0000826 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
827 case AR_accessible: return AR_accessible;
828 case AR_inaccessible: continue;
829 case AR_dependent: OnFailure = AR_dependent; continue;
830 }
831 }
John McCalld79b4d82010-04-02 00:03:43 +0000832 }
833
John McCall96329672010-08-28 07:56:00 +0000834 // [M3] and [B3] say that, if the target is protected in N, we grant
835 // access if the access occurs in a friend or member of some class P
836 // that's a subclass of N and where the target has some natural
837 // access in P. The 'member' aspect is easy to handle because P
838 // would necessarily be one of the effective-context records, and we
839 // address that above. The 'friend' aspect is completely ridiculous
840 // to implement because there are no restrictions at all on P
841 // *unless* the [class.protected] restriction applies. If it does,
842 // however, we should ignore whether the naming class is a friend,
843 // and instead rely on whether any potential P is a friend.
John McCall5dadb652012-04-07 03:04:20 +0000844 if (Access == AS_protected && Target.isInstanceMember()) {
845 // Compute the instance context if possible.
Craig Topperc3ec1492014-05-26 06:22:03 +0000846 const CXXRecordDecl *InstanceContext = nullptr;
John McCall5dadb652012-04-07 03:04:20 +0000847 if (Target.hasInstanceContext()) {
848 InstanceContext = Target.resolveInstanceContext(S);
849 if (!InstanceContext) return AR_dependent;
850 }
851
John McCall96329672010-08-28 07:56:00 +0000852 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
853 case AR_accessible: return AR_accessible;
John McCalla8ae2222010-04-06 21:38:20 +0000854 case AR_inaccessible: return OnFailure;
855 case AR_dependent: return AR_dependent;
856 }
John McCallc6af8f42010-08-28 08:10:32 +0000857 llvm_unreachable("impossible friendship kind");
John McCalla8ae2222010-04-06 21:38:20 +0000858 }
859
860 switch (GetFriendKind(S, EC, NamingClass)) {
861 case AR_accessible: return AR_accessible;
862 case AR_inaccessible: return OnFailure;
863 case AR_dependent: return AR_dependent;
864 }
865
866 // Silence bogus warnings
867 llvm_unreachable("impossible friendship kind");
John McCalld79b4d82010-04-02 00:03:43 +0000868}
869
John McCall5b0829a2010-02-10 09:31:12 +0000870/// Finds the best path from the naming class to the declaring class,
871/// taking friend declarations into account.
872///
John McCalld79b4d82010-04-02 00:03:43 +0000873/// C++0x [class.access.base]p5:
874/// A member m is accessible at the point R when named in class N if
875/// [M1] m as a member of N is public, or
876/// [M2] m as a member of N is private, and R occurs in a member or
877/// friend of class N, or
878/// [M3] m as a member of N is protected, and R occurs in a member or
879/// friend of class N, or in a member or friend of a class P
880/// derived from N, where m as a member of P is public, private,
881/// or protected, or
882/// [M4] there exists a base class B of N that is accessible at R, and
883/// m is accessible at R when named in class B.
884///
885/// C++0x [class.access.base]p4:
886/// A base class B of N is accessible at R, if
887/// [B1] an invented public member of B would be a public member of N, or
888/// [B2] R occurs in a member or friend of class N, and an invented public
889/// member of B would be a private or protected member of N, or
890/// [B3] R occurs in a member or friend of a class P derived from N, and an
891/// invented public member of B would be a private or protected member
892/// of P, or
893/// [B4] there exists a class S such that B is a base class of S accessible
894/// at R and S is a base class of N accessible at R.
895///
896/// Along a single inheritance path we can restate both of these
897/// iteratively:
898///
899/// First, we note that M1-4 are equivalent to B1-4 if the member is
900/// treated as a notional base of its declaring class with inheritance
901/// access equivalent to the member's access. Therefore we need only
902/// ask whether a class B is accessible from a class N in context R.
903///
904/// Let B_1 .. B_n be the inheritance path in question (i.e. where
905/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
906/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
907/// closest accessible base in the path:
908/// Access(a, b) = (* access on the base specifier from a to b *)
909/// Merge(a, forbidden) = forbidden
910/// Merge(a, private) = forbidden
911/// Merge(a, b) = min(a,b)
912/// Accessible(c, forbidden) = false
913/// Accessible(c, private) = (R is c) || IsFriend(c, R)
914/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
915/// Accessible(c, public) = true
916/// ACAB(n) = public
917/// ACAB(i) =
918/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
919/// if Accessible(B_i, AccessToBase) then public else AccessToBase
920///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000921/// B is an accessible base of N at R iff ACAB(1) = public.
John McCalld79b4d82010-04-02 00:03:43 +0000922///
John McCalla8ae2222010-04-06 21:38:20 +0000923/// \param FinalAccess the access of the "final step", or AS_public if
John McCalla332b952010-03-18 23:49:19 +0000924/// there is no final step.
John McCall5b0829a2010-02-10 09:31:12 +0000925/// \return null if friendship is dependent
926static CXXBasePath *FindBestPath(Sema &S,
927 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000928 AccessTarget &Target,
John McCalla332b952010-03-18 23:49:19 +0000929 AccessSpecifier FinalAccess,
John McCall5b0829a2010-02-10 09:31:12 +0000930 CXXBasePaths &Paths) {
931 // Derive the paths to the desired base.
John McCalla8ae2222010-04-06 21:38:20 +0000932 const CXXRecordDecl *Derived = Target.getNamingClass();
933 const CXXRecordDecl *Base = Target.getDeclaringClass();
934
935 // FIXME: fail correctly when there are dependent paths.
936 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
937 Paths);
John McCall5b0829a2010-02-10 09:31:12 +0000938 assert(isDerived && "derived class not actually derived from base");
939 (void) isDerived;
940
Craig Topperc3ec1492014-05-26 06:22:03 +0000941 CXXBasePath *BestPath = nullptr;
John McCall5b0829a2010-02-10 09:31:12 +0000942
John McCalla332b952010-03-18 23:49:19 +0000943 assert(FinalAccess != AS_none && "forbidden access after declaring class");
944
John McCallc62bb642010-03-24 05:22:00 +0000945 bool AnyDependent = false;
946
John McCall5b0829a2010-02-10 09:31:12 +0000947 // Derive the friend-modified access along each path.
948 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
949 PI != PE; ++PI) {
John McCalla8ae2222010-04-06 21:38:20 +0000950 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +0000951
952 // Walk through the path backwards.
John McCalla332b952010-03-18 23:49:19 +0000953 AccessSpecifier PathAccess = FinalAccess;
John McCall5b0829a2010-02-10 09:31:12 +0000954 CXXBasePath::iterator I = PI->end(), E = PI->begin();
955 while (I != E) {
956 --I;
957
John McCalla332b952010-03-18 23:49:19 +0000958 assert(PathAccess != AS_none);
959
960 // If the declaration is a private member of a base class, there
961 // is no level of friendship in derived classes that can make it
962 // accessible.
963 if (PathAccess == AS_private) {
964 PathAccess = AS_none;
965 break;
966 }
967
John McCalla8ae2222010-04-06 21:38:20 +0000968 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
969
John McCall5b0829a2010-02-10 09:31:12 +0000970 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalld79b4d82010-04-02 00:03:43 +0000971 PathAccess = std::max(PathAccess, BaseAccess);
John McCalla8ae2222010-04-06 21:38:20 +0000972
973 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
974 case AR_inaccessible: break;
975 case AR_accessible:
976 PathAccess = AS_public;
977
978 // Future tests are not against members and so do not have
979 // instance context.
980 Target.suppressInstanceContext();
981 break;
982 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +0000983 AnyDependent = true;
984 goto Next;
John McCall5b0829a2010-02-10 09:31:12 +0000985 }
John McCall5b0829a2010-02-10 09:31:12 +0000986 }
987
988 // Note that we modify the path's Access field to the
989 // friend-modified access.
Craig Topperc3ec1492014-05-26 06:22:03 +0000990 if (BestPath == nullptr || PathAccess < BestPath->Access) {
John McCall5b0829a2010-02-10 09:31:12 +0000991 BestPath = &*PI;
992 BestPath->Access = PathAccess;
John McCallc62bb642010-03-24 05:22:00 +0000993
994 // Short-circuit if we found a public path.
995 if (BestPath->Access == AS_public)
996 return BestPath;
John McCall5b0829a2010-02-10 09:31:12 +0000997 }
John McCallc62bb642010-03-24 05:22:00 +0000998
999 Next: ;
John McCall5b0829a2010-02-10 09:31:12 +00001000 }
1001
John McCallc62bb642010-03-24 05:22:00 +00001002 assert((!BestPath || BestPath->Access != AS_public) &&
1003 "fell out of loop with public path");
1004
1005 // We didn't find a public path, but at least one path was subject
1006 // to dependent friendship, so delay the check.
1007 if (AnyDependent)
Craig Topperc3ec1492014-05-26 06:22:03 +00001008 return nullptr;
John McCallc62bb642010-03-24 05:22:00 +00001009
John McCall5b0829a2010-02-10 09:31:12 +00001010 return BestPath;
1011}
1012
John McCall417e7442010-09-03 04:56:05 +00001013/// Given that an entity has protected natural access, check whether
1014/// access might be denied because of the protected member access
1015/// restriction.
1016///
1017/// \return true if a note was emitted
1018static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
1019 AccessTarget &Target) {
1020 // Only applies to instance accesses.
John McCall5dadb652012-04-07 03:04:20 +00001021 if (!Target.isInstanceMember())
John McCall417e7442010-09-03 04:56:05 +00001022 return false;
John McCall417e7442010-09-03 04:56:05 +00001023
John McCall5dadb652012-04-07 03:04:20 +00001024 assert(Target.isMemberAccess());
1025
John McCalld010ac92013-02-27 00:08:19 +00001026 const CXXRecordDecl *NamingClass = Target.getEffectiveNamingClass();
John McCall417e7442010-09-03 04:56:05 +00001027
1028 for (EffectiveContext::record_iterator
1029 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
1030 const CXXRecordDecl *ECRecord = *I;
John McCall5dadb652012-04-07 03:04:20 +00001031 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
John McCall417e7442010-09-03 04:56:05 +00001032 case AR_accessible: break;
1033 case AR_inaccessible: continue;
1034 case AR_dependent: continue;
1035 }
1036
1037 // The effective context is a subclass of the declaring class.
John McCall5dadb652012-04-07 03:04:20 +00001038 // Check whether the [class.protected] restriction is limiting
1039 // access.
John McCall417e7442010-09-03 04:56:05 +00001040
1041 // To get this exactly right, this might need to be checked more
1042 // holistically; it's not necessarily the case that gaining
1043 // access here would grant us access overall.
1044
John McCall5dadb652012-04-07 03:04:20 +00001045 NamedDecl *D = Target.getTargetDecl();
1046
1047 // If we don't have an instance context, [class.protected] says the
1048 // naming class has to equal the context class.
1049 if (!Target.hasInstanceContext()) {
1050 // If it does, the restriction doesn't apply.
1051 if (NamingClass == ECRecord) continue;
1052
1053 // TODO: it would be great to have a fixit here, since this is
1054 // such an obvious error.
1055 S.Diag(D->getLocation(), diag::note_access_protected_restricted_noobject)
1056 << S.Context.getTypeDeclType(ECRecord);
1057 return true;
1058 }
1059
John McCall417e7442010-09-03 04:56:05 +00001060 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
1061 assert(InstanceContext && "diagnosing dependent access");
1062
1063 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
1064 case AR_accessible: continue;
1065 case AR_dependent: continue;
1066 case AR_inaccessible:
John McCall5dadb652012-04-07 03:04:20 +00001067 break;
1068 }
1069
1070 // Okay, the restriction seems to be what's limiting us.
1071
1072 // Use a special diagnostic for constructors and destructors.
1073 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D) ||
1074 (isa<FunctionTemplateDecl>(D) &&
1075 isa<CXXConstructorDecl>(
1076 cast<FunctionTemplateDecl>(D)->getTemplatedDecl()))) {
Alp Tokera2794f92014-01-22 07:29:52 +00001077 return S.Diag(D->getLocation(),
1078 diag::note_access_protected_restricted_ctordtor)
1079 << isa<CXXDestructorDecl>(D->getAsFunction());
John McCall417e7442010-09-03 04:56:05 +00001080 }
John McCall5dadb652012-04-07 03:04:20 +00001081
1082 // Otherwise, use the generic diagnostic.
Alp Tokera2794f92014-01-22 07:29:52 +00001083 return S.Diag(D->getLocation(),
1084 diag::note_access_protected_restricted_object)
1085 << S.Context.getTypeDeclType(ECRecord);
John McCall417e7442010-09-03 04:56:05 +00001086 }
1087
1088 return false;
1089}
1090
John McCalld010ac92013-02-27 00:08:19 +00001091/// We are unable to access a given declaration due to its direct
1092/// access control; diagnose that.
1093static void diagnoseBadDirectAccess(Sema &S,
1094 const EffectiveContext &EC,
1095 AccessTarget &entity) {
1096 assert(entity.isMemberAccess());
1097 NamedDecl *D = entity.getTargetDecl();
1098
1099 if (D->getAccess() == AS_protected &&
1100 TryDiagnoseProtectedAccess(S, EC, entity))
1101 return;
1102
1103 // Find an original declaration.
1104 while (D->isOutOfLine()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001105 NamedDecl *PrevDecl = nullptr;
John McCalld010ac92013-02-27 00:08:19 +00001106 if (VarDecl *VD = dyn_cast<VarDecl>(D))
1107 PrevDecl = VD->getPreviousDecl();
1108 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1109 PrevDecl = FD->getPreviousDecl();
1110 else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
1111 PrevDecl = TND->getPreviousDecl();
1112 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1113 if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1114 break;
1115 PrevDecl = TD->getPreviousDecl();
1116 }
1117 if (!PrevDecl) break;
1118 D = PrevDecl;
1119 }
1120
1121 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
1122 Decl *ImmediateChild;
1123 if (D->getDeclContext() == DeclaringClass)
1124 ImmediateChild = D;
1125 else {
1126 DeclContext *DC = D->getDeclContext();
1127 while (DC->getParent() != DeclaringClass)
1128 DC = DC->getParent();
1129 ImmediateChild = cast<Decl>(DC);
1130 }
1131
1132 // Check whether there's an AccessSpecDecl preceding this in the
1133 // chain of the DeclContext.
1134 bool isImplicit = true;
Aaron Ballman629afae2014-03-07 19:56:05 +00001135 for (const auto *I : DeclaringClass->decls()) {
1136 if (I == ImmediateChild) break;
1137 if (isa<AccessSpecDecl>(I)) {
John McCalld010ac92013-02-27 00:08:19 +00001138 isImplicit = false;
1139 break;
1140 }
1141 }
1142
1143 S.Diag(D->getLocation(), diag::note_access_natural)
1144 << (unsigned) (D->getAccess() == AS_protected)
1145 << isImplicit;
1146}
1147
John McCall5b0829a2010-02-10 09:31:12 +00001148/// Diagnose the path which caused the given declaration or base class
1149/// to become inaccessible.
1150static void DiagnoseAccessPath(Sema &S,
1151 const EffectiveContext &EC,
John McCalld010ac92013-02-27 00:08:19 +00001152 AccessTarget &entity) {
1153 // Save the instance context to preserve invariants.
1154 AccessTarget::SavedInstanceContext _ = entity.saveInstanceContext();
John McCalld79b4d82010-04-02 00:03:43 +00001155
John McCalld010ac92013-02-27 00:08:19 +00001156 // This basically repeats the main algorithm but keeps some more
1157 // information.
John McCalld79b4d82010-04-02 00:03:43 +00001158
John McCalld010ac92013-02-27 00:08:19 +00001159 // The natural access so far.
1160 AccessSpecifier accessSoFar = AS_public;
John McCall417e7442010-09-03 04:56:05 +00001161
John McCalld010ac92013-02-27 00:08:19 +00001162 // Check whether we have special rights to the declaring class.
1163 if (entity.isMemberAccess()) {
1164 NamedDecl *D = entity.getTargetDecl();
1165 accessSoFar = D->getAccess();
1166 const CXXRecordDecl *declaringClass = entity.getDeclaringClass();
John McCallf551aca2010-10-20 08:15:06 +00001167
John McCalld010ac92013-02-27 00:08:19 +00001168 switch (HasAccess(S, EC, declaringClass, accessSoFar, entity)) {
1169 // If the declaration is accessible when named in its declaring
1170 // class, then we must be constrained by the path.
1171 case AR_accessible:
1172 accessSoFar = AS_public;
1173 entity.suppressInstanceContext();
1174 break;
John McCallf551aca2010-10-20 08:15:06 +00001175
John McCalld010ac92013-02-27 00:08:19 +00001176 case AR_inaccessible:
1177 if (accessSoFar == AS_private ||
1178 declaringClass == entity.getEffectiveNamingClass())
1179 return diagnoseBadDirectAccess(S, EC, entity);
1180 break;
John McCall5b0829a2010-02-10 09:31:12 +00001181
John McCalla8ae2222010-04-06 21:38:20 +00001182 case AR_dependent:
John McCalld010ac92013-02-27 00:08:19 +00001183 llvm_unreachable("cannot diagnose dependent access");
John McCall5b0829a2010-02-10 09:31:12 +00001184 }
1185 }
1186
John McCalld010ac92013-02-27 00:08:19 +00001187 CXXBasePaths paths;
1188 CXXBasePath &path = *FindBestPath(S, EC, entity, accessSoFar, paths);
1189 assert(path.Access != AS_public);
John McCall5b0829a2010-02-10 09:31:12 +00001190
John McCalld010ac92013-02-27 00:08:19 +00001191 CXXBasePath::iterator i = path.end(), e = path.begin();
1192 CXXBasePath::iterator constrainingBase = i;
1193 while (i != e) {
1194 --i;
John McCall5b0829a2010-02-10 09:31:12 +00001195
John McCalld010ac92013-02-27 00:08:19 +00001196 assert(accessSoFar != AS_none && accessSoFar != AS_private);
John McCall5b0829a2010-02-10 09:31:12 +00001197
John McCalld010ac92013-02-27 00:08:19 +00001198 // Is the entity accessible when named in the deriving class, as
1199 // modified by the base specifier?
1200 const CXXRecordDecl *derivingClass = i->Class->getCanonicalDecl();
1201 const CXXBaseSpecifier *base = i->Base;
John McCall5b0829a2010-02-10 09:31:12 +00001202
John McCalld010ac92013-02-27 00:08:19 +00001203 // If the access to this base is worse than the access we have to
1204 // the declaration, remember it.
1205 AccessSpecifier baseAccess = base->getAccessSpecifier();
1206 if (baseAccess > accessSoFar) {
1207 constrainingBase = i;
1208 accessSoFar = baseAccess;
1209 }
1210
1211 switch (HasAccess(S, EC, derivingClass, accessSoFar, entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001212 case AR_inaccessible: break;
John McCalld010ac92013-02-27 00:08:19 +00001213 case AR_accessible:
1214 accessSoFar = AS_public;
1215 entity.suppressInstanceContext();
Craig Topperc3ec1492014-05-26 06:22:03 +00001216 constrainingBase = nullptr;
John McCalld010ac92013-02-27 00:08:19 +00001217 break;
John McCalla8ae2222010-04-06 21:38:20 +00001218 case AR_dependent:
John McCalld010ac92013-02-27 00:08:19 +00001219 llvm_unreachable("cannot diagnose dependent access");
John McCall5b0829a2010-02-10 09:31:12 +00001220 }
1221
John McCalld010ac92013-02-27 00:08:19 +00001222 // If this was private inheritance, but we don't have access to
1223 // the deriving class, we're done.
1224 if (accessSoFar == AS_private) {
1225 assert(baseAccess == AS_private);
1226 assert(constrainingBase == i);
1227 break;
John McCall5b0829a2010-02-10 09:31:12 +00001228 }
1229 }
1230
John McCalld010ac92013-02-27 00:08:19 +00001231 // If we don't have a constraining base, the access failure must be
1232 // due to the original declaration.
1233 if (constrainingBase == path.end())
1234 return diagnoseBadDirectAccess(S, EC, entity);
1235
1236 // We're constrained by inheritance, but we want to say
1237 // "declared private here" if we're diagnosing a hierarchy
1238 // conversion and this is the final step.
1239 unsigned diagnostic;
1240 if (entity.isMemberAccess() ||
1241 constrainingBase + 1 != path.end()) {
1242 diagnostic = diag::note_access_constrained_by_path;
1243 } else {
1244 diagnostic = diag::note_access_natural;
1245 }
1246
1247 const CXXBaseSpecifier *base = constrainingBase->Base;
1248
1249 S.Diag(base->getSourceRange().getBegin(), diagnostic)
1250 << base->getSourceRange()
1251 << (base->getAccessSpecifier() == AS_protected)
1252 << (base->getAccessSpecifierAsWritten() == AS_none);
1253
1254 if (entity.isMemberAccess())
Alp Toker2afa8782014-05-28 12:20:14 +00001255 S.Diag(entity.getTargetDecl()->getLocation(),
1256 diag::note_member_declared_at);
John McCall5b0829a2010-02-10 09:31:12 +00001257}
1258
John McCall1064d7e2010-03-16 05:22:47 +00001259static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall5b0829a2010-02-10 09:31:12 +00001260 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +00001261 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001262 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalla8ae2222010-04-06 21:38:20 +00001263 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
Craig Topperc3ec1492014-05-26 06:22:03 +00001264 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : nullptr);
John McCalld79b4d82010-04-02 00:03:43 +00001265
1266 S.Diag(Loc, Entity.getDiag())
1267 << (Entity.getAccess() == AS_protected)
1268 << (D ? D->getDeclName() : DeclarationName())
1269 << S.Context.getTypeDeclType(NamingClass)
1270 << S.Context.getTypeDeclType(DeclaringClass);
1271 DiagnoseAccessPath(S, EC, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001272}
1273
Francois Pichetefb1af92011-05-23 03:43:44 +00001274/// MSVC has a bug where if during an using declaration name lookup,
1275/// the declaration found is unaccessible (private) and that declaration
1276/// was bring into scope via another using declaration whose target
1277/// declaration is accessible (public) then no error is generated.
1278/// Example:
1279/// class A {
1280/// public:
1281/// int f();
1282/// };
1283/// class B : public A {
1284/// private:
1285/// using A::f;
1286/// };
1287/// class C : public B {
1288/// private:
1289/// using B::f;
1290/// };
1291///
1292/// Here, B::f is private so this should fail in Standard C++, but
1293/// because B::f refers to A::f which is public MSVC accepts it.
1294static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S,
1295 SourceLocation AccessLoc,
1296 AccessTarget &Entity) {
1297 if (UsingShadowDecl *Shadow =
1298 dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) {
1299 const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl();
1300 if (Entity.getTargetDecl()->getAccess() == AS_private &&
1301 (OrigDecl->getAccess() == AS_public ||
1302 OrigDecl->getAccess() == AS_protected)) {
Richard Smithe4345902011-12-29 21:57:33 +00001303 S.Diag(AccessLoc, diag::ext_ms_using_declaration_inaccessible)
Francois Pichetefb1af92011-05-23 03:43:44 +00001304 << Shadow->getUsingDecl()->getQualifiedNameAsString()
1305 << OrigDecl->getQualifiedNameAsString();
1306 return true;
1307 }
1308 }
1309 return false;
1310}
1311
John McCalld79b4d82010-04-02 00:03:43 +00001312/// Determines whether the accessed entity is accessible. Public members
1313/// have been weeded out by this point.
John McCalla8ae2222010-04-06 21:38:20 +00001314static AccessResult IsAccessible(Sema &S,
1315 const EffectiveContext &EC,
1316 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001317 // Determine the actual naming class.
John McCalld010ac92013-02-27 00:08:19 +00001318 const CXXRecordDecl *NamingClass = Entity.getEffectiveNamingClass();
John McCall5b0829a2010-02-10 09:31:12 +00001319
John McCalld79b4d82010-04-02 00:03:43 +00001320 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1321 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1322
1323 // Before we try to recalculate access paths, try to white-list
1324 // accesses which just trade in on the final step, i.e. accesses
1325 // which don't require [M4] or [B4]. These are by far the most
John McCalla8ae2222010-04-06 21:38:20 +00001326 // common forms of privileged access.
John McCalld79b4d82010-04-02 00:03:43 +00001327 if (UnprivilegedAccess != AS_none) {
John McCalla8ae2222010-04-06 21:38:20 +00001328 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1329 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +00001330 // This is actually an interesting policy decision. We don't
1331 // *have* to delay immediately here: we can do the full access
1332 // calculation in the hope that friendship on some intermediate
1333 // class will make the declaration accessible non-dependently.
1334 // But that's not cheap, and odds are very good (note: assertion
1335 // made without data) that the friend declaration will determine
1336 // access.
John McCalla8ae2222010-04-06 21:38:20 +00001337 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001338
John McCalla8ae2222010-04-06 21:38:20 +00001339 case AR_accessible: return AR_accessible;
1340 case AR_inaccessible: break;
John McCalld79b4d82010-04-02 00:03:43 +00001341 }
1342 }
1343
John McCalla8ae2222010-04-06 21:38:20 +00001344 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +00001345
John McCalld79b4d82010-04-02 00:03:43 +00001346 // We lower member accesses to base accesses by pretending that the
1347 // member is a base class of its declaring class.
1348 AccessSpecifier FinalAccess;
1349
John McCall5b0829a2010-02-10 09:31:12 +00001350 if (Entity.isMemberAccess()) {
John McCalld79b4d82010-04-02 00:03:43 +00001351 // Determine if the declaration is accessible from EC when named
1352 // in its declaring class.
John McCall5b0829a2010-02-10 09:31:12 +00001353 NamedDecl *Target = Entity.getTargetDecl();
John McCalla8ae2222010-04-06 21:38:20 +00001354 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall5b0829a2010-02-10 09:31:12 +00001355
John McCalld79b4d82010-04-02 00:03:43 +00001356 FinalAccess = Target->getAccess();
John McCalla8ae2222010-04-06 21:38:20 +00001357 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1358 case AR_accessible:
John McCall5149fbf2013-02-22 03:52:55 +00001359 // Target is accessible at EC when named in its declaring class.
1360 // We can now hill-climb and simply check whether the declaring
1361 // class is accessible as a base of the naming class. This is
1362 // equivalent to checking the access of a notional public
1363 // member with no instance context.
John McCalla8ae2222010-04-06 21:38:20 +00001364 FinalAccess = AS_public;
John McCall5149fbf2013-02-22 03:52:55 +00001365 Entity.suppressInstanceContext();
John McCalla8ae2222010-04-06 21:38:20 +00001366 break;
1367 case AR_inaccessible: break;
1368 case AR_dependent: return AR_dependent; // see above
John McCall5b0829a2010-02-10 09:31:12 +00001369 }
1370
John McCalld79b4d82010-04-02 00:03:43 +00001371 if (DeclaringClass == NamingClass)
John McCalla8ae2222010-04-06 21:38:20 +00001372 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
John McCalld79b4d82010-04-02 00:03:43 +00001373 } else {
1374 FinalAccess = AS_public;
John McCall5b0829a2010-02-10 09:31:12 +00001375 }
1376
John McCalla8ae2222010-04-06 21:38:20 +00001377 assert(Entity.getDeclaringClass() != NamingClass);
John McCall5b0829a2010-02-10 09:31:12 +00001378
1379 // Append the declaration's access if applicable.
1380 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001381 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCallc62bb642010-03-24 05:22:00 +00001382 if (!Path)
John McCalla8ae2222010-04-06 21:38:20 +00001383 return AR_dependent;
John McCall553c0792010-01-23 00:46:32 +00001384
John McCalld79b4d82010-04-02 00:03:43 +00001385 assert(Path->Access <= UnprivilegedAccess &&
1386 "access along best path worse than direct?");
1387 if (Path->Access == AS_public)
John McCalla8ae2222010-04-06 21:38:20 +00001388 return AR_accessible;
1389 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +00001390}
1391
John McCalla8ae2222010-04-06 21:38:20 +00001392static void DelayDependentAccess(Sema &S,
1393 const EffectiveContext &EC,
1394 SourceLocation Loc,
1395 const AccessTarget &Entity) {
John McCallc62bb642010-03-24 05:22:00 +00001396 assert(EC.isDependent() && "delaying non-dependent access");
John McCall816d75b2010-03-24 07:46:06 +00001397 DeclContext *DC = EC.getInnerContext();
John McCallc62bb642010-03-24 05:22:00 +00001398 assert(DC->isDependentContext() && "delaying non-dependent access");
1399 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1400 Loc,
1401 Entity.isMemberAccess(),
1402 Entity.getAccess(),
1403 Entity.getTargetDecl(),
1404 Entity.getNamingClass(),
John McCalla8ae2222010-04-06 21:38:20 +00001405 Entity.getBaseObjectType(),
John McCallc62bb642010-03-24 05:22:00 +00001406 Entity.getDiag());
John McCall553c0792010-01-23 00:46:32 +00001407}
1408
John McCall5b0829a2010-02-10 09:31:12 +00001409/// Checks access to an entity from the given effective context.
John McCalla8ae2222010-04-06 21:38:20 +00001410static AccessResult CheckEffectiveAccess(Sema &S,
1411 const EffectiveContext &EC,
1412 SourceLocation Loc,
1413 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001414 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall553c0792010-01-23 00:46:32 +00001415
John McCalld79b4d82010-04-02 00:03:43 +00001416 switch (IsAccessible(S, EC, Entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001417 case AR_dependent:
1418 DelayDependentAccess(S, EC, Loc, Entity);
1419 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001420
John McCalla8ae2222010-04-06 21:38:20 +00001421 case AR_inaccessible:
Reid Kleckner42063b02014-02-08 02:40:20 +00001422 if (S.getLangOpts().MSVCCompat &&
1423 IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity))
1424 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +00001425 if (!Entity.isQuiet())
1426 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCalla8ae2222010-04-06 21:38:20 +00001427 return AR_inaccessible;
John McCalld79b4d82010-04-02 00:03:43 +00001428
John McCalla8ae2222010-04-06 21:38:20 +00001429 case AR_accessible:
1430 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +00001431 }
1432
John McCalla8ae2222010-04-06 21:38:20 +00001433 // silence unnecessary warning
1434 llvm_unreachable("invalid access result");
John McCall5b0829a2010-02-10 09:31:12 +00001435}
John McCall553c0792010-01-23 00:46:32 +00001436
John McCall5b0829a2010-02-10 09:31:12 +00001437static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCalla8ae2222010-04-06 21:38:20 +00001438 AccessTarget &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +00001439 // If the access path is public, it's accessible everywhere.
1440 if (Entity.getAccess() == AS_public)
1441 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001442
John McCallc1465822011-02-14 07:13:47 +00001443 // If we're currently parsing a declaration, we may need to delay
1444 // access control checking, because our effective context might be
1445 // different based on what the declaration comes out as.
1446 //
1447 // For example, we might be parsing a declaration with a scope
1448 // specifier, like this:
1449 // A::private_type A::foo() { ... }
1450 //
1451 // Or we might be parsing something that will turn out to be a friend:
1452 // void foo(A::private_type);
1453 // void B::foo(A::private_type);
1454 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1455 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall5b0829a2010-02-10 09:31:12 +00001456 return Sema::AR_delayed;
John McCall553c0792010-01-23 00:46:32 +00001457 }
1458
John McCalla8ae2222010-04-06 21:38:20 +00001459 EffectiveContext EC(S.CurContext);
1460 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1461 case AR_accessible: return Sema::AR_accessible;
1462 case AR_inaccessible: return Sema::AR_inaccessible;
1463 case AR_dependent: return Sema::AR_dependent;
1464 }
Davide Italiano24e89662015-05-17 02:27:10 +00001465 llvm_unreachable("invalid access result");
John McCall553c0792010-01-23 00:46:32 +00001466}
1467
Richard Smithad5c1ca2013-04-29 10:13:55 +00001468void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *D) {
John McCall9743e8d2011-02-15 22:51:53 +00001469 // Access control for names used in the declarations of functions
1470 // and function templates should normally be evaluated in the context
1471 // of the declaration, just in case it's a friend of something.
1472 // However, this does not apply to local extern declarations.
1473
Richard Smithad5c1ca2013-04-29 10:13:55 +00001474 DeclContext *DC = D->getDeclContext();
Richard Smith608da012013-12-11 03:35:27 +00001475 if (D->isLocalExternDecl()) {
1476 DC = D->getLexicalDeclContext();
1477 } else if (FunctionDecl *FN = dyn_cast<FunctionDecl>(D)) {
1478 DC = FN;
Richard Smithad5c1ca2013-04-29 10:13:55 +00001479 } else if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) {
1480 DC = cast<DeclContext>(TD->getTemplatedDecl());
John McCall9743e8d2011-02-15 22:51:53 +00001481 }
1482
Chandler Carruthaad30072010-04-18 08:23:21 +00001483 EffectiveContext EC(DC);
John McCall86121512010-01-27 03:50:35 +00001484
John McCalla8ae2222010-04-06 21:38:20 +00001485 AccessTarget Target(DD.getAccessData());
1486
1487 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall86121512010-01-27 03:50:35 +00001488 DD.Triggered = true;
1489}
1490
John McCallc62bb642010-03-24 05:22:00 +00001491void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1492 const MultiLevelTemplateArgumentList &TemplateArgs) {
1493 SourceLocation Loc = DD.getAccessLoc();
1494 AccessSpecifier Access = DD.getAccess();
1495
1496 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1497 TemplateArgs);
1498 if (!NamingD) return;
1499 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1500 TemplateArgs);
1501 if (!TargetD) return;
1502
1503 if (DD.isAccessToMember()) {
John McCalla8ae2222010-04-06 21:38:20 +00001504 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1505 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1506 QualType BaseObjectType = DD.getAccessBaseObjectType();
1507 if (!BaseObjectType.isNull()) {
1508 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1509 DeclarationName());
1510 if (BaseObjectType.isNull()) return;
1511 }
1512
1513 AccessTarget Entity(Context,
1514 AccessTarget::Member,
1515 NamingClass,
1516 DeclAccessPair::make(TargetDecl, Access),
1517 BaseObjectType);
John McCallc62bb642010-03-24 05:22:00 +00001518 Entity.setDiag(DD.getDiagnostic());
1519 CheckAccess(*this, Loc, Entity);
1520 } else {
John McCalla8ae2222010-04-06 21:38:20 +00001521 AccessTarget Entity(Context,
1522 AccessTarget::Base,
1523 cast<CXXRecordDecl>(TargetD),
1524 cast<CXXRecordDecl>(NamingD),
1525 Access);
John McCallc62bb642010-03-24 05:22:00 +00001526 Entity.setDiag(DD.getDiagnostic());
1527 CheckAccess(*this, Loc, Entity);
1528 }
1529}
1530
John McCall5b0829a2010-02-10 09:31:12 +00001531Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001532 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001533 if (!getLangOpts().AccessControl ||
John McCall1064d7e2010-03-16 05:22:47 +00001534 !E->getNamingClass() ||
John McCalla0296f72010-03-19 07:35:19 +00001535 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001536 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001537
John McCalla8ae2222010-04-06 21:38:20 +00001538 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1539 Found, QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001540 Entity.setDiag(diag::err_access) << E->getSourceRange();
1541
1542 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001543}
1544
1545/// Perform access-control checking on a previously-unresolved member
1546/// access which has now been resolved to a member.
John McCall5b0829a2010-02-10 09:31:12 +00001547Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001548 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001549 if (!getLangOpts().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001550 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001551 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001552
John McCalla8ae2222010-04-06 21:38:20 +00001553 QualType BaseType = E->getBaseType();
1554 if (E->isArrow())
1555 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1556
1557 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1558 Found, BaseType);
John McCall1064d7e2010-03-16 05:22:47 +00001559 Entity.setDiag(diag::err_access) << E->getSourceRange();
1560
1561 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001562}
1563
John McCalld4274212012-04-09 20:53:23 +00001564/// Is the given special member function accessible for the purposes of
1565/// deciding whether to define a special member function as deleted?
1566bool Sema::isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl,
1567 AccessSpecifier access,
1568 QualType objectType) {
1569 // Fast path.
1570 if (access == AS_public || !getLangOpts().AccessControl) return true;
1571
1572 AccessTarget entity(Context, AccessTarget::Member, decl->getParent(),
1573 DeclAccessPair::make(decl, access), objectType);
1574
1575 // Suppress diagnostics.
1576 entity.setDiag(PDiag());
1577
1578 switch (CheckAccess(*this, SourceLocation(), entity)) {
1579 case AR_accessible: return true;
1580 case AR_inaccessible: return false;
1581 case AR_dependent: llvm_unreachable("dependent for =delete computation");
1582 case AR_delayed: llvm_unreachable("cannot delay =delete computation");
1583 }
1584 llvm_unreachable("bad access result");
1585}
1586
John McCall5b0829a2010-02-10 09:31:12 +00001587Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +00001588 CXXDestructorDecl *Dtor,
John McCall5dadb652012-04-07 03:04:20 +00001589 const PartialDiagnostic &PDiag,
1590 QualType ObjectTy) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001591 if (!getLangOpts().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +00001592 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001593
John McCall1064d7e2010-03-16 05:22:47 +00001594 // There's never a path involved when checking implicit destructor access.
John McCall6781b052010-02-02 08:45:54 +00001595 AccessSpecifier Access = Dtor->getAccess();
1596 if (Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001597 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001598
John McCall1064d7e2010-03-16 05:22:47 +00001599 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCall5dadb652012-04-07 03:04:20 +00001600 if (ObjectTy.isNull()) ObjectTy = Context.getTypeDeclType(NamingClass);
1601
John McCalla8ae2222010-04-06 21:38:20 +00001602 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1603 DeclAccessPair::make(Dtor, Access),
John McCall5dadb652012-04-07 03:04:20 +00001604 ObjectTy);
John McCall1064d7e2010-03-16 05:22:47 +00001605 Entity.setDiag(PDiag); // TODO: avoid copy
1606
1607 return CheckAccess(*this, Loc, Entity);
John McCall6781b052010-02-02 08:45:54 +00001608}
1609
John McCall760af172010-02-01 03:16:54 +00001610/// Checks access to a constructor.
John McCall5b0829a2010-02-10 09:31:12 +00001611Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001612 CXXConstructorDecl *Constructor,
1613 const InitializedEntity &Entity,
1614 AccessSpecifier Access,
1615 bool IsCopyBindingRefToTemp) {
John McCall5dadb652012-04-07 03:04:20 +00001616 if (!getLangOpts().AccessControl || Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001617 return AR_accessible;
John McCall760af172010-02-01 03:16:54 +00001618
Alexis Hunteef8ee02011-06-10 03:50:41 +00001619 PartialDiagnostic PD(PDiag());
Anders Carlssona01874b2010-04-21 18:47:17 +00001620 switch (Entity.getKind()) {
1621 default:
Alexis Hunteef8ee02011-06-10 03:50:41 +00001622 PD = PDiag(IsCopyBindingRefToTemp
1623 ? diag::ext_rvalue_to_reference_access_ctor
1624 : diag::err_access_ctor);
1625
Anders Carlssona01874b2010-04-21 18:47:17 +00001626 break;
John McCall1064d7e2010-03-16 05:22:47 +00001627
Anders Carlsson05bf0092010-04-22 05:40:53 +00001628 case InitializedEntity::EK_Base:
Alexis Hunteef8ee02011-06-10 03:50:41 +00001629 PD = PDiag(diag::err_access_base_ctor);
1630 PD << Entity.isInheritedVirtualBase()
1631 << Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor);
Anders Carlssona01874b2010-04-21 18:47:17 +00001632 break;
Anders Carlsson05bf0092010-04-22 05:40:53 +00001633
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001634 case InitializedEntity::EK_Member: {
1635 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Alexis Hunteef8ee02011-06-10 03:50:41 +00001636 PD = PDiag(diag::err_access_field_ctor);
1637 PD << Field->getType() << getSpecialMember(Constructor);
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001638 break;
1639 }
Anders Carlssona01874b2010-04-21 18:47:17 +00001640
Douglas Gregor19666fb2012-02-15 16:57:26 +00001641 case InitializedEntity::EK_LambdaCapture: {
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001642 StringRef VarName = Entity.getCapturedVarName();
Douglas Gregor19666fb2012-02-15 16:57:26 +00001643 PD = PDiag(diag::err_access_lambda_capture);
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001644 PD << VarName << Entity.getType() << getSpecialMember(Constructor);
Douglas Gregor19666fb2012-02-15 16:57:26 +00001645 break;
1646 }
1647
Anders Carlsson43c64af2010-04-21 19:52:01 +00001648 }
1649
John McCall5dadb652012-04-07 03:04:20 +00001650 return CheckConstructorAccess(UseLoc, Constructor, Entity, Access, PD);
Alexis Hunteef8ee02011-06-10 03:50:41 +00001651}
1652
1653/// Checks access to a constructor.
1654Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
1655 CXXConstructorDecl *Constructor,
John McCall5dadb652012-04-07 03:04:20 +00001656 const InitializedEntity &Entity,
Alexis Hunteef8ee02011-06-10 03:50:41 +00001657 AccessSpecifier Access,
John McCall5dadb652012-04-07 03:04:20 +00001658 const PartialDiagnostic &PD) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001659 if (!getLangOpts().AccessControl ||
Alexis Hunteef8ee02011-06-10 03:50:41 +00001660 Access == AS_public)
1661 return AR_accessible;
1662
1663 CXXRecordDecl *NamingClass = Constructor->getParent();
John McCall5dadb652012-04-07 03:04:20 +00001664
1665 // Initializing a base sub-object is an instance method call on an
1666 // object of the derived class. Otherwise, we have an instance method
1667 // call on an object of the constructed type.
1668 CXXRecordDecl *ObjectClass;
1669 if (Entity.getKind() == InitializedEntity::EK_Base) {
1670 ObjectClass = cast<CXXConstructorDecl>(CurContext)->getParent();
1671 } else {
1672 ObjectClass = NamingClass;
1673 }
1674
Alexis Hunteef8ee02011-06-10 03:50:41 +00001675 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1676 DeclAccessPair::make(Constructor, Access),
John McCall5dadb652012-04-07 03:04:20 +00001677 Context.getTypeDeclType(ObjectClass));
Alexis Hunteef8ee02011-06-10 03:50:41 +00001678 AccessEntity.setDiag(PD);
1679
Anders Carlssona01874b2010-04-21 18:47:17 +00001680 return CheckAccess(*this, UseLoc, AccessEntity);
John McCall5dadb652012-04-07 03:04:20 +00001681}
John McCall760af172010-02-01 03:16:54 +00001682
John McCallfb6f5262010-03-18 08:19:33 +00001683/// Checks access to an overloaded operator new or delete.
1684Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1685 SourceRange PlacementRange,
1686 CXXRecordDecl *NamingClass,
Alexis Huntf91729462011-05-12 22:46:25 +00001687 DeclAccessPair Found,
1688 bool Diagnose) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001689 if (!getLangOpts().AccessControl ||
John McCallfb6f5262010-03-18 08:19:33 +00001690 !NamingClass ||
John McCalla0296f72010-03-19 07:35:19 +00001691 Found.getAccess() == AS_public)
John McCallfb6f5262010-03-18 08:19:33 +00001692 return AR_accessible;
1693
John McCalla8ae2222010-04-06 21:38:20 +00001694 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1695 QualType());
Alexis Huntf91729462011-05-12 22:46:25 +00001696 if (Diagnose)
1697 Entity.setDiag(diag::err_access)
1698 << PlacementRange;
John McCallfb6f5262010-03-18 08:19:33 +00001699
1700 return CheckAccess(*this, OpLoc, Entity);
1701}
1702
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001703/// \brief Checks access to a member.
1704Sema::AccessResult Sema::CheckMemberAccess(SourceLocation UseLoc,
1705 CXXRecordDecl *NamingClass,
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001706 DeclAccessPair Found) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001707 if (!getLangOpts().AccessControl ||
1708 !NamingClass ||
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001709 Found.getAccess() == AS_public)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001710 return AR_accessible;
1711
1712 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001713 Found, QualType());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001714
1715 return CheckAccess(*this, UseLoc, Entity);
1716}
1717
John McCall760af172010-02-01 03:16:54 +00001718/// Checks access to an overloaded member operator, including
1719/// conversion operators.
John McCall5b0829a2010-02-10 09:31:12 +00001720Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1721 Expr *ObjectExpr,
John McCall1064d7e2010-03-16 05:22:47 +00001722 Expr *ArgExpr,
John McCalla0296f72010-03-19 07:35:19 +00001723 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001724 if (!getLangOpts().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001725 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001726 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001727
John McCall30909032011-09-21 08:36:56 +00001728 const RecordType *RT = ObjectExpr->getType()->castAs<RecordType>();
John McCallb3a44002010-01-28 01:42:12 +00001729 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1730
John McCalla8ae2222010-04-06 21:38:20 +00001731 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1732 ObjectExpr->getType());
John McCall1064d7e2010-03-16 05:22:47 +00001733 Entity.setDiag(diag::err_access)
1734 << ObjectExpr->getSourceRange()
1735 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1736
1737 return CheckAccess(*this, OpLoc, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001738}
John McCallb3a44002010-01-28 01:42:12 +00001739
John McCalla0a96892012-08-10 03:15:35 +00001740/// Checks access to the target of a friend declaration.
1741Sema::AccessResult Sema::CheckFriendAccess(NamedDecl *target) {
Alp Tokera2794f92014-01-22 07:29:52 +00001742 assert(isa<CXXMethodDecl>(target->getAsFunction()));
John McCalla0a96892012-08-10 03:15:35 +00001743
1744 // Friendship lookup is a redeclaration lookup, so there's never an
1745 // inheritance path modifying access.
1746 AccessSpecifier access = target->getAccess();
1747
1748 if (!getLangOpts().AccessControl || access == AS_public)
1749 return AR_accessible;
1750
Alp Toker2bd4a282014-01-22 07:53:08 +00001751 CXXMethodDecl *method = cast<CXXMethodDecl>(target->getAsFunction());
John McCalla0a96892012-08-10 03:15:35 +00001752
1753 AccessTarget entity(Context, AccessTarget::Member,
1754 cast<CXXRecordDecl>(target->getDeclContext()),
1755 DeclAccessPair::make(target, access),
1756 /*no instance context*/ QualType());
1757 entity.setDiag(diag::err_access_friend_function)
Reid Kleckneraf9bf592014-12-17 23:40:46 +00001758 << (method->getQualifier() ? method->getQualifierLoc().getSourceRange()
1759 : method->getNameInfo().getSourceRange());
John McCalla0a96892012-08-10 03:15:35 +00001760
1761 // We need to bypass delayed-diagnostics because we might be called
1762 // while the ParsingDeclarator is active.
1763 EffectiveContext EC(CurContext);
1764 switch (CheckEffectiveAccess(*this, EC, target->getLocation(), entity)) {
1765 case AR_accessible: return Sema::AR_accessible;
1766 case AR_inaccessible: return Sema::AR_inaccessible;
1767 case AR_dependent: return Sema::AR_dependent;
1768 }
1769 llvm_unreachable("falling off end");
1770}
1771
John McCall16df1e52010-03-30 21:47:33 +00001772Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1773 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001774 if (!getLangOpts().AccessControl ||
John McCallb493d532010-03-30 22:20:00 +00001775 Found.getAccess() == AS_none ||
John McCall16df1e52010-03-30 21:47:33 +00001776 Found.getAccess() == AS_public)
1777 return AR_accessible;
1778
John McCall8d08b9b2010-08-27 09:08:28 +00001779 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCall8c12dc42010-04-22 18:44:12 +00001780 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall16df1e52010-03-30 21:47:33 +00001781
John McCalla8ae2222010-04-06 21:38:20 +00001782 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
John McCall5dadb652012-04-07 03:04:20 +00001783 /*no instance context*/ QualType());
John McCall16df1e52010-03-30 21:47:33 +00001784 Entity.setDiag(diag::err_access)
1785 << Ovl->getSourceRange();
1786
1787 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1788}
1789
John McCall5b0829a2010-02-10 09:31:12 +00001790/// Checks access for a hierarchy conversion.
1791///
John McCall5b0829a2010-02-10 09:31:12 +00001792/// \param ForceCheck true if this check should be performed even if access
1793/// control is disabled; some things rely on this for semantics
1794/// \param ForceUnprivileged true if this check should proceed as if the
1795/// context had no special privileges
John McCall5b0829a2010-02-10 09:31:12 +00001796Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall5b0829a2010-02-10 09:31:12 +00001797 QualType Base,
1798 QualType Derived,
1799 const CXXBasePath &Path,
John McCall1064d7e2010-03-16 05:22:47 +00001800 unsigned DiagID,
John McCall5b0829a2010-02-10 09:31:12 +00001801 bool ForceCheck,
John McCall1064d7e2010-03-16 05:22:47 +00001802 bool ForceUnprivileged) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001803 if (!ForceCheck && !getLangOpts().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +00001804 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001805
John McCall5b0829a2010-02-10 09:31:12 +00001806 if (Path.Access == AS_public)
1807 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001808
John McCall5b0829a2010-02-10 09:31:12 +00001809 CXXRecordDecl *BaseD, *DerivedD;
1810 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1811 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall1064d7e2010-03-16 05:22:47 +00001812
John McCalla8ae2222010-04-06 21:38:20 +00001813 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1814 Path.Access);
John McCall1064d7e2010-03-16 05:22:47 +00001815 if (DiagID)
1816 Entity.setDiag(DiagID) << Derived << Base;
John McCall5b0829a2010-02-10 09:31:12 +00001817
John McCalla8ae2222010-04-06 21:38:20 +00001818 if (ForceUnprivileged) {
1819 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1820 AccessLoc, Entity)) {
1821 case ::AR_accessible: return Sema::AR_accessible;
1822 case ::AR_inaccessible: return Sema::AR_inaccessible;
1823 case ::AR_dependent: return Sema::AR_dependent;
1824 }
1825 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1826 }
John McCall1064d7e2010-03-16 05:22:47 +00001827 return CheckAccess(*this, AccessLoc, Entity);
John McCallb3a44002010-01-28 01:42:12 +00001828}
1829
John McCall553c0792010-01-23 00:46:32 +00001830/// Checks access to all the declarations in the given result set.
John McCall5b0829a2010-02-10 09:31:12 +00001831void Sema::CheckLookupAccess(const LookupResult &R) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001832 assert(getLangOpts().AccessControl
John McCall5b0829a2010-02-10 09:31:12 +00001833 && "performing access check without access control");
1834 assert(R.getNamingClass() && "performing access check without naming class");
1835
John McCall1064d7e2010-03-16 05:22:47 +00001836 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1837 if (I.getAccess() != AS_public) {
John McCalla8ae2222010-04-06 21:38:20 +00001838 AccessTarget Entity(Context, AccessedEntity::Member,
1839 R.getNamingClass(), I.getPair(),
Erik Verbruggen631dfc62011-09-19 15:10:40 +00001840 R.getBaseObjectType());
John McCall1064d7e2010-03-16 05:22:47 +00001841 Entity.setDiag(diag::err_access);
John McCall1064d7e2010-03-16 05:22:47 +00001842 CheckAccess(*this, R.getNameLoc(), Entity);
1843 }
1844 }
John McCall553c0792010-01-23 00:46:32 +00001845}
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001846
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001847/// Checks access to Decl from the given class. The check will take access
1848/// specifiers into account, but no member access expressions and such.
1849///
1850/// \param Decl the declaration to check if it can be accessed
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001851/// \param Ctx the class/context from which to start the search
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001852/// \return true if the Decl is accessible from the Class, false otherwise.
Douglas Gregor03ba1882011-11-03 16:51:37 +00001853bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) {
1854 if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) {
Douglas Gregor3b52b692011-11-03 17:41:55 +00001855 if (!Decl->isCXXClassMember())
Douglas Gregor03ba1882011-11-03 16:51:37 +00001856 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001857
Douglas Gregor03ba1882011-11-03 16:51:37 +00001858 QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal();
1859 AccessTarget Entity(Context, AccessedEntity::Member, Class,
1860 DeclAccessPair::make(Decl, Decl->getAccess()),
1861 qType);
1862 if (Entity.getAccess() == AS_public)
1863 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001864
Douglas Gregor03ba1882011-11-03 16:51:37 +00001865 EffectiveContext EC(CurContext);
1866 return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible;
1867 }
1868
Douglas Gregor21ceb182011-11-03 19:00:24 +00001869 if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) {
1870 // @public and @package ivars are always accessible.
1871 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public ||
1872 Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package)
1873 return true;
Serge Pavlov64bc7032013-05-07 16:56:03 +00001874
Douglas Gregor21ceb182011-11-03 19:00:24 +00001875 // If we are inside a class or category implementation, determine the
1876 // interface we're in.
Craig Topperc3ec1492014-05-26 06:22:03 +00001877 ObjCInterfaceDecl *ClassOfMethodDecl = nullptr;
Douglas Gregor21ceb182011-11-03 19:00:24 +00001878 if (ObjCMethodDecl *MD = getCurMethodDecl())
1879 ClassOfMethodDecl = MD->getClassInterface();
1880 else if (FunctionDecl *FD = getCurFunctionDecl()) {
1881 if (ObjCImplDecl *Impl
1882 = dyn_cast<ObjCImplDecl>(FD->getLexicalDeclContext())) {
1883 if (ObjCImplementationDecl *IMPD
1884 = dyn_cast<ObjCImplementationDecl>(Impl))
1885 ClassOfMethodDecl = IMPD->getClassInterface();
1886 else if (ObjCCategoryImplDecl* CatImplClass
1887 = dyn_cast<ObjCCategoryImplDecl>(Impl))
1888 ClassOfMethodDecl = CatImplClass->getClassInterface();
1889 }
1890 }
1891
1892 // If we're not in an interface, this ivar is inaccessible.
1893 if (!ClassOfMethodDecl)
1894 return false;
1895
1896 // If we're inside the same interface that owns the ivar, we're fine.
Douglas Gregor0b144e12011-12-15 00:29:59 +00001897 if (declaresSameEntity(ClassOfMethodDecl, Ivar->getContainingInterface()))
Douglas Gregor21ceb182011-11-03 19:00:24 +00001898 return true;
1899
1900 // If the ivar is private, it's inaccessible.
1901 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Private)
1902 return false;
1903
1904 return Ivar->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl);
1905 }
1906
Douglas Gregor03ba1882011-11-03 16:51:37 +00001907 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001908}