blob: 6c01fee74c32478d290017c2958ad8a95970d3c8 [file] [log] [blame]
Anders Carlsson4742a9c2009-03-27 05:05:05 +00001//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
Anders Carlsson8ed6f362009-03-27 04:43:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides Sema routines for C++ access control semantics.
11//
12//===----------------------------------------------------------------------===//
Anders Carlsson17941122009-03-27 04:54:36 +000013
14#include "Sema.h"
John McCall553c0792010-01-23 00:46:32 +000015#include "Lookup.h"
Anders Carlsson733d77f2009-03-27 06:03:27 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000017#include "clang/AST/CXXInheritance.h"
18#include "clang/AST/DeclCXX.h"
John McCall16927f62010-03-12 01:19:31 +000019#include "clang/AST/DeclFriend.h"
John McCall58cc69d2010-01-27 01:50:18 +000020#include "clang/AST/ExprCXX.h"
21
Anders Carlsson17941122009-03-27 04:54:36 +000022using namespace clang;
23
Anders Carlsson4742a9c2009-03-27 05:05:05 +000024/// SetMemberAccessSpecifier - Set the access specifier of a member.
25/// Returns true on error (when the previous member decl access specifier
26/// is different from the new member decl access specifier).
Mike Stump11289f42009-09-09 15:08:12 +000027bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
Anders Carlsson17941122009-03-27 04:54:36 +000028 NamedDecl *PrevMemberDecl,
29 AccessSpecifier LexicalAS) {
30 if (!PrevMemberDecl) {
31 // Use the lexical access specifier.
32 MemberDecl->setAccess(LexicalAS);
33 return false;
34 }
Mike Stump11289f42009-09-09 15:08:12 +000035
Anders Carlsson17941122009-03-27 04:54:36 +000036 // C++ [class.access.spec]p3: When a member is redeclared its access
37 // specifier must be same as its initial declaration.
38 if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) {
Mike Stump11289f42009-09-09 15:08:12 +000039 Diag(MemberDecl->getLocation(),
40 diag::err_class_redeclared_with_different_access)
Anders Carlsson17941122009-03-27 04:54:36 +000041 << MemberDecl << LexicalAS;
42 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
43 << PrevMemberDecl << PrevMemberDecl->getAccess();
John McCall0a4bb262009-12-23 00:37:40 +000044
45 MemberDecl->setAccess(LexicalAS);
Anders Carlsson17941122009-03-27 04:54:36 +000046 return true;
47 }
Mike Stump11289f42009-09-09 15:08:12 +000048
Anders Carlsson17941122009-03-27 04:54:36 +000049 MemberDecl->setAccess(PrevMemberDecl->getAccess());
50 return false;
51}
Anders Carlsson4742a9c2009-03-27 05:05:05 +000052
John McCall5b0829a2010-02-10 09:31:12 +000053namespace {
54struct EffectiveContext {
John McCallfb803d72010-03-17 04:58:56 +000055 EffectiveContext() : Function(0) {}
Anders Carlsson733d77f2009-03-27 06:03:27 +000056
John McCall5b0829a2010-02-10 09:31:12 +000057 explicit EffectiveContext(DeclContext *DC) {
58 if (isa<FunctionDecl>(DC)) {
John McCall16927f62010-03-12 01:19:31 +000059 Function = cast<FunctionDecl>(DC)->getCanonicalDecl();
John McCall5b0829a2010-02-10 09:31:12 +000060 DC = Function->getDeclContext();
61 } else
62 Function = 0;
John McCallfb803d72010-03-17 04:58:56 +000063
64 // C++ [class.access.nest]p1:
65 // A nested class is a member and as such has the same access
66 // rights as any other member.
67 // C++ [class.access]p2:
68 // A member of a class can also access all the names to which
69 // the class has access.
70 // This implies that the privileges of nesting are transitive.
71 while (isa<CXXRecordDecl>(DC)) {
72 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
73 Records.push_back(Record);
74 DC = Record->getDeclContext();
75 }
Anders Carlsson733d77f2009-03-27 06:03:27 +000076 }
Sebastian Redle644e192009-07-18 14:32:15 +000077
John McCallfb803d72010-03-17 04:58:56 +000078 bool includesClass(const CXXRecordDecl *R) const {
79 R = R->getCanonicalDecl();
80 return std::find(Records.begin(), Records.end(), R)
81 != Records.end();
John McCall5b0829a2010-02-10 09:31:12 +000082 }
83
John McCallfb803d72010-03-17 04:58:56 +000084 llvm::SmallVector<CXXRecordDecl*, 4> Records;
John McCall5b0829a2010-02-10 09:31:12 +000085 FunctionDecl *Function;
86};
Anders Carlsson4742a9c2009-03-27 05:05:05 +000087}
John McCall553c0792010-01-23 00:46:32 +000088
John McCall5b0829a2010-02-10 09:31:12 +000089static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) {
90 CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(D->getDeclContext());
91 while (DeclaringClass->isAnonymousStructOrUnion())
92 DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext());
93 return DeclaringClass;
94}
95
John McCall39e82882010-03-17 20:01:29 +000096static Sema::AccessResult MatchesFriend(Sema &S,
97 const EffectiveContext &EC,
98 const CXXRecordDecl *Friend) {
99 // FIXME: close matches becuse of dependency
100 if (EC.includesClass(Friend))
101 return Sema::AR_accessible;
102
103 return Sema::AR_inaccessible;
104}
105
106static Sema::AccessResult MatchesFriend(Sema &S,
107 const EffectiveContext &EC,
108 FriendDecl *Friend) {
109 if (Type *T = Friend->getFriendType()) {
110 CanQualType CT = T->getCanonicalTypeUnqualified();
111 if (const RecordType *RT = CT->getAs<RecordType>())
112 return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl()));
113
114 // TODO: we can fail early for a lot of type classes.
115 if (T->isDependentType())
116 return Sema::AR_dependent;
117
118 return Sema::AR_inaccessible;
119 }
120
121 NamedDecl *D
122 = cast<NamedDecl>(Friend->getFriendDecl()->getCanonicalDecl());
123
124 // FIXME: declarations with dependent or templated scope.
125
126 // For class templates, we want to check whether any of the records
127 // are possible specializations of the template.
128 if (isa<ClassTemplateDecl>(D)) {
129 for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator
130 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) {
131 CXXRecordDecl *Record = *I;
132 ClassTemplateDecl *CTD;
133
134 // A specialization of the template...
135 if (isa<ClassTemplateSpecializationDecl>(Record)) {
136 CTD = cast<ClassTemplateSpecializationDecl>(Record)
137 ->getSpecializedTemplate();
138
139 // ... or the template pattern itself.
140 } else {
141 CTD = Record->getDescribedClassTemplate();
142 }
143
144 if (CTD && D == CTD->getCanonicalDecl())
145 return Sema::AR_accessible;
146 }
147
148 return Sema::AR_inaccessible;
149 }
150
151 // Same thing for function templates.
152 if (isa<FunctionTemplateDecl>(D)) {
153 if (!EC.Function) return Sema::AR_inaccessible;
154
155 FunctionTemplateDecl *FTD = EC.Function->getPrimaryTemplate();
156 if (!FTD)
157 FTD = EC.Function->getDescribedFunctionTemplate();
158
159 if (FTD && D == FTD->getCanonicalDecl())
160 return Sema::AR_accessible;
161
162 return Sema::AR_inaccessible;
163 }
164
165 // Friend functions. FIXME: close matches due to dependency.
166 //
167 // The decl pointers in EC have been canonicalized, so pointer
168 // equality is sufficient.
169 if (D == EC.Function)
170 return Sema::AR_accessible;
171
172 if (isa<CXXRecordDecl>(D))
173 return MatchesFriend(S, EC, cast<CXXRecordDecl>(D));
174
175 return Sema::AR_inaccessible;
176}
177
John McCall5b0829a2010-02-10 09:31:12 +0000178static Sema::AccessResult GetFriendKind(Sema &S,
179 const EffectiveContext &EC,
180 const CXXRecordDecl *Class) {
John McCall16927f62010-03-12 01:19:31 +0000181 // A class always has access to its own members.
John McCallfb803d72010-03-17 04:58:56 +0000182 if (EC.includesClass(Class))
John McCall5b0829a2010-02-10 09:31:12 +0000183 return Sema::AR_accessible;
184
John McCallfb803d72010-03-17 04:58:56 +0000185 Sema::AccessResult OnFailure = Sema::AR_inaccessible;
186
John McCall16927f62010-03-12 01:19:31 +0000187 // Okay, check friends.
188 for (CXXRecordDecl::friend_iterator I = Class->friend_begin(),
189 E = Class->friend_end(); I != E; ++I) {
190 FriendDecl *Friend = *I;
191
John McCall39e82882010-03-17 20:01:29 +0000192 switch (MatchesFriend(S, EC, Friend)) {
193 case Sema::AR_accessible:
194 return Sema::AR_accessible;
John McCall16927f62010-03-12 01:19:31 +0000195
John McCall39e82882010-03-17 20:01:29 +0000196 case Sema::AR_inaccessible:
197 break;
John McCallfb803d72010-03-17 04:58:56 +0000198
John McCall39e82882010-03-17 20:01:29 +0000199 case Sema::AR_dependent:
200 OnFailure = Sema::AR_dependent;
201 break;
202
203 case Sema::AR_delayed:
204 llvm_unreachable("cannot get delayed answer from MatchesFriend");
John McCall16927f62010-03-12 01:19:31 +0000205 }
John McCall16927f62010-03-12 01:19:31 +0000206 }
207
208 // That's it, give up.
John McCallfb803d72010-03-17 04:58:56 +0000209 return OnFailure;
John McCall5b0829a2010-02-10 09:31:12 +0000210}
211
212/// Finds the best path from the naming class to the declaring class,
213/// taking friend declarations into account.
214///
215/// \return null if friendship is dependent
216static CXXBasePath *FindBestPath(Sema &S,
217 const EffectiveContext &EC,
218 CXXRecordDecl *Derived,
219 CXXRecordDecl *Base,
220 CXXBasePaths &Paths) {
221 // Derive the paths to the desired base.
222 bool isDerived = Derived->isDerivedFrom(Base, Paths);
223 assert(isDerived && "derived class not actually derived from base");
224 (void) isDerived;
225
226 CXXBasePath *BestPath = 0;
227
228 // Derive the friend-modified access along each path.
229 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
230 PI != PE; ++PI) {
231
232 // Walk through the path backwards.
233 AccessSpecifier PathAccess = AS_public;
234 CXXBasePath::iterator I = PI->end(), E = PI->begin();
235 while (I != E) {
236 --I;
237
238 AccessSpecifier BaseAccess = I->Base->getAccessSpecifier();
239 if (BaseAccess != AS_public) {
240 switch (GetFriendKind(S, EC, I->Class)) {
241 case Sema::AR_inaccessible: break;
242 case Sema::AR_accessible: BaseAccess = AS_public; break;
243 case Sema::AR_dependent: return 0;
244 case Sema::AR_delayed:
245 llvm_unreachable("friend resolution is never delayed"); break;
246 }
247 }
248
249 PathAccess = CXXRecordDecl::MergeAccess(BaseAccess, PathAccess);
250 }
251
252 // Note that we modify the path's Access field to the
253 // friend-modified access.
254 if (BestPath == 0 || PathAccess < BestPath->Access) {
255 BestPath = &*PI;
256 BestPath->Access = PathAccess;
257 }
258 }
259
260 return BestPath;
261}
262
263/// Diagnose the path which caused the given declaration or base class
264/// to become inaccessible.
265static void DiagnoseAccessPath(Sema &S,
266 const EffectiveContext &EC,
267 CXXRecordDecl *NamingClass,
268 CXXRecordDecl *DeclaringClass,
269 NamedDecl *D, AccessSpecifier Access) {
John McCall553c0792010-01-23 00:46:32 +0000270 // Easy case: the decl's natural access determined its path access.
John McCall5b0829a2010-02-10 09:31:12 +0000271 // We have to check against AS_private here in case Access is AS_none,
272 // indicating a non-public member of a private base class.
273 //
274 // DependentFriend should be impossible here.
275 if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) {
276 switch (GetFriendKind(S, EC, DeclaringClass)) {
277 case Sema::AR_inaccessible: {
278 S.Diag(D->getLocation(), diag::note_access_natural)
279 << (unsigned) (Access == AS_protected)
280 << /*FIXME: not implicitly*/ 0;
281 return;
282 }
283
284 case Sema::AR_accessible: break;
285
286 case Sema::AR_dependent:
287 case Sema::AR_delayed:
288 llvm_unreachable("dependent/delayed not allowed");
289 return;
290 }
291 }
292
293 CXXBasePaths Paths;
294 CXXBasePath &Path = *FindBestPath(S, EC, NamingClass, DeclaringClass, Paths);
295
296 CXXBasePath::iterator I = Path.end(), E = Path.begin();
297 while (I != E) {
298 --I;
299
300 const CXXBaseSpecifier *BS = I->Base;
301 AccessSpecifier BaseAccess = BS->getAccessSpecifier();
302
303 // If this is public inheritance, or the derived class is a friend,
304 // skip this step.
305 if (BaseAccess == AS_public)
306 continue;
307
308 switch (GetFriendKind(S, EC, I->Class)) {
309 case Sema::AR_accessible: continue;
310 case Sema::AR_inaccessible: break;
311
312 case Sema::AR_dependent:
313 case Sema::AR_delayed:
314 llvm_unreachable("dependent friendship, should not be diagnosing");
315 }
316
317 // Check whether this base specifier is the tighest point
318 // constraining access. We have to check against AS_private for
319 // the same reasons as above.
320 if (BaseAccess == AS_private || BaseAccess >= Access) {
321
322 // We're constrained by inheritance, but we want to say
323 // "declared private here" if we're diagnosing a hierarchy
324 // conversion and this is the final step.
325 unsigned diagnostic;
326 if (D) diagnostic = diag::note_access_constrained_by_path;
327 else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural;
328 else diagnostic = diag::note_access_constrained_by_path;
329
330 S.Diag(BS->getSourceRange().getBegin(), diagnostic)
331 << BS->getSourceRange()
332 << (BaseAccess == AS_protected)
333 << (BS->getAccessSpecifierAsWritten() == AS_none);
334 return;
335 }
336 }
337
338 llvm_unreachable("access not apparently constrained by path");
339}
340
341/// Diagnose an inaccessible class member.
342static void DiagnoseInaccessibleMember(Sema &S, SourceLocation Loc,
343 const EffectiveContext &EC,
344 CXXRecordDecl *NamingClass,
345 AccessSpecifier Access,
346 const Sema::AccessedEntity &Entity) {
347 NamedDecl *D = Entity.getTargetDecl();
348 CXXRecordDecl *DeclaringClass = FindDeclaringClass(D);
349
John McCall1064d7e2010-03-16 05:22:47 +0000350 S.Diag(Loc, Entity.getDiag())
351 << (Access == AS_protected)
352 << D->getDeclName()
353 << S.Context.getTypeDeclType(NamingClass)
354 << S.Context.getTypeDeclType(DeclaringClass);
John McCall5b0829a2010-02-10 09:31:12 +0000355 DiagnoseAccessPath(S, EC, NamingClass, DeclaringClass, D, Access);
356}
357
358/// Diagnose an inaccessible hierarchy conversion.
359static void DiagnoseInaccessibleBase(Sema &S, SourceLocation Loc,
360 const EffectiveContext &EC,
361 AccessSpecifier Access,
John McCall1064d7e2010-03-16 05:22:47 +0000362 const Sema::AccessedEntity &Entity) {
363 S.Diag(Loc, Entity.getDiag())
364 << (Access == AS_protected)
365 << DeclarationName()
366 << S.Context.getTypeDeclType(Entity.getDerivedClass())
367 << S.Context.getTypeDeclType(Entity.getBaseClass());
John McCall5b0829a2010-02-10 09:31:12 +0000368 DiagnoseAccessPath(S, EC, Entity.getDerivedClass(),
369 Entity.getBaseClass(), 0, Access);
370}
371
John McCall1064d7e2010-03-16 05:22:47 +0000372static void DiagnoseBadAccess(Sema &S, SourceLocation Loc,
John McCall5b0829a2010-02-10 09:31:12 +0000373 const EffectiveContext &EC,
374 CXXRecordDecl *NamingClass,
375 AccessSpecifier Access,
John McCall1064d7e2010-03-16 05:22:47 +0000376 const Sema::AccessedEntity &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +0000377 if (Entity.isMemberAccess())
378 DiagnoseInaccessibleMember(S, Loc, EC, NamingClass, Access, Entity);
379 else
John McCall1064d7e2010-03-16 05:22:47 +0000380 DiagnoseInaccessibleBase(S, Loc, EC, Access, Entity);
John McCall5b0829a2010-02-10 09:31:12 +0000381}
382
383
384/// Try to elevate access using friend declarations. This is
385/// potentially quite expensive.
386static void TryElevateAccess(Sema &S,
387 const EffectiveContext &EC,
388 const Sema::AccessedEntity &Entity,
389 AccessSpecifier &Access) {
390 CXXRecordDecl *DeclaringClass;
391 if (Entity.isMemberAccess()) {
392 DeclaringClass = FindDeclaringClass(Entity.getTargetDecl());
393 } else {
394 DeclaringClass = Entity.getBaseClass();
395 }
396 CXXRecordDecl *NamingClass = Entity.getNamingClass();
397
398 // Adjust the declaration of the referred entity.
399 AccessSpecifier DeclAccess = AS_none;
400 if (Entity.isMemberAccess()) {
401 NamedDecl *Target = Entity.getTargetDecl();
402
403 DeclAccess = Target->getAccess();
404 if (DeclAccess != AS_public) {
405 switch (GetFriendKind(S, EC, DeclaringClass)) {
406 case Sema::AR_accessible: DeclAccess = AS_public; break;
407 case Sema::AR_inaccessible: break;
408 case Sema::AR_dependent: /* FIXME: delay dependent friendship */ return;
409 case Sema::AR_delayed: llvm_unreachable("friend status is never delayed");
410 }
411 }
412
413 if (DeclaringClass == NamingClass) {
414 Access = DeclAccess;
415 return;
416 }
417 }
418
419 assert(DeclaringClass != NamingClass);
420
421 // Append the declaration's access if applicable.
422 CXXBasePaths Paths;
423 CXXBasePath *Path = FindBestPath(S, EC, Entity.getNamingClass(),
424 DeclaringClass, Paths);
425 if (!Path) {
426 // FIXME: delay dependent friendship
John McCall553c0792010-01-23 00:46:32 +0000427 return;
428 }
429
John McCall5b0829a2010-02-10 09:31:12 +0000430 // Grab the access along the best path.
431 AccessSpecifier NewAccess = Path->Access;
432 if (Entity.isMemberAccess())
433 NewAccess = CXXRecordDecl::MergeAccess(NewAccess, DeclAccess);
434
435 assert(NewAccess <= Access && "access along best path worse than direct?");
436 Access = NewAccess;
John McCall553c0792010-01-23 00:46:32 +0000437}
438
John McCall5b0829a2010-02-10 09:31:12 +0000439/// Checks access to an entity from the given effective context.
440static Sema::AccessResult CheckEffectiveAccess(Sema &S,
441 const EffectiveContext &EC,
442 SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +0000443 Sema::AccessedEntity const &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +0000444 AccessSpecifier Access = Entity.getAccess();
John McCallfb803d72010-03-17 04:58:56 +0000445 assert(Access != AS_public && "called for public access!");
John McCall553c0792010-01-23 00:46:32 +0000446
John McCallfb803d72010-03-17 04:58:56 +0000447 // Find a non-anonymous naming class. For records with access,
448 // there should always be one of these.
John McCall5b0829a2010-02-10 09:31:12 +0000449 CXXRecordDecl *NamingClass = Entity.getNamingClass();
John McCall553c0792010-01-23 00:46:32 +0000450 while (NamingClass->isAnonymousStructOrUnion())
John McCall1064d7e2010-03-16 05:22:47 +0000451 NamingClass = cast<CXXRecordDecl>(NamingClass->getParent());
John McCall553c0792010-01-23 00:46:32 +0000452
John McCallfb803d72010-03-17 04:58:56 +0000453 // White-list accesses from classes with privileges equivalent to the
454 // naming class --- but only if the access path isn't forbidden
455 // (i.e. an access of a private member from a subclass).
456 if (Access != AS_none && EC.includesClass(NamingClass))
John McCall5b0829a2010-02-10 09:31:12 +0000457 return Sema::AR_accessible;
John McCallfb803d72010-03-17 04:58:56 +0000458
459 // Try to elevate access.
460 // FIXME: delay if elevation was dependent?
461 // TODO: on some code, it might be better to do the protected check
462 // without trying to elevate first.
463 TryElevateAccess(S, EC, Entity, Access);
464 if (Access == AS_public) return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +0000465
466 // Protected access.
467 if (Access == AS_protected) {
468 // FIXME: implement [class.protected]p1
John McCallfb803d72010-03-17 04:58:56 +0000469 for (llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator
470 I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I)
471 if ((*I)->isDerivedFrom(NamingClass))
472 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +0000473
John McCallfb803d72010-03-17 04:58:56 +0000474 // FIXME: delay if we can't decide class derivation yet.
John McCall553c0792010-01-23 00:46:32 +0000475 }
476
John McCall5b0829a2010-02-10 09:31:12 +0000477 // Okay, that's it, reject it.
John McCall1064d7e2010-03-16 05:22:47 +0000478 if (!Entity.isQuiet())
479 DiagnoseBadAccess(S, Loc, EC, NamingClass, Access, Entity);
John McCall5b0829a2010-02-10 09:31:12 +0000480 return Sema::AR_inaccessible;
481}
John McCall553c0792010-01-23 00:46:32 +0000482
John McCall5b0829a2010-02-10 09:31:12 +0000483static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +0000484 const Sema::AccessedEntity &Entity) {
John McCall5b0829a2010-02-10 09:31:12 +0000485 // If the access path is public, it's accessible everywhere.
486 if (Entity.getAccess() == AS_public)
487 return Sema::AR_accessible;
John McCall553c0792010-01-23 00:46:32 +0000488
John McCall5b0829a2010-02-10 09:31:12 +0000489 // If we're currently parsing a top-level declaration, delay
490 // diagnostics. This is the only case where parsing a declaration
491 // can actually change our effective context for the purposes of
492 // access control.
493 if (S.CurContext->isFileContext() && S.ParsingDeclDepth) {
John McCall5b0829a2010-02-10 09:31:12 +0000494 S.DelayedDiagnostics.push_back(
495 Sema::DelayedDiagnostic::makeAccess(Loc, Entity));
496 return Sema::AR_delayed;
John McCall553c0792010-01-23 00:46:32 +0000497 }
498
John McCall5b0829a2010-02-10 09:31:12 +0000499 return CheckEffectiveAccess(S, EffectiveContext(S.CurContext),
John McCall1064d7e2010-03-16 05:22:47 +0000500 Loc, Entity);
John McCall553c0792010-01-23 00:46:32 +0000501}
502
John McCall86121512010-01-27 03:50:35 +0000503void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +0000504 // Pretend we did this from the context of the newly-parsed
505 // declaration.
John McCall5b0829a2010-02-10 09:31:12 +0000506 EffectiveContext EC(Ctx->getDeclContext());
John McCall86121512010-01-27 03:50:35 +0000507
John McCall1064d7e2010-03-16 05:22:47 +0000508 if (CheckEffectiveAccess(*this, EC, DD.Loc, DD.getAccessData()))
John McCall86121512010-01-27 03:50:35 +0000509 DD.Triggered = true;
510}
511
John McCall5b0829a2010-02-10 09:31:12 +0000512Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E,
513 NamedDecl *D,
514 AccessSpecifier Access) {
John McCall1064d7e2010-03-16 05:22:47 +0000515 if (!getLangOptions().AccessControl ||
516 !E->getNamingClass() ||
517 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +0000518 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +0000519
John McCall1064d7e2010-03-16 05:22:47 +0000520 AccessedEntity Entity(AccessedEntity::Member,
521 E->getNamingClass(), Access, D);
522 Entity.setDiag(diag::err_access) << E->getSourceRange();
523
524 return CheckAccess(*this, E->getNameLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +0000525}
526
527/// Perform access-control checking on a previously-unresolved member
528/// access which has now been resolved to a member.
John McCall5b0829a2010-02-10 09:31:12 +0000529Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E,
530 NamedDecl *D,
531 AccessSpecifier Access) {
John McCall1064d7e2010-03-16 05:22:47 +0000532 if (!getLangOptions().AccessControl ||
533 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +0000534 return AR_accessible;
John McCall58cc69d2010-01-27 01:50:18 +0000535
John McCall1064d7e2010-03-16 05:22:47 +0000536 AccessedEntity Entity(AccessedEntity::Member,
537 E->getNamingClass(), Access, D);
538 Entity.setDiag(diag::err_access) << E->getSourceRange();
539
540 return CheckAccess(*this, E->getMemberLoc(), Entity);
John McCall58cc69d2010-01-27 01:50:18 +0000541}
542
John McCall5b0829a2010-02-10 09:31:12 +0000543Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
John McCall1064d7e2010-03-16 05:22:47 +0000544 CXXDestructorDecl *Dtor,
545 const PartialDiagnostic &PDiag) {
John McCall6781b052010-02-02 08:45:54 +0000546 if (!getLangOptions().AccessControl)
John McCall5b0829a2010-02-10 09:31:12 +0000547 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +0000548
John McCall1064d7e2010-03-16 05:22:47 +0000549 // There's never a path involved when checking implicit destructor access.
John McCall6781b052010-02-02 08:45:54 +0000550 AccessSpecifier Access = Dtor->getAccess();
551 if (Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +0000552 return AR_accessible;
John McCall6781b052010-02-02 08:45:54 +0000553
John McCall1064d7e2010-03-16 05:22:47 +0000554 CXXRecordDecl *NamingClass = Dtor->getParent();
555 AccessedEntity Entity(AccessedEntity::Member, NamingClass, Access, Dtor);
556 Entity.setDiag(PDiag); // TODO: avoid copy
557
558 return CheckAccess(*this, Loc, Entity);
John McCall6781b052010-02-02 08:45:54 +0000559}
560
John McCall760af172010-02-01 03:16:54 +0000561/// Checks access to a constructor.
John McCall5b0829a2010-02-10 09:31:12 +0000562Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
John McCall760af172010-02-01 03:16:54 +0000563 CXXConstructorDecl *Constructor,
564 AccessSpecifier Access) {
John McCall1064d7e2010-03-16 05:22:47 +0000565 if (!getLangOptions().AccessControl ||
566 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +0000567 return AR_accessible;
John McCall760af172010-02-01 03:16:54 +0000568
John McCall5b0829a2010-02-10 09:31:12 +0000569 CXXRecordDecl *NamingClass = Constructor->getParent();
John McCall1064d7e2010-03-16 05:22:47 +0000570 AccessedEntity Entity(AccessedEntity::Member,
571 NamingClass, Access, Constructor);
572 Entity.setDiag(diag::err_access_ctor);
573
574 return CheckAccess(*this, UseLoc, Entity);
John McCall760af172010-02-01 03:16:54 +0000575}
576
John McCallab8c2732010-03-16 06:11:48 +0000577/// Checks direct (i.e. non-inherited) access to an arbitrary class
578/// member.
579Sema::AccessResult Sema::CheckDirectMemberAccess(SourceLocation UseLoc,
580 NamedDecl *Target,
581 const PartialDiagnostic &Diag) {
582 AccessSpecifier Access = Target->getAccess();
583 if (!getLangOptions().AccessControl ||
584 Access == AS_public)
585 return AR_accessible;
586
587 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Target->getDeclContext());
588 AccessedEntity Entity(AccessedEntity::Member, NamingClass, Access, Target);
589 Entity.setDiag(Diag);
590 return CheckAccess(*this, UseLoc, Entity);
591}
592
593
John McCall760af172010-02-01 03:16:54 +0000594/// Checks access to an overloaded member operator, including
595/// conversion operators.
John McCall5b0829a2010-02-10 09:31:12 +0000596Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc,
597 Expr *ObjectExpr,
John McCall1064d7e2010-03-16 05:22:47 +0000598 Expr *ArgExpr,
John McCall5b0829a2010-02-10 09:31:12 +0000599 NamedDecl *MemberOperator,
600 AccessSpecifier Access) {
John McCall1064d7e2010-03-16 05:22:47 +0000601 if (!getLangOptions().AccessControl ||
602 Access == AS_public)
John McCall5b0829a2010-02-10 09:31:12 +0000603 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +0000604
605 const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>();
606 assert(RT && "found member operator but object expr not of record type");
607 CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl());
608
John McCall1064d7e2010-03-16 05:22:47 +0000609 AccessedEntity Entity(AccessedEntity::Member,
610 NamingClass, Access, MemberOperator);
611 Entity.setDiag(diag::err_access)
612 << ObjectExpr->getSourceRange()
613 << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange());
614
615 return CheckAccess(*this, OpLoc, Entity);
John McCall5b0829a2010-02-10 09:31:12 +0000616}
John McCallb3a44002010-01-28 01:42:12 +0000617
John McCall5b0829a2010-02-10 09:31:12 +0000618/// Checks access for a hierarchy conversion.
619///
620/// \param IsBaseToDerived whether this is a base-to-derived conversion (true)
621/// or a derived-to-base conversion (false)
622/// \param ForceCheck true if this check should be performed even if access
623/// control is disabled; some things rely on this for semantics
624/// \param ForceUnprivileged true if this check should proceed as if the
625/// context had no special privileges
626/// \param ADK controls the kind of diagnostics that are used
627Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc,
John McCall5b0829a2010-02-10 09:31:12 +0000628 QualType Base,
629 QualType Derived,
630 const CXXBasePath &Path,
John McCall1064d7e2010-03-16 05:22:47 +0000631 unsigned DiagID,
John McCall5b0829a2010-02-10 09:31:12 +0000632 bool ForceCheck,
John McCall1064d7e2010-03-16 05:22:47 +0000633 bool ForceUnprivileged) {
John McCall5b0829a2010-02-10 09:31:12 +0000634 if (!ForceCheck && !getLangOptions().AccessControl)
635 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +0000636
John McCall5b0829a2010-02-10 09:31:12 +0000637 if (Path.Access == AS_public)
638 return AR_accessible;
John McCallb3a44002010-01-28 01:42:12 +0000639
John McCall5b0829a2010-02-10 09:31:12 +0000640 CXXRecordDecl *BaseD, *DerivedD;
641 BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl());
642 DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl());
John McCall1064d7e2010-03-16 05:22:47 +0000643
644 AccessedEntity Entity(AccessedEntity::Base, BaseD, DerivedD, Path.Access);
645 if (DiagID)
646 Entity.setDiag(DiagID) << Derived << Base;
John McCall5b0829a2010-02-10 09:31:12 +0000647
648 if (ForceUnprivileged)
John McCall1064d7e2010-03-16 05:22:47 +0000649 return CheckEffectiveAccess(*this, EffectiveContext(), AccessLoc, Entity);
650 return CheckAccess(*this, AccessLoc, Entity);
John McCallb3a44002010-01-28 01:42:12 +0000651}
652
John McCall553c0792010-01-23 00:46:32 +0000653/// Checks access to all the declarations in the given result set.
John McCall5b0829a2010-02-10 09:31:12 +0000654void Sema::CheckLookupAccess(const LookupResult &R) {
655 assert(getLangOptions().AccessControl
656 && "performing access check without access control");
657 assert(R.getNamingClass() && "performing access check without naming class");
658
John McCall1064d7e2010-03-16 05:22:47 +0000659 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
660 if (I.getAccess() != AS_public) {
661 AccessedEntity Entity(AccessedEntity::Member,
662 R.getNamingClass(), I.getAccess(), *I);
663 Entity.setDiag(diag::err_access);
664
665 CheckAccess(*this, R.getNameLoc(), Entity);
666 }
667 }
John McCall553c0792010-01-23 00:46:32 +0000668}