blob: f71a38816f6ede3e84f2ceab623b15776bd0e685 [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)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000155 : AccessedEntity(Context.getDiagAllocator(), Member, NamingClass,
156 FoundDecl, BaseObjectType) {
John McCall161755a2010-04-06 21:38:20 +0000157 initialize();
158 }
159
160 AccessTarget(ASTContext &Context,
161 BaseNonce _,
162 CXXRecordDecl *BaseClass,
163 CXXRecordDecl *DerivedClass,
164 AccessSpecifier Access)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000165 : AccessedEntity(Context.getDiagAllocator(), Base, BaseClass, DerivedClass,
166 Access) {
John McCall161755a2010-04-06 21:38:20 +0000167 initialize();
168 }
169
John McCallb9abd8722012-04-07 03:04:20 +0000170 bool isInstanceMember() const {
171 return (isMemberAccess() && getTargetDecl()->isCXXInstanceMember());
172 }
173
John McCall161755a2010-04-06 21:38:20 +0000174 bool hasInstanceContext() const {
175 return HasInstanceContext;
176 }
177
178 class SavedInstanceContext {
179 public:
180 ~SavedInstanceContext() {
181 Target.HasInstanceContext = Has;
182 }
183
184 private:
John McCallc91cc662010-04-07 00:41:46 +0000185 friend struct AccessTarget;
John McCall161755a2010-04-06 21:38:20 +0000186 explicit SavedInstanceContext(AccessTarget &Target)
187 : Target(Target), Has(Target.HasInstanceContext) {}
188 AccessTarget &Target;
189 bool Has;
190 };
191
192 SavedInstanceContext saveInstanceContext() {
193 return SavedInstanceContext(*this);
194 }
195
196 void suppressInstanceContext() {
197 HasInstanceContext = false;
198 }
199
200 const CXXRecordDecl *resolveInstanceContext(Sema &S) const {
201 assert(HasInstanceContext);
202 if (CalculatedInstanceContext)
203 return InstanceContext;
204
205 CalculatedInstanceContext = true;
206 DeclContext *IC = S.computeDeclContext(getBaseObjectType());
207 InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0);
208 return InstanceContext;
209 }
210
211 const CXXRecordDecl *getDeclaringClass() const {
212 return DeclaringClass;
213 }
214
215private:
216 void initialize() {
217 HasInstanceContext = (isMemberAccess() &&
218 !getBaseObjectType().isNull() &&
219 getTargetDecl()->isCXXInstanceMember());
220 CalculatedInstanceContext = false;
221 InstanceContext = 0;
222
223 if (isMemberAccess())
224 DeclaringClass = FindDeclaringClass(getTargetDecl());
225 else
226 DeclaringClass = getBaseClass();
227 DeclaringClass = DeclaringClass->getCanonicalDecl();
228 }
229
230 bool HasInstanceContext : 1;
231 mutable bool CalculatedInstanceContext : 1;
232 mutable const CXXRecordDecl *InstanceContext;
233 const CXXRecordDecl *DeclaringClass;
234};
235
Anders Carlsson29f006b2009-03-27 05:05:05 +0000236}
John McCall92f88312010-01-23 00:46:32 +0000237
John McCall01ebd9d2010-05-04 05:11:27 +0000238/// Checks whether one class might instantiate to the other.
239static bool MightInstantiateTo(const CXXRecordDecl *From,
240 const CXXRecordDecl *To) {
241 // Declaration names are always preserved by instantiation.
242 if (From->getDeclName() != To->getDeclName())
243 return false;
244
245 const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext();
246 const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext();
247 if (FromDC == ToDC) return true;
248 if (FromDC->isFileContext() || ToDC->isFileContext()) return false;
249
250 // Be conservative.
251 return true;
252}
253
John McCall161755a2010-04-06 21:38:20 +0000254/// Checks whether one class is derived from another, inclusively.
255/// Properly indicates when it couldn't be determined due to
256/// dependence.
257///
258/// This should probably be donated to AST or at least Sema.
259static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived,
260 const CXXRecordDecl *Target) {
261 assert(Derived->getCanonicalDecl() == Derived);
262 assert(Target->getCanonicalDecl() == Target);
John McCallc1b621d2010-03-24 09:04:37 +0000263
John McCall161755a2010-04-06 21:38:20 +0000264 if (Derived == Target) return AR_accessible;
John McCallc1b621d2010-03-24 09:04:37 +0000265
John McCall01ebd9d2010-05-04 05:11:27 +0000266 bool CheckDependent = Derived->isDependentContext();
267 if (CheckDependent && MightInstantiateTo(Derived, Target))
268 return AR_dependent;
269
John McCall161755a2010-04-06 21:38:20 +0000270 AccessResult OnFailure = AR_inaccessible;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack
John McCall161755a2010-04-06 21:38:20 +0000272
273 while (true) {
Douglas Gregor7432b902011-11-14 23:00:43 +0000274 if (Derived->isDependentContext() && !Derived->hasDefinition())
275 return AR_dependent;
276
John McCall161755a2010-04-06 21:38:20 +0000277 for (CXXRecordDecl::base_class_const_iterator
278 I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) {
279
280 const CXXRecordDecl *RD;
281
282 QualType T = I->getType();
283 if (const RecordType *RT = T->getAs<RecordType>()) {
284 RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall01ebd9d2010-05-04 05:11:27 +0000285 } else if (const InjectedClassNameType *IT
286 = T->getAs<InjectedClassNameType>()) {
287 RD = IT->getDecl();
John McCall161755a2010-04-06 21:38:20 +0000288 } else {
John McCall161755a2010-04-06 21:38:20 +0000289 assert(T->isDependentType() && "non-dependent base wasn't a record?");
290 OnFailure = AR_dependent;
291 continue;
292 }
293
294 RD = RD->getCanonicalDecl();
295 if (RD == Target) return AR_accessible;
John McCall01ebd9d2010-05-04 05:11:27 +0000296 if (CheckDependent && MightInstantiateTo(RD, Target))
297 OnFailure = AR_dependent;
298
John McCall161755a2010-04-06 21:38:20 +0000299 Queue.push_back(RD);
300 }
301
302 if (Queue.empty()) break;
303
304 Derived = Queue.back();
305 Queue.pop_back();
306 }
307
308 return OnFailure;
John McCall6b2accb2010-02-10 09:31:12 +0000309}
310
John McCall161755a2010-04-06 21:38:20 +0000311
John McCall0c01d182010-03-24 05:22:00 +0000312static bool MightInstantiateTo(Sema &S, DeclContext *Context,
313 DeclContext *Friend) {
314 if (Friend == Context)
315 return true;
316
317 assert(!Friend->isDependentContext() &&
318 "can't handle friends with dependent contexts here");
319
320 if (!Context->isDependentContext())
321 return false;
322
323 if (Friend->isFileContext())
324 return false;
325
326 // TODO: this is very conservative
327 return true;
328}
329
330// Asks whether the type in 'context' can ever instantiate to the type
331// in 'friend'.
332static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) {
333 if (Friend == Context)
334 return true;
335
336 if (!Friend->isDependentType() && !Context->isDependentType())
337 return false;
338
339 // TODO: this is very conservative.
340 return true;
341}
342
343static bool MightInstantiateTo(Sema &S,
344 FunctionDecl *Context,
345 FunctionDecl *Friend) {
346 if (Context->getDeclName() != Friend->getDeclName())
347 return false;
348
349 if (!MightInstantiateTo(S,
350 Context->getDeclContext(),
351 Friend->getDeclContext()))
352 return false;
353
354 CanQual<FunctionProtoType> FriendTy
355 = S.Context.getCanonicalType(Friend->getType())
356 ->getAs<FunctionProtoType>();
357 CanQual<FunctionProtoType> ContextTy
358 = S.Context.getCanonicalType(Context->getType())
359 ->getAs<FunctionProtoType>();
360
361 // There isn't any way that I know of to add qualifiers
362 // during instantiation.
363 if (FriendTy.getQualifiers() != ContextTy.getQualifiers())
364 return false;
365
366 if (FriendTy->getNumArgs() != ContextTy->getNumArgs())
367 return false;
368
369 if (!MightInstantiateTo(S,
370 ContextTy->getResultType(),
371 FriendTy->getResultType()))
372 return false;
373
374 for (unsigned I = 0, E = FriendTy->getNumArgs(); I != E; ++I)
375 if (!MightInstantiateTo(S,
376 ContextTy->getArgType(I),
377 FriendTy->getArgType(I)))
378 return false;
379
380 return true;
381}
382
383static bool MightInstantiateTo(Sema &S,
384 FunctionTemplateDecl *Context,
385 FunctionTemplateDecl *Friend) {
386 return MightInstantiateTo(S,
387 Context->getTemplatedDecl(),
388 Friend->getTemplatedDecl());
389}
390
John McCall161755a2010-04-06 21:38:20 +0000391static AccessResult MatchesFriend(Sema &S,
392 const EffectiveContext &EC,
393 const CXXRecordDecl *Friend) {
John McCalla742db02010-03-17 20:01:29 +0000394 if (EC.includesClass(Friend))
John McCall161755a2010-04-06 21:38:20 +0000395 return AR_accessible;
John McCalla742db02010-03-17 20:01:29 +0000396
John McCall0c01d182010-03-24 05:22:00 +0000397 if (EC.isDependent()) {
398 CanQualType FriendTy
399 = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend));
400
401 for (EffectiveContext::record_iterator
402 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
403 CanQualType ContextTy
404 = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I));
405 if (MightInstantiateTo(S, ContextTy, FriendTy))
John McCall161755a2010-04-06 21:38:20 +0000406 return AR_dependent;
John McCall0c01d182010-03-24 05:22:00 +0000407 }
408 }
409
John McCall161755a2010-04-06 21:38:20 +0000410 return AR_inaccessible;
John McCalla742db02010-03-17 20:01:29 +0000411}
412
John McCall161755a2010-04-06 21:38:20 +0000413static AccessResult MatchesFriend(Sema &S,
414 const EffectiveContext &EC,
415 CanQualType Friend) {
John McCall0c01d182010-03-24 05:22:00 +0000416 if (const RecordType *RT = Friend->getAs<RecordType>())
417 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
John McCalla742db02010-03-17 20:01:29 +0000418
John McCall0c01d182010-03-24 05:22:00 +0000419 // TODO: we can do better than this
420 if (Friend->isDependentType())
John McCall161755a2010-04-06 21:38:20 +0000421 return AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000422
John McCall161755a2010-04-06 21:38:20 +0000423 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000424}
425
426/// Determines whether the given friend class template matches
427/// anything in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000428static AccessResult MatchesFriend(Sema &S,
429 const EffectiveContext &EC,
430 ClassTemplateDecl *Friend) {
431 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000432
John McCall93ba8572010-03-25 06:39:04 +0000433 // Check whether the friend is the template of a class in the
434 // context chain.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000435 for (SmallVectorImpl<CXXRecordDecl*>::const_iterator
John McCall0c01d182010-03-24 05:22:00 +0000436 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
437 CXXRecordDecl *Record = *I;
438
John McCall93ba8572010-03-25 06:39:04 +0000439 // Figure out whether the current class has a template:
John McCall0c01d182010-03-24 05:22:00 +0000440 ClassTemplateDecl *CTD;
441
442 // A specialization of the template...
443 if (isa<ClassTemplateSpecializationDecl>(Record)) {
444 CTD = cast<ClassTemplateSpecializationDecl>(Record)
445 ->getSpecializedTemplate();
446
447 // ... or the template pattern itself.
448 } else {
449 CTD = Record->getDescribedClassTemplate();
450 if (!CTD) continue;
451 }
452
453 // It's a match.
454 if (Friend == CTD->getCanonicalDecl())
John McCall161755a2010-04-06 21:38:20 +0000455 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000456
John McCall93ba8572010-03-25 06:39:04 +0000457 // If the context isn't dependent, it can't be a dependent match.
458 if (!EC.isDependent())
459 continue;
460
John McCall0c01d182010-03-24 05:22:00 +0000461 // If the template names don't match, it can't be a dependent
Richard Smith3e4c6c42011-05-05 21:57:07 +0000462 // match.
463 if (CTD->getDeclName() != Friend->getDeclName())
John McCall0c01d182010-03-24 05:22:00 +0000464 continue;
465
466 // If the class's context can't instantiate to the friend's
467 // context, it can't be a dependent match.
468 if (!MightInstantiateTo(S, CTD->getDeclContext(),
469 Friend->getDeclContext()))
470 continue;
471
472 // Otherwise, it's a dependent match.
John McCall161755a2010-04-06 21:38:20 +0000473 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000474 }
475
John McCall0c01d182010-03-24 05:22:00 +0000476 return OnFailure;
477}
478
479/// Determines whether the given friend function matches anything in
480/// the effective context.
John McCall161755a2010-04-06 21:38:20 +0000481static AccessResult MatchesFriend(Sema &S,
482 const EffectiveContext &EC,
483 FunctionDecl *Friend) {
484 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000485
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall2cc26752010-03-27 06:55:49 +0000487 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
488 if (Friend == *I)
John McCall161755a2010-04-06 21:38:20 +0000489 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +0000490
John McCall2cc26752010-03-27 06:55:49 +0000491 if (EC.isDependent() && MightInstantiateTo(S, *I, Friend))
John McCall161755a2010-04-06 21:38:20 +0000492 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000493 }
John McCall0c01d182010-03-24 05:22:00 +0000494
John McCall2cc26752010-03-27 06:55:49 +0000495 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000496}
497
498/// Determines whether the given friend function template matches
499/// anything in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000500static AccessResult MatchesFriend(Sema &S,
501 const EffectiveContext &EC,
502 FunctionTemplateDecl *Friend) {
503 if (EC.Functions.empty()) return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000504
John McCall161755a2010-04-06 21:38:20 +0000505 AccessResult OnFailure = AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +0000506
Chris Lattner5f9e2722011-07-23 10:55:15 +0000507 for (SmallVectorImpl<FunctionDecl*>::const_iterator
John McCall2cc26752010-03-27 06:55:49 +0000508 I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) {
John McCall0c01d182010-03-24 05:22:00 +0000509
John McCall2cc26752010-03-27 06:55:49 +0000510 FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate();
511 if (!FTD)
512 FTD = (*I)->getDescribedFunctionTemplate();
513 if (!FTD)
514 continue;
John McCall0c01d182010-03-24 05:22:00 +0000515
John McCall2cc26752010-03-27 06:55:49 +0000516 FTD = FTD->getCanonicalDecl();
517
518 if (Friend == FTD)
John McCall161755a2010-04-06 21:38:20 +0000519 return AR_accessible;
John McCall2cc26752010-03-27 06:55:49 +0000520
521 if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend))
John McCall161755a2010-04-06 21:38:20 +0000522 OnFailure = AR_dependent;
John McCall2cc26752010-03-27 06:55:49 +0000523 }
524
525 return OnFailure;
John McCall0c01d182010-03-24 05:22:00 +0000526}
527
528/// Determines whether the given friend declaration matches anything
529/// in the effective context.
John McCall161755a2010-04-06 21:38:20 +0000530static AccessResult MatchesFriend(Sema &S,
531 const EffectiveContext &EC,
532 FriendDecl *FriendD) {
John McCall6102ca12010-10-16 06:59:13 +0000533 // Whitelist accesses if there's an invalid or unsupported friend
534 // declaration.
535 if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend())
John McCall337ec3d2010-10-12 23:13:28 +0000536 return AR_accessible;
537
John McCall32f2fb52010-03-25 18:04:51 +0000538 if (TypeSourceInfo *T = FriendD->getFriendType())
539 return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified());
John McCall0c01d182010-03-24 05:22:00 +0000540
541 NamedDecl *Friend
542 = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl());
John McCalla742db02010-03-17 20:01:29 +0000543
544 // FIXME: declarations with dependent or templated scope.
545
John McCall0c01d182010-03-24 05:22:00 +0000546 if (isa<ClassTemplateDecl>(Friend))
547 return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000548
John McCall0c01d182010-03-24 05:22:00 +0000549 if (isa<FunctionTemplateDecl>(Friend))
550 return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000551
John McCall0c01d182010-03-24 05:22:00 +0000552 if (isa<CXXRecordDecl>(Friend))
553 return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000554
John McCall0c01d182010-03-24 05:22:00 +0000555 assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind");
556 return MatchesFriend(S, EC, cast<FunctionDecl>(Friend));
John McCalla742db02010-03-17 20:01:29 +0000557}
558
John McCall161755a2010-04-06 21:38:20 +0000559static AccessResult GetFriendKind(Sema &S,
560 const EffectiveContext &EC,
561 const CXXRecordDecl *Class) {
562 AccessResult OnFailure = AR_inaccessible;
John McCall88b6c712010-03-17 04:58:56 +0000563
John McCalld60e22e2010-03-12 01:19:31 +0000564 // Okay, check friends.
565 for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
566 E = Class->friend_end(); I != E; ++I) {
567 FriendDecl *Friend = *I;
568
John McCalla742db02010-03-17 20:01:29 +0000569 switch (MatchesFriend(S, EC, Friend)) {
John McCall161755a2010-04-06 21:38:20 +0000570 case AR_accessible:
571 return AR_accessible;
John McCalld60e22e2010-03-12 01:19:31 +0000572
John McCall161755a2010-04-06 21:38:20 +0000573 case AR_inaccessible:
574 continue;
575
576 case AR_dependent:
577 OnFailure = AR_dependent;
John McCalla742db02010-03-17 20:01:29 +0000578 break;
John McCalld60e22e2010-03-12 01:19:31 +0000579 }
John McCalld60e22e2010-03-12 01:19:31 +0000580 }
581
582 // That's it, give up.
John McCall88b6c712010-03-17 04:58:56 +0000583 return OnFailure;
John McCall6b2accb2010-02-10 09:31:12 +0000584}
585
John McCall8c77bcb2010-08-28 07:56:00 +0000586namespace {
587
588/// A helper class for checking for a friend which will grant access
589/// to a protected instance member.
590struct ProtectedFriendContext {
591 Sema &S;
592 const EffectiveContext &EC;
593 const CXXRecordDecl *NamingClass;
594 bool CheckDependent;
595 bool EverDependent;
596
597 /// The path down to the current base class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000598 SmallVector<const CXXRecordDecl*, 20> CurPath;
John McCall8c77bcb2010-08-28 07:56:00 +0000599
600 ProtectedFriendContext(Sema &S, const EffectiveContext &EC,
601 const CXXRecordDecl *InstanceContext,
602 const CXXRecordDecl *NamingClass)
603 : S(S), EC(EC), NamingClass(NamingClass),
604 CheckDependent(InstanceContext->isDependentContext() ||
605 NamingClass->isDependentContext()),
606 EverDependent(false) {}
607
John McCall326c8c72010-08-28 08:47:21 +0000608 /// Check classes in the current path for friendship, starting at
609 /// the given index.
610 bool checkFriendshipAlongPath(unsigned I) {
611 assert(I < CurPath.size());
612 for (unsigned E = CurPath.size(); I != E; ++I) {
613 switch (GetFriendKind(S, EC, CurPath[I])) {
John McCall8c77bcb2010-08-28 07:56:00 +0000614 case AR_accessible: return true;
615 case AR_inaccessible: continue;
616 case AR_dependent: EverDependent = true; continue;
617 }
618 }
619 return false;
620 }
621
622 /// Perform a search starting at the given class.
John McCall326c8c72010-08-28 08:47:21 +0000623 ///
624 /// PrivateDepth is the index of the last (least derived) class
625 /// along the current path such that a notional public member of
626 /// the final class in the path would have access in that class.
627 bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) {
John McCall8c77bcb2010-08-28 07:56:00 +0000628 // If we ever reach the naming class, check the current path for
629 // friendship. We can also stop recursing because we obviously
630 // won't find the naming class there again.
John McCall326c8c72010-08-28 08:47:21 +0000631 if (Cur == NamingClass)
632 return checkFriendshipAlongPath(PrivateDepth);
John McCall8c77bcb2010-08-28 07:56:00 +0000633
634 if (CheckDependent && MightInstantiateTo(Cur, NamingClass))
635 EverDependent = true;
636
637 // Recurse into the base classes.
638 for (CXXRecordDecl::base_class_const_iterator
639 I = Cur->bases_begin(), E = Cur->bases_end(); I != E; ++I) {
640
John McCall326c8c72010-08-28 08:47:21 +0000641 // If this is private inheritance, then a public member of the
642 // base will not have any access in classes derived from Cur.
643 unsigned BasePrivateDepth = PrivateDepth;
644 if (I->getAccessSpecifier() == AS_private)
645 BasePrivateDepth = CurPath.size() - 1;
John McCall8c77bcb2010-08-28 07:56:00 +0000646
647 const CXXRecordDecl *RD;
648
649 QualType T = I->getType();
650 if (const RecordType *RT = T->getAs<RecordType>()) {
651 RD = cast<CXXRecordDecl>(RT->getDecl());
652 } else if (const InjectedClassNameType *IT
653 = T->getAs<InjectedClassNameType>()) {
654 RD = IT->getDecl();
655 } else {
656 assert(T->isDependentType() && "non-dependent base wasn't a record?");
657 EverDependent = true;
658 continue;
659 }
660
661 // Recurse. We don't need to clean up if this returns true.
John McCall326c8c72010-08-28 08:47:21 +0000662 CurPath.push_back(RD);
663 if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth))
664 return true;
665 CurPath.pop_back();
John McCall8c77bcb2010-08-28 07:56:00 +0000666 }
667
John McCall8c77bcb2010-08-28 07:56:00 +0000668 return false;
669 }
John McCall326c8c72010-08-28 08:47:21 +0000670
671 bool findFriendship(const CXXRecordDecl *Cur) {
672 assert(CurPath.empty());
673 CurPath.push_back(Cur);
674 return findFriendship(Cur, 0);
675 }
John McCall8c77bcb2010-08-28 07:56:00 +0000676};
677}
678
679/// Search for a class P that EC is a friend of, under the constraint
John McCallb9abd8722012-04-07 03:04:20 +0000680/// InstanceContext <= P
681/// if InstanceContext exists, or else
682/// NamingClass <= P
John McCall8c77bcb2010-08-28 07:56:00 +0000683/// and with the additional restriction that a protected member of
John McCallb9abd8722012-04-07 03:04:20 +0000684/// NamingClass would have some natural access in P, which implicitly
685/// imposes the constraint that P <= NamingClass.
John McCall8c77bcb2010-08-28 07:56:00 +0000686///
John McCallb9abd8722012-04-07 03:04:20 +0000687/// This isn't quite the condition laid out in the standard.
688/// Instead of saying that a notional protected member of NamingClass
689/// would have to have some natural access in P, it says the actual
690/// target has to have some natural access in P, which opens up the
691/// possibility that the target (which is not necessarily a member
692/// of NamingClass) might be more accessible along some path not
693/// passing through it. That's really a bad idea, though, because it
John McCall8c77bcb2010-08-28 07:56:00 +0000694/// introduces two problems:
John McCallb9abd8722012-04-07 03:04:20 +0000695/// - Most importantly, it breaks encapsulation because you can
696/// access a forbidden base class's members by directly subclassing
697/// it elsewhere.
698/// - It also makes access substantially harder to compute because it
John McCall8c77bcb2010-08-28 07:56:00 +0000699/// breaks the hill-climbing algorithm: knowing that the target is
700/// accessible in some base class would no longer let you change
701/// the question solely to whether the base class is accessible,
702/// because the original target might have been more accessible
703/// because of crazy subclassing.
704/// So we don't implement that.
705static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC,
706 const CXXRecordDecl *InstanceContext,
707 const CXXRecordDecl *NamingClass) {
John McCallb9abd8722012-04-07 03:04:20 +0000708 assert(InstanceContext == 0 ||
709 InstanceContext->getCanonicalDecl() == InstanceContext);
John McCall8c77bcb2010-08-28 07:56:00 +0000710 assert(NamingClass->getCanonicalDecl() == NamingClass);
711
John McCallb9abd8722012-04-07 03:04:20 +0000712 // If we don't have an instance context, our constraints give us
713 // that NamingClass <= P <= NamingClass, i.e. P == NamingClass.
714 // This is just the usual friendship check.
715 if (!InstanceContext) return GetFriendKind(S, EC, NamingClass);
716
John McCall8c77bcb2010-08-28 07:56:00 +0000717 ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass);
718 if (PRC.findFriendship(InstanceContext)) return AR_accessible;
719 if (PRC.EverDependent) return AR_dependent;
720 return AR_inaccessible;
721}
722
John McCall161755a2010-04-06 21:38:20 +0000723static AccessResult HasAccess(Sema &S,
724 const EffectiveContext &EC,
725 const CXXRecordDecl *NamingClass,
726 AccessSpecifier Access,
727 const AccessTarget &Target) {
John McCalldb73c682010-04-02 00:03:43 +0000728 assert(NamingClass->getCanonicalDecl() == NamingClass &&
729 "declaration should be canonicalized before being passed here");
730
John McCall161755a2010-04-06 21:38:20 +0000731 if (Access == AS_public) return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000732 assert(Access == AS_private || Access == AS_protected);
733
John McCall161755a2010-04-06 21:38:20 +0000734 AccessResult OnFailure = AR_inaccessible;
735
John McCalldb73c682010-04-02 00:03:43 +0000736 for (EffectiveContext::record_iterator
737 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
738 // All the declarations in EC have been canonicalized, so pointer
739 // equality from this point on will work fine.
740 const CXXRecordDecl *ECRecord = *I;
741
742 // [B2] and [M2]
John McCall161755a2010-04-06 21:38:20 +0000743 if (Access == AS_private) {
744 if (ECRecord == NamingClass)
745 return AR_accessible;
John McCalldb73c682010-04-02 00:03:43 +0000746
John McCall01ebd9d2010-05-04 05:11:27 +0000747 if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass))
748 OnFailure = AR_dependent;
749
John McCalldb73c682010-04-02 00:03:43 +0000750 // [B3] and [M3]
John McCall161755a2010-04-06 21:38:20 +0000751 } else {
752 assert(Access == AS_protected);
753 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
754 case AR_accessible: break;
755 case AR_inaccessible: continue;
756 case AR_dependent: OnFailure = AR_dependent; continue;
757 }
758
John McCall161755a2010-04-06 21:38:20 +0000759 // C++ [class.protected]p1:
760 // An additional access check beyond those described earlier in
761 // [class.access] is applied when a non-static data member or
762 // non-static member function is a protected member of its naming
763 // class. As described earlier, access to a protected member is
764 // granted because the reference occurs in a friend or member of
765 // some class C. If the access is to form a pointer to member,
766 // the nested-name-specifier shall name C or a class derived from
767 // C. All other accesses involve a (possibly implicit) object
768 // expression. In this case, the class of the object expression
769 // shall be C or a class derived from C.
770 //
John McCallb9abd8722012-04-07 03:04:20 +0000771 // We interpret this as a restriction on [M3].
772
773 // In this part of the code, 'C' is just our context class ECRecord.
774
775 // These rules are different if we don't have an instance context.
776 if (!Target.hasInstanceContext()) {
777 // If it's not an instance member, these restrictions don't apply.
778 if (!Target.isInstanceMember()) return AR_accessible;
779
780 // If it's an instance member, use the pointer-to-member rule
781 // that the naming class has to be derived from the effective
782 // context.
783
Francois Pichetb2d899e2012-04-17 12:35:05 +0000784 // Emulate a MSVC bug where the creation of pointer-to-member
785 // to protected member of base class is allowed but only from
Francois Pichetb4143472012-04-19 07:48:57 +0000786 // static member functions.
Francois Pichet2d01f2c2012-04-18 03:24:38 +0000787 if (S.getLangOpts().MicrosoftMode && !EC.Functions.empty())
788 if (CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(EC.Functions.front()))
789 if (MD->isStatic()) return AR_accessible;
Francois Pichetb2d899e2012-04-17 12:35:05 +0000790
John McCallb9abd8722012-04-07 03:04:20 +0000791 // Despite the standard's confident wording, there is a case
792 // where you can have an instance member that's neither in a
793 // pointer-to-member expression nor in a member access: when
794 // it names a field in an unevaluated context that can't be an
795 // implicit member. Pending clarification, we just apply the
796 // same naming-class restriction here.
797 // FIXME: we're probably not correctly adding the
798 // protected-member restriction when we retroactively convert
799 // an expression to being evaluated.
800
801 // We know that ECRecord derives from NamingClass. The
802 // restriction says to check whether NamingClass derives from
803 // ECRecord, but that's not really necessary: two distinct
804 // classes can't be recursively derived from each other. So
805 // along this path, we just need to check whether the classes
806 // are equal.
807 if (NamingClass == ECRecord) return AR_accessible;
808
809 // Otherwise, this context class tells us nothing; on to the next.
810 continue;
811 }
812
813 assert(Target.isInstanceMember());
814
815 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
816 if (!InstanceContext) {
817 OnFailure = AR_dependent;
818 continue;
819 }
820
John McCall161755a2010-04-06 21:38:20 +0000821 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
822 case AR_accessible: return AR_accessible;
823 case AR_inaccessible: continue;
824 case AR_dependent: OnFailure = AR_dependent; continue;
825 }
826 }
John McCalldb73c682010-04-02 00:03:43 +0000827 }
828
John McCall8c77bcb2010-08-28 07:56:00 +0000829 // [M3] and [B3] say that, if the target is protected in N, we grant
830 // access if the access occurs in a friend or member of some class P
831 // that's a subclass of N and where the target has some natural
832 // access in P. The 'member' aspect is easy to handle because P
833 // would necessarily be one of the effective-context records, and we
834 // address that above. The 'friend' aspect is completely ridiculous
835 // to implement because there are no restrictions at all on P
836 // *unless* the [class.protected] restriction applies. If it does,
837 // however, we should ignore whether the naming class is a friend,
838 // and instead rely on whether any potential P is a friend.
John McCallb9abd8722012-04-07 03:04:20 +0000839 if (Access == AS_protected && Target.isInstanceMember()) {
840 // Compute the instance context if possible.
841 const CXXRecordDecl *InstanceContext = 0;
842 if (Target.hasInstanceContext()) {
843 InstanceContext = Target.resolveInstanceContext(S);
844 if (!InstanceContext) return AR_dependent;
845 }
846
John McCall8c77bcb2010-08-28 07:56:00 +0000847 switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) {
848 case AR_accessible: return AR_accessible;
John McCall161755a2010-04-06 21:38:20 +0000849 case AR_inaccessible: return OnFailure;
850 case AR_dependent: return AR_dependent;
851 }
John McCall1797a052010-08-28 08:10:32 +0000852 llvm_unreachable("impossible friendship kind");
John McCall161755a2010-04-06 21:38:20 +0000853 }
854
855 switch (GetFriendKind(S, EC, NamingClass)) {
856 case AR_accessible: return AR_accessible;
857 case AR_inaccessible: return OnFailure;
858 case AR_dependent: return AR_dependent;
859 }
860
861 // Silence bogus warnings
862 llvm_unreachable("impossible friendship kind");
John McCalldb73c682010-04-02 00:03:43 +0000863}
864
John McCall6b2accb2010-02-10 09:31:12 +0000865/// Finds the best path from the naming class to the declaring class,
866/// taking friend declarations into account.
867///
John McCalldb73c682010-04-02 00:03:43 +0000868/// C++0x [class.access.base]p5:
869/// A member m is accessible at the point R when named in class N if
870/// [M1] m as a member of N is public, or
871/// [M2] m as a member of N is private, and R occurs in a member or
872/// friend of class N, or
873/// [M3] m as a member of N is protected, and R occurs in a member or
874/// friend of class N, or in a member or friend of a class P
875/// derived from N, where m as a member of P is public, private,
876/// or protected, or
877/// [M4] there exists a base class B of N that is accessible at R, and
878/// m is accessible at R when named in class B.
879///
880/// C++0x [class.access.base]p4:
881/// A base class B of N is accessible at R, if
882/// [B1] an invented public member of B would be a public member of N, or
883/// [B2] R occurs in a member or friend of class N, and an invented public
884/// member of B would be a private or protected member of N, or
885/// [B3] R occurs in a member or friend of a class P derived from N, and an
886/// invented public member of B would be a private or protected member
887/// of P, or
888/// [B4] there exists a class S such that B is a base class of S accessible
889/// at R and S is a base class of N accessible at R.
890///
891/// Along a single inheritance path we can restate both of these
892/// iteratively:
893///
894/// First, we note that M1-4 are equivalent to B1-4 if the member is
895/// treated as a notional base of its declaring class with inheritance
896/// access equivalent to the member's access. Therefore we need only
897/// ask whether a class B is accessible from a class N in context R.
898///
899/// Let B_1 .. B_n be the inheritance path in question (i.e. where
900/// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
901/// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
902/// closest accessible base in the path:
903/// Access(a, b) = (* access on the base specifier from a to b *)
904/// Merge(a, forbidden) = forbidden
905/// Merge(a, private) = forbidden
906/// Merge(a, b) = min(a,b)
907/// Accessible(c, forbidden) = false
908/// Accessible(c, private) = (R is c) || IsFriend(c, R)
909/// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
910/// Accessible(c, public) = true
911/// ACAB(n) = public
912/// ACAB(i) =
913/// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
914/// if Accessible(B_i, AccessToBase) then public else AccessToBase
915///
916/// B is an accessible base of N at R iff ACAB(1) = public.
917///
John McCall161755a2010-04-06 21:38:20 +0000918/// \param FinalAccess the access of the "final step", or AS_public if
John McCall7aceaf82010-03-18 23:49:19 +0000919/// there is no final step.
John McCall6b2accb2010-02-10 09:31:12 +0000920/// \return null if friendship is dependent
921static CXXBasePath *FindBestPath(Sema &S,
922 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +0000923 AccessTarget &Target,
John McCall7aceaf82010-03-18 23:49:19 +0000924 AccessSpecifier FinalAccess,
John McCall6b2accb2010-02-10 09:31:12 +0000925 CXXBasePaths &Paths) {
926 // Derive the paths to the desired base.
John McCall161755a2010-04-06 21:38:20 +0000927 const CXXRecordDecl *Derived = Target.getNamingClass();
928 const CXXRecordDecl *Base = Target.getDeclaringClass();
929
930 // FIXME: fail correctly when there are dependent paths.
931 bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base),
932 Paths);
John McCall6b2accb2010-02-10 09:31:12 +0000933 assert(isDerived && "derived class not actually derived from base");
934 (void) isDerived;
935
936 CXXBasePath *BestPath = 0;
937
John McCall7aceaf82010-03-18 23:49:19 +0000938 assert(FinalAccess != AS_none && "forbidden access after declaring class");
939
John McCall0c01d182010-03-24 05:22:00 +0000940 bool AnyDependent = false;
941
John McCall6b2accb2010-02-10 09:31:12 +0000942 // Derive the friend-modified access along each path.
943 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
944 PI != PE; ++PI) {
John McCall161755a2010-04-06 21:38:20 +0000945 AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +0000946
947 // Walk through the path backwards.
John McCall7aceaf82010-03-18 23:49:19 +0000948 AccessSpecifier PathAccess = FinalAccess;
John McCall6b2accb2010-02-10 09:31:12 +0000949 CXXBasePath::iterator I = PI->end(), E = PI->begin();
950 while (I != E) {
951 --I;
952
John McCall7aceaf82010-03-18 23:49:19 +0000953 assert(PathAccess != AS_none);
954
955 // If the declaration is a private member of a base class, there
956 // is no level of friendship in derived classes that can make it
957 // accessible.
958 if (PathAccess == AS_private) {
959 PathAccess = AS_none;
960 break;
961 }
962
John McCall161755a2010-04-06 21:38:20 +0000963 const CXXRecordDecl *NC = I->Class->getCanonicalDecl();
964
John McCall6b2accb2010-02-10 09:31:12 +0000965 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
John McCalldb73c682010-04-02 00:03:43 +0000966 PathAccess = std::max(PathAccess, BaseAccess);
John McCall161755a2010-04-06 21:38:20 +0000967
968 switch (HasAccess(S, EC, NC, PathAccess, Target)) {
969 case AR_inaccessible: break;
970 case AR_accessible:
971 PathAccess = AS_public;
972
973 // Future tests are not against members and so do not have
974 // instance context.
975 Target.suppressInstanceContext();
976 break;
977 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +0000978 AnyDependent = true;
979 goto Next;
John McCall6b2accb2010-02-10 09:31:12 +0000980 }
John McCall6b2accb2010-02-10 09:31:12 +0000981 }
982
983 // Note that we modify the path's Access field to the
984 // friend-modified access.
985 if (BestPath == 0 || PathAccess < BestPath->Access) {
986 BestPath = &*PI;
987 BestPath->Access = PathAccess;
John McCall0c01d182010-03-24 05:22:00 +0000988
989 // Short-circuit if we found a public path.
990 if (BestPath->Access == AS_public)
991 return BestPath;
John McCall6b2accb2010-02-10 09:31:12 +0000992 }
John McCall0c01d182010-03-24 05:22:00 +0000993
994 Next: ;
John McCall6b2accb2010-02-10 09:31:12 +0000995 }
996
John McCall0c01d182010-03-24 05:22:00 +0000997 assert((!BestPath || BestPath->Access != AS_public) &&
998 "fell out of loop with public path");
999
1000 // We didn't find a public path, but at least one path was subject
1001 // to dependent friendship, so delay the check.
1002 if (AnyDependent)
1003 return 0;
1004
John McCall6b2accb2010-02-10 09:31:12 +00001005 return BestPath;
1006}
1007
John McCallfe24e052010-09-03 04:56:05 +00001008/// Given that an entity has protected natural access, check whether
1009/// access might be denied because of the protected member access
1010/// restriction.
1011///
1012/// \return true if a note was emitted
1013static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC,
1014 AccessTarget &Target) {
1015 // Only applies to instance accesses.
John McCallb9abd8722012-04-07 03:04:20 +00001016 if (!Target.isInstanceMember())
John McCallfe24e052010-09-03 04:56:05 +00001017 return false;
John McCallfe24e052010-09-03 04:56:05 +00001018
John McCallb9abd8722012-04-07 03:04:20 +00001019 assert(Target.isMemberAccess());
1020
1021 const CXXRecordDecl *NamingClass = Target.getNamingClass();
1022 NamingClass = NamingClass->getCanonicalDecl();
John McCallfe24e052010-09-03 04:56:05 +00001023
1024 for (EffectiveContext::record_iterator
1025 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
1026 const CXXRecordDecl *ECRecord = *I;
John McCallb9abd8722012-04-07 03:04:20 +00001027 switch (IsDerivedFromInclusive(ECRecord, NamingClass)) {
John McCallfe24e052010-09-03 04:56:05 +00001028 case AR_accessible: break;
1029 case AR_inaccessible: continue;
1030 case AR_dependent: continue;
1031 }
1032
1033 // The effective context is a subclass of the declaring class.
John McCallb9abd8722012-04-07 03:04:20 +00001034 // Check whether the [class.protected] restriction is limiting
1035 // access.
John McCallfe24e052010-09-03 04:56:05 +00001036
1037 // To get this exactly right, this might need to be checked more
1038 // holistically; it's not necessarily the case that gaining
1039 // access here would grant us access overall.
1040
John McCallb9abd8722012-04-07 03:04:20 +00001041 NamedDecl *D = Target.getTargetDecl();
1042
1043 // If we don't have an instance context, [class.protected] says the
1044 // naming class has to equal the context class.
1045 if (!Target.hasInstanceContext()) {
1046 // If it does, the restriction doesn't apply.
1047 if (NamingClass == ECRecord) continue;
1048
1049 // TODO: it would be great to have a fixit here, since this is
1050 // such an obvious error.
1051 S.Diag(D->getLocation(), diag::note_access_protected_restricted_noobject)
1052 << S.Context.getTypeDeclType(ECRecord);
1053 return true;
1054 }
1055
John McCallfe24e052010-09-03 04:56:05 +00001056 const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S);
1057 assert(InstanceContext && "diagnosing dependent access");
1058
1059 switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) {
1060 case AR_accessible: continue;
1061 case AR_dependent: continue;
1062 case AR_inaccessible:
John McCallb9abd8722012-04-07 03:04:20 +00001063 break;
1064 }
1065
1066 // Okay, the restriction seems to be what's limiting us.
1067
1068 // Use a special diagnostic for constructors and destructors.
1069 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D) ||
1070 (isa<FunctionTemplateDecl>(D) &&
1071 isa<CXXConstructorDecl>(
1072 cast<FunctionTemplateDecl>(D)->getTemplatedDecl()))) {
1073 S.Diag(D->getLocation(), diag::note_access_protected_restricted_ctordtor)
1074 << isa<CXXDestructorDecl>(D);
John McCallfe24e052010-09-03 04:56:05 +00001075 return true;
1076 }
John McCallb9abd8722012-04-07 03:04:20 +00001077
1078 // Otherwise, use the generic diagnostic.
1079 S.Diag(D->getLocation(), diag::note_access_protected_restricted_object)
1080 << S.Context.getTypeDeclType(ECRecord);
1081 return true;
John McCallfe24e052010-09-03 04:56:05 +00001082 }
1083
1084 return false;
1085}
1086
John McCall6b2accb2010-02-10 09:31:12 +00001087/// Diagnose the path which caused the given declaration or base class
1088/// to become inaccessible.
1089static void DiagnoseAccessPath(Sema &S,
1090 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +00001091 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001092 AccessSpecifier Access = Entity.getAccess();
John McCalldb73c682010-04-02 00:03:43 +00001093
John McCall161755a2010-04-06 21:38:20 +00001094 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
1095 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCalldb73c682010-04-02 00:03:43 +00001096
John McCall92f88312010-01-23 00:46:32 +00001097 // Easy case: the decl's natural access determined its path access.
John McCall6b2accb2010-02-10 09:31:12 +00001098 // We have to check against AS_private here in case Access is AS_none,
1099 // indicating a non-public member of a private base class.
John McCall6b2accb2010-02-10 09:31:12 +00001100 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
John McCall161755a2010-04-06 21:38:20 +00001101 switch (HasAccess(S, EC, DeclaringClass, D->getAccess(), Entity)) {
1102 case AR_inaccessible: {
John McCallfe24e052010-09-03 04:56:05 +00001103 if (Access == AS_protected &&
1104 TryDiagnoseProtectedAccess(S, EC, Entity))
1105 return;
1106
John McCallaa56a662010-10-20 08:15:06 +00001107 // Find an original declaration.
1108 while (D->isOutOfLine()) {
1109 NamedDecl *PrevDecl = 0;
Richard Smith162e1c12011-04-15 14:24:37 +00001110 if (VarDecl *VD = dyn_cast<VarDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001111 PrevDecl = VD->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001112 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001113 PrevDecl = FD->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001114 else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
Douglas Gregoref96ee02012-01-14 16:38:05 +00001115 PrevDecl = TND->getPreviousDecl();
Richard Smith162e1c12011-04-15 14:24:37 +00001116 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
John McCallaa56a662010-10-20 08:15:06 +00001117 if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1118 break;
Douglas Gregoref96ee02012-01-14 16:38:05 +00001119 PrevDecl = TD->getPreviousDecl();
John McCallaa56a662010-10-20 08:15:06 +00001120 }
1121 if (!PrevDecl) break;
1122 D = PrevDecl;
1123 }
1124
1125 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
1126 Decl *ImmediateChild;
1127 if (D->getDeclContext() == DeclaringClass)
1128 ImmediateChild = D;
1129 else {
1130 DeclContext *DC = D->getDeclContext();
1131 while (DC->getParent() != DeclaringClass)
1132 DC = DC->getParent();
1133 ImmediateChild = cast<Decl>(DC);
1134 }
1135
1136 // Check whether there's an AccessSpecDecl preceding this in the
1137 // chain of the DeclContext.
1138 bool Implicit = true;
1139 for (CXXRecordDecl::decl_iterator
1140 I = DeclaringClass->decls_begin(), E = DeclaringClass->decls_end();
1141 I != E; ++I) {
1142 if (*I == ImmediateChild) break;
1143 if (isa<AccessSpecDecl>(*I)) {
1144 Implicit = false;
1145 break;
1146 }
1147 }
1148
John McCall6b2accb2010-02-10 09:31:12 +00001149 S.Diag(D->getLocation(), diag::note_access_natural)
1150 << (unsigned) (Access == AS_protected)
John McCallaa56a662010-10-20 08:15:06 +00001151 << Implicit;
John McCall6b2accb2010-02-10 09:31:12 +00001152 return;
1153 }
1154
John McCall161755a2010-04-06 21:38:20 +00001155 case AR_accessible: break;
John McCall6b2accb2010-02-10 09:31:12 +00001156
John McCall161755a2010-04-06 21:38:20 +00001157 case AR_dependent:
1158 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001159 }
1160 }
1161
1162 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001163 CXXBasePath &Path = *FindBestPath(S, EC, Entity, AS_public, Paths);
John McCall6b2accb2010-02-10 09:31:12 +00001164
1165 CXXBasePath::iterator I = Path.end(), E = Path.begin();
1166 while (I != E) {
1167 --I;
1168
1169 const CXXBaseSpecifier *BS = I->Base;
1170 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
1171
1172 // If this is public inheritance, or the derived class is a friend,
1173 // skip this step.
1174 if (BaseAccess == AS_public)
1175 continue;
1176
1177 switch (GetFriendKind(S, EC, I->Class)) {
John McCall161755a2010-04-06 21:38:20 +00001178 case AR_accessible: continue;
1179 case AR_inaccessible: break;
1180 case AR_dependent:
1181 llvm_unreachable("can't diagnose dependent access failures");
John McCall6b2accb2010-02-10 09:31:12 +00001182 }
1183
1184 // Check whether this base specifier is the tighest point
1185 // constraining access. We have to check against AS_private for
1186 // the same reasons as above.
1187 if (BaseAccess == AS_private || BaseAccess >= Access) {
1188
1189 // We're constrained by inheritance, but we want to say
1190 // "declared private here" if we're diagnosing a hierarchy
1191 // conversion and this is the final step.
1192 unsigned diagnostic;
1193 if (D) diagnostic = diag::note_access_constrained_by_path;
1194 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
1195 else diagnostic = diag::note_access_constrained_by_path;
1196
1197 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
1198 << BS->getSourceRange()
1199 << (BaseAccess == AS_protected)
1200 << (BS->getAccessSpecifierAsWritten() == AS_none);
Douglas Gregor76ef6582010-05-28 04:34:55 +00001201
1202 if (D)
1203 S.Diag(D->getLocation(), diag::note_field_decl);
1204
John McCall6b2accb2010-02-10 09:31:12 +00001205 return;
1206 }
1207 }
1208
1209 llvm_unreachable("access not apparently constrained by path");
1210}
1211
John McCall58e6f342010-03-16 05:22:47 +00001212static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall6b2accb2010-02-10 09:31:12 +00001213 const EffectiveContext &EC,
John McCall161755a2010-04-06 21:38:20 +00001214 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001215 const CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCall161755a2010-04-06 21:38:20 +00001216 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
1217 NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0);
John McCalldb73c682010-04-02 00:03:43 +00001218
1219 S.Diag(Loc, Entity.getDiag())
1220 << (Entity.getAccess() == AS_protected)
1221 << (D ? D->getDeclName() : DeclarationName())
1222 << S.Context.getTypeDeclType(NamingClass)
1223 << S.Context.getTypeDeclType(DeclaringClass);
1224 DiagnoseAccessPath(S, EC, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001225}
1226
Francois Pichetb2ee8302011-05-23 03:43:44 +00001227/// MSVC has a bug where if during an using declaration name lookup,
1228/// the declaration found is unaccessible (private) and that declaration
1229/// was bring into scope via another using declaration whose target
1230/// declaration is accessible (public) then no error is generated.
1231/// Example:
1232/// class A {
1233/// public:
1234/// int f();
1235/// };
1236/// class B : public A {
1237/// private:
1238/// using A::f;
1239/// };
1240/// class C : public B {
1241/// private:
1242/// using B::f;
1243/// };
1244///
1245/// Here, B::f is private so this should fail in Standard C++, but
1246/// because B::f refers to A::f which is public MSVC accepts it.
1247static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S,
1248 SourceLocation AccessLoc,
1249 AccessTarget &Entity) {
1250 if (UsingShadowDecl *Shadow =
1251 dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) {
1252 const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl();
1253 if (Entity.getTargetDecl()->getAccess() == AS_private &&
1254 (OrigDecl->getAccess() == AS_public ||
1255 OrigDecl->getAccess() == AS_protected)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00001256 S.Diag(AccessLoc, diag::ext_ms_using_declaration_inaccessible)
Francois Pichetb2ee8302011-05-23 03:43:44 +00001257 << Shadow->getUsingDecl()->getQualifiedNameAsString()
1258 << OrigDecl->getQualifiedNameAsString();
1259 return true;
1260 }
1261 }
1262 return false;
1263}
1264
John McCalldb73c682010-04-02 00:03:43 +00001265/// Determines whether the accessed entity is accessible. Public members
1266/// have been weeded out by this point.
John McCall161755a2010-04-06 21:38:20 +00001267static AccessResult IsAccessible(Sema &S,
1268 const EffectiveContext &EC,
1269 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001270 // Determine the actual naming class.
1271 CXXRecordDecl *NamingClass = Entity.getNamingClass();
1272 while (NamingClass->isAnonymousStructOrUnion())
1273 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
1274 NamingClass = NamingClass->getCanonicalDecl();
John McCall6b2accb2010-02-10 09:31:12 +00001275
John McCalldb73c682010-04-02 00:03:43 +00001276 AccessSpecifier UnprivilegedAccess = Entity.getAccess();
1277 assert(UnprivilegedAccess != AS_public && "public access not weeded out");
1278
1279 // Before we try to recalculate access paths, try to white-list
1280 // accesses which just trade in on the final step, i.e. accesses
1281 // which don't require [M4] or [B4]. These are by far the most
John McCall161755a2010-04-06 21:38:20 +00001282 // common forms of privileged access.
John McCalldb73c682010-04-02 00:03:43 +00001283 if (UnprivilegedAccess != AS_none) {
John McCall161755a2010-04-06 21:38:20 +00001284 switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) {
1285 case AR_dependent:
John McCalldb73c682010-04-02 00:03:43 +00001286 // This is actually an interesting policy decision. We don't
1287 // *have* to delay immediately here: we can do the full access
1288 // calculation in the hope that friendship on some intermediate
1289 // class will make the declaration accessible non-dependently.
1290 // But that's not cheap, and odds are very good (note: assertion
1291 // made without data) that the friend declaration will determine
1292 // access.
John McCall161755a2010-04-06 21:38:20 +00001293 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001294
John McCall161755a2010-04-06 21:38:20 +00001295 case AR_accessible: return AR_accessible;
1296 case AR_inaccessible: break;
John McCalldb73c682010-04-02 00:03:43 +00001297 }
1298 }
1299
John McCall161755a2010-04-06 21:38:20 +00001300 AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext();
John McCall6b2accb2010-02-10 09:31:12 +00001301
John McCalldb73c682010-04-02 00:03:43 +00001302 // We lower member accesses to base accesses by pretending that the
1303 // member is a base class of its declaring class.
1304 AccessSpecifier FinalAccess;
1305
John McCall6b2accb2010-02-10 09:31:12 +00001306 if (Entity.isMemberAccess()) {
John McCalldb73c682010-04-02 00:03:43 +00001307 // Determine if the declaration is accessible from EC when named
1308 // in its declaring class.
John McCall6b2accb2010-02-10 09:31:12 +00001309 NamedDecl *Target = Entity.getTargetDecl();
John McCall161755a2010-04-06 21:38:20 +00001310 const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass();
John McCall6b2accb2010-02-10 09:31:12 +00001311
John McCalldb73c682010-04-02 00:03:43 +00001312 FinalAccess = Target->getAccess();
John McCall161755a2010-04-06 21:38:20 +00001313 switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
1314 case AR_accessible:
1315 FinalAccess = AS_public;
1316 break;
1317 case AR_inaccessible: break;
1318 case AR_dependent: return AR_dependent; // see above
John McCall6b2accb2010-02-10 09:31:12 +00001319 }
1320
John McCalldb73c682010-04-02 00:03:43 +00001321 if (DeclaringClass == NamingClass)
John McCall161755a2010-04-06 21:38:20 +00001322 return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
1323
1324 Entity.suppressInstanceContext();
John McCalldb73c682010-04-02 00:03:43 +00001325 } else {
1326 FinalAccess = AS_public;
John McCall6b2accb2010-02-10 09:31:12 +00001327 }
1328
John McCall161755a2010-04-06 21:38:20 +00001329 assert(Entity.getDeclaringClass() != NamingClass);
John McCall6b2accb2010-02-10 09:31:12 +00001330
1331 // Append the declaration's access if applicable.
1332 CXXBasePaths Paths;
John McCall161755a2010-04-06 21:38:20 +00001333 CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths);
John McCall0c01d182010-03-24 05:22:00 +00001334 if (!Path)
John McCall161755a2010-04-06 21:38:20 +00001335 return AR_dependent;
John McCall92f88312010-01-23 00:46:32 +00001336
John McCalldb73c682010-04-02 00:03:43 +00001337 assert(Path->Access <= UnprivilegedAccess &&
1338 "access along best path worse than direct?");
1339 if (Path->Access == AS_public)
John McCall161755a2010-04-06 21:38:20 +00001340 return AR_accessible;
1341 return AR_inaccessible;
John McCall0c01d182010-03-24 05:22:00 +00001342}
1343
John McCall161755a2010-04-06 21:38:20 +00001344static void DelayDependentAccess(Sema &S,
1345 const EffectiveContext &EC,
1346 SourceLocation Loc,
1347 const AccessTarget &Entity) {
John McCall0c01d182010-03-24 05:22:00 +00001348 assert(EC.isDependent() && "delaying non-dependent access");
John McCall7ad650f2010-03-24 07:46:06 +00001349 DeclContext *DC = EC.getInnerContext();
John McCall0c01d182010-03-24 05:22:00 +00001350 assert(DC->isDependentContext() && "delaying non-dependent access");
1351 DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
1352 Loc,
1353 Entity.isMemberAccess(),
1354 Entity.getAccess(),
1355 Entity.getTargetDecl(),
1356 Entity.getNamingClass(),
John McCall161755a2010-04-06 21:38:20 +00001357 Entity.getBaseObjectType(),
John McCall0c01d182010-03-24 05:22:00 +00001358 Entity.getDiag());
John McCall92f88312010-01-23 00:46:32 +00001359}
1360
John McCall6b2accb2010-02-10 09:31:12 +00001361/// Checks access to an entity from the given effective context.
John McCall161755a2010-04-06 21:38:20 +00001362static AccessResult CheckEffectiveAccess(Sema &S,
1363 const EffectiveContext &EC,
1364 SourceLocation Loc,
1365 AccessTarget &Entity) {
John McCalldb73c682010-04-02 00:03:43 +00001366 assert(Entity.getAccess() != AS_public && "called for public access!");
John McCall92f88312010-01-23 00:46:32 +00001367
David Blaikie4e4d0842012-03-11 07:00:24 +00001368 if (S.getLangOpts().MicrosoftMode &&
Francois Pichetb2ee8302011-05-23 03:43:44 +00001369 IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity))
1370 return AR_accessible;
1371
John McCalldb73c682010-04-02 00:03:43 +00001372 switch (IsAccessible(S, EC, Entity)) {
John McCall161755a2010-04-06 21:38:20 +00001373 case AR_dependent:
1374 DelayDependentAccess(S, EC, Loc, Entity);
1375 return AR_dependent;
John McCalldb73c682010-04-02 00:03:43 +00001376
John McCall161755a2010-04-06 21:38:20 +00001377 case AR_inaccessible:
John McCalldb73c682010-04-02 00:03:43 +00001378 if (!Entity.isQuiet())
1379 DiagnoseBadAccess(S, Loc, EC, Entity);
John McCall161755a2010-04-06 21:38:20 +00001380 return AR_inaccessible;
John McCalldb73c682010-04-02 00:03:43 +00001381
John McCall161755a2010-04-06 21:38:20 +00001382 case AR_accessible:
1383 return AR_accessible;
John McCall0c01d182010-03-24 05:22:00 +00001384 }
1385
John McCall161755a2010-04-06 21:38:20 +00001386 // silence unnecessary warning
1387 llvm_unreachable("invalid access result");
John McCall6b2accb2010-02-10 09:31:12 +00001388}
John McCall92f88312010-01-23 00:46:32 +00001389
John McCall6b2accb2010-02-10 09:31:12 +00001390static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCall161755a2010-04-06 21:38:20 +00001391 AccessTarget &Entity) {
John McCall6b2accb2010-02-10 09:31:12 +00001392 // If the access path is public, it's accessible everywhere.
1393 if (Entity.getAccess() == AS_public)
1394 return Sema::AR_accessible;
John McCall92f88312010-01-23 00:46:32 +00001395
John McCalleee1d542011-02-14 07:13:47 +00001396 // If we're currently parsing a declaration, we may need to delay
1397 // access control checking, because our effective context might be
1398 // different based on what the declaration comes out as.
1399 //
1400 // For example, we might be parsing a declaration with a scope
1401 // specifier, like this:
1402 // A::private_type A::foo() { ... }
1403 //
1404 // Or we might be parsing something that will turn out to be a friend:
1405 // void foo(A::private_type);
1406 // void B::foo(A::private_type);
1407 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1408 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
John McCall6b2accb2010-02-10 09:31:12 +00001409 return Sema::AR_delayed;
John McCall92f88312010-01-23 00:46:32 +00001410 }
1411
John McCall161755a2010-04-06 21:38:20 +00001412 EffectiveContext EC(S.CurContext);
1413 switch (CheckEffectiveAccess(S, EC, Loc, Entity)) {
1414 case AR_accessible: return Sema::AR_accessible;
1415 case AR_inaccessible: return Sema::AR_inaccessible;
1416 case AR_dependent: return Sema::AR_dependent;
1417 }
1418 llvm_unreachable("falling off end");
John McCall92f88312010-01-23 00:46:32 +00001419}
1420
John McCall4bfd6802011-02-15 22:51:53 +00001421void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *decl) {
1422 // Access control for names used in the declarations of functions
1423 // and function templates should normally be evaluated in the context
1424 // of the declaration, just in case it's a friend of something.
1425 // However, this does not apply to local extern declarations.
1426
1427 DeclContext *DC = decl->getDeclContext();
1428 if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
1429 if (!DC->isFunctionOrMethod()) DC = fn;
1430 } else if (FunctionTemplateDecl *fnt = dyn_cast<FunctionTemplateDecl>(decl)) {
1431 // Never a local declaration.
1432 DC = fnt->getTemplatedDecl();
1433 }
1434
Chandler Carruth630eb012010-04-18 08:23:21 +00001435 EffectiveContext EC(DC);
John McCall2f514482010-01-27 03:50:35 +00001436
John McCall161755a2010-04-06 21:38:20 +00001437 AccessTarget Target(DD.getAccessData());
1438
1439 if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible)
John McCall2f514482010-01-27 03:50:35 +00001440 DD.Triggered = true;
1441}
1442
John McCall0c01d182010-03-24 05:22:00 +00001443void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD,
1444 const MultiLevelTemplateArgumentList &TemplateArgs) {
1445 SourceLocation Loc = DD.getAccessLoc();
1446 AccessSpecifier Access = DD.getAccess();
1447
1448 Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(),
1449 TemplateArgs);
1450 if (!NamingD) return;
1451 Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(),
1452 TemplateArgs);
1453 if (!TargetD) return;
1454
1455 if (DD.isAccessToMember()) {
John McCall161755a2010-04-06 21:38:20 +00001456 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD);
1457 NamedDecl *TargetDecl = cast<NamedDecl>(TargetD);
1458 QualType BaseObjectType = DD.getAccessBaseObjectType();
1459 if (!BaseObjectType.isNull()) {
1460 BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc,
1461 DeclarationName());
1462 if (BaseObjectType.isNull()) return;
1463 }
1464
1465 AccessTarget Entity(Context,
1466 AccessTarget::Member,
1467 NamingClass,
1468 DeclAccessPair::make(TargetDecl, Access),
1469 BaseObjectType);
John McCall0c01d182010-03-24 05:22:00 +00001470 Entity.setDiag(DD.getDiagnostic());
1471 CheckAccess(*this, Loc, Entity);
1472 } else {
John McCall161755a2010-04-06 21:38:20 +00001473 AccessTarget Entity(Context,
1474 AccessTarget::Base,
1475 cast<CXXRecordDecl>(TargetD),
1476 cast<CXXRecordDecl>(NamingD),
1477 Access);
John McCall0c01d182010-03-24 05:22:00 +00001478 Entity.setDiag(DD.getDiagnostic());
1479 CheckAccess(*this, Loc, Entity);
1480 }
1481}
1482
John McCall6b2accb2010-02-10 09:31:12 +00001483Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001484 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001485 if (!getLangOpts().AccessControl ||
John McCall58e6f342010-03-16 05:22:47 +00001486 !E->getNamingClass() ||
John McCall9aa472c2010-03-19 07:35:19 +00001487 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001488 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001489
John McCall161755a2010-04-06 21:38:20 +00001490 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1491 Found, QualType());
John McCall58e6f342010-03-16 05:22:47 +00001492 Entity.setDiag(diag::err_access) << E->getSourceRange();
1493
1494 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001495}
1496
1497/// Perform access-control checking on a previously-unresolved member
1498/// access which has now been resolved to a member.
John McCall6b2accb2010-02-10 09:31:12 +00001499Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
John McCall9aa472c2010-03-19 07:35:19 +00001500 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001501 if (!getLangOpts().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001502 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001503 return AR_accessible;
John McCallc373d482010-01-27 01:50:18 +00001504
John McCall161755a2010-04-06 21:38:20 +00001505 QualType BaseType = E->getBaseType();
1506 if (E->isArrow())
1507 BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1508
1509 AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(),
1510 Found, BaseType);
John McCall58e6f342010-03-16 05:22:47 +00001511 Entity.setDiag(diag::err_access) << E->getSourceRange();
1512
1513 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCallc373d482010-01-27 01:50:18 +00001514}
1515
John McCall12d8d802012-04-09 20:53:23 +00001516/// Is the given special member function accessible for the purposes of
1517/// deciding whether to define a special member function as deleted?
1518bool Sema::isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl,
1519 AccessSpecifier access,
1520 QualType objectType) {
1521 // Fast path.
1522 if (access == AS_public || !getLangOpts().AccessControl) return true;
1523
1524 AccessTarget entity(Context, AccessTarget::Member, decl->getParent(),
1525 DeclAccessPair::make(decl, access), objectType);
1526
1527 // Suppress diagnostics.
1528 entity.setDiag(PDiag());
1529
1530 switch (CheckAccess(*this, SourceLocation(), entity)) {
1531 case AR_accessible: return true;
1532 case AR_inaccessible: return false;
1533 case AR_dependent: llvm_unreachable("dependent for =delete computation");
1534 case AR_delayed: llvm_unreachable("cannot delay =delete computation");
1535 }
1536 llvm_unreachable("bad access result");
1537}
1538
John McCall6b2accb2010-02-10 09:31:12 +00001539Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall58e6f342010-03-16 05:22:47 +00001540 CXXDestructorDecl *Dtor,
John McCallb9abd8722012-04-07 03:04:20 +00001541 const PartialDiagnostic &PDiag,
1542 QualType ObjectTy) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001543 if (!getLangOpts().AccessControl)
John McCall6b2accb2010-02-10 09:31:12 +00001544 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001545
John McCall58e6f342010-03-16 05:22:47 +00001546 // There's never a path involved when checking implicit destructor access.
John McCall4f9506a2010-02-02 08:45:54 +00001547 AccessSpecifier Access = Dtor->getAccess();
1548 if (Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001549 return AR_accessible;
John McCall4f9506a2010-02-02 08:45:54 +00001550
John McCall58e6f342010-03-16 05:22:47 +00001551 CXXRecordDecl *NamingClass = Dtor->getParent();
John McCallb9abd8722012-04-07 03:04:20 +00001552 if (ObjectTy.isNull()) ObjectTy = Context.getTypeDeclType(NamingClass);
1553
John McCall161755a2010-04-06 21:38:20 +00001554 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1555 DeclAccessPair::make(Dtor, Access),
John McCallb9abd8722012-04-07 03:04:20 +00001556 ObjectTy);
John McCall58e6f342010-03-16 05:22:47 +00001557 Entity.setDiag(PDiag); // TODO: avoid copy
1558
1559 return CheckAccess(*this, Loc, Entity);
John McCall4f9506a2010-02-02 08:45:54 +00001560}
1561
John McCallb13b7372010-02-01 03:16:54 +00001562/// Checks access to a constructor.
John McCall6b2accb2010-02-10 09:31:12 +00001563Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00001564 CXXConstructorDecl *Constructor,
1565 const InitializedEntity &Entity,
1566 AccessSpecifier Access,
1567 bool IsCopyBindingRefToTemp) {
John McCallb9abd8722012-04-07 03:04:20 +00001568 if (!getLangOpts().AccessControl || Access == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001569 return AR_accessible;
John McCallb13b7372010-02-01 03:16:54 +00001570
Sean Huntb320e0c2011-06-10 03:50:41 +00001571 PartialDiagnostic PD(PDiag());
Anders Carlsson9a68a672010-04-21 18:47:17 +00001572 switch (Entity.getKind()) {
1573 default:
Sean Huntb320e0c2011-06-10 03:50:41 +00001574 PD = PDiag(IsCopyBindingRefToTemp
1575 ? diag::ext_rvalue_to_reference_access_ctor
1576 : diag::err_access_ctor);
1577
Anders Carlsson9a68a672010-04-21 18:47:17 +00001578 break;
John McCall58e6f342010-03-16 05:22:47 +00001579
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001580 case InitializedEntity::EK_Base:
Sean Huntb320e0c2011-06-10 03:50:41 +00001581 PD = PDiag(diag::err_access_base_ctor);
1582 PD << Entity.isInheritedVirtualBase()
1583 << Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor);
Anders Carlsson9a68a672010-04-21 18:47:17 +00001584 break;
Anders Carlsson3b8c53b2010-04-22 05:40:53 +00001585
Anders Carlssonb99c6662010-04-21 20:28:29 +00001586 case InitializedEntity::EK_Member: {
1587 const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
Sean Huntb320e0c2011-06-10 03:50:41 +00001588 PD = PDiag(diag::err_access_field_ctor);
1589 PD << Field->getType() << getSpecialMember(Constructor);
Anders Carlssonb99c6662010-04-21 20:28:29 +00001590 break;
1591 }
Anders Carlsson9a68a672010-04-21 18:47:17 +00001592
Douglas Gregor47736542012-02-15 16:57:26 +00001593 case InitializedEntity::EK_LambdaCapture: {
1594 const VarDecl *Var = Entity.getCapturedVar();
1595 PD = PDiag(diag::err_access_lambda_capture);
1596 PD << Var->getName() << Entity.getType() << getSpecialMember(Constructor);
1597 break;
1598 }
1599
Anders Carlsson711f34a2010-04-21 19:52:01 +00001600 }
1601
John McCallb9abd8722012-04-07 03:04:20 +00001602 return CheckConstructorAccess(UseLoc, Constructor, Entity, Access, PD);
Sean Huntb320e0c2011-06-10 03:50:41 +00001603}
1604
1605/// Checks access to a constructor.
1606Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
1607 CXXConstructorDecl *Constructor,
John McCallb9abd8722012-04-07 03:04:20 +00001608 const InitializedEntity &Entity,
Sean Huntb320e0c2011-06-10 03:50:41 +00001609 AccessSpecifier Access,
John McCallb9abd8722012-04-07 03:04:20 +00001610 const PartialDiagnostic &PD) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001611 if (!getLangOpts().AccessControl ||
Sean Huntb320e0c2011-06-10 03:50:41 +00001612 Access == AS_public)
1613 return AR_accessible;
1614
1615 CXXRecordDecl *NamingClass = Constructor->getParent();
John McCallb9abd8722012-04-07 03:04:20 +00001616
1617 // Initializing a base sub-object is an instance method call on an
1618 // object of the derived class. Otherwise, we have an instance method
1619 // call on an object of the constructed type.
1620 CXXRecordDecl *ObjectClass;
1621 if (Entity.getKind() == InitializedEntity::EK_Base) {
1622 ObjectClass = cast<CXXConstructorDecl>(CurContext)->getParent();
1623 } else {
1624 ObjectClass = NamingClass;
1625 }
1626
Sean Huntb320e0c2011-06-10 03:50:41 +00001627 AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
1628 DeclAccessPair::make(Constructor, Access),
John McCallb9abd8722012-04-07 03:04:20 +00001629 Context.getTypeDeclType(ObjectClass));
Sean Huntb320e0c2011-06-10 03:50:41 +00001630 AccessEntity.setDiag(PD);
1631
Anders Carlsson9a68a672010-04-21 18:47:17 +00001632 return CheckAccess(*this, UseLoc, AccessEntity);
John McCallb9abd8722012-04-07 03:04:20 +00001633}
John McCallb13b7372010-02-01 03:16:54 +00001634
John McCallb0207482010-03-16 06:11:48 +00001635/// Checks direct (i.e. non-inherited) access to an arbitrary class
1636/// member.
1637Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
1638 NamedDecl *Target,
1639 const PartialDiagnostic &Diag) {
1640 AccessSpecifier Access = Target->getAccess();
David Blaikie4e4d0842012-03-11 07:00:24 +00001641 if (!getLangOpts().AccessControl ||
John McCallb0207482010-03-16 06:11:48 +00001642 Access == AS_public)
1643 return AR_accessible;
1644
1645 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
John McCall161755a2010-04-06 21:38:20 +00001646 AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
1647 DeclAccessPair::make(Target, Access),
1648 QualType());
John McCallb0207482010-03-16 06:11:48 +00001649 Entity.setDiag(Diag);
1650 return CheckAccess(*this, UseLoc, Entity);
1651}
1652
1653
John McCall90c8c572010-03-18 08:19:33 +00001654/// Checks access to an overloaded operator new or delete.
1655Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc,
1656 SourceRange PlacementRange,
1657 CXXRecordDecl *NamingClass,
Sean Huntcb45a0f2011-05-12 22:46:25 +00001658 DeclAccessPair Found,
1659 bool Diagnose) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001660 if (!getLangOpts().AccessControl ||
John McCall90c8c572010-03-18 08:19:33 +00001661 !NamingClass ||
John McCall9aa472c2010-03-19 07:35:19 +00001662 Found.getAccess() == AS_public)
John McCall90c8c572010-03-18 08:19:33 +00001663 return AR_accessible;
1664
John McCall161755a2010-04-06 21:38:20 +00001665 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1666 QualType());
Sean Huntcb45a0f2011-05-12 22:46:25 +00001667 if (Diagnose)
1668 Entity.setDiag(diag::err_access)
1669 << PlacementRange;
John McCall90c8c572010-03-18 08:19:33 +00001670
1671 return CheckAccess(*this, OpLoc, Entity);
1672}
1673
John McCallb13b7372010-02-01 03:16:54 +00001674/// Checks access to an overloaded member operator, including
1675/// conversion operators.
John McCall6b2accb2010-02-10 09:31:12 +00001676Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
1677 Expr *ObjectExpr,
John McCall58e6f342010-03-16 05:22:47 +00001678 Expr *ArgExpr,
John McCall9aa472c2010-03-19 07:35:19 +00001679 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001680 if (!getLangOpts().AccessControl ||
John McCall9aa472c2010-03-19 07:35:19 +00001681 Found.getAccess() == AS_public)
John McCall6b2accb2010-02-10 09:31:12 +00001682 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001683
John McCallca82a822011-09-21 08:36:56 +00001684 const RecordType *RT = ObjectExpr->getType()->castAs<RecordType>();
John McCall5357b612010-01-28 01:42:12 +00001685 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
1686
John McCall161755a2010-04-06 21:38:20 +00001687 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
1688 ObjectExpr->getType());
John McCall58e6f342010-03-16 05:22:47 +00001689 Entity.setDiag(diag::err_access)
1690 << ObjectExpr->getSourceRange()
1691 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
1692
1693 return CheckAccess(*this, OpLoc, Entity);
John McCall6b2accb2010-02-10 09:31:12 +00001694}
John McCall5357b612010-01-28 01:42:12 +00001695
John McCall6bb80172010-03-30 21:47:33 +00001696Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr,
1697 DeclAccessPair Found) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (!getLangOpts().AccessControl ||
John McCalle2f5ba92010-03-30 22:20:00 +00001699 Found.getAccess() == AS_none ||
John McCall6bb80172010-03-30 21:47:33 +00001700 Found.getAccess() == AS_public)
1701 return AR_accessible;
1702
John McCall9c72c602010-08-27 09:08:28 +00001703 OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression;
John McCalle9ee23e2010-04-22 18:44:12 +00001704 CXXRecordDecl *NamingClass = Ovl->getNamingClass();
John McCall6bb80172010-03-30 21:47:33 +00001705
John McCall161755a2010-04-06 21:38:20 +00001706 AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found,
John McCallb9abd8722012-04-07 03:04:20 +00001707 /*no instance context*/ QualType());
John McCall6bb80172010-03-30 21:47:33 +00001708 Entity.setDiag(diag::err_access)
1709 << Ovl->getSourceRange();
1710
1711 return CheckAccess(*this, Ovl->getNameLoc(), Entity);
1712}
1713
John McCall6b2accb2010-02-10 09:31:12 +00001714/// Checks access for a hierarchy conversion.
1715///
John McCall6b2accb2010-02-10 09:31:12 +00001716/// \param ForceCheck true if this check should be performed even if access
1717/// control is disabled; some things rely on this for semantics
1718/// \param ForceUnprivileged true if this check should proceed as if the
1719/// context had no special privileges
John McCall6b2accb2010-02-10 09:31:12 +00001720Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall6b2accb2010-02-10 09:31:12 +00001721 QualType Base,
1722 QualType Derived,
1723 const CXXBasePath &Path,
John McCall58e6f342010-03-16 05:22:47 +00001724 unsigned DiagID,
John McCall6b2accb2010-02-10 09:31:12 +00001725 bool ForceCheck,
John McCall58e6f342010-03-16 05:22:47 +00001726 bool ForceUnprivileged) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001727 if (!ForceCheck && !getLangOpts().AccessControl)
John McCall6b2accb2010-02-10 09:31:12 +00001728 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001729
John McCall6b2accb2010-02-10 09:31:12 +00001730 if (Path.Access == AS_public)
1731 return AR_accessible;
John McCall5357b612010-01-28 01:42:12 +00001732
John McCall6b2accb2010-02-10 09:31:12 +00001733 CXXRecordDecl *BaseD, *DerivedD;
1734 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
1735 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall58e6f342010-03-16 05:22:47 +00001736
John McCall161755a2010-04-06 21:38:20 +00001737 AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD,
1738 Path.Access);
John McCall58e6f342010-03-16 05:22:47 +00001739 if (DiagID)
1740 Entity.setDiag(DiagID) << Derived << Base;
John McCall6b2accb2010-02-10 09:31:12 +00001741
John McCall161755a2010-04-06 21:38:20 +00001742 if (ForceUnprivileged) {
1743 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1744 AccessLoc, Entity)) {
1745 case ::AR_accessible: return Sema::AR_accessible;
1746 case ::AR_inaccessible: return Sema::AR_inaccessible;
1747 case ::AR_dependent: return Sema::AR_dependent;
1748 }
1749 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1750 }
John McCall58e6f342010-03-16 05:22:47 +00001751 return CheckAccess(*this, AccessLoc, Entity);
John McCall5357b612010-01-28 01:42:12 +00001752}
1753
John McCall92f88312010-01-23 00:46:32 +00001754/// Checks access to all the declarations in the given result set.
John McCall6b2accb2010-02-10 09:31:12 +00001755void Sema::CheckLookupAccess(const LookupResult &R) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001756 assert(getLangOpts().AccessControl
John McCall6b2accb2010-02-10 09:31:12 +00001757 && "performing access check without access control");
1758 assert(R.getNamingClass() && "performing access check without naming class");
1759
John McCall58e6f342010-03-16 05:22:47 +00001760 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1761 if (I.getAccess() != AS_public) {
John McCall161755a2010-04-06 21:38:20 +00001762 AccessTarget Entity(Context, AccessedEntity::Member,
1763 R.getNamingClass(), I.getPair(),
Erik Verbruggen24dd9ad2011-09-19 15:10:40 +00001764 R.getBaseObjectType());
John McCall58e6f342010-03-16 05:22:47 +00001765 Entity.setDiag(diag::err_access);
John McCall58e6f342010-03-16 05:22:47 +00001766 CheckAccess(*this, R.getNameLoc(), Entity);
1767 }
1768 }
John McCall92f88312010-01-23 00:46:32 +00001769}
Chandler Carruth926c4b42010-06-28 08:39:25 +00001770
Erik Verbruggend1205962011-10-06 07:27:49 +00001771/// Checks access to Decl from the given class. The check will take access
1772/// specifiers into account, but no member access expressions and such.
1773///
1774/// \param Decl the declaration to check if it can be accessed
1775/// \param Class the class/context from which to start the search
1776/// \return true if the Decl is accessible from the Class, false otherwise.
Douglas Gregor17015ef2011-11-03 16:51:37 +00001777bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) {
1778 if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) {
Douglas Gregora885dce2011-11-03 17:41:55 +00001779 if (!Decl->isCXXClassMember())
Douglas Gregor17015ef2011-11-03 16:51:37 +00001780 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001781
Douglas Gregor17015ef2011-11-03 16:51:37 +00001782 QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal();
1783 AccessTarget Entity(Context, AccessedEntity::Member, Class,
1784 DeclAccessPair::make(Decl, Decl->getAccess()),
1785 qType);
1786 if (Entity.getAccess() == AS_public)
1787 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001788
Douglas Gregor17015ef2011-11-03 16:51:37 +00001789 EffectiveContext EC(CurContext);
1790 return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible;
1791 }
1792
Douglas Gregorf3c02862011-11-03 19:00:24 +00001793 if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) {
1794 // @public and @package ivars are always accessible.
1795 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public ||
1796 Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package)
1797 return true;
1798
1799
1800
1801 // If we are inside a class or category implementation, determine the
1802 // interface we're in.
1803 ObjCInterfaceDecl *ClassOfMethodDecl = 0;
1804 if (ObjCMethodDecl *MD = getCurMethodDecl())
1805 ClassOfMethodDecl = MD->getClassInterface();
1806 else if (FunctionDecl *FD = getCurFunctionDecl()) {
1807 if (ObjCImplDecl *Impl
1808 = dyn_cast<ObjCImplDecl>(FD->getLexicalDeclContext())) {
1809 if (ObjCImplementationDecl *IMPD
1810 = dyn_cast<ObjCImplementationDecl>(Impl))
1811 ClassOfMethodDecl = IMPD->getClassInterface();
1812 else if (ObjCCategoryImplDecl* CatImplClass
1813 = dyn_cast<ObjCCategoryImplDecl>(Impl))
1814 ClassOfMethodDecl = CatImplClass->getClassInterface();
1815 }
1816 }
1817
1818 // If we're not in an interface, this ivar is inaccessible.
1819 if (!ClassOfMethodDecl)
1820 return false;
1821
1822 // If we're inside the same interface that owns the ivar, we're fine.
Douglas Gregor60ef3082011-12-15 00:29:59 +00001823 if (declaresSameEntity(ClassOfMethodDecl, Ivar->getContainingInterface()))
Douglas Gregorf3c02862011-11-03 19:00:24 +00001824 return true;
1825
1826 // If the ivar is private, it's inaccessible.
1827 if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Private)
1828 return false;
1829
1830 return Ivar->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl);
1831 }
1832
Douglas Gregor17015ef2011-11-03 16:51:37 +00001833 return true;
Erik Verbruggend1205962011-10-06 07:27:49 +00001834}