blob: 01c141e57f923a88253a002709bb7ba70e406ffc [file] [log] [blame]
Anders Carlsson29f006b2009-03-27 05:05:05 +00001//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
Anders Carlsson60d6b0d2009-03-27 04:43:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides Sema routines for C++ access control semantics.
11//
12//===----------------------------------------------------------------------===//
Anders Carlssonc60e8882009-03-27 04:54:36 +000013
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall9c3087b2010-08-26 02:13:20 +000015#include "clang/Sema/DelayedDiagnostic.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
17#include "clang/Sema/Lookup.h"
Anders Carlssonc4f1e872009-03-27 06:03:27 +000018#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000019#include "clang/AST/CXXInheritance.h"
20#include "clang/AST/DeclCXX.h"
John McCalld60e22e2010-03-12 01:19:31 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregorf3c02862011-11-03 19:00:24 +000022#include "clang/AST/DeclObjC.h"
John McCall0c01d182010-03-24 05:22:00 +000023#include "clang/AST/DependentDiagnostic.h"
John McCallc373d482010-01-27 01:50:18 +000024#include "clang/AST/ExprCXX.h"
25
Anders Carlssonc60e8882009-03-27 04:54:36 +000026using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000027using namespace sema;
Anders Carlssonc60e8882009-03-27 04:54:36 +000028
John McCall161755a2010-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 Carlsson29f006b2009-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 Stump1eb44332009-09-09 15:08:12 +000039bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
Anders Carlssonc60e8882009-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 Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlssonc60e8882009-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 Stump1eb44332009-09-09 15:08:12 +000051 Diag(MemberDecl->getLocation(),
52 diag::err_class_redeclared_with_different_access)
Anders Carlssonc60e8882009-03-27 04:54:36 +000053 << MemberDecl << LexicalAS;
54 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
55 << PrevMemberDecl << PrevMemberDecl->getAccess();
John McCall44e067b2009-12-23 00:37:40 +000056
57 MemberDecl->setAccess(LexicalAS);
Anders Carlssonc60e8882009-03-27 04:54:36 +000058 return true;
59 }
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlssonc60e8882009-03-27 04:54:36 +000061 MemberDecl->setAccess(PrevMemberDecl->getAccess());
62 return false;
63}
Anders Carlsson29f006b2009-03-27 05:05:05 +000064
John McCall161755a2010-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 McCall6b2accb2010-02-10 09:31:12 +000079namespace {
80struct EffectiveContext {
John McCall2cc26752010-03-27 06:55:49 +000081 EffectiveContext() : Inner(0), Dependent(false) {}
Anders Carlssonc4f1e872009-03-27 06:03:27 +000082
John McCall7ad650f2010-03-24 07:46:06 +000083 explicit EffectiveContext(DeclContext *DC)
84 : Inner(DC),
85 Dependent(DC->isDependentContext()) {
John McCall0c01d182010-03-24 05:22:00 +000086
John McCall88b6c712010-03-17 04:58:56 +000087 // C++ [class.access.nest]p1:
88 // A nested class is a member and as such has the same access
89 // rights as any other member.
90 // C++ [class.access]p2:
91 // A member of a class can also access all the names to which
John McCall2cc26752010-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) {
100 if (isa<CXXRecordDecl>(DC)) {
101 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
102 Records.push_back(Record);
103 DC = Record->getDeclContext();
104 } else if (isa<FunctionDecl>(DC)) {
105 FunctionDecl *Function = cast<FunctionDecl>(DC)->getCanonicalDecl();
106 Functions.push_back(Function);
Douglas Gregorac57f0b2011-10-09 22:38:36 +0000107
108 if (Function->getFriendObjectKind())
109 DC = Function->getLexicalDeclContext();
110 else
111 DC = Function->getDeclContext();
John McCall2cc26752010-03-27 06:55:49 +0000112 } else if (DC->isFileContext()) {
113 break;
114 } else {
115 DC = DC->getParent();
116 }
John McCall88b6c712010-03-17 04:58:56 +0000117 }
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000118 }
Sebastian Redl726212f2009-07-18 14:32:15 +0000119
John McCall0c01d182010-03-24 05:22:00 +0000120 bool isDependent() const { return Dependent; }
121
John McCall88b6c712010-03-17 04:58:56 +0000122 bool includesClass(const CXXRecordDecl *R) const {
123 R = R->getCanonicalDecl();
124 return std::find(Records.begin(), Records.end(), R)
125 != Records.end();
John McCall6b2accb2010-02-10 09:31:12 +0000126 }
127
John McCall7ad650f2010-03-24 07:46:06 +0000128 /// Retrieves the innermost "useful" context. Can be null if we're
129 /// doing access-control without privileges.
130 DeclContext *getInnerContext() const {
131 return Inner;
John McCall0c01d182010-03-24 05:22:00 +0000132 }
133
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 typedef SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
John McCall0c01d182010-03-24 05:22:00 +0000135
John McCall7ad650f2010-03-24 07:46:06 +0000136 DeclContext *Inner;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 SmallVector<FunctionDecl*, 4> Functions;
138 SmallVector<CXXRecordDecl*, 4> Records;
John McCall0c01d182010-03-24 05:22:00 +0000139 bool Dependent;
John McCall6b2accb2010-02-10 09:31:12 +0000140};
John McCall161755a2010-04-06 21:38:20 +0000141
Nico Weber6bb4dcb2010-11-28 22:53:37 +0000142/// Like sema::AccessedEntity, but kindly lets us scribble all over
John McCall161755a2010-04-06 21:38:20 +0000143/// it.
John McCall9c3087b2010-08-26 02:13:20 +0000144struct AccessTarget : public AccessedEntity {
145 AccessTarget(const AccessedEntity &Entity)
John McCall161755a2010-04-06 21:38:20 +0000146 : AccessedEntity(Entity) {
147 initialize();
148 }
149
150 AccessTarget(ASTContext &Context,
151 MemberNonce _,
152 CXXRecordDecl *NamingClass,
153 DeclAccessPair FoundDecl,
Erik Verbruggen24dd9ad2011-09-19 15:10:40 +0000154 QualType BaseObjectType)
155 : AccessedEntity(Context, Member, NamingClass, FoundDecl, BaseObjectType) {
John McCall161755a2010-04-06 21:38:20 +0000156 initialize();
157 }
158
159 AccessTarget(ASTContext &Context,
160 BaseNonce _,
161 CXXRecordDecl *BaseClass,
162 CXXRecordDecl *DerivedClass,
163 AccessSpecifier Access)
164 : AccessedEntity(Context, Base, BaseClass, DerivedClass, Access) {
165 initialize();
166 }
167
John McCallb9abd8722012-04-07 03:04:20 +0000168 bool isInstanceMember() const {
169 return (isMemberAccess() && getTargetDecl()->isCXXInstanceMember());
170 }
171
John McCall161755a2010-04-06 21:38:20 +0000172 bool hasInstanceContext() const {
173 return HasInstanceContext;
174 }
175
176 class SavedInstanceContext {
177 public:
178 ~SavedInstanceContext() {
179 Target.HasInstanceContext = Has;
180 }
181
182 private:
John McCallc91cc662010-04-07 00:41:46 +0000183 friend struct AccessTarget;
John McCall161755a2010-04-06 21:38:20 +0000184 explicit SavedInstanceContext(AccessTarget &Target)
185 : Target(Target), Has(Target.HasInstanceContext) {}
186 AccessTarget &Target;
187 bool Has;
188 };
189
190 SavedInstanceContext saveInstanceContext() {
191 return SavedInstanceContext(*this);
192 }
193
194 void suppressInstanceContext() {
195 HasInstanceContext = false;
196 }
197
198 const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
199 assert(HasInstanceContext);
200 if (CalculatedInstanceContext)
201 return InstanceContext;
202
203 CalculatedInstanceContext = true;
204 DeclContext *IC = S.computeDeclContext(getBaseObjectType());
205 InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0);
206 return InstanceContext;
207 }
208
209 const CXXRecordDecl *getDeclaringClass() const {
210 return DeclaringClass;
211 }
212
213private:
214 void initialize() {
215 HasInstanceContext = (isMemberAccess() &&
216 !getBaseObjectType().isNull() &&
217 getTargetDecl()->isCXXInstanceMember());
218 CalculatedInstanceContext = false;
219 InstanceContext = 0;
220
221 if (isMemberAccess())
222 DeclaringClass = FindDeclaringClass(getTargetDecl());
223 else
224 DeclaringClass = getBaseClass();
225 DeclaringClass = DeclaringClass->getCanonicalDecl();
226 }
227
228 bool HasInstanceContext : 1;
229 mutable bool CalculatedInstanceContext : 1;
230 mutable const CXXRecordDecl *InstanceContext;
231 const CXXRecordDecl *DeclaringClass;
232};
233
Anders Carlsson29f006b2009-03-27 05:05:05 +0000234}
John McCall92f88312010-01-23 00:46:32 +0000235
John McCall01ebd9d2010-05-04 05:11:27 +0000236/// Checks whether one class might instantiate to the other.
237static bool MightInstantiateTo(const CXXRecordDecl *From,
238 const CXXRecordDecl *To) {
239 // Declaration names are always preserved by instantiation.
240 if (From->getDeclName() != To->getDeclName())
241 return false;
242
243 const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext();
244 const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext();
245 if (FromDC == ToDC) return true;
246 if (FromDC->isFileContext() || ToDC->isFileContext()) return false;
247
248 // Be conservative.
249 return true;
250}
251
John McCall161755a2010-04-06 21:38:20 +0000252/// Checks whether one class is derived from another, inclusively.
253/// Properly indicates when it couldn't be determined due to
254/// dependence.
255///
256/// This should probably be donated to AST or at least Sema.
257static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
258 const CXXRecordDecl *Target) {
259 assert(Derived->getCanonicalDecl() == Derived);
260 assert(Target->getCanonicalDecl() == Target);
John McCallc1b621d2010-03-24 09:04:37 +0000261
John McCall161755a2010-04-06 21:38:20 +0000262 if (Derived == Target) return AR_accessible;
John McCallc1b621d2010-03-24 09:04:37 +0000263
John McCall01ebd9d2010-05-04 05:11:27 +0000264 bool CheckDependent = Derived->isDependentContext();
265 if (CheckDependent && MightInstantiateTo(Derived, Target))
266 return AR_dependent;
267
John McCall161755a2010-04-06 21:38:20 +0000268 AccessResult OnFailure = AR_inaccessible;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
John McCall161755a2010-04-06 21:38:20 +0000270
271 while (true) {
Douglas Gregor7432b902011-11-14 23:00:43 +0000272 if (Derived->isDependentContext() && !Derived->hasDefinition())
273 return AR_dependent;
274
John McCall161755a2010-04-06 21:38:20 +0000275 for (CXXRecordDecl::base_class_const_iterator
276 I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) {
277
278 const CXXRecordDecl *RD;
279
280 QualType T = I->getType();
281 if (const RecordType *RT = T->getAs<RecordType>()) {
282 RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall01ebd9d2010-05-04 05:11:27 +0000283 } else if (const InjectedClassNameType *IT
284 = T->getAs<InjectedClassNameType>()) {
285 RD = IT->getDecl();
John McCall161755a2010-04-06 21:38:20 +0000286 } else {
John McCall161755a2010-04-06 21:38:20 +0000287 assert(T->isDependentType() && "non-dependent base wasn't a record?");
288 OnFailure = AR_dependent;
289 continue;
290 }
291
292 RD = RD->getCanonicalDecl();
293 if (RD == Target) return AR_accessible;
John McCall01ebd9d2010-05-04 05:11:27 +0000294 if (CheckDependent && MightInstantiateTo(RD, Target))
295 OnFailure = AR_dependent;
296
John McCall161755a2010-04-06 21:38:20 +0000297 Queue.push_back(RD);
298 }
299
300 if (Queue.empty()) break;
301
302 Derived = Queue.back();
303 Queue.pop_back();
304 }
305
306 return OnFailure;
John McCall6b2accb2010-02-10 09:31:12 +0000307}
308
John McCall161755a2010-04-06 21:38:20 +0000309
John McCall0c01d182010-03-24 05:22:00 +0000310static bool MightInstantiateTo(Sema &S, DeclContext *Context,
311 DeclContext *Friend) {
312 if (Friend == Context)
313 return true;
314
315 assert(!Friend->isDependentContext() &&
316 "can't handle friends with dependent contexts here");
317
318 if (!Context->isDependentContext())
319 return false;
320
321 if (Friend->isFileContext())
322 return false;
323
324 // TODO: this is very conservative
325 return true;
326}
327
328// Asks whether the type in 'context' can ever instantiate to the type
329// in 'friend'.
330static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
331 if (Friend == Context)
332 return true;
333
334 if (!Friend->isDependentType() && !Context->isDependentType())
335 return false;
336
337 // TODO: this is very conservative.
338 return true;
339}
340
341static bool MightInstantiateTo(Sema &S,
342 FunctionDecl *Context,
343 FunctionDecl *Friend) {
344 if (Context->getDeclName() != Friend->getDeclName())
345 return false;
346
347 if (!MightInstantiateTo(S,
348 Context->getDeclContext(),
349 Friend->getDeclContext()))
350 return false;
351
352 CanQual<FunctionProtoType> FriendTy
353 = S.Context.getCanonicalType(Friend->getType())
354 ->getAs<FunctionProtoType>();
355 CanQual<FunctionProtoType> ContextTy
356 = S.Context.getCanonicalType(Context->getType())
357 ->getAs<FunctionProtoType>();
358
359 // There isn't any way that I know of to add qualifiers
360 // during instantiation.
361 if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
362 return false;
363
364 if (FriendTy->getNumArgs() != ContextTy->getNumArgs())
365 return false;
366
367 if (!MightInstantiateTo(S,
368 ContextTy->getResultType(),
369 FriendTy->getResultType()))
370 return false;
371
372 for (unsigned I = 0, E = FriendTy->getNumArgs(); I != E; ++I)
373 if (!MightInstantiateTo(S,
374 ContextTy->getArgType(I),
375 FriendTy->getArgType(I)))
376 return false;
377
378 return true;
379}
380
381static bool MightInstantiateTo(Sema &S,
382 FunctionTemplateDecl *Context,
383 FunctionTemplateDecl *Friend) {
384 return MightInstantiateTo(S,
385 Context->getTemplatedDecl(),
386 Friend->getTemplatedDecl());
387}
388
John McCall161755a2010-04-06 21:38:20 +0000389static AccessResult MatchesFriend(Sema &S,
390 const EffectiveContext &EC,
391 const CXXRecordDecl *Friend) {
John McCalla742db02010-03-17 20:01:29 +0000392 if (EC.includesClass(Friend))
John McCall161755a2010-04-06 21:38:20 +0000393 return AR_accessible;
John McCalla742db02010-03-17 20:01:29 +0000394
John McCall0c01d182010-03-24 05:22:00 +0000395 if (EC.isDependent()) {
396 CanQualType FriendTy
397 = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
398
399 for (EffectiveContext::record_iterator
400 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
401 CanQualType ContextTy
402 = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
403 if (MightInstantiateTo(S, ContextTy, FriendTy))
John McCall161755a2010-04-06 21:38:20 +0000404 return AR_dependent;
John McCall0c01d182010-03-24 05:22:00 +0000405 }
406 }
407
John McCall161755a2010-04-06 21:38:20 +0000408 return AR_inaccessible;
John McCalla742db02010-03-17 20:01:29 +0000409}
410
John McCall161755a2010-04-06 21:38:20 +0000411static AccessResult MatchesFriend(Sema &S,
412 const EffectiveContext &EC,
413 CanQualType Friend) {
John McCall0c01d182010-03-24 05:22:00 +0000414 if (const RecordType *RT = Friend->getAs<RecordType>())
415 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCalla742db02010-03-17 20:01:29 +0000416
John McCall0c01d182010-03-24 05:22:00 +0000417 // TODO: we can do better than this
418 if (Friend->isDependentType())
John McCall161755a2010-04-06 21:38:20 +0000419 return AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000420
John McCall161755a2010-04-06 21:38:20 +0000421 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000422}
423
424/// Determines whether the given friend class template matches
425/// anything in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000426static AccessResult MatchesFriend(Sema &S,
427 const EffectiveContext &EC,
428 ClassTemplateDecl *Friend) {
429 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000430
John McCall93ba8572010-03-25 06:39:04 +0000431 // Check whether the friend is the template of a class in the
432 // context chain.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000433 for (SmallVectorImpl<CXXRecordDecl*>::const_iterator
John McCall0c01d182010-03-24 05:22:00 +0000434 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
435 CXXRecordDecl *Record = *I;
436
John McCall93ba8572010-03-25 06:39:04 +0000437 // Figure out whether the current class has a template:
John McCall0c01d182010-03-24 05:22:00 +0000438 ClassTemplateDecl *CTD;
439
440 // A specialization of the template...
441 if (isa<ClassTemplateSpecializationDecl>(Record)) {
442 CTD = cast<ClassTemplateSpecializationDecl>(Record)
443 ->getSpecializedTemplate();
444
445 // ... or the template pattern itself.
446 } else {
447 CTD = Record->getDescribedClassTemplate();
448 if (!CTD) continue;
449 }
450
451 // It's a match.
452 if (Friend == CTD->getCanonicalDecl())
John McCall161755a2010-04-06 21:38:20 +0000453 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000454
John McCall93ba8572010-03-25 06:39:04 +0000455 // If the context isn't dependent, it can't be a dependent match.
456 if (!EC.isDependent())
457 continue;
458
John McCall0c01d182010-03-24 05:22:00 +0000459 // If the template names don't match, it can't be a dependent
Richard Smith3e4c6c42011-05-05 21:57:07 +0000460 // match.
461 if (CTD->getDeclName() != Friend->getDeclName())
John McCall0c01d182010-03-24 05:22:00 +0000462 continue;
463
464 // If the class's context can't instantiate to the friend's
465 // context, it can't be a dependent match.
466 if (!MightInstantiateTo(S, CTD->getDeclContext(),
467 Friend->getDeclContext()))
468 continue;
469
470 // Otherwise, it's a dependent match.
John McCall161755a2010-04-06 21:38:20 +0000471 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000472 }
473
John McCall0c01d182010-03-24 05:22:00 +0000474 return OnFailure;
475}
476
477/// Determines whether the given friend function matches anything in
478/// the effective context.
John McCall161755a2010-04-06 21:38:20 +0000479static AccessResult MatchesFriend(Sema &S,
480 const EffectiveContext &EC,
481 FunctionDecl *Friend) {
482 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000483
Chris Lattner5f9e2722011-07-23 10:55:15 +0000484 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall2cc26752010-03-27 06:55:49 +0000485 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
486 if (Friend == *I)
John McCall161755a2010-04-06 21:38:20 +0000487 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000488
John McCall2cc26752010-03-27 06:55:49 +0000489 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCall161755a2010-04-06 21:38:20 +0000490 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000491 }
John McCall0c01d182010-03-24 05:22:00 +0000492
John McCall2cc26752010-03-27 06:55:49 +0000493 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000494}
495
496/// Determines whether the given friend function template matches
497/// anything in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000498static AccessResult MatchesFriend(Sema &S,
499 const EffectiveContext &EC,
500 FunctionTemplateDecl *Friend) {
501 if (EC.Functions.empty()) return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000502
John McCall161755a2010-04-06 21:38:20 +0000503 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000504
Chris Lattner5f9e2722011-07-23 10:55:15 +0000505 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall2cc26752010-03-27 06:55:49 +0000506 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCall0c01d182010-03-24 05:22:00 +0000507
John McCall2cc26752010-03-27 06:55:49 +0000508 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
509 if (!FTD)
510 FTD = (*I)->getDescribedFunctionTemplate();
511 if (!FTD)
512 continue;
John McCall0c01d182010-03-24 05:22:00 +0000513
John McCall2cc26752010-03-27 06:55:49 +0000514 FTD = FTD->getCanonicalDecl();
515
516 if (Friend == FTD)
John McCall161755a2010-04-06 21:38:20 +0000517 return AR_accessible;
John McCall2cc26752010-03-27 06:55:49 +0000518
519 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCall161755a2010-04-06 21:38:20 +0000520 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000521 }
522
523 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000524}
525
526/// Determines whether the given friend declaration matches anything
527/// in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000528static AccessResult MatchesFriend(Sema &S,
529 const EffectiveContext &EC,
530 FriendDecl *FriendD) {
John McCall6102ca12010-10-16 06:59:13 +0000531 // Whitelist accesses if there's an invalid or unsupported friend
532 // declaration.
533 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCall337ec3d2010-10-12 23:13:28 +0000534 return AR_accessible;
535
John McCall32f2fb52010-03-25 18:04:51 +0000536 if (TypeSourceInfo *T = FriendD->getFriendType())
537 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCall0c01d182010-03-24 05:22:00 +0000538
539 NamedDecl *Friend
540 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCalla742db02010-03-17 20:01:29 +0000541
542 // FIXME: declarations with dependent or templated scope.
543
John McCall0c01d182010-03-24 05:22:00 +0000544 if (isa<ClassTemplateDecl>(Friend))
545 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000546
John McCall0c01d182010-03-24 05:22:00 +0000547 if (isa<FunctionTemplateDecl>(Friend))
548 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000549
John McCall0c01d182010-03-24 05:22:00 +0000550 if (isa<CXXRecordDecl>(Friend))
551 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000552
John McCall0c01d182010-03-24 05:22:00 +0000553 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
554 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000555}
556
John McCall161755a2010-04-06 21:38:20 +0000557static AccessResult GetFriendKind(Sema &S,
558 const EffectiveContext &EC,
559 const CXXRecordDecl *Class) {
560 AccessResult OnFailure = AR_inaccessible;
John McCall88b6c712010-03-17 04:58:56 +0000561
John McCalld60e22e2010-03-12 01:19:31 +0000562 // Okay, check friends.
563 for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
564 E = Class->friend_end(); I != E; ++I) {
565 FriendDecl *Friend = *I;
566
John McCalla742db02010-03-17 20:01:29 +0000567 switch (MatchesFriend(S, EC, Friend)) {
John McCall161755a2010-04-06 21:38:20 +0000568 case AR_accessible:
569 return AR_accessible;
John McCalld60e22e2010-03-12 01:19:31 +0000570
John McCall161755a2010-04-06 21:38:20 +0000571 case AR_inaccessible:
572 continue;
573
574 case AR_dependent:
575 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000576 break;
John McCalld60e22e2010-03-12 01:19:31 +0000577 }
John McCalld60e22e2010-03-12 01:19:31 +0000578 }
579
580 // That's it, give up.
John McCall88b6c712010-03-17 04:58:56 +0000581 return OnFailure;
John McCall6b2accb2010-02-10 09:31:12 +0000582}
583
John McCall8c77bcb2010-08-28 07:56:00 +0000584namespace {
585
586/// A helper class for checking for a friend which will grant access
587/// to a protected instance member.
588struct ProtectedFriendContext {
589 Sema &S;
590 const EffectiveContext &EC;
591 const CXXRecordDecl *NamingClass;
592 bool CheckDependent;
593 bool EverDependent;
594
595 /// The path down to the current base class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000596 SmallVector<const CXXRecordDecl*, 20> CurPath;
John McCall8c77bcb2010-08-28 07:56:00 +0000597
598 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
599 const CXXRecordDecl *InstanceContext,
600 const CXXRecordDecl *NamingClass)
601 : S(S), EC(EC), NamingClass(NamingClass),
602 CheckDependent(InstanceContext->isDependentContext() ||
603 NamingClass->isDependentContext()),
604 EverDependent(false) {}
605
John McCall326c8c72010-08-28 08:47:21 +0000606 /// Check classes in the current path for friendship, starting at
607 /// the given index.
608 bool checkFriendshipAlongPath(unsigned I) {
609 assert(I < CurPath.size());
610 for (unsigned E = CurPath.size(); I != E; ++I) {
611 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall8c77bcb2010-08-28 07:56:00 +0000612 case AR_accessible: return true;
613 case AR_inaccessible: continue;
614 case AR_dependent: EverDependent = true; continue;
615 }
616 }
617 return false;
618 }
619
620 /// Perform a search starting at the given class.
John McCall326c8c72010-08-28 08:47:21 +0000621 ///
622 /// PrivateDepth is the index of the last (least derived) class
623 /// along the current path such that a notional public member of
624 /// the final class in the path would have access in that class.
625 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall8c77bcb2010-08-28 07:56:00 +0000626 // If we ever reach the naming class, check the current path for
627 // friendship. We can also stop recursing because we obviously
628 // won't find the naming class there again.
John McCall326c8c72010-08-28 08:47:21 +0000629 if (Cur == NamingClass)
630 return checkFriendshipAlongPath(PrivateDepth);
John McCall8c77bcb2010-08-28 07:56:00 +0000631
632 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
633 EverDependent = true;
634
635 // Recurse into the base classes.
636 for (CXXRecordDecl::base_class_const_iterator
637 I = Cur->bases_begin(), E = Cur->bases_end(); I != E; ++I) {
638
John McCall326c8c72010-08-28 08:47:21 +0000639 // If this is private inheritance, then a public member of the
640 // base will not have any access in classes derived from Cur.
641 unsigned BasePrivateDepth = PrivateDepth;
642 if (I->getAccessSpecifier() == AS_private)
643 BasePrivateDepth = CurPath.size() - 1;
John McCall8c77bcb2010-08-28 07:56:00 +0000644
645 const CXXRecordDecl *RD;
646
647 QualType T = I->getType();
648 if (const RecordType *RT = T->getAs<RecordType>()) {
649 RD = cast<CXXRecordDecl>(RT->getDecl());
650 } else if (const InjectedClassNameType *IT
651 = T->getAs<InjectedClassNameType>()) {
652 RD = IT->getDecl();
653 } else {
654 assert(T->isDependentType() && "non-dependent base wasn't a record?");
655 EverDependent = true;
656 continue;
657 }
658
659 // Recurse. We don't need to clean up if this returns true.
John McCall326c8c72010-08-28 08:47:21 +0000660 CurPath.push_back(RD);
661 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
662 return true;
663 CurPath.pop_back();
John McCall8c77bcb2010-08-28 07:56:00 +0000664 }
665
John McCall8c77bcb2010-08-28 07:56:00 +0000666 return false;
667 }
John McCall326c8c72010-08-28 08:47:21 +0000668
669 bool findFriendship(const CXXRecordDecl *Cur) {
670 assert(CurPath.empty());
671 CurPath.push_back(Cur);
672 return findFriendship(Cur, 0);
673 }
John McCall8c77bcb2010-08-28 07:56:00 +0000674};
675}
676
677/// Search for a class P that EC is a friend of, under the constraint
John McCallb9abd8722012-04-07 03:04:20 +0000678/// InstanceContext <= P
679/// if InstanceContext exists, or else
680/// NamingClass <= P
John McCall8c77bcb2010-08-28 07:56:00 +0000681/// and with the additional restriction that a protected member of
John McCallb9abd8722012-04-07 03:04:20 +0000682/// NamingClass would have some natural access in P, which implicitly
683/// imposes the constraint that P <= NamingClass.
John McCall8c77bcb2010-08-28 07:56:00 +0000684///
John McCallb9abd8722012-04-07 03:04:20 +0000685/// This isn't quite the condition laid out in the standard.
686/// Instead of saying that a notional protected member of NamingClass
687/// would have to have some natural access in P, it says the actual
688/// target has to have some natural access in P, which opens up the
689/// possibility that the target (which is not necessarily a member
690/// of NamingClass) might be more accessible along some path not
691/// passing through it. That's really a bad idea, though, because it
John McCall8c77bcb2010-08-28 07:56:00 +0000692/// introduces two problems:
John McCallb9abd8722012-04-07 03:04:20 +0000693/// - Most importantly, it breaks encapsulation because you can
694/// access a forbidden base class's members by directly subclassing
695/// it elsewhere.
696/// - It also makes access substantially harder to compute because it
John McCall8c77bcb2010-08-28 07:56:00 +0000697/// breaks the hill-climbing algorithm: knowing that the target is
698/// accessible in some base class would no longer let you change
699/// the question solely to whether the base class is accessible,
700/// because the original target might have been more accessible
701/// because of crazy subclassing.
702/// So we don't implement that.
703static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
704 const CXXRecordDecl *InstanceContext,
705 const CXXRecordDecl *NamingClass) {
John McCallb9abd8722012-04-07 03:04:20 +0000706 assert(InstanceContext == 0 ||
707 InstanceContext->getCanonicalDecl() == InstanceContext);
John McCall8c77bcb2010-08-28 07:56:00 +0000708 assert(NamingClass->getCanonicalDecl() == NamingClass);
709
John McCallb9abd8722012-04-07 03:04:20 +0000710 // If we don't have an instance context, our constraints give us
711 // that NamingClass <= P <= NamingClass, i.e. P == NamingClass.
712 // This is just the usual friendship check.
713 if (!InstanceContext) return GetFriendKind(S, EC, NamingClass);
714
John McCall8c77bcb2010-08-28 07:56:00 +0000715 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
716 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
717 if (PRC.EverDependent) return AR_dependent;
718 return AR_inaccessible;
719}
720
John McCall161755a2010-04-06 21:38:20 +0000721static AccessResult HasAccess(Sema &S,
722 const EffectiveContext &EC,
723 const CXXRecordDecl *NamingClass,
724 AccessSpecifier Access,
725 const AccessTarget &Target) {
John McCalldb73c682010-04-02 00:03:43 +0000726 assert(NamingClass->getCanonicalDecl() == NamingClass &&
727 "declaration should be canonicalized before being passed here");
728
John McCall161755a2010-04-06 21:38:20 +0000729 if (Access == AS_public) return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000730 assert(Access == AS_private || Access == AS_protected);
731
John McCall161755a2010-04-06 21:38:20 +0000732 AccessResult OnFailure = AR_inaccessible;
733
John McCalldb73c682010-04-02 00:03:43 +0000734 for (EffectiveContext::record_iterator
735 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
736 // All the declarations in EC have been canonicalized, so pointer
737 // equality from this point on will work fine.
738 const CXXRecordDecl *ECRecord = *I;
739
740 // [B2] and [M2]
John McCall161755a2010-04-06 21:38:20 +0000741 if (Access == AS_private) {
742 if (ECRecord == NamingClass)
743 return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000744
John McCall01ebd9d2010-05-04 05:11:27 +0000745 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
746 OnFailure = AR_dependent;
747
John McCalldb73c682010-04-02 00:03:43 +0000748 // [B3] and [M3]
John McCall161755a2010-04-06 21:38:20 +0000749 } else {
750 assert(Access == AS_protected);
751 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
752 case AR_accessible: break;
753 case AR_inaccessible: continue;
754 case AR_dependent: OnFailure = AR_dependent; continue;
755 }
756
John McCall161755a2010-04-06 21:38:20 +0000757 // C++ [class.protected]p1:
758 // An additional access check beyond those described earlier in
759 // [class.access] is applied when a non-static data member or
760 // non-static member function is a protected member of its naming
761 // class. As described earlier, access to a protected member is
762 // granted because the reference occurs in a friend or member of
763 // some class C. If the access is to form a pointer to member,
764 // the nested-name-specifier shall name C or a class derived from
765 // C. All other accesses involve a (possibly implicit) object
766 // expression. In this case, the class of the object expression
767 // shall be C or a class derived from C.
768 //
John McCallb9abd8722012-04-07 03:04:20 +0000769 // We interpret this as a restriction on [M3].
770
771 // In this part of the code, 'C' is just our context class ECRecord.
772
773 // These rules are different if we don't have an instance context.
774 if (!Target.hasInstanceContext()) {
775 // If it's not an instance member, these restrictions don't apply.
776 if (!Target.isInstanceMember()) return AR_accessible;
777
778 // If it's an instance member, use the pointer-to-member rule
779 // that the naming class has to be derived from the effective
780 // context.
781
Francois Pichetb2d899e2012-04-17 12:35:05 +0000782 // Emulate a MSVC bug where the creation of pointer-to-member
783 // to protected member of base class is allowed but only from
Francois Pichet2d01f2c2012-04-18 03:24:38 +0000784 // a static function member functions.
785 if (S.getLangOpts().MicrosoftMode && !EC.Functions.empty())
786 if (CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(EC.Functions.front()))
787 if (MD->isStatic()) return AR_accessible;
Francois Pichetb2d899e2012-04-17 12:35:05 +0000788
John McCallb9abd8722012-04-07 03:04:20 +0000789 // Despite the standard's confident wording, there is a case
790 // where you can have an instance member that's neither in a
791 // pointer-to-member expression nor in a member access: when
792 // it names a field in an unevaluated context that can't be an
793 // implicit member. Pending clarification, we just apply the
794 // same naming-class restriction here.
795 // FIXME: we're probably not correctly adding the
796 // protected-member restriction when we retroactively convert
797 // an expression to being evaluated.
798
799 // We know that ECRecord derives from NamingClass. The
800 // restriction says to check whether NamingClass derives from
801 // ECRecord, but that's not really necessary: two distinct
802 // classes can't be recursively derived from each other. So
803 // along this path, we just need to check whether the classes
804 // are equal.
805 if (NamingClass == ECRecord) return AR_accessible;
806
807 // Otherwise, this context class tells us nothing; on to the next.
808 continue;
809 }
810
811 assert(Target.isInstanceMember());
812
813 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
814 if (!InstanceContext) {
815 OnFailure = AR_dependent;
816 continue;
817 }
818
John McCall161755a2010-04-06 21:38:20 +0000819 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
820 case AR_accessible: return AR_accessible;
821 case AR_inaccessible: continue;
822 case AR_dependent: OnFailure = AR_dependent; continue;
823 }
824 }
John McCalldb73c682010-04-02 00:03:43 +0000825 }
826
John McCall8c77bcb2010-08-28 07:56:00 +0000827 // [M3] and [B3] say that, if the target is protected in N, we grant
828 // access if the access occurs in a friend or member of some class P
829 // that's a subclass of N and where the target has some natural
830 // access in P. The 'member' aspect is easy to handle because P
831 // would necessarily be one of the effective-context records, and we
832 // address that above. The 'friend' aspect is completely ridiculous
833 // to implement because there are no restrictions at all on P
834 // *unless* the [class.protected] restriction applies. If it does,
835 // however, we should ignore whether the naming class is a friend,
836 // and instead rely on whether any potential P is a friend.
John McCallb9abd8722012-04-07 03:04:20 +0000837 if (Access == AS_protected && Target.isInstanceMember()) {
838 // Compute the instance context if possible.
839 const CXXRecordDecl *InstanceContext = 0;
840 if (Target.hasInstanceContext()) {
841 InstanceContext = Target.resolveInstanceContext(S);
842 if (!InstanceContext) return AR_dependent;
843 }
844
John McCall8c77bcb2010-08-28 07:56:00 +0000845 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
846 case AR_accessible: return AR_accessible;
John McCall161755a2010-04-06 21:38:20 +0000847 case AR_inaccessible: return OnFailure;
848 case AR_dependent: return AR_dependent;
849 }
John McCall1797a052010-08-28 08:10:32 +0000850 llvm_unreachable("impossible friendship kind");
John McCall161755a2010-04-06 21:38:20 +0000851 }
852
853 switch (GetFriendKind(S, EC, NamingClass)) {
854 case AR_accessible: return AR_accessible;
855 case AR_inaccessible: return OnFailure;
856 case AR_dependent: return AR_dependent;
857 }
858
859 // Silence bogus warnings
860 llvm_unreachable("impossible friendship kind");
John McCalldb73c682010-04-02 00:03:43 +0000861}
862
John McCall6b2accb2010-02-10 09:31:12 +0000863/// Finds the best path from the naming class to the declaring class,
864/// taking friend declarations into account.
865///
John McCalldb73c682010-04-02 00:03:43 +0000866/// C++0x [class.access.base]p5:
867/// A member m is accessible at the point R when named in class N if
868/// [M1] m as a member of N is public, or
869/// [M2] m as a member of N is private, and R occurs in a member or
870/// friend of class N, or
871/// [M3] m as a member of N is protected, and R occurs in a member or
872/// friend of class N, or in a member or friend of a class P
873/// derived from N, where m as a member of P is public, private,
874/// or protected, or
875/// [M4] there exists a base class B of N that is accessible at R, and
876/// m is accessible at R when named in class B.
877///
878/// C++0x [class.access.base]p4:
879/// A base class B of N is accessible at R, if
880/// [B1] an invented public member of B would be a public member of N, or
881/// [B2] R occurs in a member or friend of class N, and an invented public
882/// member of B would be a private or protected member of N, or
883/// [B3] R occurs in a member or friend of a class P derived from N, and an
884/// invented public member of B would be a private or protected member
885/// of P, or
886/// [B4] there exists a class S such that B is a base class of S accessible
887/// at R and S is a base class of N accessible at R.
888///
889/// Along a single inheritance path we can restate both of these
890/// iteratively:
891///
892/// First, we note that M1-4 are equivalent to B1-4 if the member is
893/// treated as a notional base of its declaring class with inheritance
894/// access equivalent to the member's access. Therefore we need only
895/// ask whether a class B is accessible from a class N in context R.
896///
897/// Let B_1 .. B_n be the inheritance path in question (i.e. where
898/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
899/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
900/// closest accessible base in the path:
901/// Access(a, b) = (* access on the base specifier from a to b *)
902/// Merge(a, forbidden) = forbidden
903/// Merge(a, private) = forbidden
904/// Merge(a, b) = min(a,b)
905/// Accessible(c, forbidden) = false
906/// Accessible(c, private) = (R is c) || IsFriend(c, R)
907/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
908/// Accessible(c, public) = true
909/// ACAB(n) = public
910/// ACAB(i) =
911/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
912/// if Accessible(B_i, AccessToBase) then public else AccessToBase
913///
914/// B is an accessible base of N at R iff ACAB(1) = public.
915///
John McCall161755a2010-04-06 21:38:20 +0000916/// \param FinalAccess the access of the "final step", or AS_public if
John McCall7aceaf82010-03-18 23:49:19 +0000917/// there is no final step.
John McCall6b2accb2010-02-10 09:31:12 +0000918/// \return null if friendship is dependent
919static CXXBasePath *FindBestPath(Sema &S,
920 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +0000921 AccessTarget &Target,
John McCall7aceaf82010-03-18 23:49:19 +0000922 AccessSpecifier FinalAccess,
John McCall6b2accb2010-02-10 09:31:12 +0000923 CXXBasePaths &Paths) {
924 // Derive the paths to the desired base.
John McCall161755a2010-04-06 21:38:20 +0000925 const CXXRecordDecl *Derived = Target.getNamingClass();
926 const CXXRecordDecl *Base = Target.getDeclaringClass();
927
928 // FIXME: fail correctly when there are dependent paths.
929 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
930 Paths);
John McCall6b2accb2010-02-10 09:31:12 +0000931 assert(isDerived && "derived class not actually derived from base");
932 (void) isDerived;
933
934 CXXBasePath *BestPath = 0;
935
John McCall7aceaf82010-03-18 23:49:19 +0000936 assert(FinalAccess != AS_none && "forbidden access after declaring class");
937
John McCall0c01d182010-03-24 05:22:00 +0000938 bool AnyDependent = false;
939
John McCall6b2accb2010-02-10 09:31:12 +0000940 // Derive the friend-modified access along each path.
941 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
942 PI != PE; ++PI) {
John McCall161755a2010-04-06 21:38:20 +0000943 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +0000944
945 // Walk through the path backwards.
John McCall7aceaf82010-03-18 23:49:19 +0000946 AccessSpecifier PathAccess = FinalAccess;
John McCall6b2accb2010-02-10 09:31:12 +0000947 CXXBasePath::iterator I = PI->end(), E = PI->begin();
948 while (I != E) {
949 --I;
950
John McCall7aceaf82010-03-18 23:49:19 +0000951 assert(PathAccess != AS_none);
952
953 // If the declaration is a private member of a base class, there
954 // is no level of friendship in derived classes that can make it
955 // accessible.
956 if (PathAccess == AS_private) {
957 PathAccess = AS_none;
958 break;
959 }
960
John McCall161755a2010-04-06 21:38:20 +0000961 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
962
John McCall6b2accb2010-02-10 09:31:12 +0000963 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalldb73c682010-04-02 00:03:43 +0000964 PathAccess = std::max(PathAccess, BaseAccess);
John McCall161755a2010-04-06 21:38:20 +0000965
966 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
967 case AR_inaccessible: break;
968 case AR_accessible:
969 PathAccess = AS_public;
970
971 // Future tests are not against members and so do not have
972 // instance context.
973 Target.suppressInstanceContext();
974 break;
975 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +0000976 AnyDependent = true;
977 goto Next;
John McCall6b2accb2010-02-10 09:31:12 +0000978 }
John McCall6b2accb2010-02-10 09:31:12 +0000979 }
980
981 // Note that we modify the path's Access field to the
982 // friend-modified access.
983 if (BestPath == 0 || PathAccess < BestPath->Access) {
984 BestPath = &*PI;
985 BestPath->Access = PathAccess;
John McCall0c01d182010-03-24 05:22:00 +0000986
987 // Short-circuit if we found a public path.
988 if (BestPath->Access == AS_public)
989 return BestPath;
John McCall6b2accb2010-02-10 09:31:12 +0000990 }
John McCall0c01d182010-03-24 05:22:00 +0000991
992 Next: ;
John McCall6b2accb2010-02-10 09:31:12 +0000993 }
994
John McCall0c01d182010-03-24 05:22:00 +0000995 assert((!BestPath || BestPath->Access != AS_public) &&
996 "fell out of loop with public path");
997
998 // We didn't find a public path, but at least one path was subject
999 // to dependent friendship, so delay the check.
1000 if (AnyDependent)
1001 return 0;
1002
John McCall6b2accb2010-02-10 09:31:12 +00001003 return BestPath;
1004}
1005
John McCallfe24e052010-09-03 04:56:05 +00001006/// Given that an entity has protected natural access, check whether
1007/// access might be denied because of the protected member access
1008/// restriction.
1009///
1010/// \return true if a note was emitted
1011static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
1012 AccessTarget &Target) {
1013 // Only applies to instance accesses.
John McCallb9abd8722012-04-07 03:04:20 +00001014 if (!Target.isInstanceMember())
John McCallfe24e052010-09-03 04:56:05 +00001015 return false;
John McCallfe24e052010-09-03 04:56:05 +00001016
John McCallb9abd8722012-04-07 03:04:20 +00001017 assert(Target.isMemberAccess());
1018
1019 const CXXRecordDecl *NamingClass = Target.getNamingClass();
1020 NamingClass = NamingClass->getCanonicalDecl();
John McCallfe24e052010-09-03 04:56:05 +00001021
1022 for (EffectiveContext::record_iterator
1023 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
1024 const CXXRecordDecl *ECRecord = *I;
John McCallb9abd8722012-04-07 03:04:20 +00001025 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
John McCallfe24e052010-09-03 04:56:05 +00001026 case AR_accessible: break;
1027 case AR_inaccessible: continue;
1028 case AR_dependent: continue;
1029 }
1030
1031 // The effective context is a subclass of the declaring class.
John McCallb9abd8722012-04-07 03:04:20 +00001032 // Check whether the [class.protected] restriction is limiting
1033 // access.
John McCallfe24e052010-09-03 04:56:05 +00001034
1035 // To get this exactly right, this might need to be checked more
1036 // holistically; it's not necessarily the case that gaining
1037 // access here would grant us access overall.
1038
John McCallb9abd8722012-04-07 03:04:20 +00001039 NamedDecl *D = Target.getTargetDecl();
1040
1041 // If we don't have an instance context, [class.protected] says the
1042 // naming class has to equal the context class.
1043 if (!Target.hasInstanceContext()) {
1044 // If it does, the restriction doesn't apply.
1045 if (NamingClass == ECRecord) continue;
1046
1047 // TODO: it would be great to have a fixit here, since this is
1048 // such an obvious error.
1049 S.Diag(D->getLocation(), diag::note_access_protected_restricted_noobject)
1050 << S.Context.getTypeDeclType(ECRecord);
1051 return true;
1052 }
1053
John McCallfe24e052010-09-03 04:56:05 +00001054 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
1055 assert(InstanceContext && "diagnosing dependent access");
1056
1057 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
1058 case AR_accessible: continue;
1059 case AR_dependent: continue;
1060 case AR_inaccessible:
John McCallb9abd8722012-04-07 03:04:20 +00001061 break;
1062 }
1063
1064 // Okay, the restriction seems to be what's limiting us.
1065
1066 // Use a special diagnostic for constructors and destructors.
1067 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D) ||
1068 (isa<FunctionTemplateDecl>(D) &&
1069 isa<CXXConstructorDecl>(
1070 cast<FunctionTemplateDecl>(D)->getTemplatedDecl()))) {
1071 S.Diag(D->getLocation(), diag::note_access_protected_restricted_ctordtor)
1072 << isa<CXXDestructorDecl>(D);
John McCallfe24e052010-09-03 04:56:05 +00001073 return true;
1074 }
John McCallb9abd8722012-04-07 03:04:20 +00001075
1076 // Otherwise, use the generic diagnostic.
1077 S.Diag(D->getLocation(), diag::note_access_protected_restricted_object)
1078 << S.Context.getTypeDeclType(ECRecord);
1079 return true;
John McCallfe24e052010-09-03 04:56:05 +00001080 }
1081
1082 return false;
1083}
1084
John McCall6b2accb2010-02-10 09:31:12 +00001085/// Diagnose the path which caused the given declaration or base class
1086/// to become inaccessible.
1087static void DiagnoseAccessPath(Sema &S,
1088 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +00001089 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001090 AccessSpecifier Access = Entity.getAccess();
John McCalldb73c682010-04-02 00:03:43 +00001091
John McCall161755a2010-04-06 21:38:20 +00001092 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
1093 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCalldb73c682010-04-02 00:03:43 +00001094
John McCall92f88312010-01-23 00:46:32 +00001095 // Easy case: the decl's natural access determined its path access.
John McCall6b2accb2010-02-10 09:31:12 +00001096 // We have to check against AS_private here in case Access is AS_none,
1097 // indicating a non-public member of a private base class.
John McCall6b2accb2010-02-10 09:31:12 +00001098 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
John McCall161755a2010-04-06 21:38:20 +00001099 switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
1100 case AR_inaccessible: {
John McCallfe24e052010-09-03 04:56:05 +00001101 if (Access == AS_protected &&
1102 TryDiagnoseProtectedAccess(S, EC, Entity))
1103 return;
1104
John McCallaa56a662010-10-20 08:15:06 +00001105 // Find an original declaration.
1106 while (D->isOutOfLine()) {
1107 NamedDecl *PrevDecl = 0;
Richard Smith162e1c12011-04-15 14:24:37 +00001108 if (VarDecl *VD = dyn_cast<VarDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001109 PrevDecl = VD->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001110 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001111 PrevDecl = FD->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001112 else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001113 PrevDecl = TND->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001114 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
John McCallaa56a662010-10-20 08:15:06 +00001115 if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1116 break;
Douglas Gregoref96ee02012-01-14 16:38:05 +00001117 PrevDecl = TD->getPreviousDecl();
John McCallaa56a662010-10-20 08:15:06 +00001118 }
1119 if (!PrevDecl) break;
1120 D = PrevDecl;
1121 }
1122
1123 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
1124 Decl *ImmediateChild;
1125 if (D->getDeclContext() == DeclaringClass)
1126 ImmediateChild = D;
1127 else {
1128 DeclContext *DC = D->getDeclContext();
1129 while (DC->getParent() != DeclaringClass)
1130 DC = DC->getParent();
1131 ImmediateChild = cast<Decl>(DC);
1132 }
1133
1134 // Check whether there's an AccessSpecDecl preceding this in the
1135 // chain of the DeclContext.
1136 bool Implicit = true;
1137 for (CXXRecordDecl::decl_iterator
1138 I = DeclaringClass->decls_begin(), E = DeclaringClass->decls_end();
1139 I != E; ++I) {
1140 if (*I == ImmediateChild) break;
1141 if (isa<AccessSpecDecl>(*I)) {
1142 Implicit = false;
1143 break;
1144 }
1145 }
1146
John McCall6b2accb2010-02-10 09:31:12 +00001147 S.Diag(D->getLocation(), diag::note_access_natural)
1148 << (unsigned) (Access == AS_protected)
John McCallaa56a662010-10-20 08:15:06 +00001149 << Implicit;
John McCall6b2accb2010-02-10 09:31:12 +00001150 return;
1151 }
1152
John McCall161755a2010-04-06 21:38:20 +00001153 case AR_accessible: break;
John McCall6b2accb2010-02-10 09:31:12 +00001154
John McCall161755a2010-04-06 21:38:20 +00001155 case AR_dependent:
1156 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001157 }
1158 }
1159
1160 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001161 CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
John McCall6b2accb2010-02-10 09:31:12 +00001162
1163 CXXBasePath::iterator I = Path.end(), E = Path.begin();
1164 while (I != E) {
1165 --I;
1166
1167 const CXXBaseSpecifier *BS = I->Base;
1168 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
1169
1170 // If this is public inheritance, or the derived class is a friend,
1171 // skip this step.
1172 if (BaseAccess == AS_public)
1173 continue;
1174
1175 switch (GetFriendKind(S, EC, I->Class)) {
John McCall161755a2010-04-06 21:38:20 +00001176 case AR_accessible: continue;
1177 case AR_inaccessible: break;
1178 case AR_dependent:
1179 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001180 }
1181
1182 // Check whether this base specifier is the tighest point
1183 // constraining access. We have to check against AS_private for
1184 // the same reasons as above.
1185 if (BaseAccess == AS_private || BaseAccess >= Access) {
1186
1187 // We're constrained by inheritance, but we want to say
1188 // "declared private here" if we're diagnosing a hierarchy
1189 // conversion and this is the final step.
1190 unsigned diagnostic;
1191 if (D) diagnostic = diag::note_access_constrained_by_path;
1192 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
1193 else diagnostic = diag::note_access_constrained_by_path;
1194
1195 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
1196 << BS->getSourceRange()
1197 << (BaseAccess == AS_protected)
1198 << (BS->getAccessSpecifierAsWritten() == AS_none);
Douglas Gregor76ef6582010-05-28 04:34:55 +00001199
1200 if (D)
1201 S.Diag(D->getLocation(), diag::note_field_decl);
1202
John McCall6b2accb2010-02-10 09:31:12 +00001203 return;
1204 }
1205 }
1206
1207 llvm_unreachable("access not apparently constrained by path");
1208}
1209
John McCall58e6f342010-03-16 05:22:47 +00001210static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall6b2accb2010-02-10 09:31:12 +00001211 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +00001212 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001213 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCall161755a2010-04-06 21:38:20 +00001214 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
1215 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
John McCalldb73c682010-04-02 00:03:43 +00001216
1217 S.Diag(Loc, Entity.getDiag())
1218 << (Entity.getAccess() == AS_protected)
1219 << (D ? D->getDeclName() : DeclarationName())
1220 << S.Context.getTypeDeclType(NamingClass)
1221 << S.Context.getTypeDeclType(DeclaringClass);
1222 DiagnoseAccessPath(S, EC, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001223}
1224
Francois Pichetb2ee8302011-05-23 03:43:44 +00001225/// MSVC has a bug where if during an using declaration name lookup,
1226/// the declaration found is unaccessible (private) and that declaration
1227/// was bring into scope via another using declaration whose target
1228/// declaration is accessible (public) then no error is generated.
1229/// Example:
1230/// class A {
1231/// public:
1232/// int f();
1233/// };
1234/// class B : public A {
1235/// private:
1236/// using A::f;
1237/// };
1238/// class C : public B {
1239/// private:
1240/// using B::f;
1241/// };
1242///
1243/// Here, B::f is private so this should fail in Standard C++, but
1244/// because B::f refers to A::f which is public MSVC accepts it.
1245static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S,
1246 SourceLocation AccessLoc,
1247 AccessTarget &Entity) {
1248 if (UsingShadowDecl *Shadow =
1249 dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) {
1250 const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl();
1251 if (Entity.getTargetDecl()->getAccess() == AS_private &&
1252 (OrigDecl->getAccess() == AS_public ||
1253 OrigDecl->getAccess() == AS_protected)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00001254 S.Diag(AccessLoc, diag::ext_ms_using_declaration_inaccessible)
Francois Pichetb2ee8302011-05-23 03:43:44 +00001255 << Shadow->getUsingDecl()->getQualifiedNameAsString()
1256 << OrigDecl->getQualifiedNameAsString();
1257 return true;
1258 }
1259 }
1260 return false;
1261}
1262
John McCalldb73c682010-04-02 00:03:43 +00001263/// Determines whether the accessed entity is accessible. Public members
1264/// have been weeded out by this point.
John McCall161755a2010-04-06 21:38:20 +00001265static AccessResult IsAccessible(Sema &S,
1266 const EffectiveContext &EC,
1267 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001268 // Determine the actual naming class.
1269 CXXRecordDecl *NamingClass = Entity.getNamingClass();
1270 while (NamingClass->isAnonymousStructOrUnion())
1271 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
1272 NamingClass = NamingClass->getCanonicalDecl();
John McCall6b2accb2010-02-10 09:31:12 +00001273
John McCalldb73c682010-04-02 00:03:43 +00001274 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1275 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1276
1277 // Before we try to recalculate access paths, try to white-list
1278 // accesses which just trade in on the final step, i.e. accesses
1279 // which don't require [M4] or [B4]. These are by far the most
John McCall161755a2010-04-06 21:38:20 +00001280 // common forms of privileged access.
John McCalldb73c682010-04-02 00:03:43 +00001281 if (UnprivilegedAccess != AS_none) {
John McCall161755a2010-04-06 21:38:20 +00001282 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1283 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +00001284 // This is actually an interesting policy decision. We don't
1285 // *have* to delay immediately here: we can do the full access
1286 // calculation in the hope that friendship on some intermediate
1287 // class will make the declaration accessible non-dependently.
1288 // But that's not cheap, and odds are very good (note: assertion
1289 // made without data) that the friend declaration will determine
1290 // access.
John McCall161755a2010-04-06 21:38:20 +00001291 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001292
John McCall161755a2010-04-06 21:38:20 +00001293 case AR_accessible: return AR_accessible;
1294 case AR_inaccessible: break;
John McCalldb73c682010-04-02 00:03:43 +00001295 }
1296 }
1297
John McCall161755a2010-04-06 21:38:20 +00001298 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +00001299
John McCalldb73c682010-04-02 00:03:43 +00001300 // We lower member accesses to base accesses by pretending that the
1301 // member is a base class of its declaring class.
1302 AccessSpecifier FinalAccess;
1303
John McCall6b2accb2010-02-10 09:31:12 +00001304 if (Entity.isMemberAccess()) {
John McCalldb73c682010-04-02 00:03:43 +00001305 // Determine if the declaration is accessible from EC when named
1306 // in its declaring class.
John McCall6b2accb2010-02-10 09:31:12 +00001307 NamedDecl *Target = Entity.getTargetDecl();
John McCall161755a2010-04-06 21:38:20 +00001308 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall6b2accb2010-02-10 09:31:12 +00001309
John McCalldb73c682010-04-02 00:03:43 +00001310 FinalAccess = Target->getAccess();
John McCall161755a2010-04-06 21:38:20 +00001311 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1312 case AR_accessible:
1313 FinalAccess = AS_public;
1314 break;
1315 case AR_inaccessible: break;
1316 case AR_dependent: return AR_dependent; // see above
John McCall6b2accb2010-02-10 09:31:12 +00001317 }
1318
John McCalldb73c682010-04-02 00:03:43 +00001319 if (DeclaringClass == NamingClass)
John McCall161755a2010-04-06 21:38:20 +00001320 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
1321
1322 Entity.suppressInstanceContext();
John McCalldb73c682010-04-02 00:03:43 +00001323 } else {
1324 FinalAccess = AS_public;
John McCall6b2accb2010-02-10 09:31:12 +00001325 }
1326
John McCall161755a2010-04-06 21:38:20 +00001327 assert(Entity.getDeclaringClass() != NamingClass);
John McCall6b2accb2010-02-10 09:31:12 +00001328
1329 // Append the declaration's access if applicable.
1330 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001331 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCall0c01d182010-03-24 05:22:00 +00001332 if (!Path)
John McCall161755a2010-04-06 21:38:20 +00001333 return AR_dependent;
John McCall92f88312010-01-23 00:46:32 +00001334
John McCalldb73c682010-04-02 00:03:43 +00001335 assert(Path->Access <= UnprivilegedAccess &&
1336 "access along best path worse than direct?");
1337 if (Path->Access == AS_public)
John McCall161755a2010-04-06 21:38:20 +00001338 return AR_accessible;
1339 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +00001340}
1341
John McCall161755a2010-04-06 21:38:20 +00001342static void DelayDependentAccess(Sema &S,
1343 const EffectiveContext &EC,
1344 SourceLocation Loc,
1345 const AccessTarget &Entity) {
John McCall0c01d182010-03-24 05:22:00 +00001346 assert(EC.isDependent() && "delaying non-dependent access");
John McCall7ad650f2010-03-24 07:46:06 +00001347 DeclContext *DC = EC.getInnerContext();
John McCall0c01d182010-03-24 05:22:00 +00001348 assert(DC->isDependentContext() && "delaying non-dependent access");
1349 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1350 Loc,
1351 Entity.isMemberAccess(),
1352 Entity.getAccess(),
1353 Entity.getTargetDecl(),
1354 Entity.getNamingClass(),
John McCall161755a2010-04-06 21:38:20 +00001355 Entity.getBaseObjectType(),
John McCall0c01d182010-03-24 05:22:00 +00001356 Entity.getDiag());
John McCall92f88312010-01-23 00:46:32 +00001357}
1358
John McCall6b2accb2010-02-10 09:31:12 +00001359/// Checks access to an entity from the given effective context.
John McCall161755a2010-04-06 21:38:20 +00001360static AccessResult CheckEffectiveAccess(Sema &S,
1361 const EffectiveContext &EC,
1362 SourceLocation Loc,
1363 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001364 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall92f88312010-01-23 00:46:32 +00001365
David Blaikie4e4d0842012-03-11 07:00:24 +00001366 if (S.getLangOpts().MicrosoftMode &&
Francois Pichetb2ee8302011-05-23 03:43:44 +00001367 IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity))
1368 return AR_accessible;
1369
John McCalldb73c682010-04-02 00:03:43 +00001370 switch (IsAccessible(S, EC, Entity)) {
John McCall161755a2010-04-06 21:38:20 +00001371 case AR_dependent:
1372 DelayDependentAccess(S, EC, Loc, Entity);
1373 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001374
John McCall161755a2010-04-06 21:38:20 +00001375 case AR_inaccessible:
John McCalldb73c682010-04-02 00:03:43 +00001376 if (!Entity.isQuiet())
1377 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCall161755a2010-04-06 21:38:20 +00001378 return AR_inaccessible;
John McCalldb73c682010-04-02 00:03:43 +00001379
John McCall161755a2010-04-06 21:38:20 +00001380 case AR_accessible:
1381 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +00001382 }
1383
John McCall161755a2010-04-06 21:38:20 +00001384 // silence unnecessary warning
1385 llvm_unreachable("invalid access result");
John McCall6b2accb2010-02-10 09:31:12 +00001386}
John McCall92f88312010-01-23 00:46:32 +00001387
John McCall6b2accb2010-02-10 09:31:12 +00001388static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCall161755a2010-04-06 21:38:20 +00001389 AccessTarget &Entity) {
John McCall6b2accb2010-02-10 09:31:12 +00001390 // If the access path is public, it's accessible everywhere.
1391 if (Entity.getAccess() == AS_public)
1392 return Sema::AR_accessible;
John McCall92f88312010-01-23 00:46:32 +00001393
Chandler Carruth926c4b42010-06-28 08:39:25 +00001394 if (S.SuppressAccessChecking)
1395 return Sema::AR_accessible;
1396
John McCalleee1d542011-02-14 07:13:47 +00001397 // If we're currently parsing a declaration, we may need to delay
1398 // access control checking, because our effective context might be
1399 // different based on what the declaration comes out as.
1400 //
1401 // For example, we might be parsing a declaration with a scope
1402 // specifier, like this:
1403 // A::private_type A::foo() { ... }
1404 //
1405 // Or we might be parsing something that will turn out to be a friend:
1406 // void foo(A::private_type);
1407 // void B::foo(A::private_type);
1408 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1409 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall6b2accb2010-02-10 09:31:12 +00001410 return Sema::AR_delayed;
John McCall92f88312010-01-23 00:46:32 +00001411 }
1412
John McCall161755a2010-04-06 21:38:20 +00001413 EffectiveContext EC(S.CurContext);
1414 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1415 case AR_accessible: return Sema::AR_accessible;
1416 case AR_inaccessible: return Sema::AR_inaccessible;
1417 case AR_dependent: return Sema::AR_dependent;
1418 }
1419 llvm_unreachable("falling off end");
John McCall92f88312010-01-23 00:46:32 +00001420}
1421
John McCall4bfd6802011-02-15 22:51:53 +00001422void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *decl) {
1423 // Access control for names used in the declarations of functions
1424 // and function templates should normally be evaluated in the context
1425 // of the declaration, just in case it's a friend of something.
1426 // However, this does not apply to local extern declarations.
1427
1428 DeclContext *DC = decl->getDeclContext();
1429 if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
1430 if (!DC->isFunctionOrMethod()) DC = fn;
1431 } else if (FunctionTemplateDecl *fnt = dyn_cast<FunctionTemplateDecl>(decl)) {
1432 // Never a local declaration.
1433 DC = fnt->getTemplatedDecl();
1434 }
1435
Chandler Carruth630eb012010-04-18 08:23:21 +00001436 EffectiveContext EC(DC);
John McCall2f514482010-01-27 03:50:35 +00001437
John McCall161755a2010-04-06 21:38:20 +00001438 AccessTarget Target(DD.getAccessData());
1439
1440 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall2f514482010-01-27 03:50:35 +00001441 DD.Triggered = true;
1442}
1443
John McCall0c01d182010-03-24 05:22:00 +00001444void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1445 const MultiLevelTemplateArgumentList &TemplateArgs) {
1446 SourceLocation Loc = DD.getAccessLoc();
1447 AccessSpecifier Access = DD.getAccess();
1448
1449 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1450 TemplateArgs);
1451 if (!NamingD) return;
1452 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1453 TemplateArgs);
1454 if (!TargetD) return;
1455
1456 if (DD.isAccessToMember()) {
John McCall161755a2010-04-06 21:38:20 +00001457 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1458 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1459 QualType BaseObjectType = DD.getAccessBaseObjectType();
1460 if (!BaseObjectType.isNull()) {
1461 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1462 DeclarationName());
1463 if (BaseObjectType.isNull()) return;
1464 }
1465
1466 AccessTarget Entity(Context,
1467 AccessTarget::Member,
1468 NamingClass,
1469 DeclAccessPair::make(TargetDecl, Access),
1470 BaseObjectType);
John McCall0c01d182010-03-24 05:22:00 +00001471 Entity.setDiag(DD.getDiagnostic());
1472 CheckAccess(*this, Loc, Entity);
1473 } else {
John McCall161755a2010-04-06 21:38:20 +00001474 AccessTarget Entity(Context,
1475 AccessTarget::Base,
1476 cast<CXXRecordDecl>(TargetD),
1477 cast<CXXRecordDecl>(NamingD),
1478 Access);
John McCall0c01d182010-03-24 05:22:00 +00001479 Entity.setDiag(DD.getDiagnostic());
1480 CheckAccess(*this, Loc, Entity);
1481 }
1482}
1483
John McCall6b2accb2010-02-10 09:31:12 +00001484Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001485 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001486 if (!getLangOpts().AccessControl ||
John McCall58e6f342010-03-16 05:22:47 +00001487 !E->getNamingClass() ||
John McCall9aa472c2010-03-19 07:35:19 +00001488 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001489 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001490
John McCall161755a2010-04-06 21:38:20 +00001491 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1492 Found, QualType());
John McCall58e6f342010-03-16 05:22:47 +00001493 Entity.setDiag(diag::err_access) << E->getSourceRange();
1494
1495 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001496}
1497
1498/// Perform access-control checking on a previously-unresolved member
1499/// access which has now been resolved to a member.
John McCall6b2accb2010-02-10 09:31:12 +00001500Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001501 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001502 if (!getLangOpts().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001503 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001504 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001505
John McCall161755a2010-04-06 21:38:20 +00001506 QualType BaseType = E->getBaseType();
1507 if (E->isArrow())
1508 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1509
1510 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1511 Found, BaseType);
John McCall58e6f342010-03-16 05:22:47 +00001512 Entity.setDiag(diag::err_access) << E->getSourceRange();
1513
1514 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001515}
1516
John McCall12d8d802012-04-09 20:53:23 +00001517/// Is the given special member function accessible for the purposes of
1518/// deciding whether to define a special member function as deleted?
1519bool Sema::isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl,
1520 AccessSpecifier access,
1521 QualType objectType) {
1522 // Fast path.
1523 if (access == AS_public || !getLangOpts().AccessControl) return true;
1524
1525 AccessTarget entity(Context, AccessTarget::Member, decl->getParent(),
1526 DeclAccessPair::make(decl, access), objectType);
1527
1528 // Suppress diagnostics.
1529 entity.setDiag(PDiag());
1530
1531 switch (CheckAccess(*this, SourceLocation(), entity)) {
1532 case AR_accessible: return true;
1533 case AR_inaccessible: return false;
1534 case AR_dependent: llvm_unreachable("dependent for =delete computation");
1535 case AR_delayed: llvm_unreachable("cannot delay =delete computation");
1536 }
1537 llvm_unreachable("bad access result");
1538}
1539
John McCall6b2accb2010-02-10 09:31:12 +00001540Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall58e6f342010-03-16 05:22:47 +00001541 CXXDestructorDecl *Dtor,
John McCallb9abd8722012-04-07 03:04:20 +00001542 const PartialDiagnostic &PDiag,
1543 QualType ObjectTy) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001544 if (!getLangOpts().AccessControl)
John McCall6b2accb2010-02-10 09:31:12 +00001545 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001546
John McCall58e6f342010-03-16 05:22:47 +00001547 // There's never a path involved when checking implicit destructor access.
John McCall4f9506a2010-02-02 08:45:54 +00001548 AccessSpecifier Access = Dtor->getAccess();
1549 if (Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001550 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001551
John McCall58e6f342010-03-16 05:22:47 +00001552 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCallb9abd8722012-04-07 03:04:20 +00001553 if (ObjectTy.isNull()) ObjectTy = Context.getTypeDeclType(NamingClass);
1554
John McCall161755a2010-04-06 21:38:20 +00001555 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1556 DeclAccessPair::make(Dtor, Access),
John McCallb9abd8722012-04-07 03:04:20 +00001557 ObjectTy);
John McCall58e6f342010-03-16 05:22:47 +00001558 Entity.setDiag(PDiag); // TODO: avoid copy
1559
1560 return CheckAccess(*this, Loc, Entity);
John McCall4f9506a2010-02-02 08:45:54 +00001561}
1562
John McCallb13b7372010-02-01 03:16:54 +00001563/// Checks access to a constructor.
John McCall6b2accb2010-02-10 09:31:12 +00001564Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00001565 CXXConstructorDecl *Constructor,
1566 const InitializedEntity &Entity,
1567 AccessSpecifier Access,
1568 bool IsCopyBindingRefToTemp) {
John McCallb9abd8722012-04-07 03:04:20 +00001569 if (!getLangOpts().AccessControl || Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001570 return AR_accessible;
John McCallb13b7372010-02-01 03:16:54 +00001571
Sean Huntb320e0c2011-06-10 03:50:41 +00001572 PartialDiagnostic PD(PDiag());
Anders Carlsson9a68a672010-04-21 18:47:17 +00001573 switch (Entity.getKind()) {
1574 default:
Sean Huntb320e0c2011-06-10 03:50:41 +00001575 PD = PDiag(IsCopyBindingRefToTemp
1576 ? diag::ext_rvalue_to_reference_access_ctor
1577 : diag::err_access_ctor);
1578
Anders Carlsson9a68a672010-04-21 18:47:17 +00001579 break;
John McCall58e6f342010-03-16 05:22:47 +00001580
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001581 case InitializedEntity::EK_Base:
Sean Huntb320e0c2011-06-10 03:50:41 +00001582 PD = PDiag(diag::err_access_base_ctor);
1583 PD << Entity.isInheritedVirtualBase()
1584 << Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor);
Anders Carlsson9a68a672010-04-21 18:47:17 +00001585 break;
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001586
Anders Carlssonb99c6662010-04-21 20:28:29 +00001587 case InitializedEntity::EK_Member: {
1588 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Sean Huntb320e0c2011-06-10 03:50:41 +00001589 PD = PDiag(diag::err_access_field_ctor);
1590 PD << Field->getType() << getSpecialMember(Constructor);
Anders Carlssonb99c6662010-04-21 20:28:29 +00001591 break;
1592 }
Anders Carlsson9a68a672010-04-21 18:47:17 +00001593
Douglas Gregor47736542012-02-15 16:57:26 +00001594 case InitializedEntity::EK_LambdaCapture: {
1595 const VarDecl *Var = Entity.getCapturedVar();
1596 PD = PDiag(diag::err_access_lambda_capture);
1597 PD << Var->getName() << Entity.getType() << getSpecialMember(Constructor);
1598 break;
1599 }
1600
Anders Carlsson711f34a2010-04-21 19:52:01 +00001601 }
1602
John McCallb9abd8722012-04-07 03:04:20 +00001603 return CheckConstructorAccess(UseLoc, Constructor, Entity, Access, PD);
Sean Huntb320e0c2011-06-10 03:50:41 +00001604}
1605
1606/// Checks access to a constructor.
1607Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
1608 CXXConstructorDecl *Constructor,
John McCallb9abd8722012-04-07 03:04:20 +00001609 const InitializedEntity &Entity,
Sean Huntb320e0c2011-06-10 03:50:41 +00001610 AccessSpecifier Access,
John McCallb9abd8722012-04-07 03:04:20 +00001611 const PartialDiagnostic &PD) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001612 if (!getLangOpts().AccessControl ||
Sean Huntb320e0c2011-06-10 03:50:41 +00001613 Access == AS_public)
1614 return AR_accessible;
1615
1616 CXXRecordDecl *NamingClass = Constructor->getParent();
John McCallb9abd8722012-04-07 03:04:20 +00001617
1618 // Initializing a base sub-object is an instance method call on an
1619 // object of the derived class. Otherwise, we have an instance method
1620 // call on an object of the constructed type.
1621 CXXRecordDecl *ObjectClass;
1622 if (Entity.getKind() == InitializedEntity::EK_Base) {
1623 ObjectClass = cast<CXXConstructorDecl>(CurContext)->getParent();
1624 } else {
1625 ObjectClass = NamingClass;
1626 }
1627
Sean Huntb320e0c2011-06-10 03:50:41 +00001628 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1629 DeclAccessPair::make(Constructor, Access),
John McCallb9abd8722012-04-07 03:04:20 +00001630 Context.getTypeDeclType(ObjectClass));
Sean Huntb320e0c2011-06-10 03:50:41 +00001631 AccessEntity.setDiag(PD);
1632
Anders Carlsson9a68a672010-04-21 18:47:17 +00001633 return CheckAccess(*this, UseLoc, AccessEntity);
John McCallb9abd8722012-04-07 03:04:20 +00001634}
John McCallb13b7372010-02-01 03:16:54 +00001635
John McCallb0207482010-03-16 06:11:48 +00001636/// Checks direct (i.e. non-inherited) access to an arbitrary class
1637/// member.
1638Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
1639 NamedDecl *Target,
1640 const PartialDiagnostic &Diag) {
1641 AccessSpecifier Access = Target->getAccess();
David Blaikie4e4d0842012-03-11 07:00:24 +00001642 if (!getLangOpts().AccessControl ||
John McCallb0207482010-03-16 06:11:48 +00001643 Access == AS_public)
1644 return AR_accessible;
1645
1646 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
John McCall161755a2010-04-06 21:38:20 +00001647 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1648 DeclAccessPair::make(Target, Access),
1649 QualType());
John McCallb0207482010-03-16 06:11:48 +00001650 Entity.setDiag(Diag);
1651 return CheckAccess(*this, UseLoc, Entity);
1652}
1653
1654
John McCall90c8c572010-03-18 08:19:33 +00001655/// Checks access to an overloaded operator new or delete.
1656Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1657 SourceRange PlacementRange,
1658 CXXRecordDecl *NamingClass,
Sean Huntcb45a0f2011-05-12 22:46:25 +00001659 DeclAccessPair Found,
1660 bool Diagnose) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001661 if (!getLangOpts().AccessControl ||
John McCall90c8c572010-03-18 08:19:33 +00001662 !NamingClass ||
John McCall9aa472c2010-03-19 07:35:19 +00001663 Found.getAccess() == AS_public)
John McCall90c8c572010-03-18 08:19:33 +00001664 return AR_accessible;
1665
John McCall161755a2010-04-06 21:38:20 +00001666 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1667 QualType());
Sean Huntcb45a0f2011-05-12 22:46:25 +00001668 if (Diagnose)
1669 Entity.setDiag(diag::err_access)
1670 << PlacementRange;
John McCall90c8c572010-03-18 08:19:33 +00001671
1672 return CheckAccess(*this, OpLoc, Entity);
1673}
1674
John McCallb13b7372010-02-01 03:16:54 +00001675/// Checks access to an overloaded member operator, including
1676/// conversion operators.
John McCall6b2accb2010-02-10 09:31:12 +00001677Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1678 Expr *ObjectExpr,
John McCall58e6f342010-03-16 05:22:47 +00001679 Expr *ArgExpr,
John McCall9aa472c2010-03-19 07:35:19 +00001680 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001681 if (!getLangOpts().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001682 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001683 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001684
John McCallca82a822011-09-21 08:36:56 +00001685 const RecordType *RT = ObjectExpr->getType()->castAs<RecordType>();
John McCall5357b612010-01-28 01:42:12 +00001686 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1687
John McCall161755a2010-04-06 21:38:20 +00001688 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1689 ObjectExpr->getType());
John McCall58e6f342010-03-16 05:22:47 +00001690 Entity.setDiag(diag::err_access)
1691 << ObjectExpr->getSourceRange()
1692 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1693
1694 return CheckAccess(*this, OpLoc, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001695}
John McCall5357b612010-01-28 01:42:12 +00001696
John McCall6bb80172010-03-30 21:47:33 +00001697Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1698 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001699 if (!getLangOpts().AccessControl ||
John McCalle2f5ba92010-03-30 22:20:00 +00001700 Found.getAccess() == AS_none ||
John McCall6bb80172010-03-30 21:47:33 +00001701 Found.getAccess() == AS_public)
1702 return AR_accessible;
1703
John McCall9c72c602010-08-27 09:08:28 +00001704 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCalle9ee23e2010-04-22 18:44:12 +00001705 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall6bb80172010-03-30 21:47:33 +00001706
John McCall161755a2010-04-06 21:38:20 +00001707 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
John McCallb9abd8722012-04-07 03:04:20 +00001708 /*no instance context*/ QualType());
John McCall6bb80172010-03-30 21:47:33 +00001709 Entity.setDiag(diag::err_access)
1710 << Ovl->getSourceRange();
1711
1712 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1713}
1714
John McCall6b2accb2010-02-10 09:31:12 +00001715/// Checks access for a hierarchy conversion.
1716///
1717/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
1718/// or a derived-to-base conversion (false)
1719/// \param ForceCheck true if this check should be performed even if access
1720/// control is disabled; some things rely on this for semantics
1721/// \param ForceUnprivileged true if this check should proceed as if the
1722/// context had no special privileges
1723/// \param ADK controls the kind of diagnostics that are used
1724Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall6b2accb2010-02-10 09:31:12 +00001725 QualType Base,
1726 QualType Derived,
1727 const CXXBasePath &Path,
John McCall58e6f342010-03-16 05:22:47 +00001728 unsigned DiagID,
John McCall6b2accb2010-02-10 09:31:12 +00001729 bool ForceCheck,
John McCall58e6f342010-03-16 05:22:47 +00001730 bool ForceUnprivileged) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001731 if (!ForceCheck && !getLangOpts().AccessControl)
John McCall6b2accb2010-02-10 09:31:12 +00001732 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001733
John McCall6b2accb2010-02-10 09:31:12 +00001734 if (Path.Access == AS_public)
1735 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001736
John McCall6b2accb2010-02-10 09:31:12 +00001737 CXXRecordDecl *BaseD, *DerivedD;
1738 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1739 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall58e6f342010-03-16 05:22:47 +00001740
John McCall161755a2010-04-06 21:38:20 +00001741 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1742 Path.Access);
John McCall58e6f342010-03-16 05:22:47 +00001743 if (DiagID)
1744 Entity.setDiag(DiagID) << Derived << Base;
John McCall6b2accb2010-02-10 09:31:12 +00001745
John McCall161755a2010-04-06 21:38:20 +00001746 if (ForceUnprivileged) {
1747 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1748 AccessLoc, Entity)) {
1749 case ::AR_accessible: return Sema::AR_accessible;
1750 case ::AR_inaccessible: return Sema::AR_inaccessible;
1751 case ::AR_dependent: return Sema::AR_dependent;
1752 }
1753 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1754 }
John McCall58e6f342010-03-16 05:22:47 +00001755 return CheckAccess(*this, AccessLoc, Entity);
John McCall5357b612010-01-28 01:42:12 +00001756}
1757
John McCall92f88312010-01-23 00:46:32 +00001758/// Checks access to all the declarations in the given result set.
John McCall6b2accb2010-02-10 09:31:12 +00001759void Sema::CheckLookupAccess(const LookupResult &R) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001760 assert(getLangOpts().AccessControl
John McCall6b2accb2010-02-10 09:31:12 +00001761 && "performing access check without access control");
1762 assert(R.getNamingClass() && "performing access check without naming class");
1763
John McCall58e6f342010-03-16 05:22:47 +00001764 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1765 if (I.getAccess() != AS_public) {
John McCall161755a2010-04-06 21:38:20 +00001766 AccessTarget Entity(Context, AccessedEntity::Member,
1767 R.getNamingClass(), I.getPair(),
Erik Verbruggen24dd9ad2011-09-19 15:10:40 +00001768 R.getBaseObjectType());
John McCall58e6f342010-03-16 05:22:47 +00001769 Entity.setDiag(diag::err_access);
John McCall58e6f342010-03-16 05:22:47 +00001770 CheckAccess(*this, R.getNameLoc(), Entity);
1771 }
1772 }
John McCall92f88312010-01-23 00:46:32 +00001773}
Chandler Carruth926c4b42010-06-28 08:39:25 +00001774
Erik Verbruggend1205962011-10-06 07:27:49 +00001775/// Checks access to Decl from the given class. The check will take access
1776/// specifiers into account, but no member access expressions and such.
1777///
1778/// \param Decl the declaration to check if it can be accessed
1779/// \param Class the class/context from which to start the search
1780/// \return true if the Decl is accessible from the Class, false otherwise.
Douglas Gregor17015ef2011-11-03 16:51:37 +00001781bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) {
1782 if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) {
Douglas Gregora885dce2011-11-03 17:41:55 +00001783 if (!Decl->isCXXClassMember())
Douglas Gregor17015ef2011-11-03 16:51:37 +00001784 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001785
Douglas Gregor17015ef2011-11-03 16:51:37 +00001786 QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal();
1787 AccessTarget Entity(Context, AccessedEntity::Member, Class,
1788 DeclAccessPair::make(Decl, Decl->getAccess()),
1789 qType);
1790 if (Entity.getAccess() == AS_public)
1791 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001792
Douglas Gregor17015ef2011-11-03 16:51:37 +00001793 EffectiveContext EC(CurContext);
1794 return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible;
1795 }
1796
Douglas Gregorf3c02862011-11-03 19:00:24 +00001797 if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) {
1798 // @public and @package ivars are always accessible.
1799 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public ||
1800 Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package)
1801 return true;
1802
1803
1804
1805 // If we are inside a class or category implementation, determine the
1806 // interface we're in.
1807 ObjCInterfaceDecl *ClassOfMethodDecl = 0;
1808 if (ObjCMethodDecl *MD = getCurMethodDecl())
1809 ClassOfMethodDecl = MD->getClassInterface();
1810 else if (FunctionDecl *FD = getCurFunctionDecl()) {
1811 if (ObjCImplDecl *Impl
1812 = dyn_cast<ObjCImplDecl>(FD->getLexicalDeclContext())) {
1813 if (ObjCImplementationDecl *IMPD
1814 = dyn_cast<ObjCImplementationDecl>(Impl))
1815 ClassOfMethodDecl = IMPD->getClassInterface();
1816 else if (ObjCCategoryImplDecl* CatImplClass
1817 = dyn_cast<ObjCCategoryImplDecl>(Impl))
1818 ClassOfMethodDecl = CatImplClass->getClassInterface();
1819 }
1820 }
1821
1822 // If we're not in an interface, this ivar is inaccessible.
1823 if (!ClassOfMethodDecl)
1824 return false;
1825
1826 // If we're inside the same interface that owns the ivar, we're fine.
Douglas Gregor60ef3082011-12-15 00:29:59 +00001827 if (declaresSameEntity(ClassOfMethodDecl, Ivar->getContainingInterface()))
Douglas Gregorf3c02862011-11-03 19:00:24 +00001828 return true;
1829
1830 // If the ivar is private, it's inaccessible.
1831 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Private)
1832 return false;
1833
1834 return Ivar->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl);
1835 }
1836
Douglas Gregor17015ef2011-11-03 16:51:37 +00001837 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001838}
1839
Chandler Carruth926c4b42010-06-28 08:39:25 +00001840void Sema::ActOnStartSuppressingAccessChecks() {
1841 assert(!SuppressAccessChecking &&
1842 "Tried to start access check suppression when already started.");
1843 SuppressAccessChecking = true;
1844}
1845
1846void Sema::ActOnStopSuppressingAccessChecks() {
1847 assert(SuppressAccessChecking &&
1848 "Tried to stop access check suprression when already stopped.");
1849 SuppressAccessChecking = false;
1850}