blob: e9772bc520494cbeda1bfc9c460b4bfb8e5e65d5 [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:
David Blaikie8533e962015-08-12 22:58:10 +0000185 SavedInstanceContext(SavedInstanceContext &&S)
186 : Target(S.Target), Has(S.Has) {
187 S.Target = nullptr;
188 }
John McCalla8ae2222010-04-06 21:38:20 +0000189 ~SavedInstanceContext() {
David Blaikie8533e962015-08-12 22:58:10 +0000190 if (Target)
191 Target->HasInstanceContext = Has;
John McCalla8ae2222010-04-06 21:38:20 +0000192 }
193
194 private:
John McCall8e36d532010-04-07 00:41:46 +0000195 friend struct AccessTarget;
John McCalla8ae2222010-04-06 21:38:20 +0000196 explicit SavedInstanceContext(AccessTarget &Target)
David Blaikie8533e962015-08-12 22:58:10 +0000197 : Target(&Target), Has(Target.HasInstanceContext) {}
198 AccessTarget *Target;
John McCalla8ae2222010-04-06 21:38:20 +0000199 bool Has;
200 };
201
202 SavedInstanceContext saveInstanceContext() {
203 return SavedInstanceContext(*this);
204 }
205
206 void suppressInstanceContext() {
207 HasInstanceContext = false;
208 }
209
210 const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
211 assert(HasInstanceContext);
212 if (CalculatedInstanceContext)
213 return InstanceContext;
214
215 CalculatedInstanceContext = true;
216 DeclContext *IC = S.computeDeclContext(getBaseObjectType());
Craig Topperc3ec1492014-05-26 06:22:03 +0000217 InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl()
218 : nullptr);
John McCalla8ae2222010-04-06 21:38:20 +0000219 return InstanceContext;
220 }
221
222 const CXXRecordDecl *getDeclaringClass() const {
223 return DeclaringClass;
224 }
225
John McCalld010ac92013-02-27 00:08:19 +0000226 /// The "effective" naming class is the canonical non-anonymous
227 /// class containing the actual naming class.
228 const CXXRecordDecl *getEffectiveNamingClass() const {
229 const CXXRecordDecl *namingClass = getNamingClass();
230 while (namingClass->isAnonymousStructOrUnion())
231 namingClass = cast<CXXRecordDecl>(namingClass->getParent());
232 return namingClass->getCanonicalDecl();
233 }
234
John McCalla8ae2222010-04-06 21:38:20 +0000235private:
236 void initialize() {
237 HasInstanceContext = (isMemberAccess() &&
238 !getBaseObjectType().isNull() &&
239 getTargetDecl()->isCXXInstanceMember());
240 CalculatedInstanceContext = false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000241 InstanceContext = nullptr;
John McCalla8ae2222010-04-06 21:38:20 +0000242
243 if (isMemberAccess())
244 DeclaringClass = FindDeclaringClass(getTargetDecl());
245 else
246 DeclaringClass = getBaseClass();
247 DeclaringClass = DeclaringClass->getCanonicalDecl();
248 }
249
250 bool HasInstanceContext : 1;
251 mutable bool CalculatedInstanceContext : 1;
252 mutable const CXXRecordDecl *InstanceContext;
253 const CXXRecordDecl *DeclaringClass;
254};
255
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000256}
John McCall553c0792010-01-23 00:46:32 +0000257
John McCall97205142010-05-04 05:11:27 +0000258/// Checks whether one class might instantiate to the other.
259static bool MightInstantiateTo(const CXXRecordDecl *From,
260 const CXXRecordDecl *To) {
261 // Declaration names are always preserved by instantiation.
262 if (From->getDeclName() != To->getDeclName())
263 return false;
264
265 const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext();
266 const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext();
267 if (FromDC == ToDC) return true;
268 if (FromDC->isFileContext() || ToDC->isFileContext()) return false;
269
270 // Be conservative.
271 return true;
272}
273
John McCalla8ae2222010-04-06 21:38:20 +0000274/// Checks whether one class is derived from another, inclusively.
275/// Properly indicates when it couldn't be determined due to
276/// dependence.
277///
278/// This should probably be donated to AST or at least Sema.
279static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
280 const CXXRecordDecl *Target) {
281 assert(Derived->getCanonicalDecl() == Derived);
282 assert(Target->getCanonicalDecl() == Target);
John McCall69f75862010-03-24 09:04:37 +0000283
John McCalla8ae2222010-04-06 21:38:20 +0000284 if (Derived == Target) return AR_accessible;
John McCall69f75862010-03-24 09:04:37 +0000285
John McCall97205142010-05-04 05:11:27 +0000286 bool CheckDependent = Derived->isDependentContext();
287 if (CheckDependent && MightInstantiateTo(Derived, Target))
288 return AR_dependent;
289
John McCalla8ae2222010-04-06 21:38:20 +0000290 AccessResult OnFailure = AR_inaccessible;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
John McCalla8ae2222010-04-06 21:38:20 +0000292
293 while (true) {
Douglas Gregor0201a4c2011-11-14 23:00:43 +0000294 if (Derived->isDependentContext() && !Derived->hasDefinition())
295 return AR_dependent;
296
Aaron Ballman574705e2014-03-13 15:41:46 +0000297 for (const auto &I : Derived->bases()) {
John McCalla8ae2222010-04-06 21:38:20 +0000298 const CXXRecordDecl *RD;
299
Aaron Ballman574705e2014-03-13 15:41:46 +0000300 QualType T = I.getType();
John McCalla8ae2222010-04-06 21:38:20 +0000301 if (const RecordType *RT = T->getAs<RecordType>()) {
302 RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall97205142010-05-04 05:11:27 +0000303 } else if (const InjectedClassNameType *IT
304 = T->getAs<InjectedClassNameType>()) {
305 RD = IT->getDecl();
John McCalla8ae2222010-04-06 21:38:20 +0000306 } else {
John McCalla8ae2222010-04-06 21:38:20 +0000307 assert(T->isDependentType() && "non-dependent base wasn't a record?");
308 OnFailure = AR_dependent;
309 continue;
310 }
311
312 RD = RD->getCanonicalDecl();
313 if (RD == Target) return AR_accessible;
John McCall97205142010-05-04 05:11:27 +0000314 if (CheckDependent && MightInstantiateTo(RD, Target))
315 OnFailure = AR_dependent;
316
John McCalla8ae2222010-04-06 21:38:20 +0000317 Queue.push_back(RD);
318 }
319
320 if (Queue.empty()) break;
321
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000322 Derived = Queue.pop_back_val();
John McCalla8ae2222010-04-06 21:38:20 +0000323 }
324
325 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000326}
327
John McCalla8ae2222010-04-06 21:38:20 +0000328
John McCallc62bb642010-03-24 05:22:00 +0000329static bool MightInstantiateTo(Sema &S, DeclContext *Context,
330 DeclContext *Friend) {
331 if (Friend == Context)
332 return true;
333
334 assert(!Friend->isDependentContext() &&
335 "can't handle friends with dependent contexts here");
336
337 if (!Context->isDependentContext())
338 return false;
339
340 if (Friend->isFileContext())
341 return false;
342
343 // TODO: this is very conservative
344 return true;
345}
346
347// Asks whether the type in 'context' can ever instantiate to the type
348// in 'friend'.
349static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
350 if (Friend == Context)
351 return true;
352
353 if (!Friend->isDependentType() && !Context->isDependentType())
354 return false;
355
356 // TODO: this is very conservative.
357 return true;
358}
359
360static bool MightInstantiateTo(Sema &S,
361 FunctionDecl *Context,
362 FunctionDecl *Friend) {
363 if (Context->getDeclName() != Friend->getDeclName())
364 return false;
365
366 if (!MightInstantiateTo(S,
367 Context->getDeclContext(),
368 Friend->getDeclContext()))
369 return false;
370
371 CanQual<FunctionProtoType> FriendTy
372 = S.Context.getCanonicalType(Friend->getType())
373 ->getAs<FunctionProtoType>();
374 CanQual<FunctionProtoType> ContextTy
375 = S.Context.getCanonicalType(Context->getType())
376 ->getAs<FunctionProtoType>();
377
378 // There isn't any way that I know of to add qualifiers
379 // during instantiation.
380 if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
381 return false;
382
Alp Toker9cacbab2014-01-20 20:26:09 +0000383 if (FriendTy->getNumParams() != ContextTy->getNumParams())
John McCallc62bb642010-03-24 05:22:00 +0000384 return false;
385
Alp Toker314cc812014-01-25 16:55:45 +0000386 if (!MightInstantiateTo(S, ContextTy->getReturnType(),
387 FriendTy->getReturnType()))
John McCallc62bb642010-03-24 05:22:00 +0000388 return false;
389
Alp Toker9cacbab2014-01-20 20:26:09 +0000390 for (unsigned I = 0, E = FriendTy->getNumParams(); I != E; ++I)
391 if (!MightInstantiateTo(S, ContextTy->getParamType(I),
392 FriendTy->getParamType(I)))
John McCallc62bb642010-03-24 05:22:00 +0000393 return false;
394
395 return true;
396}
397
398static bool MightInstantiateTo(Sema &S,
399 FunctionTemplateDecl *Context,
400 FunctionTemplateDecl *Friend) {
401 return MightInstantiateTo(S,
402 Context->getTemplatedDecl(),
403 Friend->getTemplatedDecl());
404}
405
John McCalla8ae2222010-04-06 21:38:20 +0000406static AccessResult MatchesFriend(Sema &S,
407 const EffectiveContext &EC,
408 const CXXRecordDecl *Friend) {
John McCall39e82882010-03-17 20:01:29 +0000409 if (EC.includesClass(Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000410 return AR_accessible;
John McCall39e82882010-03-17 20:01:29 +0000411
John McCallc62bb642010-03-24 05:22:00 +0000412 if (EC.isDependent()) {
413 CanQualType FriendTy
414 = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
415
416 for (EffectiveContext::record_iterator
417 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
418 CanQualType ContextTy
419 = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
420 if (MightInstantiateTo(S, ContextTy, FriendTy))
John McCalla8ae2222010-04-06 21:38:20 +0000421 return AR_dependent;
John McCallc62bb642010-03-24 05:22:00 +0000422 }
423 }
424
John McCalla8ae2222010-04-06 21:38:20 +0000425 return AR_inaccessible;
John McCall39e82882010-03-17 20:01:29 +0000426}
427
John McCalla8ae2222010-04-06 21:38:20 +0000428static AccessResult MatchesFriend(Sema &S,
429 const EffectiveContext &EC,
430 CanQualType Friend) {
John McCallc62bb642010-03-24 05:22:00 +0000431 if (const RecordType *RT = Friend->getAs<RecordType>())
432 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCall39e82882010-03-17 20:01:29 +0000433
John McCallc62bb642010-03-24 05:22:00 +0000434 // TODO: we can do better than this
435 if (Friend->isDependentType())
John McCalla8ae2222010-04-06 21:38:20 +0000436 return AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000437
John McCalla8ae2222010-04-06 21:38:20 +0000438 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000439}
440
441/// Determines whether the given friend class template matches
442/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000443static AccessResult MatchesFriend(Sema &S,
444 const EffectiveContext &EC,
445 ClassTemplateDecl *Friend) {
446 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000447
John McCall598b4402010-03-25 06:39:04 +0000448 // Check whether the friend is the template of a class in the
449 // context chain.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000450 for (SmallVectorImpl<CXXRecordDecl*>::const_iterator
John McCallc62bb642010-03-24 05:22:00 +0000451 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
452 CXXRecordDecl *Record = *I;
453
John McCall598b4402010-03-25 06:39:04 +0000454 // Figure out whether the current class has a template:
John McCallc62bb642010-03-24 05:22:00 +0000455 ClassTemplateDecl *CTD;
456
457 // A specialization of the template...
458 if (isa<ClassTemplateSpecializationDecl>(Record)) {
459 CTD = cast<ClassTemplateSpecializationDecl>(Record)
460 ->getSpecializedTemplate();
461
462 // ... or the template pattern itself.
463 } else {
464 CTD = Record->getDescribedClassTemplate();
465 if (!CTD) continue;
466 }
467
468 // It's a match.
469 if (Friend == CTD->getCanonicalDecl())
John McCalla8ae2222010-04-06 21:38:20 +0000470 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000471
John McCall598b4402010-03-25 06:39:04 +0000472 // If the context isn't dependent, it can't be a dependent match.
473 if (!EC.isDependent())
474 continue;
475
John McCallc62bb642010-03-24 05:22:00 +0000476 // If the template names don't match, it can't be a dependent
Richard Smith3f1b5d02011-05-05 21:57:07 +0000477 // match.
478 if (CTD->getDeclName() != Friend->getDeclName())
John McCallc62bb642010-03-24 05:22:00 +0000479 continue;
480
481 // If the class's context can't instantiate to the friend's
482 // context, it can't be a dependent match.
483 if (!MightInstantiateTo(S, CTD->getDeclContext(),
484 Friend->getDeclContext()))
485 continue;
486
487 // Otherwise, it's a dependent match.
John McCalla8ae2222010-04-06 21:38:20 +0000488 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000489 }
490
John McCallc62bb642010-03-24 05:22:00 +0000491 return OnFailure;
492}
493
494/// Determines whether the given friend function matches anything in
495/// the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000496static AccessResult MatchesFriend(Sema &S,
497 const EffectiveContext &EC,
498 FunctionDecl *Friend) {
499 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000500
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000501 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall3dc81f72010-03-27 06:55:49 +0000502 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
503 if (Friend == *I)
John McCalla8ae2222010-04-06 21:38:20 +0000504 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +0000505
John McCall3dc81f72010-03-27 06:55:49 +0000506 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000507 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000508 }
John McCallc62bb642010-03-24 05:22:00 +0000509
John McCall3dc81f72010-03-27 06:55:49 +0000510 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000511}
512
513/// Determines whether the given friend function template matches
514/// anything in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000515static AccessResult MatchesFriend(Sema &S,
516 const EffectiveContext &EC,
517 FunctionTemplateDecl *Friend) {
518 if (EC.Functions.empty()) return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000519
John McCalla8ae2222010-04-06 21:38:20 +0000520 AccessResult OnFailure = AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +0000521
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000522 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall3dc81f72010-03-27 06:55:49 +0000523 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCallc62bb642010-03-24 05:22:00 +0000524
John McCall3dc81f72010-03-27 06:55:49 +0000525 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
526 if (!FTD)
527 FTD = (*I)->getDescribedFunctionTemplate();
528 if (!FTD)
529 continue;
John McCallc62bb642010-03-24 05:22:00 +0000530
John McCall3dc81f72010-03-27 06:55:49 +0000531 FTD = FTD->getCanonicalDecl();
532
533 if (Friend == FTD)
John McCalla8ae2222010-04-06 21:38:20 +0000534 return AR_accessible;
John McCall3dc81f72010-03-27 06:55:49 +0000535
536 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCalla8ae2222010-04-06 21:38:20 +0000537 OnFailure = AR_dependent;
John McCall3dc81f72010-03-27 06:55:49 +0000538 }
539
540 return OnFailure;
John McCallc62bb642010-03-24 05:22:00 +0000541}
542
543/// Determines whether the given friend declaration matches anything
544/// in the effective context.
John McCalla8ae2222010-04-06 21:38:20 +0000545static AccessResult MatchesFriend(Sema &S,
546 const EffectiveContext &EC,
547 FriendDecl *FriendD) {
John McCall2c2eb122010-10-16 06:59:13 +0000548 // Whitelist accesses if there's an invalid or unsupported friend
549 // declaration.
550 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCallde3fd222010-10-12 23:13:28 +0000551 return AR_accessible;
552
John McCall15ad0962010-03-25 18:04:51 +0000553 if (TypeSourceInfo *T = FriendD->getFriendType())
554 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCallc62bb642010-03-24 05:22:00 +0000555
556 NamedDecl *Friend
557 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCall39e82882010-03-17 20:01:29 +0000558
559 // FIXME: declarations with dependent or templated scope.
560
John McCallc62bb642010-03-24 05:22:00 +0000561 if (isa<ClassTemplateDecl>(Friend))
562 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000563
John McCallc62bb642010-03-24 05:22:00 +0000564 if (isa<FunctionTemplateDecl>(Friend))
565 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000566
John McCallc62bb642010-03-24 05:22:00 +0000567 if (isa<CXXRecordDecl>(Friend))
568 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000569
John McCallc62bb642010-03-24 05:22:00 +0000570 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
571 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCall39e82882010-03-17 20:01:29 +0000572}
573
John McCalla8ae2222010-04-06 21:38:20 +0000574static AccessResult GetFriendKind(Sema &S,
575 const EffectiveContext &EC,
576 const CXXRecordDecl *Class) {
577 AccessResult OnFailure = AR_inaccessible;
John McCallfb803d72010-03-17 04:58:56 +0000578
John McCall16927f62010-03-12 01:19:31 +0000579 // Okay, check friends.
Aaron Ballman522a6dd2014-03-13 17:00:06 +0000580 for (auto *Friend : Class->friends()) {
John McCall39e82882010-03-17 20:01:29 +0000581 switch (MatchesFriend(S, EC, Friend)) {
John McCalla8ae2222010-04-06 21:38:20 +0000582 case AR_accessible:
583 return AR_accessible;
John McCall16927f62010-03-12 01:19:31 +0000584
John McCalla8ae2222010-04-06 21:38:20 +0000585 case AR_inaccessible:
586 continue;
587
588 case AR_dependent:
589 OnFailure = AR_dependent;
John McCall39e82882010-03-17 20:01:29 +0000590 break;
John McCall16927f62010-03-12 01:19:31 +0000591 }
John McCall16927f62010-03-12 01:19:31 +0000592 }
593
594 // That's it, give up.
John McCallfb803d72010-03-17 04:58:56 +0000595 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000596}
597
John McCall96329672010-08-28 07:56:00 +0000598namespace {
599
600/// A helper class for checking for a friend which will grant access
601/// to a protected instance member.
602struct ProtectedFriendContext {
603 Sema &S;
604 const EffectiveContext &EC;
605 const CXXRecordDecl *NamingClass;
606 bool CheckDependent;
607 bool EverDependent;
608
609 /// The path down to the current base class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000610 SmallVector<const CXXRecordDecl*, 20> CurPath;
John McCall96329672010-08-28 07:56:00 +0000611
612 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
613 const CXXRecordDecl *InstanceContext,
614 const CXXRecordDecl *NamingClass)
615 : S(S), EC(EC), NamingClass(NamingClass),
616 CheckDependent(InstanceContext->isDependentContext() ||
617 NamingClass->isDependentContext()),
618 EverDependent(false) {}
619
John McCall1177ff12010-08-28 08:47:21 +0000620 /// Check classes in the current path for friendship, starting at
621 /// the given index.
622 bool checkFriendshipAlongPath(unsigned I) {
623 assert(I < CurPath.size());
624 for (unsigned E = CurPath.size(); I != E; ++I) {
625 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall96329672010-08-28 07:56:00 +0000626 case AR_accessible: return true;
627 case AR_inaccessible: continue;
628 case AR_dependent: EverDependent = true; continue;
629 }
630 }
631 return false;
632 }
633
634 /// Perform a search starting at the given class.
John McCall1177ff12010-08-28 08:47:21 +0000635 ///
636 /// PrivateDepth is the index of the last (least derived) class
637 /// along the current path such that a notional public member of
638 /// the final class in the path would have access in that class.
639 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall96329672010-08-28 07:56:00 +0000640 // If we ever reach the naming class, check the current path for
641 // friendship. We can also stop recursing because we obviously
642 // won't find the naming class there again.
John McCall1177ff12010-08-28 08:47:21 +0000643 if (Cur == NamingClass)
644 return checkFriendshipAlongPath(PrivateDepth);
John McCall96329672010-08-28 07:56:00 +0000645
646 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
647 EverDependent = true;
648
649 // Recurse into the base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +0000650 for (const auto &I : Cur->bases()) {
John McCall1177ff12010-08-28 08:47:21 +0000651 // If this is private inheritance, then a public member of the
652 // base will not have any access in classes derived from Cur.
653 unsigned BasePrivateDepth = PrivateDepth;
Aaron Ballman574705e2014-03-13 15:41:46 +0000654 if (I.getAccessSpecifier() == AS_private)
John McCall1177ff12010-08-28 08:47:21 +0000655 BasePrivateDepth = CurPath.size() - 1;
John McCall96329672010-08-28 07:56:00 +0000656
657 const CXXRecordDecl *RD;
658
Aaron Ballman574705e2014-03-13 15:41:46 +0000659 QualType T = I.getType();
John McCall96329672010-08-28 07:56:00 +0000660 if (const RecordType *RT = T->getAs<RecordType>()) {
661 RD = cast<CXXRecordDecl>(RT->getDecl());
662 } else if (const InjectedClassNameType *IT
663 = T->getAs<InjectedClassNameType>()) {
664 RD = IT->getDecl();
665 } else {
666 assert(T->isDependentType() && "non-dependent base wasn't a record?");
667 EverDependent = true;
668 continue;
669 }
670
671 // Recurse. We don't need to clean up if this returns true.
John McCall1177ff12010-08-28 08:47:21 +0000672 CurPath.push_back(RD);
673 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
674 return true;
675 CurPath.pop_back();
John McCall96329672010-08-28 07:56:00 +0000676 }
677
John McCall96329672010-08-28 07:56:00 +0000678 return false;
679 }
John McCall1177ff12010-08-28 08:47:21 +0000680
681 bool findFriendship(const CXXRecordDecl *Cur) {
682 assert(CurPath.empty());
683 CurPath.push_back(Cur);
684 return findFriendship(Cur, 0);
685 }
John McCall96329672010-08-28 07:56:00 +0000686};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000687}
John McCall96329672010-08-28 07:56:00 +0000688
689/// Search for a class P that EC is a friend of, under the constraint
John McCall5dadb652012-04-07 03:04:20 +0000690/// InstanceContext <= P
691/// if InstanceContext exists, or else
692/// NamingClass <= P
John McCall96329672010-08-28 07:56:00 +0000693/// and with the additional restriction that a protected member of
John McCall5dadb652012-04-07 03:04:20 +0000694/// NamingClass would have some natural access in P, which implicitly
695/// imposes the constraint that P <= NamingClass.
John McCall96329672010-08-28 07:56:00 +0000696///
John McCall5dadb652012-04-07 03:04:20 +0000697/// This isn't quite the condition laid out in the standard.
698/// Instead of saying that a notional protected member of NamingClass
699/// would have to have some natural access in P, it says the actual
700/// target has to have some natural access in P, which opens up the
701/// possibility that the target (which is not necessarily a member
702/// of NamingClass) might be more accessible along some path not
703/// passing through it. That's really a bad idea, though, because it
John McCall96329672010-08-28 07:56:00 +0000704/// introduces two problems:
John McCall5dadb652012-04-07 03:04:20 +0000705/// - Most importantly, it breaks encapsulation because you can
706/// access a forbidden base class's members by directly subclassing
707/// it elsewhere.
708/// - It also makes access substantially harder to compute because it
John McCall96329672010-08-28 07:56:00 +0000709/// breaks the hill-climbing algorithm: knowing that the target is
710/// accessible in some base class would no longer let you change
711/// the question solely to whether the base class is accessible,
712/// because the original target might have been more accessible
713/// because of crazy subclassing.
714/// So we don't implement that.
715static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
716 const CXXRecordDecl *InstanceContext,
717 const CXXRecordDecl *NamingClass) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000718 assert(InstanceContext == nullptr ||
John McCall5dadb652012-04-07 03:04:20 +0000719 InstanceContext->getCanonicalDecl() == InstanceContext);
John McCall96329672010-08-28 07:56:00 +0000720 assert(NamingClass->getCanonicalDecl() == NamingClass);
721
John McCall5dadb652012-04-07 03:04:20 +0000722 // If we don't have an instance context, our constraints give us
723 // that NamingClass <= P <= NamingClass, i.e. P == NamingClass.
724 // This is just the usual friendship check.
725 if (!InstanceContext) return GetFriendKind(S, EC, NamingClass);
726
John McCall96329672010-08-28 07:56:00 +0000727 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
728 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
729 if (PRC.EverDependent) return AR_dependent;
730 return AR_inaccessible;
731}
732
John McCalla8ae2222010-04-06 21:38:20 +0000733static AccessResult HasAccess(Sema &S,
734 const EffectiveContext &EC,
735 const CXXRecordDecl *NamingClass,
736 AccessSpecifier Access,
737 const AccessTarget &Target) {
John McCalld79b4d82010-04-02 00:03:43 +0000738 assert(NamingClass->getCanonicalDecl() == NamingClass &&
739 "declaration should be canonicalized before being passed here");
740
John McCalla8ae2222010-04-06 21:38:20 +0000741 if (Access == AS_public) return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000742 assert(Access == AS_private || Access == AS_protected);
743
John McCalla8ae2222010-04-06 21:38:20 +0000744 AccessResult OnFailure = AR_inaccessible;
745
John McCalld79b4d82010-04-02 00:03:43 +0000746 for (EffectiveContext::record_iterator
747 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
748 // All the declarations in EC have been canonicalized, so pointer
749 // equality from this point on will work fine.
750 const CXXRecordDecl *ECRecord = *I;
751
752 // [B2] and [M2]
John McCalla8ae2222010-04-06 21:38:20 +0000753 if (Access == AS_private) {
754 if (ECRecord == NamingClass)
755 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +0000756
John McCall97205142010-05-04 05:11:27 +0000757 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
758 OnFailure = AR_dependent;
759
John McCalld79b4d82010-04-02 00:03:43 +0000760 // [B3] and [M3]
John McCalla8ae2222010-04-06 21:38:20 +0000761 } else {
762 assert(Access == AS_protected);
763 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
764 case AR_accessible: break;
765 case AR_inaccessible: continue;
766 case AR_dependent: OnFailure = AR_dependent; continue;
767 }
768
John McCalla8ae2222010-04-06 21:38:20 +0000769 // C++ [class.protected]p1:
770 // An additional access check beyond those described earlier in
771 // [class.access] is applied when a non-static data member or
772 // non-static member function is a protected member of its naming
773 // class. As described earlier, access to a protected member is
774 // granted because the reference occurs in a friend or member of
775 // some class C. If the access is to form a pointer to member,
776 // the nested-name-specifier shall name C or a class derived from
777 // C. All other accesses involve a (possibly implicit) object
778 // expression. In this case, the class of the object expression
779 // shall be C or a class derived from C.
780 //
John McCall5dadb652012-04-07 03:04:20 +0000781 // We interpret this as a restriction on [M3].
782
783 // In this part of the code, 'C' is just our context class ECRecord.
784
785 // These rules are different if we don't have an instance context.
786 if (!Target.hasInstanceContext()) {
787 // If it's not an instance member, these restrictions don't apply.
788 if (!Target.isInstanceMember()) return AR_accessible;
789
790 // If it's an instance member, use the pointer-to-member rule
791 // that the naming class has to be derived from the effective
792 // context.
793
Francois Picheta39371c2012-04-17 12:35:05 +0000794 // Emulate a MSVC bug where the creation of pointer-to-member
795 // to protected member of base class is allowed but only from
Francois Pichet52d38982012-04-19 07:48:57 +0000796 // static member functions.
Alp Tokerbfa39342014-01-14 12:51:41 +0000797 if (S.getLangOpts().MSVCCompat && !EC.Functions.empty())
Francois Pichet5b061f02012-04-18 03:24:38 +0000798 if (CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(EC.Functions.front()))
799 if (MD->isStatic()) return AR_accessible;
Francois Picheta39371c2012-04-17 12:35:05 +0000800
John McCall5dadb652012-04-07 03:04:20 +0000801 // Despite the standard's confident wording, there is a case
802 // where you can have an instance member that's neither in a
803 // pointer-to-member expression nor in a member access: when
804 // it names a field in an unevaluated context that can't be an
805 // implicit member. Pending clarification, we just apply the
806 // same naming-class restriction here.
807 // FIXME: we're probably not correctly adding the
808 // protected-member restriction when we retroactively convert
809 // an expression to being evaluated.
810
811 // We know that ECRecord derives from NamingClass. The
812 // restriction says to check whether NamingClass derives from
813 // ECRecord, but that's not really necessary: two distinct
814 // classes can't be recursively derived from each other. So
815 // along this path, we just need to check whether the classes
816 // are equal.
817 if (NamingClass == ECRecord) return AR_accessible;
818
819 // Otherwise, this context class tells us nothing; on to the next.
820 continue;
821 }
822
823 assert(Target.isInstanceMember());
824
825 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
826 if (!InstanceContext) {
827 OnFailure = AR_dependent;
828 continue;
829 }
830
John McCalla8ae2222010-04-06 21:38:20 +0000831 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
832 case AR_accessible: return AR_accessible;
833 case AR_inaccessible: continue;
834 case AR_dependent: OnFailure = AR_dependent; continue;
835 }
836 }
John McCalld79b4d82010-04-02 00:03:43 +0000837 }
838
John McCall96329672010-08-28 07:56:00 +0000839 // [M3] and [B3] say that, if the target is protected in N, we grant
840 // access if the access occurs in a friend or member of some class P
841 // that's a subclass of N and where the target has some natural
842 // access in P. The 'member' aspect is easy to handle because P
843 // would necessarily be one of the effective-context records, and we
844 // address that above. The 'friend' aspect is completely ridiculous
845 // to implement because there are no restrictions at all on P
846 // *unless* the [class.protected] restriction applies. If it does,
847 // however, we should ignore whether the naming class is a friend,
848 // and instead rely on whether any potential P is a friend.
John McCall5dadb652012-04-07 03:04:20 +0000849 if (Access == AS_protected && Target.isInstanceMember()) {
850 // Compute the instance context if possible.
Craig Topperc3ec1492014-05-26 06:22:03 +0000851 const CXXRecordDecl *InstanceContext = nullptr;
John McCall5dadb652012-04-07 03:04:20 +0000852 if (Target.hasInstanceContext()) {
853 InstanceContext = Target.resolveInstanceContext(S);
854 if (!InstanceContext) return AR_dependent;
855 }
856
John McCall96329672010-08-28 07:56:00 +0000857 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
858 case AR_accessible: return AR_accessible;
John McCalla8ae2222010-04-06 21:38:20 +0000859 case AR_inaccessible: return OnFailure;
860 case AR_dependent: return AR_dependent;
861 }
John McCallc6af8f42010-08-28 08:10:32 +0000862 llvm_unreachable("impossible friendship kind");
John McCalla8ae2222010-04-06 21:38:20 +0000863 }
864
865 switch (GetFriendKind(S, EC, NamingClass)) {
866 case AR_accessible: return AR_accessible;
867 case AR_inaccessible: return OnFailure;
868 case AR_dependent: return AR_dependent;
869 }
870
871 // Silence bogus warnings
872 llvm_unreachable("impossible friendship kind");
John McCalld79b4d82010-04-02 00:03:43 +0000873}
874
John McCall5b0829a2010-02-10 09:31:12 +0000875/// Finds the best path from the naming class to the declaring class,
876/// taking friend declarations into account.
877///
John McCalld79b4d82010-04-02 00:03:43 +0000878/// C++0x [class.access.base]p5:
879/// A member m is accessible at the point R when named in class N if
880/// [M1] m as a member of N is public, or
881/// [M2] m as a member of N is private, and R occurs in a member or
882/// friend of class N, or
883/// [M3] m as a member of N is protected, and R occurs in a member or
884/// friend of class N, or in a member or friend of a class P
885/// derived from N, where m as a member of P is public, private,
886/// or protected, or
887/// [M4] there exists a base class B of N that is accessible at R, and
888/// m is accessible at R when named in class B.
889///
890/// C++0x [class.access.base]p4:
891/// A base class B of N is accessible at R, if
892/// [B1] an invented public member of B would be a public member of N, or
893/// [B2] R occurs in a member or friend of class N, and an invented public
894/// member of B would be a private or protected member of N, or
895/// [B3] R occurs in a member or friend of a class P derived from N, and an
896/// invented public member of B would be a private or protected member
897/// of P, or
898/// [B4] there exists a class S such that B is a base class of S accessible
899/// at R and S is a base class of N accessible at R.
900///
901/// Along a single inheritance path we can restate both of these
902/// iteratively:
903///
904/// First, we note that M1-4 are equivalent to B1-4 if the member is
905/// treated as a notional base of its declaring class with inheritance
906/// access equivalent to the member's access. Therefore we need only
907/// ask whether a class B is accessible from a class N in context R.
908///
909/// Let B_1 .. B_n be the inheritance path in question (i.e. where
910/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
911/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
912/// closest accessible base in the path:
913/// Access(a, b) = (* access on the base specifier from a to b *)
914/// Merge(a, forbidden) = forbidden
915/// Merge(a, private) = forbidden
916/// Merge(a, b) = min(a,b)
917/// Accessible(c, forbidden) = false
918/// Accessible(c, private) = (R is c) || IsFriend(c, R)
919/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
920/// Accessible(c, public) = true
921/// ACAB(n) = public
922/// ACAB(i) =
923/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
924/// if Accessible(B_i, AccessToBase) then public else AccessToBase
925///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000926/// B is an accessible base of N at R iff ACAB(1) = public.
John McCalld79b4d82010-04-02 00:03:43 +0000927///
John McCalla8ae2222010-04-06 21:38:20 +0000928/// \param FinalAccess the access of the "final step", or AS_public if
John McCalla332b952010-03-18 23:49:19 +0000929/// there is no final step.
John McCall5b0829a2010-02-10 09:31:12 +0000930/// \return null if friendship is dependent
931static CXXBasePath *FindBestPath(Sema &S,
932 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +0000933 AccessTarget &Target,
John McCalla332b952010-03-18 23:49:19 +0000934 AccessSpecifier FinalAccess,
John McCall5b0829a2010-02-10 09:31:12 +0000935 CXXBasePaths &Paths) {
936 // Derive the paths to the desired base.
John McCalla8ae2222010-04-06 21:38:20 +0000937 const CXXRecordDecl *Derived = Target.getNamingClass();
938 const CXXRecordDecl *Base = Target.getDeclaringClass();
939
940 // FIXME: fail correctly when there are dependent paths.
941 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
942 Paths);
John McCall5b0829a2010-02-10 09:31:12 +0000943 assert(isDerived && "derived class not actually derived from base");
944 (void) isDerived;
945
Craig Topperc3ec1492014-05-26 06:22:03 +0000946 CXXBasePath *BestPath = nullptr;
John McCall5b0829a2010-02-10 09:31:12 +0000947
John McCalla332b952010-03-18 23:49:19 +0000948 assert(FinalAccess != AS_none && "forbidden access after declaring class");
949
John McCallc62bb642010-03-24 05:22:00 +0000950 bool AnyDependent = false;
951
John McCall5b0829a2010-02-10 09:31:12 +0000952 // Derive the friend-modified access along each path.
953 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
954 PI != PE; ++PI) {
John McCalla8ae2222010-04-06 21:38:20 +0000955 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +0000956
957 // Walk through the path backwards.
John McCalla332b952010-03-18 23:49:19 +0000958 AccessSpecifier PathAccess = FinalAccess;
John McCall5b0829a2010-02-10 09:31:12 +0000959 CXXBasePath::iterator I = PI->end(), E = PI->begin();
960 while (I != E) {
961 --I;
962
John McCalla332b952010-03-18 23:49:19 +0000963 assert(PathAccess != AS_none);
964
965 // If the declaration is a private member of a base class, there
966 // is no level of friendship in derived classes that can make it
967 // accessible.
968 if (PathAccess == AS_private) {
969 PathAccess = AS_none;
970 break;
971 }
972
John McCalla8ae2222010-04-06 21:38:20 +0000973 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
974
John McCall5b0829a2010-02-10 09:31:12 +0000975 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalld79b4d82010-04-02 00:03:43 +0000976 PathAccess = std::max(PathAccess, BaseAccess);
John McCalla8ae2222010-04-06 21:38:20 +0000977
978 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
979 case AR_inaccessible: break;
980 case AR_accessible:
981 PathAccess = AS_public;
982
983 // Future tests are not against members and so do not have
984 // instance context.
985 Target.suppressInstanceContext();
986 break;
987 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +0000988 AnyDependent = true;
989 goto Next;
John McCall5b0829a2010-02-10 09:31:12 +0000990 }
John McCall5b0829a2010-02-10 09:31:12 +0000991 }
992
993 // Note that we modify the path's Access field to the
994 // friend-modified access.
Craig Topperc3ec1492014-05-26 06:22:03 +0000995 if (BestPath == nullptr || PathAccess < BestPath->Access) {
John McCall5b0829a2010-02-10 09:31:12 +0000996 BestPath = &*PI;
997 BestPath->Access = PathAccess;
John McCallc62bb642010-03-24 05:22:00 +0000998
999 // Short-circuit if we found a public path.
1000 if (BestPath->Access == AS_public)
1001 return BestPath;
John McCall5b0829a2010-02-10 09:31:12 +00001002 }
John McCallc62bb642010-03-24 05:22:00 +00001003
1004 Next: ;
John McCall5b0829a2010-02-10 09:31:12 +00001005 }
1006
John McCallc62bb642010-03-24 05:22:00 +00001007 assert((!BestPath || BestPath->Access != AS_public) &&
1008 "fell out of loop with public path");
1009
1010 // We didn't find a public path, but at least one path was subject
1011 // to dependent friendship, so delay the check.
1012 if (AnyDependent)
Craig Topperc3ec1492014-05-26 06:22:03 +00001013 return nullptr;
John McCallc62bb642010-03-24 05:22:00 +00001014
John McCall5b0829a2010-02-10 09:31:12 +00001015 return BestPath;
1016}
1017
John McCall417e7442010-09-03 04:56:05 +00001018/// Given that an entity has protected natural access, check whether
1019/// access might be denied because of the protected member access
1020/// restriction.
1021///
1022/// \return true if a note was emitted
1023static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
1024 AccessTarget &Target) {
1025 // Only applies to instance accesses.
John McCall5dadb652012-04-07 03:04:20 +00001026 if (!Target.isInstanceMember())
John McCall417e7442010-09-03 04:56:05 +00001027 return false;
John McCall417e7442010-09-03 04:56:05 +00001028
John McCall5dadb652012-04-07 03:04:20 +00001029 assert(Target.isMemberAccess());
1030
John McCalld010ac92013-02-27 00:08:19 +00001031 const CXXRecordDecl *NamingClass = Target.getEffectiveNamingClass();
John McCall417e7442010-09-03 04:56:05 +00001032
1033 for (EffectiveContext::record_iterator
1034 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
1035 const CXXRecordDecl *ECRecord = *I;
John McCall5dadb652012-04-07 03:04:20 +00001036 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
John McCall417e7442010-09-03 04:56:05 +00001037 case AR_accessible: break;
1038 case AR_inaccessible: continue;
1039 case AR_dependent: continue;
1040 }
1041
1042 // The effective context is a subclass of the declaring class.
John McCall5dadb652012-04-07 03:04:20 +00001043 // Check whether the [class.protected] restriction is limiting
1044 // access.
John McCall417e7442010-09-03 04:56:05 +00001045
1046 // To get this exactly right, this might need to be checked more
1047 // holistically; it's not necessarily the case that gaining
1048 // access here would grant us access overall.
1049
John McCall5dadb652012-04-07 03:04:20 +00001050 NamedDecl *D = Target.getTargetDecl();
1051
1052 // If we don't have an instance context, [class.protected] says the
1053 // naming class has to equal the context class.
1054 if (!Target.hasInstanceContext()) {
1055 // If it does, the restriction doesn't apply.
1056 if (NamingClass == ECRecord) continue;
1057
1058 // TODO: it would be great to have a fixit here, since this is
1059 // such an obvious error.
1060 S.Diag(D->getLocation(), diag::note_access_protected_restricted_noobject)
1061 << S.Context.getTypeDeclType(ECRecord);
1062 return true;
1063 }
1064
John McCall417e7442010-09-03 04:56:05 +00001065 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
1066 assert(InstanceContext && "diagnosing dependent access");
1067
1068 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
1069 case AR_accessible: continue;
1070 case AR_dependent: continue;
1071 case AR_inaccessible:
John McCall5dadb652012-04-07 03:04:20 +00001072 break;
1073 }
1074
1075 // Okay, the restriction seems to be what's limiting us.
1076
1077 // Use a special diagnostic for constructors and destructors.
1078 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D) ||
1079 (isa<FunctionTemplateDecl>(D) &&
1080 isa<CXXConstructorDecl>(
1081 cast<FunctionTemplateDecl>(D)->getTemplatedDecl()))) {
Alp Tokera2794f92014-01-22 07:29:52 +00001082 return S.Diag(D->getLocation(),
1083 diag::note_access_protected_restricted_ctordtor)
1084 << isa<CXXDestructorDecl>(D->getAsFunction());
John McCall417e7442010-09-03 04:56:05 +00001085 }
John McCall5dadb652012-04-07 03:04:20 +00001086
1087 // Otherwise, use the generic diagnostic.
Alp Tokera2794f92014-01-22 07:29:52 +00001088 return S.Diag(D->getLocation(),
1089 diag::note_access_protected_restricted_object)
1090 << S.Context.getTypeDeclType(ECRecord);
John McCall417e7442010-09-03 04:56:05 +00001091 }
1092
1093 return false;
1094}
1095
John McCalld010ac92013-02-27 00:08:19 +00001096/// We are unable to access a given declaration due to its direct
1097/// access control; diagnose that.
1098static void diagnoseBadDirectAccess(Sema &S,
1099 const EffectiveContext &EC,
1100 AccessTarget &entity) {
1101 assert(entity.isMemberAccess());
1102 NamedDecl *D = entity.getTargetDecl();
1103
1104 if (D->getAccess() == AS_protected &&
1105 TryDiagnoseProtectedAccess(S, EC, entity))
1106 return;
1107
1108 // Find an original declaration.
1109 while (D->isOutOfLine()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001110 NamedDecl *PrevDecl = nullptr;
John McCalld010ac92013-02-27 00:08:19 +00001111 if (VarDecl *VD = dyn_cast<VarDecl>(D))
1112 PrevDecl = VD->getPreviousDecl();
1113 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1114 PrevDecl = FD->getPreviousDecl();
1115 else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
1116 PrevDecl = TND->getPreviousDecl();
1117 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1118 if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1119 break;
1120 PrevDecl = TD->getPreviousDecl();
1121 }
1122 if (!PrevDecl) break;
1123 D = PrevDecl;
1124 }
1125
1126 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
1127 Decl *ImmediateChild;
1128 if (D->getDeclContext() == DeclaringClass)
1129 ImmediateChild = D;
1130 else {
1131 DeclContext *DC = D->getDeclContext();
1132 while (DC->getParent() != DeclaringClass)
1133 DC = DC->getParent();
1134 ImmediateChild = cast<Decl>(DC);
1135 }
1136
1137 // Check whether there's an AccessSpecDecl preceding this in the
1138 // chain of the DeclContext.
1139 bool isImplicit = true;
Aaron Ballman629afae2014-03-07 19:56:05 +00001140 for (const auto *I : DeclaringClass->decls()) {
1141 if (I == ImmediateChild) break;
1142 if (isa<AccessSpecDecl>(I)) {
John McCalld010ac92013-02-27 00:08:19 +00001143 isImplicit = false;
1144 break;
1145 }
1146 }
1147
1148 S.Diag(D->getLocation(), diag::note_access_natural)
1149 << (unsigned) (D->getAccess() == AS_protected)
1150 << isImplicit;
1151}
1152
John McCall5b0829a2010-02-10 09:31:12 +00001153/// Diagnose the path which caused the given declaration or base class
1154/// to become inaccessible.
1155static void DiagnoseAccessPath(Sema &S,
1156 const EffectiveContext &EC,
John McCalld010ac92013-02-27 00:08:19 +00001157 AccessTarget &entity) {
1158 // Save the instance context to preserve invariants.
1159 AccessTarget::SavedInstanceContext _ = entity.saveInstanceContext();
John McCalld79b4d82010-04-02 00:03:43 +00001160
John McCalld010ac92013-02-27 00:08:19 +00001161 // This basically repeats the main algorithm but keeps some more
1162 // information.
John McCalld79b4d82010-04-02 00:03:43 +00001163
John McCalld010ac92013-02-27 00:08:19 +00001164 // The natural access so far.
1165 AccessSpecifier accessSoFar = AS_public;
John McCall417e7442010-09-03 04:56:05 +00001166
John McCalld010ac92013-02-27 00:08:19 +00001167 // Check whether we have special rights to the declaring class.
1168 if (entity.isMemberAccess()) {
1169 NamedDecl *D = entity.getTargetDecl();
1170 accessSoFar = D->getAccess();
1171 const CXXRecordDecl *declaringClass = entity.getDeclaringClass();
John McCallf551aca2010-10-20 08:15:06 +00001172
John McCalld010ac92013-02-27 00:08:19 +00001173 switch (HasAccess(S, EC, declaringClass, accessSoFar, entity)) {
1174 // If the declaration is accessible when named in its declaring
1175 // class, then we must be constrained by the path.
1176 case AR_accessible:
1177 accessSoFar = AS_public;
1178 entity.suppressInstanceContext();
1179 break;
John McCallf551aca2010-10-20 08:15:06 +00001180
John McCalld010ac92013-02-27 00:08:19 +00001181 case AR_inaccessible:
1182 if (accessSoFar == AS_private ||
1183 declaringClass == entity.getEffectiveNamingClass())
1184 return diagnoseBadDirectAccess(S, EC, entity);
1185 break;
John McCall5b0829a2010-02-10 09:31:12 +00001186
John McCalla8ae2222010-04-06 21:38:20 +00001187 case AR_dependent:
John McCalld010ac92013-02-27 00:08:19 +00001188 llvm_unreachable("cannot diagnose dependent access");
John McCall5b0829a2010-02-10 09:31:12 +00001189 }
1190 }
1191
John McCalld010ac92013-02-27 00:08:19 +00001192 CXXBasePaths paths;
1193 CXXBasePath &path = *FindBestPath(S, EC, entity, accessSoFar, paths);
1194 assert(path.Access != AS_public);
John McCall5b0829a2010-02-10 09:31:12 +00001195
John McCalld010ac92013-02-27 00:08:19 +00001196 CXXBasePath::iterator i = path.end(), e = path.begin();
1197 CXXBasePath::iterator constrainingBase = i;
1198 while (i != e) {
1199 --i;
John McCall5b0829a2010-02-10 09:31:12 +00001200
John McCalld010ac92013-02-27 00:08:19 +00001201 assert(accessSoFar != AS_none && accessSoFar != AS_private);
John McCall5b0829a2010-02-10 09:31:12 +00001202
John McCalld010ac92013-02-27 00:08:19 +00001203 // Is the entity accessible when named in the deriving class, as
1204 // modified by the base specifier?
1205 const CXXRecordDecl *derivingClass = i->Class->getCanonicalDecl();
1206 const CXXBaseSpecifier *base = i->Base;
John McCall5b0829a2010-02-10 09:31:12 +00001207
John McCalld010ac92013-02-27 00:08:19 +00001208 // If the access to this base is worse than the access we have to
1209 // the declaration, remember it.
1210 AccessSpecifier baseAccess = base->getAccessSpecifier();
1211 if (baseAccess > accessSoFar) {
1212 constrainingBase = i;
1213 accessSoFar = baseAccess;
1214 }
1215
1216 switch (HasAccess(S, EC, derivingClass, accessSoFar, entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001217 case AR_inaccessible: break;
John McCalld010ac92013-02-27 00:08:19 +00001218 case AR_accessible:
1219 accessSoFar = AS_public;
1220 entity.suppressInstanceContext();
Craig Topperc3ec1492014-05-26 06:22:03 +00001221 constrainingBase = nullptr;
John McCalld010ac92013-02-27 00:08:19 +00001222 break;
John McCalla8ae2222010-04-06 21:38:20 +00001223 case AR_dependent:
John McCalld010ac92013-02-27 00:08:19 +00001224 llvm_unreachable("cannot diagnose dependent access");
John McCall5b0829a2010-02-10 09:31:12 +00001225 }
1226
John McCalld010ac92013-02-27 00:08:19 +00001227 // If this was private inheritance, but we don't have access to
1228 // the deriving class, we're done.
1229 if (accessSoFar == AS_private) {
1230 assert(baseAccess == AS_private);
1231 assert(constrainingBase == i);
1232 break;
John McCall5b0829a2010-02-10 09:31:12 +00001233 }
1234 }
1235
John McCalld010ac92013-02-27 00:08:19 +00001236 // If we don't have a constraining base, the access failure must be
1237 // due to the original declaration.
1238 if (constrainingBase == path.end())
1239 return diagnoseBadDirectAccess(S, EC, entity);
1240
1241 // We're constrained by inheritance, but we want to say
1242 // "declared private here" if we're diagnosing a hierarchy
1243 // conversion and this is the final step.
1244 unsigned diagnostic;
1245 if (entity.isMemberAccess() ||
1246 constrainingBase + 1 != path.end()) {
1247 diagnostic = diag::note_access_constrained_by_path;
1248 } else {
1249 diagnostic = diag::note_access_natural;
1250 }
1251
1252 const CXXBaseSpecifier *base = constrainingBase->Base;
1253
1254 S.Diag(base->getSourceRange().getBegin(), diagnostic)
1255 << base->getSourceRange()
1256 << (base->getAccessSpecifier() == AS_protected)
1257 << (base->getAccessSpecifierAsWritten() == AS_none);
1258
1259 if (entity.isMemberAccess())
Alp Toker2afa8782014-05-28 12:20:14 +00001260 S.Diag(entity.getTargetDecl()->getLocation(),
1261 diag::note_member_declared_at);
John McCall5b0829a2010-02-10 09:31:12 +00001262}
1263
John McCall1064d7e2010-03-16 05:22:47 +00001264static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall5b0829a2010-02-10 09:31:12 +00001265 const EffectiveContext &EC,
John McCalla8ae2222010-04-06 21:38:20 +00001266 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001267 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCalla8ae2222010-04-06 21:38:20 +00001268 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
Craig Topperc3ec1492014-05-26 06:22:03 +00001269 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : nullptr);
John McCalld79b4d82010-04-02 00:03:43 +00001270
1271 S.Diag(Loc, Entity.getDiag())
1272 << (Entity.getAccess() == AS_protected)
1273 << (D ? D->getDeclName() : DeclarationName())
1274 << S.Context.getTypeDeclType(NamingClass)
1275 << S.Context.getTypeDeclType(DeclaringClass);
1276 DiagnoseAccessPath(S, EC, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001277}
1278
Francois Pichetefb1af92011-05-23 03:43:44 +00001279/// MSVC has a bug where if during an using declaration name lookup,
1280/// the declaration found is unaccessible (private) and that declaration
1281/// was bring into scope via another using declaration whose target
1282/// declaration is accessible (public) then no error is generated.
1283/// Example:
1284/// class A {
1285/// public:
1286/// int f();
1287/// };
1288/// class B : public A {
1289/// private:
1290/// using A::f;
1291/// };
1292/// class C : public B {
1293/// private:
1294/// using B::f;
1295/// };
1296///
1297/// Here, B::f is private so this should fail in Standard C++, but
1298/// because B::f refers to A::f which is public MSVC accepts it.
1299static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S,
1300 SourceLocation AccessLoc,
1301 AccessTarget &Entity) {
1302 if (UsingShadowDecl *Shadow =
1303 dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) {
1304 const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl();
1305 if (Entity.getTargetDecl()->getAccess() == AS_private &&
1306 (OrigDecl->getAccess() == AS_public ||
1307 OrigDecl->getAccess() == AS_protected)) {
Richard Smithe4345902011-12-29 21:57:33 +00001308 S.Diag(AccessLoc, diag::ext_ms_using_declaration_inaccessible)
Francois Pichetefb1af92011-05-23 03:43:44 +00001309 << Shadow->getUsingDecl()->getQualifiedNameAsString()
1310 << OrigDecl->getQualifiedNameAsString();
1311 return true;
1312 }
1313 }
1314 return false;
1315}
1316
John McCalld79b4d82010-04-02 00:03:43 +00001317/// Determines whether the accessed entity is accessible. Public members
1318/// have been weeded out by this point.
John McCalla8ae2222010-04-06 21:38:20 +00001319static AccessResult IsAccessible(Sema &S,
1320 const EffectiveContext &EC,
1321 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001322 // Determine the actual naming class.
John McCalld010ac92013-02-27 00:08:19 +00001323 const CXXRecordDecl *NamingClass = Entity.getEffectiveNamingClass();
John McCall5b0829a2010-02-10 09:31:12 +00001324
John McCalld79b4d82010-04-02 00:03:43 +00001325 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1326 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1327
1328 // Before we try to recalculate access paths, try to white-list
1329 // accesses which just trade in on the final step, i.e. accesses
1330 // which don't require [M4] or [B4]. These are by far the most
John McCalla8ae2222010-04-06 21:38:20 +00001331 // common forms of privileged access.
John McCalld79b4d82010-04-02 00:03:43 +00001332 if (UnprivilegedAccess != AS_none) {
John McCalla8ae2222010-04-06 21:38:20 +00001333 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1334 case AR_dependent:
John McCalld79b4d82010-04-02 00:03:43 +00001335 // This is actually an interesting policy decision. We don't
1336 // *have* to delay immediately here: we can do the full access
1337 // calculation in the hope that friendship on some intermediate
1338 // class will make the declaration accessible non-dependently.
1339 // But that's not cheap, and odds are very good (note: assertion
1340 // made without data) that the friend declaration will determine
1341 // access.
John McCalla8ae2222010-04-06 21:38:20 +00001342 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001343
John McCalla8ae2222010-04-06 21:38:20 +00001344 case AR_accessible: return AR_accessible;
1345 case AR_inaccessible: break;
John McCalld79b4d82010-04-02 00:03:43 +00001346 }
1347 }
1348
John McCalla8ae2222010-04-06 21:38:20 +00001349 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall5b0829a2010-02-10 09:31:12 +00001350
John McCalld79b4d82010-04-02 00:03:43 +00001351 // We lower member accesses to base accesses by pretending that the
1352 // member is a base class of its declaring class.
1353 AccessSpecifier FinalAccess;
1354
John McCall5b0829a2010-02-10 09:31:12 +00001355 if (Entity.isMemberAccess()) {
John McCalld79b4d82010-04-02 00:03:43 +00001356 // Determine if the declaration is accessible from EC when named
1357 // in its declaring class.
John McCall5b0829a2010-02-10 09:31:12 +00001358 NamedDecl *Target = Entity.getTargetDecl();
John McCalla8ae2222010-04-06 21:38:20 +00001359 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall5b0829a2010-02-10 09:31:12 +00001360
John McCalld79b4d82010-04-02 00:03:43 +00001361 FinalAccess = Target->getAccess();
John McCalla8ae2222010-04-06 21:38:20 +00001362 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1363 case AR_accessible:
John McCall5149fbf2013-02-22 03:52:55 +00001364 // Target is accessible at EC when named in its declaring class.
1365 // We can now hill-climb and simply check whether the declaring
1366 // class is accessible as a base of the naming class. This is
1367 // equivalent to checking the access of a notional public
1368 // member with no instance context.
John McCalla8ae2222010-04-06 21:38:20 +00001369 FinalAccess = AS_public;
John McCall5149fbf2013-02-22 03:52:55 +00001370 Entity.suppressInstanceContext();
John McCalla8ae2222010-04-06 21:38:20 +00001371 break;
1372 case AR_inaccessible: break;
1373 case AR_dependent: return AR_dependent; // see above
John McCall5b0829a2010-02-10 09:31:12 +00001374 }
1375
John McCalld79b4d82010-04-02 00:03:43 +00001376 if (DeclaringClass == NamingClass)
John McCalla8ae2222010-04-06 21:38:20 +00001377 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
John McCalld79b4d82010-04-02 00:03:43 +00001378 } else {
1379 FinalAccess = AS_public;
John McCall5b0829a2010-02-10 09:31:12 +00001380 }
1381
John McCalla8ae2222010-04-06 21:38:20 +00001382 assert(Entity.getDeclaringClass() != NamingClass);
John McCall5b0829a2010-02-10 09:31:12 +00001383
1384 // Append the declaration's access if applicable.
1385 CXXBasePaths Paths;
John McCalla8ae2222010-04-06 21:38:20 +00001386 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCallc62bb642010-03-24 05:22:00 +00001387 if (!Path)
John McCalla8ae2222010-04-06 21:38:20 +00001388 return AR_dependent;
John McCall553c0792010-01-23 00:46:32 +00001389
John McCalld79b4d82010-04-02 00:03:43 +00001390 assert(Path->Access <= UnprivilegedAccess &&
1391 "access along best path worse than direct?");
1392 if (Path->Access == AS_public)
John McCalla8ae2222010-04-06 21:38:20 +00001393 return AR_accessible;
1394 return AR_inaccessible;
John McCallc62bb642010-03-24 05:22:00 +00001395}
1396
John McCalla8ae2222010-04-06 21:38:20 +00001397static void DelayDependentAccess(Sema &S,
1398 const EffectiveContext &EC,
1399 SourceLocation Loc,
1400 const AccessTarget &Entity) {
John McCallc62bb642010-03-24 05:22:00 +00001401 assert(EC.isDependent() && "delaying non-dependent access");
John McCall816d75b2010-03-24 07:46:06 +00001402 DeclContext *DC = EC.getInnerContext();
John McCallc62bb642010-03-24 05:22:00 +00001403 assert(DC->isDependentContext() && "delaying non-dependent access");
1404 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1405 Loc,
1406 Entity.isMemberAccess(),
1407 Entity.getAccess(),
1408 Entity.getTargetDecl(),
1409 Entity.getNamingClass(),
John McCalla8ae2222010-04-06 21:38:20 +00001410 Entity.getBaseObjectType(),
John McCallc62bb642010-03-24 05:22:00 +00001411 Entity.getDiag());
John McCall553c0792010-01-23 00:46:32 +00001412}
1413
John McCall5b0829a2010-02-10 09:31:12 +00001414/// Checks access to an entity from the given effective context.
John McCalla8ae2222010-04-06 21:38:20 +00001415static AccessResult CheckEffectiveAccess(Sema &S,
1416 const EffectiveContext &EC,
1417 SourceLocation Loc,
1418 AccessTarget &Entity) {
John McCalld79b4d82010-04-02 00:03:43 +00001419 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall553c0792010-01-23 00:46:32 +00001420
John McCalld79b4d82010-04-02 00:03:43 +00001421 switch (IsAccessible(S, EC, Entity)) {
John McCalla8ae2222010-04-06 21:38:20 +00001422 case AR_dependent:
1423 DelayDependentAccess(S, EC, Loc, Entity);
1424 return AR_dependent;
John McCalld79b4d82010-04-02 00:03:43 +00001425
John McCalla8ae2222010-04-06 21:38:20 +00001426 case AR_inaccessible:
Reid Kleckner42063b02014-02-08 02:40:20 +00001427 if (S.getLangOpts().MSVCCompat &&
1428 IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity))
1429 return AR_accessible;
John McCalld79b4d82010-04-02 00:03:43 +00001430 if (!Entity.isQuiet())
1431 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCalla8ae2222010-04-06 21:38:20 +00001432 return AR_inaccessible;
John McCalld79b4d82010-04-02 00:03:43 +00001433
John McCalla8ae2222010-04-06 21:38:20 +00001434 case AR_accessible:
1435 return AR_accessible;
John McCallc62bb642010-03-24 05:22:00 +00001436 }
1437
John McCalla8ae2222010-04-06 21:38:20 +00001438 // silence unnecessary warning
1439 llvm_unreachable("invalid access result");
John McCall5b0829a2010-02-10 09:31:12 +00001440}
John McCall553c0792010-01-23 00:46:32 +00001441
John McCall5b0829a2010-02-10 09:31:12 +00001442static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCalla8ae2222010-04-06 21:38:20 +00001443 AccessTarget &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +00001444 // If the access path is public, it's accessible everywhere.
1445 if (Entity.getAccess() == AS_public)
1446 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +00001447
John McCallc1465822011-02-14 07:13:47 +00001448 // If we're currently parsing a declaration, we may need to delay
1449 // access control checking, because our effective context might be
1450 // different based on what the declaration comes out as.
1451 //
1452 // For example, we might be parsing a declaration with a scope
1453 // specifier, like this:
1454 // A::private_type A::foo() { ... }
1455 //
1456 // Or we might be parsing something that will turn out to be a friend:
1457 // void foo(A::private_type);
1458 // void B::foo(A::private_type);
1459 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1460 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall5b0829a2010-02-10 09:31:12 +00001461 return Sema::AR_delayed;
John McCall553c0792010-01-23 00:46:32 +00001462 }
1463
John McCalla8ae2222010-04-06 21:38:20 +00001464 EffectiveContext EC(S.CurContext);
1465 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1466 case AR_accessible: return Sema::AR_accessible;
1467 case AR_inaccessible: return Sema::AR_inaccessible;
1468 case AR_dependent: return Sema::AR_dependent;
1469 }
Davide Italiano24e89662015-05-17 02:27:10 +00001470 llvm_unreachable("invalid access result");
John McCall553c0792010-01-23 00:46:32 +00001471}
1472
Richard Smithad5c1ca2013-04-29 10:13:55 +00001473void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *D) {
John McCall9743e8d2011-02-15 22:51:53 +00001474 // Access control for names used in the declarations of functions
1475 // and function templates should normally be evaluated in the context
1476 // of the declaration, just in case it's a friend of something.
1477 // However, this does not apply to local extern declarations.
1478
Richard Smithad5c1ca2013-04-29 10:13:55 +00001479 DeclContext *DC = D->getDeclContext();
Richard Smith608da012013-12-11 03:35:27 +00001480 if (D->isLocalExternDecl()) {
1481 DC = D->getLexicalDeclContext();
1482 } else if (FunctionDecl *FN = dyn_cast<FunctionDecl>(D)) {
1483 DC = FN;
Richard Smithad5c1ca2013-04-29 10:13:55 +00001484 } else if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) {
1485 DC = cast<DeclContext>(TD->getTemplatedDecl());
John McCall9743e8d2011-02-15 22:51:53 +00001486 }
1487
Chandler Carruthaad30072010-04-18 08:23:21 +00001488 EffectiveContext EC(DC);
John McCall86121512010-01-27 03:50:35 +00001489
John McCalla8ae2222010-04-06 21:38:20 +00001490 AccessTarget Target(DD.getAccessData());
1491
1492 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall86121512010-01-27 03:50:35 +00001493 DD.Triggered = true;
1494}
1495
John McCallc62bb642010-03-24 05:22:00 +00001496void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1497 const MultiLevelTemplateArgumentList &TemplateArgs) {
1498 SourceLocation Loc = DD.getAccessLoc();
1499 AccessSpecifier Access = DD.getAccess();
1500
1501 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1502 TemplateArgs);
1503 if (!NamingD) return;
1504 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1505 TemplateArgs);
1506 if (!TargetD) return;
1507
1508 if (DD.isAccessToMember()) {
John McCalla8ae2222010-04-06 21:38:20 +00001509 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1510 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1511 QualType BaseObjectType = DD.getAccessBaseObjectType();
1512 if (!BaseObjectType.isNull()) {
1513 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1514 DeclarationName());
1515 if (BaseObjectType.isNull()) return;
1516 }
1517
1518 AccessTarget Entity(Context,
1519 AccessTarget::Member,
1520 NamingClass,
1521 DeclAccessPair::make(TargetDecl, Access),
1522 BaseObjectType);
John McCallc62bb642010-03-24 05:22:00 +00001523 Entity.setDiag(DD.getDiagnostic());
1524 CheckAccess(*this, Loc, Entity);
1525 } else {
John McCalla8ae2222010-04-06 21:38:20 +00001526 AccessTarget Entity(Context,
1527 AccessTarget::Base,
1528 cast<CXXRecordDecl>(TargetD),
1529 cast<CXXRecordDecl>(NamingD),
1530 Access);
John McCallc62bb642010-03-24 05:22:00 +00001531 Entity.setDiag(DD.getDiagnostic());
1532 CheckAccess(*this, Loc, Entity);
1533 }
1534}
1535
John McCall5b0829a2010-02-10 09:31:12 +00001536Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001537 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001538 if (!getLangOpts().AccessControl ||
John McCall1064d7e2010-03-16 05:22:47 +00001539 !E->getNamingClass() ||
John McCalla0296f72010-03-19 07:35:19 +00001540 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001541 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001542
John McCalla8ae2222010-04-06 21:38:20 +00001543 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1544 Found, QualType());
John McCall1064d7e2010-03-16 05:22:47 +00001545 Entity.setDiag(diag::err_access) << E->getSourceRange();
1546
1547 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001548}
1549
1550/// Perform access-control checking on a previously-unresolved member
1551/// access which has now been resolved to a member.
John McCall5b0829a2010-02-10 09:31:12 +00001552Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCalla0296f72010-03-19 07:35:19 +00001553 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001554 if (!getLangOpts().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001555 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001556 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +00001557
John McCalla8ae2222010-04-06 21:38:20 +00001558 QualType BaseType = E->getBaseType();
1559 if (E->isArrow())
1560 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1561
1562 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1563 Found, BaseType);
John McCall1064d7e2010-03-16 05:22:47 +00001564 Entity.setDiag(diag::err_access) << E->getSourceRange();
1565
1566 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +00001567}
1568
John McCalld4274212012-04-09 20:53:23 +00001569/// Is the given special member function accessible for the purposes of
1570/// deciding whether to define a special member function as deleted?
1571bool Sema::isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl,
1572 AccessSpecifier access,
1573 QualType objectType) {
1574 // Fast path.
1575 if (access == AS_public || !getLangOpts().AccessControl) return true;
1576
1577 AccessTarget entity(Context, AccessTarget::Member, decl->getParent(),
1578 DeclAccessPair::make(decl, access), objectType);
1579
1580 // Suppress diagnostics.
1581 entity.setDiag(PDiag());
1582
1583 switch (CheckAccess(*this, SourceLocation(), entity)) {
1584 case AR_accessible: return true;
1585 case AR_inaccessible: return false;
1586 case AR_dependent: llvm_unreachable("dependent for =delete computation");
1587 case AR_delayed: llvm_unreachable("cannot delay =delete computation");
1588 }
1589 llvm_unreachable("bad access result");
1590}
1591
John McCall5b0829a2010-02-10 09:31:12 +00001592Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +00001593 CXXDestructorDecl *Dtor,
John McCall5dadb652012-04-07 03:04:20 +00001594 const PartialDiagnostic &PDiag,
1595 QualType ObjectTy) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001596 if (!getLangOpts().AccessControl)
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 // There's never a path involved when checking implicit destructor access.
John McCall6781b052010-02-02 08:45:54 +00001600 AccessSpecifier Access = Dtor->getAccess();
1601 if (Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001602 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +00001603
John McCall1064d7e2010-03-16 05:22:47 +00001604 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCall5dadb652012-04-07 03:04:20 +00001605 if (ObjectTy.isNull()) ObjectTy = Context.getTypeDeclType(NamingClass);
1606
John McCalla8ae2222010-04-06 21:38:20 +00001607 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1608 DeclAccessPair::make(Dtor, Access),
John McCall5dadb652012-04-07 03:04:20 +00001609 ObjectTy);
John McCall1064d7e2010-03-16 05:22:47 +00001610 Entity.setDiag(PDiag); // TODO: avoid copy
1611
1612 return CheckAccess(*this, Loc, Entity);
John McCall6781b052010-02-02 08:45:54 +00001613}
1614
John McCall760af172010-02-01 03:16:54 +00001615/// Checks access to a constructor.
John McCall5b0829a2010-02-10 09:31:12 +00001616Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00001617 CXXConstructorDecl *Constructor,
1618 const InitializedEntity &Entity,
1619 AccessSpecifier Access,
1620 bool IsCopyBindingRefToTemp) {
John McCall5dadb652012-04-07 03:04:20 +00001621 if (!getLangOpts().AccessControl || Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001622 return AR_accessible;
John McCall760af172010-02-01 03:16:54 +00001623
Alexis Hunteef8ee02011-06-10 03:50:41 +00001624 PartialDiagnostic PD(PDiag());
Anders Carlssona01874b2010-04-21 18:47:17 +00001625 switch (Entity.getKind()) {
1626 default:
Alexis Hunteef8ee02011-06-10 03:50:41 +00001627 PD = PDiag(IsCopyBindingRefToTemp
1628 ? diag::ext_rvalue_to_reference_access_ctor
1629 : diag::err_access_ctor);
1630
Anders Carlssona01874b2010-04-21 18:47:17 +00001631 break;
John McCall1064d7e2010-03-16 05:22:47 +00001632
Anders Carlsson05bf0092010-04-22 05:40:53 +00001633 case InitializedEntity::EK_Base:
Alexis Hunteef8ee02011-06-10 03:50:41 +00001634 PD = PDiag(diag::err_access_base_ctor);
1635 PD << Entity.isInheritedVirtualBase()
1636 << Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor);
Anders Carlssona01874b2010-04-21 18:47:17 +00001637 break;
Anders Carlsson05bf0092010-04-22 05:40:53 +00001638
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001639 case InitializedEntity::EK_Member: {
1640 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Alexis Hunteef8ee02011-06-10 03:50:41 +00001641 PD = PDiag(diag::err_access_field_ctor);
1642 PD << Field->getType() << getSpecialMember(Constructor);
Anders Carlsson4bb6e922010-04-21 20:28:29 +00001643 break;
1644 }
Anders Carlssona01874b2010-04-21 18:47:17 +00001645
Douglas Gregor19666fb2012-02-15 16:57:26 +00001646 case InitializedEntity::EK_LambdaCapture: {
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001647 StringRef VarName = Entity.getCapturedVarName();
Douglas Gregor19666fb2012-02-15 16:57:26 +00001648 PD = PDiag(diag::err_access_lambda_capture);
Faisal Vali5fb7c3c2013-12-05 01:40:41 +00001649 PD << VarName << Entity.getType() << getSpecialMember(Constructor);
Douglas Gregor19666fb2012-02-15 16:57:26 +00001650 break;
1651 }
1652
Anders Carlsson43c64af2010-04-21 19:52:01 +00001653 }
1654
John McCall5dadb652012-04-07 03:04:20 +00001655 return CheckConstructorAccess(UseLoc, Constructor, Entity, Access, PD);
Alexis Hunteef8ee02011-06-10 03:50:41 +00001656}
1657
1658/// Checks access to a constructor.
1659Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
1660 CXXConstructorDecl *Constructor,
John McCall5dadb652012-04-07 03:04:20 +00001661 const InitializedEntity &Entity,
Alexis Hunteef8ee02011-06-10 03:50:41 +00001662 AccessSpecifier Access,
John McCall5dadb652012-04-07 03:04:20 +00001663 const PartialDiagnostic &PD) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001664 if (!getLangOpts().AccessControl ||
Alexis Hunteef8ee02011-06-10 03:50:41 +00001665 Access == AS_public)
1666 return AR_accessible;
1667
1668 CXXRecordDecl *NamingClass = Constructor->getParent();
John McCall5dadb652012-04-07 03:04:20 +00001669
1670 // Initializing a base sub-object is an instance method call on an
1671 // object of the derived class. Otherwise, we have an instance method
1672 // call on an object of the constructed type.
1673 CXXRecordDecl *ObjectClass;
1674 if (Entity.getKind() == InitializedEntity::EK_Base) {
1675 ObjectClass = cast<CXXConstructorDecl>(CurContext)->getParent();
1676 } else {
1677 ObjectClass = NamingClass;
1678 }
1679
Alexis Hunteef8ee02011-06-10 03:50:41 +00001680 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1681 DeclAccessPair::make(Constructor, Access),
John McCall5dadb652012-04-07 03:04:20 +00001682 Context.getTypeDeclType(ObjectClass));
Alexis Hunteef8ee02011-06-10 03:50:41 +00001683 AccessEntity.setDiag(PD);
1684
Anders Carlssona01874b2010-04-21 18:47:17 +00001685 return CheckAccess(*this, UseLoc, AccessEntity);
John McCall5dadb652012-04-07 03:04:20 +00001686}
John McCall760af172010-02-01 03:16:54 +00001687
John McCallfb6f5262010-03-18 08:19:33 +00001688/// Checks access to an overloaded operator new or delete.
1689Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1690 SourceRange PlacementRange,
1691 CXXRecordDecl *NamingClass,
Alexis Huntf91729462011-05-12 22:46:25 +00001692 DeclAccessPair Found,
1693 bool Diagnose) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001694 if (!getLangOpts().AccessControl ||
John McCallfb6f5262010-03-18 08:19:33 +00001695 !NamingClass ||
John McCalla0296f72010-03-19 07:35:19 +00001696 Found.getAccess() == AS_public)
John McCallfb6f5262010-03-18 08:19:33 +00001697 return AR_accessible;
1698
John McCalla8ae2222010-04-06 21:38:20 +00001699 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1700 QualType());
Alexis Huntf91729462011-05-12 22:46:25 +00001701 if (Diagnose)
1702 Entity.setDiag(diag::err_access)
1703 << PlacementRange;
John McCallfb6f5262010-03-18 08:19:33 +00001704
1705 return CheckAccess(*this, OpLoc, Entity);
1706}
1707
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001708/// \brief Checks access to a member.
1709Sema::AccessResult Sema::CheckMemberAccess(SourceLocation UseLoc,
1710 CXXRecordDecl *NamingClass,
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001711 DeclAccessPair Found) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001712 if (!getLangOpts().AccessControl ||
1713 !NamingClass ||
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001714 Found.getAccess() == AS_public)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001715 return AR_accessible;
1716
1717 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
Eli Friedman3be1a1c2013-10-01 02:44:48 +00001718 Found, QualType());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001719
1720 return CheckAccess(*this, UseLoc, Entity);
1721}
1722
John McCall760af172010-02-01 03:16:54 +00001723/// Checks access to an overloaded member operator, including
1724/// conversion operators.
John McCall5b0829a2010-02-10 09:31:12 +00001725Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1726 Expr *ObjectExpr,
John McCall1064d7e2010-03-16 05:22:47 +00001727 Expr *ArgExpr,
John McCalla0296f72010-03-19 07:35:19 +00001728 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001729 if (!getLangOpts().AccessControl ||
John McCalla0296f72010-03-19 07:35:19 +00001730 Found.getAccess() == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +00001731 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001732
John McCall30909032011-09-21 08:36:56 +00001733 const RecordType *RT = ObjectExpr->getType()->castAs<RecordType>();
John McCallb3a44002010-01-28 01:42:12 +00001734 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1735
John McCalla8ae2222010-04-06 21:38:20 +00001736 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1737 ObjectExpr->getType());
John McCall1064d7e2010-03-16 05:22:47 +00001738 Entity.setDiag(diag::err_access)
1739 << ObjectExpr->getSourceRange()
1740 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1741
1742 return CheckAccess(*this, OpLoc, Entity);
John McCall5b0829a2010-02-10 09:31:12 +00001743}
John McCallb3a44002010-01-28 01:42:12 +00001744
John McCalla0a96892012-08-10 03:15:35 +00001745/// Checks access to the target of a friend declaration.
1746Sema::AccessResult Sema::CheckFriendAccess(NamedDecl *target) {
Alp Tokera2794f92014-01-22 07:29:52 +00001747 assert(isa<CXXMethodDecl>(target->getAsFunction()));
John McCalla0a96892012-08-10 03:15:35 +00001748
1749 // Friendship lookup is a redeclaration lookup, so there's never an
1750 // inheritance path modifying access.
1751 AccessSpecifier access = target->getAccess();
1752
1753 if (!getLangOpts().AccessControl || access == AS_public)
1754 return AR_accessible;
1755
Alp Toker2bd4a282014-01-22 07:53:08 +00001756 CXXMethodDecl *method = cast<CXXMethodDecl>(target->getAsFunction());
John McCalla0a96892012-08-10 03:15:35 +00001757
1758 AccessTarget entity(Context, AccessTarget::Member,
1759 cast<CXXRecordDecl>(target->getDeclContext()),
1760 DeclAccessPair::make(target, access),
1761 /*no instance context*/ QualType());
1762 entity.setDiag(diag::err_access_friend_function)
Reid Kleckneraf9bf592014-12-17 23:40:46 +00001763 << (method->getQualifier() ? method->getQualifierLoc().getSourceRange()
1764 : method->getNameInfo().getSourceRange());
John McCalla0a96892012-08-10 03:15:35 +00001765
1766 // We need to bypass delayed-diagnostics because we might be called
1767 // while the ParsingDeclarator is active.
1768 EffectiveContext EC(CurContext);
1769 switch (CheckEffectiveAccess(*this, EC, target->getLocation(), entity)) {
1770 case AR_accessible: return Sema::AR_accessible;
1771 case AR_inaccessible: return Sema::AR_inaccessible;
1772 case AR_dependent: return Sema::AR_dependent;
1773 }
Davide Italiano9b950d82015-07-29 20:55:04 +00001774 llvm_unreachable("invalid access result");
John McCalla0a96892012-08-10 03:15:35 +00001775}
1776
John McCall16df1e52010-03-30 21:47:33 +00001777Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1778 DeclAccessPair Found) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001779 if (!getLangOpts().AccessControl ||
John McCallb493d532010-03-30 22:20:00 +00001780 Found.getAccess() == AS_none ||
John McCall16df1e52010-03-30 21:47:33 +00001781 Found.getAccess() == AS_public)
1782 return AR_accessible;
1783
John McCall8d08b9b2010-08-27 09:08:28 +00001784 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCall8c12dc42010-04-22 18:44:12 +00001785 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall16df1e52010-03-30 21:47:33 +00001786
John McCalla8ae2222010-04-06 21:38:20 +00001787 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
John McCall5dadb652012-04-07 03:04:20 +00001788 /*no instance context*/ QualType());
John McCall16df1e52010-03-30 21:47:33 +00001789 Entity.setDiag(diag::err_access)
1790 << Ovl->getSourceRange();
1791
1792 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1793}
1794
John McCall5b0829a2010-02-10 09:31:12 +00001795/// Checks access for a hierarchy conversion.
1796///
John McCall5b0829a2010-02-10 09:31:12 +00001797/// \param ForceCheck true if this check should be performed even if access
1798/// control is disabled; some things rely on this for semantics
1799/// \param ForceUnprivileged true if this check should proceed as if the
1800/// context had no special privileges
John McCall5b0829a2010-02-10 09:31:12 +00001801Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall5b0829a2010-02-10 09:31:12 +00001802 QualType Base,
1803 QualType Derived,
1804 const CXXBasePath &Path,
John McCall1064d7e2010-03-16 05:22:47 +00001805 unsigned DiagID,
John McCall5b0829a2010-02-10 09:31:12 +00001806 bool ForceCheck,
John McCall1064d7e2010-03-16 05:22:47 +00001807 bool ForceUnprivileged) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001808 if (!ForceCheck && !getLangOpts().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +00001809 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001810
John McCall5b0829a2010-02-10 09:31:12 +00001811 if (Path.Access == AS_public)
1812 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +00001813
John McCall5b0829a2010-02-10 09:31:12 +00001814 CXXRecordDecl *BaseD, *DerivedD;
1815 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1816 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall1064d7e2010-03-16 05:22:47 +00001817
John McCalla8ae2222010-04-06 21:38:20 +00001818 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1819 Path.Access);
John McCall1064d7e2010-03-16 05:22:47 +00001820 if (DiagID)
1821 Entity.setDiag(DiagID) << Derived << Base;
John McCall5b0829a2010-02-10 09:31:12 +00001822
John McCalla8ae2222010-04-06 21:38:20 +00001823 if (ForceUnprivileged) {
1824 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1825 AccessLoc, Entity)) {
1826 case ::AR_accessible: return Sema::AR_accessible;
1827 case ::AR_inaccessible: return Sema::AR_inaccessible;
1828 case ::AR_dependent: return Sema::AR_dependent;
1829 }
1830 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1831 }
John McCall1064d7e2010-03-16 05:22:47 +00001832 return CheckAccess(*this, AccessLoc, Entity);
John McCallb3a44002010-01-28 01:42:12 +00001833}
1834
John McCall553c0792010-01-23 00:46:32 +00001835/// Checks access to all the declarations in the given result set.
John McCall5b0829a2010-02-10 09:31:12 +00001836void Sema::CheckLookupAccess(const LookupResult &R) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001837 assert(getLangOpts().AccessControl
John McCall5b0829a2010-02-10 09:31:12 +00001838 && "performing access check without access control");
1839 assert(R.getNamingClass() && "performing access check without naming class");
1840
John McCall1064d7e2010-03-16 05:22:47 +00001841 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1842 if (I.getAccess() != AS_public) {
John McCalla8ae2222010-04-06 21:38:20 +00001843 AccessTarget Entity(Context, AccessedEntity::Member,
1844 R.getNamingClass(), I.getPair(),
Erik Verbruggen631dfc62011-09-19 15:10:40 +00001845 R.getBaseObjectType());
John McCall1064d7e2010-03-16 05:22:47 +00001846 Entity.setDiag(diag::err_access);
John McCall1064d7e2010-03-16 05:22:47 +00001847 CheckAccess(*this, R.getNameLoc(), Entity);
1848 }
1849 }
John McCall553c0792010-01-23 00:46:32 +00001850}
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001851
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001852/// Checks access to Decl from the given class. The check will take access
1853/// specifiers into account, but no member access expressions and such.
1854///
1855/// \param Decl the declaration to check if it can be accessed
Dmitri Gribenkodd28e792012-08-24 00:01:24 +00001856/// \param Ctx the class/context from which to start the search
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001857/// \return true if the Decl is accessible from the Class, false otherwise.
Douglas Gregor03ba1882011-11-03 16:51:37 +00001858bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) {
1859 if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) {
Douglas Gregor3b52b692011-11-03 17:41:55 +00001860 if (!Decl->isCXXClassMember())
Douglas Gregor03ba1882011-11-03 16:51:37 +00001861 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001862
Douglas Gregor03ba1882011-11-03 16:51:37 +00001863 QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal();
1864 AccessTarget Entity(Context, AccessedEntity::Member, Class,
1865 DeclAccessPair::make(Decl, Decl->getAccess()),
1866 qType);
1867 if (Entity.getAccess() == AS_public)
1868 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001869
Douglas Gregor03ba1882011-11-03 16:51:37 +00001870 EffectiveContext EC(CurContext);
1871 return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible;
1872 }
1873
Douglas Gregor21ceb182011-11-03 19:00:24 +00001874 if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) {
1875 // @public and @package ivars are always accessible.
1876 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public ||
1877 Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package)
1878 return true;
Serge Pavlov64bc7032013-05-07 16:56:03 +00001879
Douglas Gregor21ceb182011-11-03 19:00:24 +00001880 // If we are inside a class or category implementation, determine the
1881 // interface we're in.
Craig Topperc3ec1492014-05-26 06:22:03 +00001882 ObjCInterfaceDecl *ClassOfMethodDecl = nullptr;
Douglas Gregor21ceb182011-11-03 19:00:24 +00001883 if (ObjCMethodDecl *MD = getCurMethodDecl())
1884 ClassOfMethodDecl = MD->getClassInterface();
1885 else if (FunctionDecl *FD = getCurFunctionDecl()) {
1886 if (ObjCImplDecl *Impl
1887 = dyn_cast<ObjCImplDecl>(FD->getLexicalDeclContext())) {
1888 if (ObjCImplementationDecl *IMPD
1889 = dyn_cast<ObjCImplementationDecl>(Impl))
1890 ClassOfMethodDecl = IMPD->getClassInterface();
1891 else if (ObjCCategoryImplDecl* CatImplClass
1892 = dyn_cast<ObjCCategoryImplDecl>(Impl))
1893 ClassOfMethodDecl = CatImplClass->getClassInterface();
1894 }
1895 }
1896
1897 // If we're not in an interface, this ivar is inaccessible.
1898 if (!ClassOfMethodDecl)
1899 return false;
1900
1901 // If we're inside the same interface that owns the ivar, we're fine.
Douglas Gregor0b144e12011-12-15 00:29:59 +00001902 if (declaresSameEntity(ClassOfMethodDecl, Ivar->getContainingInterface()))
Douglas Gregor21ceb182011-11-03 19:00:24 +00001903 return true;
1904
1905 // If the ivar is private, it's inaccessible.
1906 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Private)
1907 return false;
1908
1909 return Ivar->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl);
1910 }
1911
Douglas Gregor03ba1882011-11-03 16:51:37 +00001912 return true;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001913}