SemaLookup.cpp revision 208600
1193326Sed//===--------------------- SemaLookup.cpp - Name Lookup ------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements name lookup for C, C++, Objective-C, and 11193326Sed// Objective-C++. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed#include "Sema.h" 15199482Srdivacky#include "Lookup.h" 16193326Sed#include "clang/AST/ASTContext.h" 17198092Srdivacky#include "clang/AST/CXXInheritance.h" 18193326Sed#include "clang/AST/Decl.h" 19193326Sed#include "clang/AST/DeclCXX.h" 20193326Sed#include "clang/AST/DeclObjC.h" 21193326Sed#include "clang/AST/DeclTemplate.h" 22193326Sed#include "clang/AST/Expr.h" 23198092Srdivacky#include "clang/AST/ExprCXX.h" 24193326Sed#include "clang/Parse/DeclSpec.h" 25194179Sed#include "clang/Basic/Builtins.h" 26193326Sed#include "clang/Basic/LangOptions.h" 27193326Sed#include "llvm/ADT/STLExtras.h" 28193326Sed#include "llvm/ADT/SmallPtrSet.h" 29198092Srdivacky#include "llvm/Support/ErrorHandling.h" 30201361Srdivacky#include <list> 31193326Sed#include <set> 32193326Sed#include <vector> 33193326Sed#include <iterator> 34193326Sed#include <utility> 35193326Sed#include <algorithm> 36193326Sed 37193326Sedusing namespace clang; 38193326Sed 39199482Srdivackynamespace { 40199482Srdivacky class UnqualUsingEntry { 41199482Srdivacky const DeclContext *Nominated; 42199482Srdivacky const DeclContext *CommonAncestor; 43193326Sed 44199482Srdivacky public: 45199482Srdivacky UnqualUsingEntry(const DeclContext *Nominated, 46199482Srdivacky const DeclContext *CommonAncestor) 47199482Srdivacky : Nominated(Nominated), CommonAncestor(CommonAncestor) { 48199482Srdivacky } 49193326Sed 50199482Srdivacky const DeclContext *getCommonAncestor() const { 51199482Srdivacky return CommonAncestor; 52199482Srdivacky } 53193326Sed 54199482Srdivacky const DeclContext *getNominatedNamespace() const { 55199482Srdivacky return Nominated; 56199482Srdivacky } 57193326Sed 58199482Srdivacky // Sort by the pointer value of the common ancestor. 59199482Srdivacky struct Comparator { 60199482Srdivacky bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { 61199482Srdivacky return L.getCommonAncestor() < R.getCommonAncestor(); 62199482Srdivacky } 63193326Sed 64199482Srdivacky bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { 65199482Srdivacky return E.getCommonAncestor() < DC; 66199482Srdivacky } 67193326Sed 68199482Srdivacky bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { 69199482Srdivacky return DC < E.getCommonAncestor(); 70199482Srdivacky } 71199482Srdivacky }; 72199482Srdivacky }; 73193326Sed 74199482Srdivacky /// A collection of using directives, as used by C++ unqualified 75199482Srdivacky /// lookup. 76199482Srdivacky class UnqualUsingDirectiveSet { 77199482Srdivacky typedef llvm::SmallVector<UnqualUsingEntry, 8> ListTy; 78193326Sed 79199482Srdivacky ListTy list; 80199482Srdivacky llvm::SmallPtrSet<DeclContext*, 8> visited; 81193326Sed 82199482Srdivacky public: 83199482Srdivacky UnqualUsingDirectiveSet() {} 84193326Sed 85199482Srdivacky void visitScopeChain(Scope *S, Scope *InnermostFileScope) { 86199482Srdivacky // C++ [namespace.udir]p1: 87199482Srdivacky // During unqualified name lookup, the names appear as if they 88199482Srdivacky // were declared in the nearest enclosing namespace which contains 89199482Srdivacky // both the using-directive and the nominated namespace. 90199482Srdivacky DeclContext *InnermostFileDC 91199482Srdivacky = static_cast<DeclContext*>(InnermostFileScope->getEntity()); 92199482Srdivacky assert(InnermostFileDC && InnermostFileDC->isFileContext()); 93193326Sed 94199482Srdivacky for (; S; S = S->getParent()) { 95199482Srdivacky if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { 96199482Srdivacky DeclContext *EffectiveDC = (Ctx->isFileContext() ? Ctx : InnermostFileDC); 97199482Srdivacky visit(Ctx, EffectiveDC); 98199482Srdivacky } else { 99199482Srdivacky Scope::udir_iterator I = S->using_directives_begin(), 100199482Srdivacky End = S->using_directives_end(); 101199482Srdivacky 102199482Srdivacky for (; I != End; ++I) 103199482Srdivacky visit(I->getAs<UsingDirectiveDecl>(), InnermostFileDC); 104199482Srdivacky } 105199482Srdivacky } 106199482Srdivacky } 107193326Sed 108199482Srdivacky // Visits a context and collect all of its using directives 109199482Srdivacky // recursively. Treats all using directives as if they were 110199482Srdivacky // declared in the context. 111199482Srdivacky // 112199482Srdivacky // A given context is only every visited once, so it is important 113199482Srdivacky // that contexts be visited from the inside out in order to get 114199482Srdivacky // the effective DCs right. 115199482Srdivacky void visit(DeclContext *DC, DeclContext *EffectiveDC) { 116199482Srdivacky if (!visited.insert(DC)) 117199482Srdivacky return; 118193326Sed 119199482Srdivacky addUsingDirectives(DC, EffectiveDC); 120199482Srdivacky } 121199482Srdivacky 122199482Srdivacky // Visits a using directive and collects all of its using 123199482Srdivacky // directives recursively. Treats all using directives as if they 124199482Srdivacky // were declared in the effective DC. 125199482Srdivacky void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 126199482Srdivacky DeclContext *NS = UD->getNominatedNamespace(); 127199482Srdivacky if (!visited.insert(NS)) 128199482Srdivacky return; 129199482Srdivacky 130199482Srdivacky addUsingDirective(UD, EffectiveDC); 131199482Srdivacky addUsingDirectives(NS, EffectiveDC); 132199482Srdivacky } 133199482Srdivacky 134199482Srdivacky // Adds all the using directives in a context (and those nominated 135199482Srdivacky // by its using directives, transitively) as if they appeared in 136199482Srdivacky // the given effective context. 137199482Srdivacky void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { 138199482Srdivacky llvm::SmallVector<DeclContext*,4> queue; 139199482Srdivacky while (true) { 140199482Srdivacky DeclContext::udir_iterator I, End; 141199482Srdivacky for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) { 142199482Srdivacky UsingDirectiveDecl *UD = *I; 143199482Srdivacky DeclContext *NS = UD->getNominatedNamespace(); 144199482Srdivacky if (visited.insert(NS)) { 145199482Srdivacky addUsingDirective(UD, EffectiveDC); 146199482Srdivacky queue.push_back(NS); 147199482Srdivacky } 148199482Srdivacky } 149199482Srdivacky 150199482Srdivacky if (queue.empty()) 151199482Srdivacky return; 152199482Srdivacky 153199482Srdivacky DC = queue.back(); 154199482Srdivacky queue.pop_back(); 155193326Sed } 156193326Sed } 157199482Srdivacky 158199482Srdivacky // Add a using directive as if it had been declared in the given 159199482Srdivacky // context. This helps implement C++ [namespace.udir]p3: 160199482Srdivacky // The using-directive is transitive: if a scope contains a 161199482Srdivacky // using-directive that nominates a second namespace that itself 162199482Srdivacky // contains using-directives, the effect is as if the 163199482Srdivacky // using-directives from the second namespace also appeared in 164199482Srdivacky // the first. 165199482Srdivacky void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 166199482Srdivacky // Find the common ancestor between the effective context and 167199482Srdivacky // the nominated namespace. 168199482Srdivacky DeclContext *Common = UD->getNominatedNamespace(); 169199482Srdivacky while (!Common->Encloses(EffectiveDC)) 170199482Srdivacky Common = Common->getParent(); 171199482Srdivacky Common = Common->getPrimaryContext(); 172199482Srdivacky 173199482Srdivacky list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); 174199482Srdivacky } 175199482Srdivacky 176199482Srdivacky void done() { 177199482Srdivacky std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); 178199482Srdivacky } 179199482Srdivacky 180199482Srdivacky typedef ListTy::iterator iterator; 181199482Srdivacky typedef ListTy::const_iterator const_iterator; 182199482Srdivacky 183199482Srdivacky iterator begin() { return list.begin(); } 184199482Srdivacky iterator end() { return list.end(); } 185199482Srdivacky const_iterator begin() const { return list.begin(); } 186199482Srdivacky const_iterator end() const { return list.end(); } 187199482Srdivacky 188199482Srdivacky std::pair<const_iterator,const_iterator> 189199482Srdivacky getNamespacesFor(DeclContext *DC) const { 190199482Srdivacky return std::equal_range(begin(), end(), DC->getPrimaryContext(), 191199482Srdivacky UnqualUsingEntry::Comparator()); 192199482Srdivacky } 193199482Srdivacky }; 194193326Sed} 195193326Sed 196193326Sed// Retrieve the set of identifier namespaces that correspond to a 197193326Sed// specific kind of name lookup. 198201361Srdivackystatic inline unsigned getIDNS(Sema::LookupNameKind NameKind, 199201361Srdivacky bool CPlusPlus, 200201361Srdivacky bool Redeclaration) { 201193326Sed unsigned IDNS = 0; 202193326Sed switch (NameKind) { 203193326Sed case Sema::LookupOrdinaryName: 204193326Sed case Sema::LookupRedeclarationWithLinkage: 205193326Sed IDNS = Decl::IDNS_Ordinary; 206201361Srdivacky if (CPlusPlus) { 207207619Srdivacky IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; 208201361Srdivacky if (Redeclaration) IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; 209201361Srdivacky } 210193326Sed break; 211193326Sed 212207619Srdivacky case Sema::LookupOperatorName: 213207619Srdivacky // Operator lookup is its own crazy thing; it is not the same 214207619Srdivacky // as (e.g.) looking up an operator name for redeclaration. 215207619Srdivacky assert(!Redeclaration && "cannot do redeclaration operator lookup"); 216207619Srdivacky IDNS = Decl::IDNS_NonMemberOperator; 217207619Srdivacky break; 218207619Srdivacky 219193326Sed case Sema::LookupTagName: 220207619Srdivacky if (CPlusPlus) { 221207619Srdivacky IDNS = Decl::IDNS_Type; 222207619Srdivacky 223207619Srdivacky // When looking for a redeclaration of a tag name, we add: 224207619Srdivacky // 1) TagFriend to find undeclared friend decls 225207619Srdivacky // 2) Namespace because they can't "overload" with tag decls. 226207619Srdivacky // 3) Tag because it includes class templates, which can't 227207619Srdivacky // "overload" with tag decls. 228207619Srdivacky if (Redeclaration) 229207619Srdivacky IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; 230207619Srdivacky } else { 231207619Srdivacky IDNS = Decl::IDNS_Tag; 232207619Srdivacky } 233193326Sed break; 234193326Sed 235193326Sed case Sema::LookupMemberName: 236193326Sed IDNS = Decl::IDNS_Member; 237193326Sed if (CPlusPlus) 238198092Srdivacky IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; 239193326Sed break; 240193326Sed 241193326Sed case Sema::LookupNestedNameSpecifierName: 242207619Srdivacky IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; 243207619Srdivacky break; 244207619Srdivacky 245193326Sed case Sema::LookupNamespaceName: 246207619Srdivacky IDNS = Decl::IDNS_Namespace; 247193326Sed break; 248193326Sed 249200583Srdivacky case Sema::LookupUsingDeclName: 250200583Srdivacky IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag 251200583Srdivacky | Decl::IDNS_Member | Decl::IDNS_Using; 252200583Srdivacky break; 253200583Srdivacky 254193326Sed case Sema::LookupObjCProtocolName: 255193326Sed IDNS = Decl::IDNS_ObjCProtocol; 256193326Sed break; 257193326Sed } 258193326Sed return IDNS; 259193326Sed} 260193326Sed 261201361Srdivackyvoid LookupResult::configure() { 262201361Srdivacky IDNS = getIDNS(LookupKind, 263201361Srdivacky SemaRef.getLangOptions().CPlusPlus, 264201361Srdivacky isForRedeclaration()); 265206084Srdivacky 266206084Srdivacky // If we're looking for one of the allocation or deallocation 267206084Srdivacky // operators, make sure that the implicitly-declared new and delete 268206084Srdivacky // operators can be found. 269206084Srdivacky if (!isForRedeclaration()) { 270206084Srdivacky switch (Name.getCXXOverloadedOperator()) { 271206084Srdivacky case OO_New: 272206084Srdivacky case OO_Delete: 273206084Srdivacky case OO_Array_New: 274206084Srdivacky case OO_Array_Delete: 275206084Srdivacky SemaRef.DeclareGlobalNewDelete(); 276206084Srdivacky break; 277206084Srdivacky 278206084Srdivacky default: 279206084Srdivacky break; 280206084Srdivacky } 281206084Srdivacky } 282201361Srdivacky} 283201361Srdivacky 284198092Srdivacky// Necessary because CXXBasePaths is not complete in Sema.h 285199482Srdivackyvoid LookupResult::deletePaths(CXXBasePaths *Paths) { 286198092Srdivacky delete Paths; 287193326Sed} 288193326Sed 289199990Srdivacky/// Resolves the result kind of this lookup. 290199482Srdivackyvoid LookupResult::resolveKind() { 291198092Srdivacky unsigned N = Decls.size(); 292200583Srdivacky 293198092Srdivacky // Fast case: no possible ambiguity. 294199512Srdivacky if (N == 0) { 295202879Srdivacky assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); 296199512Srdivacky return; 297199512Srdivacky } 298199512Srdivacky 299199990Srdivacky // If there's a single decl, we need to examine it to decide what 300199990Srdivacky // kind of lookup this is. 301199482Srdivacky if (N == 1) { 302207619Srdivacky NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); 303207619Srdivacky if (isa<FunctionTemplateDecl>(D)) 304199990Srdivacky ResultKind = FoundOverloaded; 305207619Srdivacky else if (isa<UnresolvedUsingValueDecl>(D)) 306199482Srdivacky ResultKind = FoundUnresolvedValue; 307199482Srdivacky return; 308199482Srdivacky } 309193326Sed 310198092Srdivacky // Don't do any extra resolution if we've already resolved as ambiguous. 311199482Srdivacky if (ResultKind == Ambiguous) return; 312195099Sed 313198092Srdivacky llvm::SmallPtrSet<NamedDecl*, 16> Unique; 314193326Sed 315198092Srdivacky bool Ambiguous = false; 316198092Srdivacky bool HasTag = false, HasFunction = false, HasNonFunction = false; 317199990Srdivacky bool HasFunctionTemplate = false, HasUnresolved = false; 318193326Sed 319198092Srdivacky unsigned UniqueTagIndex = 0; 320198092Srdivacky 321198092Srdivacky unsigned I = 0; 322198092Srdivacky while (I < N) { 323199482Srdivacky NamedDecl *D = Decls[I]->getUnderlyingDecl(); 324199482Srdivacky D = cast<NamedDecl>(D->getCanonicalDecl()); 325198092Srdivacky 326199482Srdivacky if (!Unique.insert(D)) { 327198092Srdivacky // If it's not unique, pull something off the back (and 328198092Srdivacky // continue at this index). 329198092Srdivacky Decls[I] = Decls[--N]; 330198092Srdivacky } else { 331198092Srdivacky // Otherwise, do some decl type analysis and then continue. 332199482Srdivacky 333199482Srdivacky if (isa<UnresolvedUsingValueDecl>(D)) { 334199482Srdivacky HasUnresolved = true; 335199482Srdivacky } else if (isa<TagDecl>(D)) { 336198092Srdivacky if (HasTag) 337198092Srdivacky Ambiguous = true; 338198092Srdivacky UniqueTagIndex = I; 339198092Srdivacky HasTag = true; 340199990Srdivacky } else if (isa<FunctionTemplateDecl>(D)) { 341198092Srdivacky HasFunction = true; 342199990Srdivacky HasFunctionTemplate = true; 343199990Srdivacky } else if (isa<FunctionDecl>(D)) { 344199990Srdivacky HasFunction = true; 345198092Srdivacky } else { 346198092Srdivacky if (HasNonFunction) 347198092Srdivacky Ambiguous = true; 348198092Srdivacky HasNonFunction = true; 349198092Srdivacky } 350198092Srdivacky I++; 351193326Sed } 352193326Sed } 353193326Sed 354198092Srdivacky // C++ [basic.scope.hiding]p2: 355198092Srdivacky // A class name or enumeration name can be hidden by the name of 356198092Srdivacky // an object, function, or enumerator declared in the same 357198092Srdivacky // scope. If a class or enumeration name and an object, function, 358198092Srdivacky // or enumerator are declared in the same scope (in any order) 359198092Srdivacky // with the same name, the class or enumeration name is hidden 360198092Srdivacky // wherever the object, function, or enumerator name is visible. 361198092Srdivacky // But it's still an error if there are distinct tag types found, 362198092Srdivacky // even if they're not visible. (ref?) 363200583Srdivacky if (HideTags && HasTag && !Ambiguous && 364200583Srdivacky (HasFunction || HasNonFunction || HasUnresolved)) 365198092Srdivacky Decls[UniqueTagIndex] = Decls[--N]; 366193326Sed 367198092Srdivacky Decls.set_size(N); 368193326Sed 369200583Srdivacky if (HasNonFunction && (HasFunction || HasUnresolved)) 370198092Srdivacky Ambiguous = true; 371193326Sed 372198092Srdivacky if (Ambiguous) 373198092Srdivacky setAmbiguous(LookupResult::AmbiguousReference); 374199482Srdivacky else if (HasUnresolved) 375199482Srdivacky ResultKind = LookupResult::FoundUnresolvedValue; 376199990Srdivacky else if (N > 1 || HasFunctionTemplate) 377199482Srdivacky ResultKind = LookupResult::FoundOverloaded; 378198092Srdivacky else 379199482Srdivacky ResultKind = LookupResult::Found; 380193326Sed} 381193326Sed 382199482Srdivackyvoid LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { 383203955Srdivacky CXXBasePaths::const_paths_iterator I, E; 384198092Srdivacky DeclContext::lookup_iterator DI, DE; 385198092Srdivacky for (I = P.begin(), E = P.end(); I != E; ++I) 386198092Srdivacky for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI) 387198092Srdivacky addDecl(*DI); 388193326Sed} 389193326Sed 390199482Srdivackyvoid LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { 391198092Srdivacky Paths = new CXXBasePaths; 392198092Srdivacky Paths->swap(P); 393198092Srdivacky addDeclsFromBasePaths(*Paths); 394198092Srdivacky resolveKind(); 395198092Srdivacky setAmbiguous(AmbiguousBaseSubobjects); 396193326Sed} 397193326Sed 398199482Srdivackyvoid LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { 399198092Srdivacky Paths = new CXXBasePaths; 400198092Srdivacky Paths->swap(P); 401198092Srdivacky addDeclsFromBasePaths(*Paths); 402198092Srdivacky resolveKind(); 403198092Srdivacky setAmbiguous(AmbiguousBaseSubobjectTypes); 404193326Sed} 405193326Sed 406199482Srdivackyvoid LookupResult::print(llvm::raw_ostream &Out) { 407198092Srdivacky Out << Decls.size() << " result(s)"; 408198092Srdivacky if (isAmbiguous()) Out << ", ambiguous"; 409198092Srdivacky if (Paths) Out << ", base paths present"; 410198092Srdivacky 411198092Srdivacky for (iterator I = begin(), E = end(); I != E; ++I) { 412198092Srdivacky Out << "\n"; 413198092Srdivacky (*I)->print(Out, 2); 414193326Sed } 415193326Sed} 416193326Sed 417203955Srdivacky/// \brief Lookup a builtin function, when name lookup would otherwise 418203955Srdivacky/// fail. 419203955Srdivackystatic bool LookupBuiltin(Sema &S, LookupResult &R) { 420203955Srdivacky Sema::LookupNameKind NameKind = R.getLookupKind(); 421203955Srdivacky 422203955Srdivacky // If we didn't find a use of this identifier, and if the identifier 423203955Srdivacky // corresponds to a compiler builtin, create the decl object for the builtin 424203955Srdivacky // now, injecting it into translation unit scope, and return it. 425203955Srdivacky if (NameKind == Sema::LookupOrdinaryName || 426203955Srdivacky NameKind == Sema::LookupRedeclarationWithLinkage) { 427203955Srdivacky IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); 428203955Srdivacky if (II) { 429203955Srdivacky // If this is a builtin on this (or all) targets, create the decl. 430203955Srdivacky if (unsigned BuiltinID = II->getBuiltinID()) { 431203955Srdivacky // In C++, we don't have any predefined library functions like 432203955Srdivacky // 'malloc'. Instead, we'll just error. 433203955Srdivacky if (S.getLangOptions().CPlusPlus && 434203955Srdivacky S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 435203955Srdivacky return false; 436203955Srdivacky 437203955Srdivacky NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, 438203955Srdivacky S.TUScope, R.isForRedeclaration(), 439203955Srdivacky R.getNameLoc()); 440203955Srdivacky if (D) 441203955Srdivacky R.addDecl(D); 442203955Srdivacky return (D != NULL); 443203955Srdivacky } 444203955Srdivacky } 445203955Srdivacky } 446203955Srdivacky 447203955Srdivacky return false; 448203955Srdivacky} 449203955Srdivacky 450198092Srdivacky// Adds all qualifying matches for a name within a decl context to the 451198092Srdivacky// given lookup result. Returns true if any matches were found. 452203955Srdivackystatic bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { 453198092Srdivacky bool Found = false; 454193326Sed 455199482Srdivacky DeclContext::lookup_const_iterator I, E; 456202379Srdivacky for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I) { 457202879Srdivacky NamedDecl *D = *I; 458202879Srdivacky if (R.isAcceptableDecl(D)) { 459202879Srdivacky R.addDecl(D); 460202379Srdivacky Found = true; 461202379Srdivacky } 462202379Srdivacky } 463193326Sed 464203955Srdivacky if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) 465203955Srdivacky return true; 466203955Srdivacky 467202379Srdivacky if (R.getLookupName().getNameKind() 468203955Srdivacky != DeclarationName::CXXConversionFunctionName || 469203955Srdivacky R.getLookupName().getCXXNameType()->isDependentType() || 470203955Srdivacky !isa<CXXRecordDecl>(DC)) 471203955Srdivacky return Found; 472202379Srdivacky 473203955Srdivacky // C++ [temp.mem]p6: 474203955Srdivacky // A specialization of a conversion function template is not found by 475203955Srdivacky // name lookup. Instead, any conversion function templates visible in the 476203955Srdivacky // context of the use are considered. [...] 477203955Srdivacky const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); 478203955Srdivacky if (!Record->isDefinition()) 479203955Srdivacky return Found; 480202379Srdivacky 481203955Srdivacky const UnresolvedSetImpl *Unresolved = Record->getConversionFunctions(); 482203955Srdivacky for (UnresolvedSetImpl::iterator U = Unresolved->begin(), 483203955Srdivacky UEnd = Unresolved->end(); U != UEnd; ++U) { 484203955Srdivacky FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); 485203955Srdivacky if (!ConvTemplate) 486203955Srdivacky continue; 487203955Srdivacky 488203955Srdivacky // When we're performing lookup for the purposes of redeclaration, just 489203955Srdivacky // add the conversion function template. When we deduce template 490203955Srdivacky // arguments for specializations, we'll end up unifying the return 491203955Srdivacky // type of the new declaration with the type of the function template. 492203955Srdivacky if (R.isForRedeclaration()) { 493203955Srdivacky R.addDecl(ConvTemplate); 494203955Srdivacky Found = true; 495203955Srdivacky continue; 496202379Srdivacky } 497203955Srdivacky 498203955Srdivacky // C++ [temp.mem]p6: 499203955Srdivacky // [...] For each such operator, if argument deduction succeeds 500203955Srdivacky // (14.9.2.3), the resulting specialization is used as if found by 501203955Srdivacky // name lookup. 502203955Srdivacky // 503203955Srdivacky // When referencing a conversion function for any purpose other than 504203955Srdivacky // a redeclaration (such that we'll be building an expression with the 505203955Srdivacky // result), perform template argument deduction and place the 506203955Srdivacky // specialization into the result set. We do this to avoid forcing all 507203955Srdivacky // callers to perform special deduction for conversion functions. 508203955Srdivacky Sema::TemplateDeductionInfo Info(R.getSema().Context, R.getNameLoc()); 509203955Srdivacky FunctionDecl *Specialization = 0; 510203955Srdivacky 511203955Srdivacky const FunctionProtoType *ConvProto 512203955Srdivacky = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); 513203955Srdivacky assert(ConvProto && "Nonsensical conversion function template type"); 514203955Srdivacky 515203955Srdivacky // Compute the type of the function that we would expect the conversion 516203955Srdivacky // function to have, if it were to match the name given. 517203955Srdivacky // FIXME: Calling convention! 518206084Srdivacky FunctionType::ExtInfo ConvProtoInfo = ConvProto->getExtInfo(); 519203955Srdivacky QualType ExpectedType 520203955Srdivacky = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), 521203955Srdivacky 0, 0, ConvProto->isVariadic(), 522203955Srdivacky ConvProto->getTypeQuals(), 523203955Srdivacky false, false, 0, 0, 524206084Srdivacky ConvProtoInfo.withCallingConv(CC_Default)); 525203955Srdivacky 526203955Srdivacky // Perform template argument deduction against the type that we would 527203955Srdivacky // expect the function to have. 528203955Srdivacky if (R.getSema().DeduceTemplateArguments(ConvTemplate, 0, ExpectedType, 529203955Srdivacky Specialization, Info) 530203955Srdivacky == Sema::TDK_Success) { 531203955Srdivacky R.addDecl(Specialization); 532203955Srdivacky Found = true; 533203955Srdivacky } 534202379Srdivacky } 535203955Srdivacky 536198092Srdivacky return Found; 537193326Sed} 538193326Sed 539199482Srdivacky// Performs C++ unqualified lookup into the given file context. 540198092Srdivackystatic bool 541203955SrdivackyCppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, 542203955Srdivacky DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { 543193326Sed 544193326Sed assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); 545193326Sed 546199482Srdivacky // Perform direct name lookup into the LookupCtx. 547203955Srdivacky bool Found = LookupDirect(S, R, NS); 548193326Sed 549199482Srdivacky // Perform direct name lookup into the namespaces nominated by the 550199482Srdivacky // using directives whose common ancestor is this namespace. 551199482Srdivacky UnqualUsingDirectiveSet::const_iterator UI, UEnd; 552199482Srdivacky llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); 553198092Srdivacky 554199482Srdivacky for (; UI != UEnd; ++UI) 555203955Srdivacky if (LookupDirect(S, R, UI->getNominatedNamespace())) 556199482Srdivacky Found = true; 557198092Srdivacky 558198092Srdivacky R.resolveKind(); 559198092Srdivacky 560198092Srdivacky return Found; 561193326Sed} 562193326Sed 563193326Sedstatic bool isNamespaceOrTranslationUnitScope(Scope *S) { 564193326Sed if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) 565193326Sed return Ctx->isFileContext(); 566193326Sed return false; 567193326Sed} 568193326Sed 569205219Srdivacky// Find the next outer declaration context from this scope. This 570205219Srdivacky// routine actually returns the semantic outer context, which may 571205219Srdivacky// differ from the lexical context (encoded directly in the Scope 572205219Srdivacky// stack) when we are parsing a member of a class template. In this 573205219Srdivacky// case, the second element of the pair will be true, to indicate that 574205219Srdivacky// name lookup should continue searching in this semantic context when 575205219Srdivacky// it leaves the current template parameter scope. 576205219Srdivackystatic std::pair<DeclContext *, bool> findOuterContext(Scope *S) { 577205219Srdivacky DeclContext *DC = static_cast<DeclContext *>(S->getEntity()); 578205219Srdivacky DeclContext *Lexical = 0; 579205219Srdivacky for (Scope *OuterS = S->getParent(); OuterS; 580205219Srdivacky OuterS = OuterS->getParent()) { 581205219Srdivacky if (OuterS->getEntity()) { 582205219Srdivacky Lexical = static_cast<DeclContext *>(OuterS->getEntity()); 583205219Srdivacky break; 584205219Srdivacky } 585205219Srdivacky } 586205219Srdivacky 587205219Srdivacky // C++ [temp.local]p8: 588205219Srdivacky // In the definition of a member of a class template that appears 589205219Srdivacky // outside of the namespace containing the class template 590205219Srdivacky // definition, the name of a template-parameter hides the name of 591205219Srdivacky // a member of this namespace. 592205219Srdivacky // 593205219Srdivacky // Example: 594205219Srdivacky // 595205219Srdivacky // namespace N { 596205219Srdivacky // class C { }; 597205219Srdivacky // 598205219Srdivacky // template<class T> class B { 599205219Srdivacky // void f(T); 600205219Srdivacky // }; 601205219Srdivacky // } 602205219Srdivacky // 603205219Srdivacky // template<class C> void N::B<C>::f(C) { 604205219Srdivacky // C b; // C is the template parameter, not N::C 605205219Srdivacky // } 606205219Srdivacky // 607205219Srdivacky // In this example, the lexical context we return is the 608205219Srdivacky // TranslationUnit, while the semantic context is the namespace N. 609205219Srdivacky if (!Lexical || !DC || !S->getParent() || 610205219Srdivacky !S->getParent()->isTemplateParamScope()) 611205219Srdivacky return std::make_pair(Lexical, false); 612205219Srdivacky 613205219Srdivacky // Find the outermost template parameter scope. 614205219Srdivacky // For the example, this is the scope for the template parameters of 615205219Srdivacky // template<class C>. 616205219Srdivacky Scope *OutermostTemplateScope = S->getParent(); 617205219Srdivacky while (OutermostTemplateScope->getParent() && 618205219Srdivacky OutermostTemplateScope->getParent()->isTemplateParamScope()) 619205219Srdivacky OutermostTemplateScope = OutermostTemplateScope->getParent(); 620198092Srdivacky 621205219Srdivacky // Find the namespace context in which the original scope occurs. In 622205219Srdivacky // the example, this is namespace N. 623205219Srdivacky DeclContext *Semantic = DC; 624205219Srdivacky while (!Semantic->isFileContext()) 625205219Srdivacky Semantic = Semantic->getParent(); 626205219Srdivacky 627205219Srdivacky // Find the declaration context just outside of the template 628205219Srdivacky // parameter scope. This is the context in which the template is 629205219Srdivacky // being lexically declaration (a namespace context). In the 630205219Srdivacky // example, this is the global scope. 631205219Srdivacky if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && 632205219Srdivacky Lexical->Encloses(Semantic)) 633205219Srdivacky return std::make_pair(Semantic, true); 634205219Srdivacky 635205219Srdivacky return std::make_pair(Lexical, false); 636198092Srdivacky} 637198092Srdivacky 638199482Srdivackybool Sema::CppLookupName(LookupResult &R, Scope *S) { 639201361Srdivacky assert(getLangOptions().CPlusPlus && "Can perform only C++ lookup"); 640198092Srdivacky 641199482Srdivacky DeclarationName Name = R.getLookupName(); 642199482Srdivacky 643193326Sed Scope *Initial = S; 644198092Srdivacky IdentifierResolver::iterator 645193326Sed I = IdResolver.begin(Name), 646193326Sed IEnd = IdResolver.end(); 647193326Sed 648193326Sed // First we lookup local scope. 649193326Sed // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] 650193326Sed // ...During unqualified name lookup (3.4.1), the names appear as if 651193326Sed // they were declared in the nearest enclosing namespace which contains 652193326Sed // both the using-directive and the nominated namespace. 653198092Srdivacky // [Note: in this context, "contains" means "contains directly or 654198092Srdivacky // indirectly". 655193326Sed // 656193326Sed // For example: 657193326Sed // namespace A { int i; } 658193326Sed // void foo() { 659193326Sed // int i; 660193326Sed // { 661193326Sed // using namespace A; 662193326Sed // ++i; // finds local 'i', A::i appears at global scope 663193326Sed // } 664193326Sed // } 665193326Sed // 666205219Srdivacky DeclContext *OutsideOfTemplateParamDC = 0; 667193326Sed for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { 668208600Srdivacky DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); 669208600Srdivacky 670193326Sed // Check whether the IdResolver has anything in this scope. 671198092Srdivacky bool Found = false; 672193326Sed for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { 673201361Srdivacky if (R.isAcceptableDecl(*I)) { 674198092Srdivacky Found = true; 675198092Srdivacky R.addDecl(*I); 676193326Sed } 677193326Sed } 678198092Srdivacky if (Found) { 679198092Srdivacky R.resolveKind(); 680208600Srdivacky if (S->isClassScope()) 681208600Srdivacky if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) 682208600Srdivacky R.setNamingClass(Record); 683198092Srdivacky return true; 684198092Srdivacky } 685198092Srdivacky 686205219Srdivacky if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 687205219Srdivacky S->getParent() && !S->getParent()->isTemplateParamScope()) { 688205219Srdivacky // We've just searched the last template parameter scope and 689205219Srdivacky // found nothing, so look into the the contexts between the 690205219Srdivacky // lexical and semantic declaration contexts returned by 691205219Srdivacky // findOuterContext(). This implements the name lookup behavior 692205219Srdivacky // of C++ [temp.local]p8. 693205219Srdivacky Ctx = OutsideOfTemplateParamDC; 694205219Srdivacky OutsideOfTemplateParamDC = 0; 695205219Srdivacky } 696205219Srdivacky 697205219Srdivacky if (Ctx) { 698205219Srdivacky DeclContext *OuterCtx; 699205219Srdivacky bool SearchAfterTemplateScope; 700205219Srdivacky llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 701205219Srdivacky if (SearchAfterTemplateScope) 702205219Srdivacky OutsideOfTemplateParamDC = OuterCtx; 703205219Srdivacky 704205219Srdivacky for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 705204643Srdivacky // We do not directly look into transparent contexts, since 706204643Srdivacky // those entities will be found in the nearest enclosing 707204643Srdivacky // non-transparent context. 708204643Srdivacky if (Ctx->isTransparentContext()) 709198092Srdivacky continue; 710204643Srdivacky 711204643Srdivacky // We do not look directly into function or method contexts, 712204643Srdivacky // since all of the local variables and parameters of the 713204643Srdivacky // function/method are present within the Scope. 714204643Srdivacky if (Ctx->isFunctionOrMethod()) { 715204643Srdivacky // If we have an Objective-C instance method, look for ivars 716204643Srdivacky // in the corresponding interface. 717204643Srdivacky if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 718204643Srdivacky if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) 719204643Srdivacky if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { 720204643Srdivacky ObjCInterfaceDecl *ClassDeclared; 721204643Srdivacky if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( 722204643Srdivacky Name.getAsIdentifierInfo(), 723204643Srdivacky ClassDeclared)) { 724204643Srdivacky if (R.isAcceptableDecl(Ivar)) { 725204643Srdivacky R.addDecl(Ivar); 726204643Srdivacky R.resolveKind(); 727204643Srdivacky return true; 728204643Srdivacky } 729204643Srdivacky } 730204643Srdivacky } 731204643Srdivacky } 732204643Srdivacky 733204643Srdivacky continue; 734204643Srdivacky } 735204643Srdivacky 736198092Srdivacky // Perform qualified name lookup into this context. 737198092Srdivacky // FIXME: In some cases, we know that every name that could be found by 738198092Srdivacky // this qualified name lookup will also be on the identifier chain. For 739198092Srdivacky // example, inside a class without any base classes, we never need to 740198092Srdivacky // perform qualified lookup because all of the members are on top of the 741198092Srdivacky // identifier chain. 742202379Srdivacky if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) 743198092Srdivacky return true; 744193326Sed } 745193326Sed } 746193326Sed } 747193326Sed 748199482Srdivacky // Stop if we ran out of scopes. 749199482Srdivacky // FIXME: This really, really shouldn't be happening. 750199482Srdivacky if (!S) return false; 751199482Srdivacky 752193326Sed // Collect UsingDirectiveDecls in all scopes, and recursively all 753193326Sed // nominated namespaces by those using-directives. 754199482Srdivacky // 755193326Sed // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we 756193326Sed // don't build it for each lookup! 757193326Sed 758199482Srdivacky UnqualUsingDirectiveSet UDirs; 759199482Srdivacky UDirs.visitScopeChain(Initial, S); 760199482Srdivacky UDirs.done(); 761193326Sed 762193326Sed // Lookup namespace scope, and global scope. 763193326Sed // Unqualified name lookup in C++ requires looking into scopes 764193326Sed // that aren't strictly lexical, and therefore we walk through the 765193326Sed // context as well as walking through the scopes. 766193326Sed 767193326Sed for (; S; S = S->getParent()) { 768193326Sed // Check whether the IdResolver has anything in this scope. 769198092Srdivacky bool Found = false; 770193326Sed for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { 771201361Srdivacky if (R.isAcceptableDecl(*I)) { 772193326Sed // We found something. Look for anything else in our scope 773193326Sed // with this same name and in an acceptable identifier 774193326Sed // namespace, so that we can construct an overload set if we 775193326Sed // need to. 776198092Srdivacky Found = true; 777198092Srdivacky R.addDecl(*I); 778193326Sed } 779193326Sed } 780193326Sed 781208600Srdivacky if (Found && S->isTemplateParamScope()) { 782208600Srdivacky R.resolveKind(); 783208600Srdivacky return true; 784208600Srdivacky } 785193326Sed 786208600Srdivacky DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); 787208600Srdivacky if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 788208600Srdivacky S->getParent() && !S->getParent()->isTemplateParamScope()) { 789208600Srdivacky // We've just searched the last template parameter scope and 790208600Srdivacky // found nothing, so look into the the contexts between the 791208600Srdivacky // lexical and semantic declaration contexts returned by 792208600Srdivacky // findOuterContext(). This implements the name lookup behavior 793208600Srdivacky // of C++ [temp.local]p8. 794208600Srdivacky Ctx = OutsideOfTemplateParamDC; 795208600Srdivacky OutsideOfTemplateParamDC = 0; 796203955Srdivacky } 797208600Srdivacky 798208600Srdivacky if (Ctx) { 799208600Srdivacky DeclContext *OuterCtx; 800208600Srdivacky bool SearchAfterTemplateScope; 801208600Srdivacky llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 802208600Srdivacky if (SearchAfterTemplateScope) 803208600Srdivacky OutsideOfTemplateParamDC = OuterCtx; 804203955Srdivacky 805208600Srdivacky for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 806208600Srdivacky // We do not directly look into transparent contexts, since 807208600Srdivacky // those entities will be found in the nearest enclosing 808208600Srdivacky // non-transparent context. 809208600Srdivacky if (Ctx->isTransparentContext()) 810208600Srdivacky continue; 811208600Srdivacky 812208600Srdivacky // If we have a context, and it's not a context stashed in the 813208600Srdivacky // template parameter scope for an out-of-line definition, also 814208600Srdivacky // look into that context. 815208600Srdivacky if (!(Found && S && S->isTemplateParamScope())) { 816208600Srdivacky assert(Ctx->isFileContext() && 817208600Srdivacky "We should have been looking only at file context here already."); 818208600Srdivacky 819208600Srdivacky // Look into context considering using-directives. 820208600Srdivacky if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) 821208600Srdivacky Found = true; 822208600Srdivacky } 823208600Srdivacky 824208600Srdivacky if (Found) { 825208600Srdivacky R.resolveKind(); 826208600Srdivacky return true; 827208600Srdivacky } 828208600Srdivacky 829208600Srdivacky if (R.isForRedeclaration() && !Ctx->isTransparentContext()) 830208600Srdivacky return false; 831208600Srdivacky } 832198092Srdivacky } 833198092Srdivacky 834203955Srdivacky if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) 835198092Srdivacky return false; 836193326Sed } 837193326Sed 838198092Srdivacky return !R.empty(); 839193326Sed} 840193326Sed 841193326Sed/// @brief Perform unqualified name lookup starting from a given 842193326Sed/// scope. 843193326Sed/// 844193326Sed/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is 845193326Sed/// used to find names within the current scope. For example, 'x' in 846193326Sed/// @code 847193326Sed/// int x; 848193326Sed/// int f() { 849193326Sed/// return x; // unqualified name look finds 'x' in the global scope 850193326Sed/// } 851193326Sed/// @endcode 852193326Sed/// 853193326Sed/// Different lookup criteria can find different names. For example, a 854193326Sed/// particular scope can have both a struct and a function of the same 855193326Sed/// name, and each can be found by certain lookup criteria. For more 856193326Sed/// information about lookup criteria, see the documentation for the 857193326Sed/// class LookupCriteria. 858193326Sed/// 859193326Sed/// @param S The scope from which unqualified name lookup will 860193326Sed/// begin. If the lookup criteria permits, name lookup may also search 861193326Sed/// in the parent scopes. 862193326Sed/// 863193326Sed/// @param Name The name of the entity that we are searching for. 864193326Sed/// 865193326Sed/// @param Loc If provided, the source location where we're performing 866198092Srdivacky/// name lookup. At present, this is only used to produce diagnostics when 867193326Sed/// C library functions (like "malloc") are implicitly declared. 868193326Sed/// 869193326Sed/// @returns The result of name lookup, which includes zero or more 870193326Sed/// declarations and possibly additional information used to diagnose 871193326Sed/// ambiguities. 872199482Srdivackybool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { 873199482Srdivacky DeclarationName Name = R.getLookupName(); 874198092Srdivacky if (!Name) return false; 875193326Sed 876199482Srdivacky LookupNameKind NameKind = R.getLookupKind(); 877199482Srdivacky 878193326Sed if (!getLangOptions().CPlusPlus) { 879193326Sed // Unqualified name lookup in C/Objective-C is purely lexical, so 880193326Sed // search in the declarations attached to the name. 881193326Sed 882201361Srdivacky if (NameKind == Sema::LookupRedeclarationWithLinkage) { 883193326Sed // Find the nearest non-transparent declaration scope. 884193326Sed while (!(S->getFlags() & Scope::DeclScope) || 885198092Srdivacky (S->getEntity() && 886193326Sed static_cast<DeclContext *>(S->getEntity()) 887193326Sed ->isTransparentContext())) 888193326Sed S = S->getParent(); 889201361Srdivacky } 890193326Sed 891201361Srdivacky unsigned IDNS = R.getIdentifierNamespace(); 892193326Sed 893193326Sed // Scan up the scope chain looking for a decl that matches this 894193326Sed // identifier that is in the appropriate namespace. This search 895193326Sed // should not take long, as shadowing of names is uncommon, and 896193326Sed // deep shadowing is extremely uncommon. 897193326Sed bool LeftStartingScope = false; 898193326Sed 899193326Sed for (IdentifierResolver::iterator I = IdResolver.begin(Name), 900198092Srdivacky IEnd = IdResolver.end(); 901193326Sed I != IEnd; ++I) 902193326Sed if ((*I)->isInIdentifierNamespace(IDNS)) { 903193326Sed if (NameKind == LookupRedeclarationWithLinkage) { 904193326Sed // Determine whether this (or a previous) declaration is 905193326Sed // out-of-scope. 906193326Sed if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I))) 907193326Sed LeftStartingScope = true; 908193326Sed 909193326Sed // If we found something outside of our starting scope that 910193326Sed // does not have linkage, skip it. 911193326Sed if (LeftStartingScope && !((*I)->hasLinkage())) 912193326Sed continue; 913193326Sed } 914193326Sed 915198092Srdivacky R.addDecl(*I); 916198092Srdivacky 917195341Sed if ((*I)->getAttr<OverloadableAttr>()) { 918193326Sed // If this declaration has the "overloadable" attribute, we 919193326Sed // might have a set of overloaded functions. 920193326Sed 921193326Sed // Figure out what scope the identifier is in. 922193326Sed while (!(S->getFlags() & Scope::DeclScope) || 923193326Sed !S->isDeclScope(DeclPtrTy::make(*I))) 924193326Sed S = S->getParent(); 925193326Sed 926193326Sed // Find the last declaration in this scope (with the same 927193326Sed // name, naturally). 928193326Sed IdentifierResolver::iterator LastI = I; 929193326Sed for (++LastI; LastI != IEnd; ++LastI) { 930193326Sed if (!S->isDeclScope(DeclPtrTy::make(*LastI))) 931193326Sed break; 932198092Srdivacky R.addDecl(*LastI); 933193326Sed } 934193326Sed } 935193326Sed 936198092Srdivacky R.resolveKind(); 937198092Srdivacky 938198092Srdivacky return true; 939193326Sed } 940193326Sed } else { 941193326Sed // Perform C++ unqualified name lookup. 942199482Srdivacky if (CppLookupName(R, S)) 943198092Srdivacky return true; 944193326Sed } 945193326Sed 946193326Sed // If we didn't find a use of this identifier, and if the identifier 947193326Sed // corresponds to a compiler builtin, create the decl object for the builtin 948193326Sed // now, injecting it into translation unit scope, and return it. 949203955Srdivacky if (AllowBuiltinCreation) 950203955Srdivacky return LookupBuiltin(*this, R); 951193326Sed 952198092Srdivacky return false; 953193326Sed} 954193326Sed 955198092Srdivacky/// @brief Perform qualified name lookup in the namespaces nominated by 956198092Srdivacky/// using directives by the given context. 957198092Srdivacky/// 958198092Srdivacky/// C++98 [namespace.qual]p2: 959198092Srdivacky/// Given X::m (where X is a user-declared namespace), or given ::m 960198092Srdivacky/// (where X is the global namespace), let S be the set of all 961198092Srdivacky/// declarations of m in X and in the transitive closure of all 962198092Srdivacky/// namespaces nominated by using-directives in X and its used 963198092Srdivacky/// namespaces, except that using-directives are ignored in any 964198092Srdivacky/// namespace, including X, directly containing one or more 965198092Srdivacky/// declarations of m. No namespace is searched more than once in 966198092Srdivacky/// the lookup of a name. If S is the empty set, the program is 967198092Srdivacky/// ill-formed. Otherwise, if S has exactly one member, or if the 968198092Srdivacky/// context of the reference is a using-declaration 969198092Srdivacky/// (namespace.udecl), S is the required set of declarations of 970198092Srdivacky/// m. Otherwise if the use of m is not one that allows a unique 971198092Srdivacky/// declaration to be chosen from S, the program is ill-formed. 972198092Srdivacky/// C++98 [namespace.qual]p5: 973198092Srdivacky/// During the lookup of a qualified namespace member name, if the 974198092Srdivacky/// lookup finds more than one declaration of the member, and if one 975198092Srdivacky/// declaration introduces a class name or enumeration name and the 976198092Srdivacky/// other declarations either introduce the same object, the same 977198092Srdivacky/// enumerator or a set of functions, the non-type name hides the 978198092Srdivacky/// class or enumeration name if and only if the declarations are 979198092Srdivacky/// from the same namespace; otherwise (the declarations are from 980198092Srdivacky/// different namespaces), the program is ill-formed. 981203955Srdivackystatic bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, 982199482Srdivacky DeclContext *StartDC) { 983198092Srdivacky assert(StartDC->isFileContext() && "start context is not a file context"); 984198092Srdivacky 985198092Srdivacky DeclContext::udir_iterator I = StartDC->using_directives_begin(); 986198092Srdivacky DeclContext::udir_iterator E = StartDC->using_directives_end(); 987198092Srdivacky 988198092Srdivacky if (I == E) return false; 989198092Srdivacky 990198092Srdivacky // We have at least added all these contexts to the queue. 991198092Srdivacky llvm::DenseSet<DeclContext*> Visited; 992198092Srdivacky Visited.insert(StartDC); 993198092Srdivacky 994198092Srdivacky // We have not yet looked into these namespaces, much less added 995198092Srdivacky // their "using-children" to the queue. 996198092Srdivacky llvm::SmallVector<NamespaceDecl*, 8> Queue; 997198092Srdivacky 998198092Srdivacky // We have already looked into the initial namespace; seed the queue 999198092Srdivacky // with its using-children. 1000198092Srdivacky for (; I != E; ++I) { 1001199482Srdivacky NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace(); 1002198092Srdivacky if (Visited.insert(ND).second) 1003198092Srdivacky Queue.push_back(ND); 1004198092Srdivacky } 1005198092Srdivacky 1006198092Srdivacky // The easiest way to implement the restriction in [namespace.qual]p5 1007198092Srdivacky // is to check whether any of the individual results found a tag 1008198092Srdivacky // and, if so, to declare an ambiguity if the final result is not 1009198092Srdivacky // a tag. 1010198092Srdivacky bool FoundTag = false; 1011198092Srdivacky bool FoundNonTag = false; 1012198092Srdivacky 1013199482Srdivacky LookupResult LocalR(LookupResult::Temporary, R); 1014198092Srdivacky 1015198092Srdivacky bool Found = false; 1016198092Srdivacky while (!Queue.empty()) { 1017198092Srdivacky NamespaceDecl *ND = Queue.back(); 1018198092Srdivacky Queue.pop_back(); 1019198092Srdivacky 1020198092Srdivacky // We go through some convolutions here to avoid copying results 1021198092Srdivacky // between LookupResults. 1022198092Srdivacky bool UseLocal = !R.empty(); 1023199482Srdivacky LookupResult &DirectR = UseLocal ? LocalR : R; 1024203955Srdivacky bool FoundDirect = LookupDirect(S, DirectR, ND); 1025198092Srdivacky 1026198092Srdivacky if (FoundDirect) { 1027198092Srdivacky // First do any local hiding. 1028198092Srdivacky DirectR.resolveKind(); 1029198092Srdivacky 1030198092Srdivacky // If the local result is a tag, remember that. 1031198092Srdivacky if (DirectR.isSingleTagDecl()) 1032198092Srdivacky FoundTag = true; 1033198092Srdivacky else 1034198092Srdivacky FoundNonTag = true; 1035198092Srdivacky 1036198092Srdivacky // Append the local results to the total results if necessary. 1037198092Srdivacky if (UseLocal) { 1038198092Srdivacky R.addAllDecls(LocalR); 1039198092Srdivacky LocalR.clear(); 1040198092Srdivacky } 1041198092Srdivacky } 1042198092Srdivacky 1043198092Srdivacky // If we find names in this namespace, ignore its using directives. 1044198092Srdivacky if (FoundDirect) { 1045198092Srdivacky Found = true; 1046198092Srdivacky continue; 1047198092Srdivacky } 1048198092Srdivacky 1049198092Srdivacky for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) { 1050198092Srdivacky NamespaceDecl *Nom = (*I)->getNominatedNamespace(); 1051198092Srdivacky if (Visited.insert(Nom).second) 1052198092Srdivacky Queue.push_back(Nom); 1053198092Srdivacky } 1054198092Srdivacky } 1055198092Srdivacky 1056198092Srdivacky if (Found) { 1057198092Srdivacky if (FoundTag && FoundNonTag) 1058198092Srdivacky R.setAmbiguousQualifiedTagHiding(); 1059198092Srdivacky else 1060198092Srdivacky R.resolveKind(); 1061198092Srdivacky } 1062198092Srdivacky 1063198092Srdivacky return Found; 1064198092Srdivacky} 1065198092Srdivacky 1066202379Srdivacky/// \brief Perform qualified name lookup into a given context. 1067193326Sed/// 1068193326Sed/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find 1069193326Sed/// names when the context of those names is explicit specified, e.g., 1070202379Srdivacky/// "std::vector" or "x->member", or as part of unqualified name lookup. 1071193326Sed/// 1072193326Sed/// Different lookup criteria can find different names. For example, a 1073193326Sed/// particular scope can have both a struct and a function of the same 1074193326Sed/// name, and each can be found by certain lookup criteria. For more 1075193326Sed/// information about lookup criteria, see the documentation for the 1076193326Sed/// class LookupCriteria. 1077193326Sed/// 1078202379Srdivacky/// \param R captures both the lookup criteria and any lookup results found. 1079202379Srdivacky/// 1080202379Srdivacky/// \param LookupCtx The context in which qualified name lookup will 1081193326Sed/// search. If the lookup criteria permits, name lookup may also search 1082193326Sed/// in the parent contexts or (for C++ classes) base classes. 1083193326Sed/// 1084202379Srdivacky/// \param InUnqualifiedLookup true if this is qualified name lookup that 1085202379Srdivacky/// occurs as part of unqualified name lookup. 1086193326Sed/// 1087202379Srdivacky/// \returns true if lookup succeeded, false if it failed. 1088202379Srdivackybool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, 1089202379Srdivacky bool InUnqualifiedLookup) { 1090193326Sed assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); 1091193326Sed 1092199482Srdivacky if (!R.getLookupName()) 1093198092Srdivacky return false; 1094198092Srdivacky 1095198092Srdivacky // Make sure that the declaration context is complete. 1096198092Srdivacky assert((!isa<TagDecl>(LookupCtx) || 1097198092Srdivacky LookupCtx->isDependentContext() || 1098198092Srdivacky cast<TagDecl>(LookupCtx)->isDefinition() || 1099198092Srdivacky Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>() 1100198092Srdivacky ->isBeingDefined()) && 1101198092Srdivacky "Declaration context must already be complete!"); 1102198092Srdivacky 1103193326Sed // Perform qualified name lookup into the LookupCtx. 1104203955Srdivacky if (LookupDirect(*this, R, LookupCtx)) { 1105198092Srdivacky R.resolveKind(); 1106202879Srdivacky if (isa<CXXRecordDecl>(LookupCtx)) 1107202879Srdivacky R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); 1108198092Srdivacky return true; 1109198092Srdivacky } 1110193326Sed 1111198092Srdivacky // Don't descend into implied contexts for redeclarations. 1112198092Srdivacky // C++98 [namespace.qual]p6: 1113198092Srdivacky // In a declaration for a namespace member in which the 1114198092Srdivacky // declarator-id is a qualified-id, given that the qualified-id 1115198092Srdivacky // for the namespace member has the form 1116198092Srdivacky // nested-name-specifier unqualified-id 1117198092Srdivacky // the unqualified-id shall name a member of the namespace 1118198092Srdivacky // designated by the nested-name-specifier. 1119198092Srdivacky // See also [class.mfct]p5 and [class.static.data]p2. 1120199482Srdivacky if (R.isForRedeclaration()) 1121198092Srdivacky return false; 1122198092Srdivacky 1123199482Srdivacky // If this is a namespace, look it up in the implied namespaces. 1124198092Srdivacky if (LookupCtx->isFileContext()) 1125203955Srdivacky return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); 1126198092Srdivacky 1127198092Srdivacky // If this isn't a C++ class, we aren't allowed to look into base 1128193326Sed // classes, we're done. 1129202379Srdivacky CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); 1130202379Srdivacky if (!LookupRec) 1131198092Srdivacky return false; 1132193326Sed 1133202379Srdivacky // If we're performing qualified name lookup into a dependent class, 1134202379Srdivacky // then we are actually looking into a current instantiation. If we have any 1135202379Srdivacky // dependent base classes, then we either have to delay lookup until 1136202379Srdivacky // template instantiation time (at which point all bases will be available) 1137202379Srdivacky // or we have to fail. 1138202379Srdivacky if (!InUnqualifiedLookup && LookupRec->isDependentContext() && 1139202379Srdivacky LookupRec->hasAnyDependentBases()) { 1140202379Srdivacky R.setNotFoundInCurrentInstantiation(); 1141202379Srdivacky return false; 1142202379Srdivacky } 1143202379Srdivacky 1144193326Sed // Perform lookup into our base classes. 1145198092Srdivacky CXXBasePaths Paths; 1146198092Srdivacky Paths.setOrigin(LookupRec); 1147193326Sed 1148193326Sed // Look for this member in our base classes 1149198092Srdivacky CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0; 1150199482Srdivacky switch (R.getLookupKind()) { 1151198092Srdivacky case LookupOrdinaryName: 1152198092Srdivacky case LookupMemberName: 1153198092Srdivacky case LookupRedeclarationWithLinkage: 1154198092Srdivacky BaseCallback = &CXXRecordDecl::FindOrdinaryMember; 1155198092Srdivacky break; 1156198092Srdivacky 1157198092Srdivacky case LookupTagName: 1158198092Srdivacky BaseCallback = &CXXRecordDecl::FindTagMember; 1159198092Srdivacky break; 1160200583Srdivacky 1161200583Srdivacky case LookupUsingDeclName: 1162200583Srdivacky // This lookup is for redeclarations only. 1163198092Srdivacky 1164198092Srdivacky case LookupOperatorName: 1165198092Srdivacky case LookupNamespaceName: 1166198092Srdivacky case LookupObjCProtocolName: 1167198092Srdivacky // These lookups will never find a member in a C++ class (or base class). 1168198092Srdivacky return false; 1169198092Srdivacky 1170198092Srdivacky case LookupNestedNameSpecifierName: 1171198092Srdivacky BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; 1172198092Srdivacky break; 1173198092Srdivacky } 1174198092Srdivacky 1175199482Srdivacky if (!LookupRec->lookupInBases(BaseCallback, 1176199482Srdivacky R.getLookupName().getAsOpaquePtr(), Paths)) 1177198092Srdivacky return false; 1178193326Sed 1179202879Srdivacky R.setNamingClass(LookupRec); 1180202879Srdivacky 1181193326Sed // C++ [class.member.lookup]p2: 1182193326Sed // [...] If the resulting set of declarations are not all from 1183193326Sed // sub-objects of the same type, or the set has a nonstatic member 1184193326Sed // and includes members from distinct sub-objects, there is an 1185193326Sed // ambiguity and the program is ill-formed. Otherwise that set is 1186193326Sed // the result of the lookup. 1187193326Sed // FIXME: support using declarations! 1188193326Sed QualType SubobjectType; 1189193326Sed int SubobjectNumber = 0; 1190205408Srdivacky AccessSpecifier SubobjectAccess = AS_none; 1191198092Srdivacky for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); 1192193326Sed Path != PathEnd; ++Path) { 1193198092Srdivacky const CXXBasePathElement &PathElement = Path->back(); 1194193326Sed 1195202879Srdivacky // Pick the best (i.e. most permissive i.e. numerically lowest) access 1196202879Srdivacky // across all paths. 1197202879Srdivacky SubobjectAccess = std::min(SubobjectAccess, Path->Access); 1198202879Srdivacky 1199193326Sed // Determine whether we're looking at a distinct sub-object or not. 1200193326Sed if (SubobjectType.isNull()) { 1201198092Srdivacky // This is the first subobject we've looked at. Record its type. 1202193326Sed SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); 1203193326Sed SubobjectNumber = PathElement.SubobjectNumber; 1204198092Srdivacky } else if (SubobjectType 1205193326Sed != Context.getCanonicalType(PathElement.Base->getType())) { 1206193326Sed // We found members of the given name in two subobjects of 1207193326Sed // different types. This lookup is ambiguous. 1208198092Srdivacky R.setAmbiguousBaseSubobjectTypes(Paths); 1209198092Srdivacky return true; 1210193326Sed } else if (SubobjectNumber != PathElement.SubobjectNumber) { 1211193326Sed // We have a different subobject of the same type. 1212193326Sed 1213193326Sed // C++ [class.member.lookup]p5: 1214193326Sed // A static member, a nested type or an enumerator defined in 1215193326Sed // a base class T can unambiguously be found even if an object 1216198092Srdivacky // has more than one base class subobject of type T. 1217193326Sed Decl *FirstDecl = *Path->Decls.first; 1218193326Sed if (isa<VarDecl>(FirstDecl) || 1219193326Sed isa<TypeDecl>(FirstDecl) || 1220193326Sed isa<EnumConstantDecl>(FirstDecl)) 1221193326Sed continue; 1222193326Sed 1223193326Sed if (isa<CXXMethodDecl>(FirstDecl)) { 1224193326Sed // Determine whether all of the methods are static. 1225193326Sed bool AllMethodsAreStatic = true; 1226193326Sed for (DeclContext::lookup_iterator Func = Path->Decls.first; 1227193326Sed Func != Path->Decls.second; ++Func) { 1228193326Sed if (!isa<CXXMethodDecl>(*Func)) { 1229193326Sed assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); 1230193326Sed break; 1231193326Sed } 1232193326Sed 1233193326Sed if (!cast<CXXMethodDecl>(*Func)->isStatic()) { 1234193326Sed AllMethodsAreStatic = false; 1235193326Sed break; 1236193326Sed } 1237193326Sed } 1238193326Sed 1239193326Sed if (AllMethodsAreStatic) 1240193326Sed continue; 1241193326Sed } 1242193326Sed 1243193326Sed // We have found a nonstatic member name in multiple, distinct 1244193326Sed // subobjects. Name lookup is ambiguous. 1245198092Srdivacky R.setAmbiguousBaseSubobjects(Paths); 1246198092Srdivacky return true; 1247193326Sed } 1248193326Sed } 1249193326Sed 1250193326Sed // Lookup in a base class succeeded; return these results. 1251193326Sed 1252198092Srdivacky DeclContext::lookup_iterator I, E; 1253202879Srdivacky for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I) { 1254202879Srdivacky NamedDecl *D = *I; 1255202879Srdivacky AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, 1256202879Srdivacky D->getAccess()); 1257202879Srdivacky R.addDecl(D, AS); 1258202879Srdivacky } 1259198092Srdivacky R.resolveKind(); 1260198092Srdivacky return true; 1261193326Sed} 1262193326Sed 1263193326Sed/// @brief Performs name lookup for a name that was parsed in the 1264193326Sed/// source code, and may contain a C++ scope specifier. 1265193326Sed/// 1266193326Sed/// This routine is a convenience routine meant to be called from 1267193326Sed/// contexts that receive a name and an optional C++ scope specifier 1268193326Sed/// (e.g., "N::M::x"). It will then perform either qualified or 1269193326Sed/// unqualified name lookup (with LookupQualifiedName or LookupName, 1270193326Sed/// respectively) on the given name and return those results. 1271193326Sed/// 1272193326Sed/// @param S The scope from which unqualified name lookup will 1273193326Sed/// begin. 1274193326Sed/// 1275198092Srdivacky/// @param SS An optional C++ scope-specifier, e.g., "::N::M". 1276198092Srdivacky/// 1277193326Sed/// @param Name The name of the entity that name lookup will 1278193326Sed/// search for. 1279193326Sed/// 1280193326Sed/// @param Loc If provided, the source location where we're performing 1281198092Srdivacky/// name lookup. At present, this is only used to produce diagnostics when 1282193326Sed/// C library functions (like "malloc") are implicitly declared. 1283193326Sed/// 1284198092Srdivacky/// @param EnteringContext Indicates whether we are going to enter the 1285198092Srdivacky/// context of the scope-specifier SS (if present). 1286198092Srdivacky/// 1287198092Srdivacky/// @returns True if any decls were found (but possibly ambiguous) 1288207619Srdivackybool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, 1289199482Srdivacky bool AllowBuiltinCreation, bool EnteringContext) { 1290198092Srdivacky if (SS && SS->isInvalid()) { 1291198092Srdivacky // When the scope specifier is invalid, don't even look for 1292193326Sed // anything. 1293198092Srdivacky return false; 1294198092Srdivacky } 1295193326Sed 1296198092Srdivacky if (SS && SS->isSet()) { 1297198092Srdivacky if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { 1298198092Srdivacky // We have resolved the scope specifier to a particular declaration 1299198092Srdivacky // contex, and will perform name lookup in that context. 1300207619Srdivacky if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) 1301198092Srdivacky return false; 1302193326Sed 1303199482Srdivacky R.setContextRange(SS->getRange()); 1304199482Srdivacky 1305199482Srdivacky return LookupQualifiedName(R, DC); 1306193326Sed } 1307193326Sed 1308198092Srdivacky // We could not resolve the scope specified to a specific declaration 1309198092Srdivacky // context, which means that SS refers to an unknown specialization. 1310198092Srdivacky // Name lookup can't find anything in this case. 1311198092Srdivacky return false; 1312193326Sed } 1313193326Sed 1314198092Srdivacky // Perform unqualified name lookup starting in the given scope. 1315199482Srdivacky return LookupName(R, S, AllowBuiltinCreation); 1316193326Sed} 1317193326Sed 1318193326Sed 1319193326Sed/// @brief Produce a diagnostic describing the ambiguity that resulted 1320193326Sed/// from name lookup. 1321193326Sed/// 1322193326Sed/// @param Result The ambiguous name lookup result. 1323198092Srdivacky/// 1324193326Sed/// @param Name The name of the entity that name lookup was 1325193326Sed/// searching for. 1326193326Sed/// 1327193326Sed/// @param NameLoc The location of the name within the source code. 1328193326Sed/// 1329193326Sed/// @param LookupRange A source range that provides more 1330193326Sed/// source-location information concerning the lookup itself. For 1331193326Sed/// example, this range might highlight a nested-name-specifier that 1332193326Sed/// precedes the name. 1333193326Sed/// 1334193326Sed/// @returns true 1335199482Srdivackybool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { 1336193326Sed assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); 1337193326Sed 1338199482Srdivacky DeclarationName Name = Result.getLookupName(); 1339199482Srdivacky SourceLocation NameLoc = Result.getNameLoc(); 1340199482Srdivacky SourceRange LookupRange = Result.getContextRange(); 1341199482Srdivacky 1342198092Srdivacky switch (Result.getAmbiguityKind()) { 1343198092Srdivacky case LookupResult::AmbiguousBaseSubobjects: { 1344198092Srdivacky CXXBasePaths *Paths = Result.getBasePaths(); 1345198092Srdivacky QualType SubobjectType = Paths->front().back().Base->getType(); 1346198092Srdivacky Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) 1347198092Srdivacky << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) 1348198092Srdivacky << LookupRange; 1349198092Srdivacky 1350198092Srdivacky DeclContext::lookup_iterator Found = Paths->front().Decls.first; 1351198092Srdivacky while (isa<CXXMethodDecl>(*Found) && 1352198092Srdivacky cast<CXXMethodDecl>(*Found)->isStatic()) 1353198092Srdivacky ++Found; 1354198092Srdivacky 1355198092Srdivacky Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); 1356198092Srdivacky 1357198092Srdivacky return true; 1358198092Srdivacky } 1359193326Sed 1360198092Srdivacky case LookupResult::AmbiguousBaseSubobjectTypes: { 1361193326Sed Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) 1362193326Sed << Name << LookupRange; 1363198092Srdivacky 1364198092Srdivacky CXXBasePaths *Paths = Result.getBasePaths(); 1365193326Sed std::set<Decl *> DeclsPrinted; 1366198092Srdivacky for (CXXBasePaths::paths_iterator Path = Paths->begin(), 1367198092Srdivacky PathEnd = Paths->end(); 1368193326Sed Path != PathEnd; ++Path) { 1369193326Sed Decl *D = *Path->Decls.first; 1370193326Sed if (DeclsPrinted.insert(D).second) 1371193326Sed Diag(D->getLocation(), diag::note_ambiguous_member_found); 1372193326Sed } 1373193326Sed 1374193326Sed return true; 1375198092Srdivacky } 1376193326Sed 1377198092Srdivacky case LookupResult::AmbiguousTagHiding: { 1378198092Srdivacky Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; 1379193326Sed 1380198092Srdivacky llvm::SmallPtrSet<NamedDecl*,8> TagDecls; 1381198092Srdivacky 1382198092Srdivacky LookupResult::iterator DI, DE = Result.end(); 1383198092Srdivacky for (DI = Result.begin(); DI != DE; ++DI) 1384198092Srdivacky if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { 1385198092Srdivacky TagDecls.insert(TD); 1386198092Srdivacky Diag(TD->getLocation(), diag::note_hidden_tag); 1387198092Srdivacky } 1388198092Srdivacky 1389198092Srdivacky for (DI = Result.begin(); DI != DE; ++DI) 1390198092Srdivacky if (!isa<TagDecl>(*DI)) 1391198092Srdivacky Diag((*DI)->getLocation(), diag::note_hiding_object); 1392198092Srdivacky 1393198092Srdivacky // For recovery purposes, go ahead and implement the hiding. 1394202879Srdivacky LookupResult::Filter F = Result.makeFilter(); 1395202879Srdivacky while (F.hasNext()) { 1396202879Srdivacky if (TagDecls.count(F.next())) 1397202879Srdivacky F.erase(); 1398202879Srdivacky } 1399202879Srdivacky F.done(); 1400198092Srdivacky 1401198092Srdivacky return true; 1402198092Srdivacky } 1403198092Srdivacky 1404198092Srdivacky case LookupResult::AmbiguousReference: { 1405198092Srdivacky Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; 1406198092Srdivacky 1407198092Srdivacky LookupResult::iterator DI = Result.begin(), DE = Result.end(); 1408198092Srdivacky for (; DI != DE; ++DI) 1409193326Sed Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; 1410193326Sed 1411193326Sed return true; 1412193326Sed } 1413198092Srdivacky } 1414193326Sed 1415200583Srdivacky llvm_unreachable("unknown ambiguity kind"); 1416193326Sed return true; 1417193326Sed} 1418193326Sed 1419198092Srdivackystatic void 1420198092SrdivackyaddAssociatedClassesAndNamespaces(QualType T, 1421198092Srdivacky ASTContext &Context, 1422198092Srdivacky Sema::AssociatedNamespaceSet &AssociatedNamespaces, 1423198092Srdivacky Sema::AssociatedClassSet &AssociatedClasses); 1424198092Srdivacky 1425207619Srdivackystatic void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, 1426207619Srdivacky DeclContext *Ctx) { 1427207619Srdivacky // Add the associated namespace for this class. 1428207619Srdivacky 1429207619Srdivacky // We don't use DeclContext::getEnclosingNamespaceContext() as this may 1430207619Srdivacky // be a locally scoped record. 1431207619Srdivacky 1432207619Srdivacky while (Ctx->isRecord() || Ctx->isTransparentContext()) 1433207619Srdivacky Ctx = Ctx->getParent(); 1434207619Srdivacky 1435198092Srdivacky if (Ctx->isFileContext()) 1436207619Srdivacky Namespaces.insert(Ctx->getPrimaryContext()); 1437198092Srdivacky} 1438198092Srdivacky 1439198092Srdivacky// \brief Add the associated classes and namespaces for argument-dependent 1440198092Srdivacky// lookup that involves a template argument (C++ [basic.lookup.koenig]p2). 1441198092Srdivackystatic void 1442198092SrdivackyaddAssociatedClassesAndNamespaces(const TemplateArgument &Arg, 1443198092Srdivacky ASTContext &Context, 1444198092Srdivacky Sema::AssociatedNamespaceSet &AssociatedNamespaces, 1445198092Srdivacky Sema::AssociatedClassSet &AssociatedClasses) { 1446198092Srdivacky // C++ [basic.lookup.koenig]p2, last bullet: 1447198092Srdivacky // -- [...] ; 1448198092Srdivacky switch (Arg.getKind()) { 1449198092Srdivacky case TemplateArgument::Null: 1450198092Srdivacky break; 1451198092Srdivacky 1452198092Srdivacky case TemplateArgument::Type: 1453198092Srdivacky // [...] the namespaces and classes associated with the types of the 1454198092Srdivacky // template arguments provided for template type parameters (excluding 1455198092Srdivacky // template template parameters) 1456198092Srdivacky addAssociatedClassesAndNamespaces(Arg.getAsType(), Context, 1457198092Srdivacky AssociatedNamespaces, 1458198092Srdivacky AssociatedClasses); 1459198092Srdivacky break; 1460198092Srdivacky 1461199482Srdivacky case TemplateArgument::Template: { 1462198092Srdivacky // [...] the namespaces in which any template template arguments are 1463198092Srdivacky // defined; and the classes in which any member templates used as 1464198092Srdivacky // template template arguments are defined. 1465199482Srdivacky TemplateName Template = Arg.getAsTemplate(); 1466198092Srdivacky if (ClassTemplateDecl *ClassTemplate 1467199482Srdivacky = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { 1468198092Srdivacky DeclContext *Ctx = ClassTemplate->getDeclContext(); 1469198092Srdivacky if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1470198092Srdivacky AssociatedClasses.insert(EnclosingClass); 1471198092Srdivacky // Add the associated namespace for this class. 1472207619Srdivacky CollectEnclosingNamespace(AssociatedNamespaces, Ctx); 1473198092Srdivacky } 1474198092Srdivacky break; 1475199482Srdivacky } 1476199482Srdivacky 1477199482Srdivacky case TemplateArgument::Declaration: 1478198092Srdivacky case TemplateArgument::Integral: 1479198092Srdivacky case TemplateArgument::Expression: 1480198092Srdivacky // [Note: non-type template arguments do not contribute to the set of 1481198092Srdivacky // associated namespaces. ] 1482198092Srdivacky break; 1483198092Srdivacky 1484198092Srdivacky case TemplateArgument::Pack: 1485198092Srdivacky for (TemplateArgument::pack_iterator P = Arg.pack_begin(), 1486198092Srdivacky PEnd = Arg.pack_end(); 1487198092Srdivacky P != PEnd; ++P) 1488198092Srdivacky addAssociatedClassesAndNamespaces(*P, Context, 1489198092Srdivacky AssociatedNamespaces, 1490198092Srdivacky AssociatedClasses); 1491198092Srdivacky break; 1492198092Srdivacky } 1493198092Srdivacky} 1494198092Srdivacky 1495193326Sed// \brief Add the associated classes and namespaces for 1496198092Srdivacky// argument-dependent lookup with an argument of class type 1497198092Srdivacky// (C++ [basic.lookup.koenig]p2). 1498198092Srdivackystatic void 1499198092SrdivackyaddAssociatedClassesAndNamespaces(CXXRecordDecl *Class, 1500193326Sed ASTContext &Context, 1501193326Sed Sema::AssociatedNamespaceSet &AssociatedNamespaces, 1502198092Srdivacky Sema::AssociatedClassSet &AssociatedClasses) { 1503193326Sed // C++ [basic.lookup.koenig]p2: 1504193326Sed // [...] 1505193326Sed // -- If T is a class type (including unions), its associated 1506193326Sed // classes are: the class itself; the class of which it is a 1507193326Sed // member, if any; and its direct and indirect base 1508193326Sed // classes. Its associated namespaces are the namespaces in 1509198092Srdivacky // which its associated classes are defined. 1510193326Sed 1511193326Sed // Add the class of which it is a member, if any. 1512193326Sed DeclContext *Ctx = Class->getDeclContext(); 1513193326Sed if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1514193326Sed AssociatedClasses.insert(EnclosingClass); 1515193326Sed // Add the associated namespace for this class. 1516207619Srdivacky CollectEnclosingNamespace(AssociatedNamespaces, Ctx); 1517198092Srdivacky 1518193326Sed // Add the class itself. If we've already seen this class, we don't 1519193326Sed // need to visit base classes. 1520193326Sed if (!AssociatedClasses.insert(Class)) 1521193326Sed return; 1522193326Sed 1523198092Srdivacky // -- If T is a template-id, its associated namespaces and classes are 1524198092Srdivacky // the namespace in which the template is defined; for member 1525198092Srdivacky // templates, the member template���s class; the namespaces and classes 1526198092Srdivacky // associated with the types of the template arguments provided for 1527198092Srdivacky // template type parameters (excluding template template parameters); the 1528198092Srdivacky // namespaces in which any template template arguments are defined; and 1529198092Srdivacky // the classes in which any member templates used as template template 1530198092Srdivacky // arguments are defined. [Note: non-type template arguments do not 1531198092Srdivacky // contribute to the set of associated namespaces. ] 1532198092Srdivacky if (ClassTemplateSpecializationDecl *Spec 1533198092Srdivacky = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { 1534198092Srdivacky DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); 1535198092Srdivacky if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1536198092Srdivacky AssociatedClasses.insert(EnclosingClass); 1537198092Srdivacky // Add the associated namespace for this class. 1538207619Srdivacky CollectEnclosingNamespace(AssociatedNamespaces, Ctx); 1539193326Sed 1540198092Srdivacky const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1541198092Srdivacky for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 1542198092Srdivacky addAssociatedClassesAndNamespaces(TemplateArgs[I], Context, 1543198092Srdivacky AssociatedNamespaces, 1544198092Srdivacky AssociatedClasses); 1545198092Srdivacky } 1546198092Srdivacky 1547203955Srdivacky // Only recurse into base classes for complete types. 1548203955Srdivacky if (!Class->hasDefinition()) { 1549203955Srdivacky // FIXME: we might need to instantiate templates here 1550203955Srdivacky return; 1551203955Srdivacky } 1552203955Srdivacky 1553193326Sed // Add direct and indirect base classes along with their associated 1554193326Sed // namespaces. 1555193326Sed llvm::SmallVector<CXXRecordDecl *, 32> Bases; 1556193326Sed Bases.push_back(Class); 1557193326Sed while (!Bases.empty()) { 1558193326Sed // Pop this class off the stack. 1559193326Sed Class = Bases.back(); 1560193326Sed Bases.pop_back(); 1561193326Sed 1562193326Sed // Visit the base classes. 1563193326Sed for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), 1564193326Sed BaseEnd = Class->bases_end(); 1565193326Sed Base != BaseEnd; ++Base) { 1566198092Srdivacky const RecordType *BaseType = Base->getType()->getAs<RecordType>(); 1567198893Srdivacky // In dependent contexts, we do ADL twice, and the first time around, 1568198893Srdivacky // the base type might be a dependent TemplateSpecializationType, or a 1569198893Srdivacky // TemplateTypeParmType. If that happens, simply ignore it. 1570198893Srdivacky // FIXME: If we want to support export, we probably need to add the 1571198893Srdivacky // namespace of the template in a TemplateSpecializationType, or even 1572198893Srdivacky // the classes and namespaces of known non-dependent arguments. 1573198893Srdivacky if (!BaseType) 1574198893Srdivacky continue; 1575193326Sed CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); 1576193326Sed if (AssociatedClasses.insert(BaseDecl)) { 1577193326Sed // Find the associated namespace for this base class. 1578193326Sed DeclContext *BaseCtx = BaseDecl->getDeclContext(); 1579207619Srdivacky CollectEnclosingNamespace(AssociatedNamespaces, BaseCtx); 1580193326Sed 1581193326Sed // Make sure we visit the bases of this base class. 1582193326Sed if (BaseDecl->bases_begin() != BaseDecl->bases_end()) 1583193326Sed Bases.push_back(BaseDecl); 1584193326Sed } 1585193326Sed } 1586193326Sed } 1587193326Sed} 1588193326Sed 1589193326Sed// \brief Add the associated classes and namespaces for 1590193326Sed// argument-dependent lookup with an argument of type T 1591198092Srdivacky// (C++ [basic.lookup.koenig]p2). 1592198092Srdivackystatic void 1593198092SrdivackyaddAssociatedClassesAndNamespaces(QualType T, 1594193326Sed ASTContext &Context, 1595193326Sed Sema::AssociatedNamespaceSet &AssociatedNamespaces, 1596198092Srdivacky Sema::AssociatedClassSet &AssociatedClasses) { 1597193326Sed // C++ [basic.lookup.koenig]p2: 1598193326Sed // 1599193326Sed // For each argument type T in the function call, there is a set 1600193326Sed // of zero or more associated namespaces and a set of zero or more 1601193326Sed // associated classes to be considered. The sets of namespaces and 1602193326Sed // classes is determined entirely by the types of the function 1603193326Sed // arguments (and the namespace of any template template 1604193326Sed // argument). Typedef names and using-declarations used to specify 1605193326Sed // the types do not contribute to this set. The sets of namespaces 1606193326Sed // and classes are determined in the following way: 1607193326Sed T = Context.getCanonicalType(T).getUnqualifiedType(); 1608193326Sed 1609193326Sed // -- If T is a pointer to U or an array of U, its associated 1610198092Srdivacky // namespaces and classes are those associated with U. 1611193326Sed // 1612193326Sed // We handle this by unwrapping pointer and array types immediately, 1613193326Sed // to avoid unnecessary recursion. 1614193326Sed while (true) { 1615198092Srdivacky if (const PointerType *Ptr = T->getAs<PointerType>()) 1616193326Sed T = Ptr->getPointeeType(); 1617193326Sed else if (const ArrayType *Ptr = Context.getAsArrayType(T)) 1618193326Sed T = Ptr->getElementType(); 1619198092Srdivacky else 1620193326Sed break; 1621193326Sed } 1622193326Sed 1623193326Sed // -- If T is a fundamental type, its associated sets of 1624193326Sed // namespaces and classes are both empty. 1625198092Srdivacky if (T->getAs<BuiltinType>()) 1626193326Sed return; 1627193326Sed 1628193326Sed // -- If T is a class type (including unions), its associated 1629193326Sed // classes are: the class itself; the class of which it is a 1630193326Sed // member, if any; and its direct and indirect base 1631193326Sed // classes. Its associated namespaces are the namespaces in 1632198092Srdivacky // which its associated classes are defined. 1633198092Srdivacky if (const RecordType *ClassType = T->getAs<RecordType>()) 1634198092Srdivacky if (CXXRecordDecl *ClassDecl 1635193326Sed = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) { 1636198092Srdivacky addAssociatedClassesAndNamespaces(ClassDecl, Context, 1637198092Srdivacky AssociatedNamespaces, 1638198092Srdivacky AssociatedClasses); 1639193326Sed return; 1640193326Sed } 1641193326Sed 1642193326Sed // -- If T is an enumeration type, its associated namespace is 1643193326Sed // the namespace in which it is defined. If it is class 1644193326Sed // member, its associated class is the member���s class; else 1645198092Srdivacky // it has no associated class. 1646198092Srdivacky if (const EnumType *EnumT = T->getAs<EnumType>()) { 1647193326Sed EnumDecl *Enum = EnumT->getDecl(); 1648193326Sed 1649193326Sed DeclContext *Ctx = Enum->getDeclContext(); 1650193326Sed if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1651193326Sed AssociatedClasses.insert(EnclosingClass); 1652193326Sed 1653193326Sed // Add the associated namespace for this class. 1654207619Srdivacky CollectEnclosingNamespace(AssociatedNamespaces, Ctx); 1655193326Sed 1656193326Sed return; 1657193326Sed } 1658193326Sed 1659193326Sed // -- If T is a function type, its associated namespaces and 1660193326Sed // classes are those associated with the function parameter 1661193326Sed // types and those associated with the return type. 1662198092Srdivacky if (const FunctionType *FnType = T->getAs<FunctionType>()) { 1663193326Sed // Return type 1664198092Srdivacky addAssociatedClassesAndNamespaces(FnType->getResultType(), 1665193326Sed Context, 1666198092Srdivacky AssociatedNamespaces, AssociatedClasses); 1667193326Sed 1668198092Srdivacky const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType); 1669193326Sed if (!Proto) 1670193326Sed return; 1671193326Sed 1672193326Sed // Argument types 1673193326Sed for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), 1674198092Srdivacky ArgEnd = Proto->arg_type_end(); 1675193326Sed Arg != ArgEnd; ++Arg) 1676193326Sed addAssociatedClassesAndNamespaces(*Arg, Context, 1677198092Srdivacky AssociatedNamespaces, AssociatedClasses); 1678198092Srdivacky 1679193326Sed return; 1680193326Sed } 1681193326Sed 1682193326Sed // -- If T is a pointer to a member function of a class X, its 1683193326Sed // associated namespaces and classes are those associated 1684193326Sed // with the function parameter types and return type, 1685198092Srdivacky // together with those associated with X. 1686193326Sed // 1687193326Sed // -- If T is a pointer to a data member of class X, its 1688193326Sed // associated namespaces and classes are those associated 1689193326Sed // with the member type together with those associated with 1690198092Srdivacky // X. 1691198092Srdivacky if (const MemberPointerType *MemberPtr = T->getAs<MemberPointerType>()) { 1692193326Sed // Handle the type that the pointer to member points to. 1693193326Sed addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(), 1694193326Sed Context, 1695198092Srdivacky AssociatedNamespaces, 1696198092Srdivacky AssociatedClasses); 1697193326Sed 1698193326Sed // Handle the class type into which this points. 1699198092Srdivacky if (const RecordType *Class = MemberPtr->getClass()->getAs<RecordType>()) 1700193326Sed addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()), 1701193326Sed Context, 1702198092Srdivacky AssociatedNamespaces, 1703198092Srdivacky AssociatedClasses); 1704193326Sed 1705193326Sed return; 1706193326Sed } 1707193326Sed 1708193326Sed // FIXME: What about block pointers? 1709193326Sed // FIXME: What about Objective-C message sends? 1710193326Sed} 1711193326Sed 1712193326Sed/// \brief Find the associated classes and namespaces for 1713193326Sed/// argument-dependent lookup for a call with the given set of 1714193326Sed/// arguments. 1715193326Sed/// 1716193326Sed/// This routine computes the sets of associated classes and associated 1717198092Srdivacky/// namespaces searched by argument-dependent lookup 1718193326Sed/// (C++ [basic.lookup.argdep]) for a given set of arguments. 1719198092Srdivackyvoid 1720193326SedSema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, 1721193326Sed AssociatedNamespaceSet &AssociatedNamespaces, 1722198092Srdivacky AssociatedClassSet &AssociatedClasses) { 1723193326Sed AssociatedNamespaces.clear(); 1724193326Sed AssociatedClasses.clear(); 1725193326Sed 1726193326Sed // C++ [basic.lookup.koenig]p2: 1727193326Sed // For each argument type T in the function call, there is a set 1728193326Sed // of zero or more associated namespaces and a set of zero or more 1729193326Sed // associated classes to be considered. The sets of namespaces and 1730193326Sed // classes is determined entirely by the types of the function 1731193326Sed // arguments (and the namespace of any template template 1732198092Srdivacky // argument). 1733193326Sed for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { 1734193326Sed Expr *Arg = Args[ArgIdx]; 1735193326Sed 1736193326Sed if (Arg->getType() != Context.OverloadTy) { 1737193326Sed addAssociatedClassesAndNamespaces(Arg->getType(), Context, 1738198092Srdivacky AssociatedNamespaces, 1739198092Srdivacky AssociatedClasses); 1740193326Sed continue; 1741193326Sed } 1742193326Sed 1743193326Sed // [...] In addition, if the argument is the name or address of a 1744193326Sed // set of overloaded functions and/or function templates, its 1745193326Sed // associated classes and namespaces are the union of those 1746193326Sed // associated with each of the members of the set: the namespace 1747193326Sed // in which the function or function template is defined and the 1748193326Sed // classes and namespaces associated with its (non-dependent) 1749193326Sed // parameter types and return type. 1750198092Srdivacky Arg = Arg->IgnoreParens(); 1751199990Srdivacky if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) 1752199990Srdivacky if (unaryOp->getOpcode() == UnaryOperator::AddrOf) 1753199990Srdivacky Arg = unaryOp->getSubExpr(); 1754193326Sed 1755199990Srdivacky // TODO: avoid the copies. This should be easy when the cases 1756199990Srdivacky // share a storage implementation. 1757199990Srdivacky llvm::SmallVector<NamedDecl*, 8> Functions; 1758199990Srdivacky 1759199990Srdivacky if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg)) 1760199990Srdivacky Functions.append(ULE->decls_begin(), ULE->decls_end()); 1761199990Srdivacky else 1762193326Sed continue; 1763193326Sed 1764199990Srdivacky for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Functions.begin(), 1765199990Srdivacky E = Functions.end(); I != E; ++I) { 1766201361Srdivacky // Look through any using declarations to find the underlying function. 1767201361Srdivacky NamedDecl *Fn = (*I)->getUnderlyingDecl(); 1768201361Srdivacky 1769201361Srdivacky FunctionDecl *FDecl = dyn_cast<FunctionDecl>(Fn); 1770195099Sed if (!FDecl) 1771201361Srdivacky FDecl = cast<FunctionTemplateDecl>(Fn)->getTemplatedDecl(); 1772193326Sed 1773193326Sed // Add the classes and namespaces associated with the parameter 1774193326Sed // types and return type of this function. 1775193326Sed addAssociatedClassesAndNamespaces(FDecl->getType(), Context, 1776198092Srdivacky AssociatedNamespaces, 1777198092Srdivacky AssociatedClasses); 1778193326Sed } 1779193326Sed } 1780193326Sed} 1781193326Sed 1782193326Sed/// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is 1783193326Sed/// an acceptable non-member overloaded operator for a call whose 1784193326Sed/// arguments have types T1 (and, if non-empty, T2). This routine 1785193326Sed/// implements the check in C++ [over.match.oper]p3b2 concerning 1786193326Sed/// enumeration types. 1787198092Srdivackystatic bool 1788193326SedIsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, 1789193326Sed QualType T1, QualType T2, 1790193326Sed ASTContext &Context) { 1791193326Sed if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) 1792193326Sed return true; 1793193326Sed 1794193326Sed if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) 1795193326Sed return true; 1796193326Sed 1797198092Srdivacky const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); 1798193326Sed if (Proto->getNumArgs() < 1) 1799193326Sed return false; 1800193326Sed 1801193326Sed if (T1->isEnumeralType()) { 1802193326Sed QualType ArgType = Proto->getArgType(0).getNonReferenceType(); 1803199482Srdivacky if (Context.hasSameUnqualifiedType(T1, ArgType)) 1804193326Sed return true; 1805193326Sed } 1806193326Sed 1807193326Sed if (Proto->getNumArgs() < 2) 1808193326Sed return false; 1809193326Sed 1810193326Sed if (!T2.isNull() && T2->isEnumeralType()) { 1811193326Sed QualType ArgType = Proto->getArgType(1).getNonReferenceType(); 1812199482Srdivacky if (Context.hasSameUnqualifiedType(T2, ArgType)) 1813193326Sed return true; 1814193326Sed } 1815193326Sed 1816193326Sed return false; 1817193326Sed} 1818193326Sed 1819199482SrdivackyNamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, 1820207619Srdivacky SourceLocation Loc, 1821199482Srdivacky LookupNameKind NameKind, 1822199482Srdivacky RedeclarationKind Redecl) { 1823207619Srdivacky LookupResult R(*this, Name, Loc, NameKind, Redecl); 1824199482Srdivacky LookupName(R, S); 1825200583Srdivacky return R.getAsSingle<NamedDecl>(); 1826199482Srdivacky} 1827199482Srdivacky 1828193326Sed/// \brief Find the protocol with the given name, if any. 1829207619SrdivackyObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, 1830207619Srdivacky SourceLocation IdLoc) { 1831207619Srdivacky Decl *D = LookupSingleName(TUScope, II, IdLoc, 1832207619Srdivacky LookupObjCProtocolName); 1833193326Sed return cast_or_null<ObjCProtocolDecl>(D); 1834193326Sed} 1835193326Sed 1836193326Sedvoid Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, 1837198092Srdivacky QualType T1, QualType T2, 1838203955Srdivacky UnresolvedSetImpl &Functions) { 1839193326Sed // C++ [over.match.oper]p3: 1840193326Sed // -- The set of non-member candidates is the result of the 1841193326Sed // unqualified lookup of operator@ in the context of the 1842193326Sed // expression according to the usual rules for name lookup in 1843193326Sed // unqualified function calls (3.4.2) except that all member 1844193326Sed // functions are ignored. However, if no operand has a class 1845193326Sed // type, only those non-member functions in the lookup set 1846198092Srdivacky // that have a first parameter of type T1 or "reference to 1847198092Srdivacky // (possibly cv-qualified) T1", when T1 is an enumeration 1848193326Sed // type, or (if there is a right operand) a second parameter 1849198092Srdivacky // of type T2 or "reference to (possibly cv-qualified) T2", 1850193326Sed // when T2 is an enumeration type, are candidate functions. 1851193326Sed DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); 1852199482Srdivacky LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); 1853199482Srdivacky LookupName(Operators, S); 1854198092Srdivacky 1855193326Sed assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); 1856193326Sed 1857198092Srdivacky if (Operators.empty()) 1858193326Sed return; 1859193326Sed 1860193326Sed for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); 1861193326Sed Op != OpEnd; ++Op) { 1862207619Srdivacky NamedDecl *Found = (*Op)->getUnderlyingDecl(); 1863207619Srdivacky if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Found)) { 1864193326Sed if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) 1865207619Srdivacky Functions.addDecl(*Op, Op.getAccess()); // FIXME: canonical FD 1866198092Srdivacky } else if (FunctionTemplateDecl *FunTmpl 1867207619Srdivacky = dyn_cast<FunctionTemplateDecl>(Found)) { 1868195341Sed // FIXME: friend operators? 1869198092Srdivacky // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate, 1870195341Sed // later? 1871195341Sed if (!FunTmpl->getDeclContext()->isRecord()) 1872207619Srdivacky Functions.addDecl(*Op, Op.getAccess()); 1873195341Sed } 1874193326Sed } 1875193326Sed} 1876193326Sed 1877203955Srdivackyvoid ADLResult::insert(NamedDecl *New) { 1878203955Srdivacky NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; 1879203955Srdivacky 1880203955Srdivacky // If we haven't yet seen a decl for this key, or the last decl 1881203955Srdivacky // was exactly this one, we're done. 1882203955Srdivacky if (Old == 0 || Old == New) { 1883203955Srdivacky Old = New; 1884203955Srdivacky return; 1885203955Srdivacky } 1886203955Srdivacky 1887203955Srdivacky // Otherwise, decide which is a more recent redeclaration. 1888203955Srdivacky FunctionDecl *OldFD, *NewFD; 1889203955Srdivacky if (isa<FunctionTemplateDecl>(New)) { 1890203955Srdivacky OldFD = cast<FunctionTemplateDecl>(Old)->getTemplatedDecl(); 1891203955Srdivacky NewFD = cast<FunctionTemplateDecl>(New)->getTemplatedDecl(); 1892203955Srdivacky } else { 1893203955Srdivacky OldFD = cast<FunctionDecl>(Old); 1894203955Srdivacky NewFD = cast<FunctionDecl>(New); 1895203955Srdivacky } 1896203955Srdivacky 1897203955Srdivacky FunctionDecl *Cursor = NewFD; 1898203955Srdivacky while (true) { 1899203955Srdivacky Cursor = Cursor->getPreviousDeclaration(); 1900203955Srdivacky 1901203955Srdivacky // If we got to the end without finding OldFD, OldFD is the newer 1902203955Srdivacky // declaration; leave things as they are. 1903203955Srdivacky if (!Cursor) return; 1904203955Srdivacky 1905203955Srdivacky // If we do find OldFD, then NewFD is newer. 1906203955Srdivacky if (Cursor == OldFD) break; 1907203955Srdivacky 1908203955Srdivacky // Otherwise, keep looking. 1909203955Srdivacky } 1910203955Srdivacky 1911203955Srdivacky Old = New; 1912198092Srdivacky} 1913198092Srdivacky 1914198893Srdivackyvoid Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator, 1915193326Sed Expr **Args, unsigned NumArgs, 1916203955Srdivacky ADLResult &Result) { 1917193326Sed // Find all of the associated namespaces and classes based on the 1918193326Sed // arguments we have. 1919193326Sed AssociatedNamespaceSet AssociatedNamespaces; 1920193326Sed AssociatedClassSet AssociatedClasses; 1921198092Srdivacky FindAssociatedClassesAndNamespaces(Args, NumArgs, 1922198092Srdivacky AssociatedNamespaces, 1923198092Srdivacky AssociatedClasses); 1924193326Sed 1925198893Srdivacky QualType T1, T2; 1926198893Srdivacky if (Operator) { 1927198893Srdivacky T1 = Args[0]->getType(); 1928198893Srdivacky if (NumArgs >= 2) 1929198893Srdivacky T2 = Args[1]->getType(); 1930198893Srdivacky } 1931198893Srdivacky 1932193326Sed // C++ [basic.lookup.argdep]p3: 1933193326Sed // Let X be the lookup set produced by unqualified lookup (3.4.1) 1934193326Sed // and let Y be the lookup set produced by argument dependent 1935193326Sed // lookup (defined as follows). If X contains [...] then Y is 1936193326Sed // empty. Otherwise Y is the set of declarations found in the 1937193326Sed // namespaces associated with the argument types as described 1938193326Sed // below. The set of declarations found by the lookup of the name 1939193326Sed // is the union of X and Y. 1940193326Sed // 1941193326Sed // Here, we compute Y and add its members to the overloaded 1942193326Sed // candidate set. 1943193326Sed for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), 1944198092Srdivacky NSEnd = AssociatedNamespaces.end(); 1945198092Srdivacky NS != NSEnd; ++NS) { 1946193326Sed // When considering an associated namespace, the lookup is the 1947193326Sed // same as the lookup performed when the associated namespace is 1948193326Sed // used as a qualifier (3.4.3.2) except that: 1949193326Sed // 1950193326Sed // -- Any using-directives in the associated namespace are 1951193326Sed // ignored. 1952193326Sed // 1953198092Srdivacky // -- Any namespace-scope friend functions declared in 1954193326Sed // associated classes are visible within their respective 1955193326Sed // namespaces even if they are not visible during an ordinary 1956193326Sed // lookup (11.4). 1957193326Sed DeclContext::lookup_iterator I, E; 1958195341Sed for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) { 1959203955Srdivacky NamedDecl *D = *I; 1960198092Srdivacky // If the only declaration here is an ordinary friend, consider 1961198092Srdivacky // it only if it was declared in an associated classes. 1962198092Srdivacky if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) { 1963198092Srdivacky DeclContext *LexDC = D->getLexicalDeclContext(); 1964198092Srdivacky if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) 1965198092Srdivacky continue; 1966198092Srdivacky } 1967198092Srdivacky 1968203955Srdivacky if (isa<UsingShadowDecl>(D)) 1969203955Srdivacky D = cast<UsingShadowDecl>(D)->getTargetDecl(); 1970203955Srdivacky 1971203955Srdivacky if (isa<FunctionDecl>(D)) { 1972203955Srdivacky if (Operator && 1973203955Srdivacky !IsAcceptableNonMemberOperatorCandidate(cast<FunctionDecl>(D), 1974203955Srdivacky T1, T2, Context)) 1975203955Srdivacky continue; 1976203955Srdivacky } else if (!isa<FunctionTemplateDecl>(D)) 1977203955Srdivacky continue; 1978203955Srdivacky 1979203955Srdivacky Result.insert(D); 1980193326Sed } 1981193326Sed } 1982193326Sed} 1983201361Srdivacky 1984201361Srdivacky//---------------------------------------------------------------------------- 1985201361Srdivacky// Search for all visible declarations. 1986201361Srdivacky//---------------------------------------------------------------------------- 1987201361SrdivackyVisibleDeclConsumer::~VisibleDeclConsumer() { } 1988201361Srdivacky 1989201361Srdivackynamespace { 1990201361Srdivacky 1991201361Srdivackyclass ShadowContextRAII; 1992201361Srdivacky 1993201361Srdivackyclass VisibleDeclsRecord { 1994201361Srdivackypublic: 1995201361Srdivacky /// \brief An entry in the shadow map, which is optimized to store a 1996201361Srdivacky /// single declaration (the common case) but can also store a list 1997201361Srdivacky /// of declarations. 1998201361Srdivacky class ShadowMapEntry { 1999201361Srdivacky typedef llvm::SmallVector<NamedDecl *, 4> DeclVector; 2000201361Srdivacky 2001201361Srdivacky /// \brief Contains either the solitary NamedDecl * or a vector 2002201361Srdivacky /// of declarations. 2003201361Srdivacky llvm::PointerUnion<NamedDecl *, DeclVector*> DeclOrVector; 2004201361Srdivacky 2005201361Srdivacky public: 2006201361Srdivacky ShadowMapEntry() : DeclOrVector() { } 2007201361Srdivacky 2008201361Srdivacky void Add(NamedDecl *ND); 2009201361Srdivacky void Destroy(); 2010201361Srdivacky 2011201361Srdivacky // Iteration. 2012201361Srdivacky typedef NamedDecl **iterator; 2013201361Srdivacky iterator begin(); 2014201361Srdivacky iterator end(); 2015201361Srdivacky }; 2016201361Srdivacky 2017201361Srdivackyprivate: 2018201361Srdivacky /// \brief A mapping from declaration names to the declarations that have 2019201361Srdivacky /// this name within a particular scope. 2020201361Srdivacky typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; 2021201361Srdivacky 2022201361Srdivacky /// \brief A list of shadow maps, which is used to model name hiding. 2023201361Srdivacky std::list<ShadowMap> ShadowMaps; 2024201361Srdivacky 2025201361Srdivacky /// \brief The declaration contexts we have already visited. 2026201361Srdivacky llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; 2027201361Srdivacky 2028201361Srdivacky friend class ShadowContextRAII; 2029201361Srdivacky 2030201361Srdivackypublic: 2031201361Srdivacky /// \brief Determine whether we have already visited this context 2032201361Srdivacky /// (and, if not, note that we are going to visit that context now). 2033201361Srdivacky bool visitedContext(DeclContext *Ctx) { 2034201361Srdivacky return !VisitedContexts.insert(Ctx); 2035201361Srdivacky } 2036201361Srdivacky 2037201361Srdivacky /// \brief Determine whether the given declaration is hidden in the 2038201361Srdivacky /// current scope. 2039201361Srdivacky /// 2040201361Srdivacky /// \returns the declaration that hides the given declaration, or 2041201361Srdivacky /// NULL if no such declaration exists. 2042201361Srdivacky NamedDecl *checkHidden(NamedDecl *ND); 2043201361Srdivacky 2044201361Srdivacky /// \brief Add a declaration to the current shadow map. 2045201361Srdivacky void add(NamedDecl *ND) { ShadowMaps.back()[ND->getDeclName()].Add(ND); } 2046201361Srdivacky}; 2047201361Srdivacky 2048201361Srdivacky/// \brief RAII object that records when we've entered a shadow context. 2049201361Srdivackyclass ShadowContextRAII { 2050201361Srdivacky VisibleDeclsRecord &Visible; 2051201361Srdivacky 2052201361Srdivacky typedef VisibleDeclsRecord::ShadowMap ShadowMap; 2053201361Srdivacky 2054201361Srdivackypublic: 2055201361Srdivacky ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { 2056201361Srdivacky Visible.ShadowMaps.push_back(ShadowMap()); 2057201361Srdivacky } 2058201361Srdivacky 2059201361Srdivacky ~ShadowContextRAII() { 2060201361Srdivacky for (ShadowMap::iterator E = Visible.ShadowMaps.back().begin(), 2061201361Srdivacky EEnd = Visible.ShadowMaps.back().end(); 2062201361Srdivacky E != EEnd; 2063201361Srdivacky ++E) 2064201361Srdivacky E->second.Destroy(); 2065201361Srdivacky 2066201361Srdivacky Visible.ShadowMaps.pop_back(); 2067201361Srdivacky } 2068201361Srdivacky}; 2069201361Srdivacky 2070201361Srdivacky} // end anonymous namespace 2071201361Srdivacky 2072201361Srdivackyvoid VisibleDeclsRecord::ShadowMapEntry::Add(NamedDecl *ND) { 2073201361Srdivacky if (DeclOrVector.isNull()) { 2074201361Srdivacky // 0 - > 1 elements: just set the single element information. 2075201361Srdivacky DeclOrVector = ND; 2076201361Srdivacky return; 2077201361Srdivacky } 2078201361Srdivacky 2079201361Srdivacky if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { 2080201361Srdivacky // 1 -> 2 elements: create the vector of results and push in the 2081201361Srdivacky // existing declaration. 2082201361Srdivacky DeclVector *Vec = new DeclVector; 2083201361Srdivacky Vec->push_back(PrevND); 2084201361Srdivacky DeclOrVector = Vec; 2085201361Srdivacky } 2086201361Srdivacky 2087201361Srdivacky // Add the new element to the end of the vector. 2088201361Srdivacky DeclOrVector.get<DeclVector*>()->push_back(ND); 2089201361Srdivacky} 2090201361Srdivacky 2091201361Srdivackyvoid VisibleDeclsRecord::ShadowMapEntry::Destroy() { 2092201361Srdivacky if (DeclVector *Vec = DeclOrVector.dyn_cast<DeclVector *>()) { 2093201361Srdivacky delete Vec; 2094201361Srdivacky DeclOrVector = ((NamedDecl *)0); 2095201361Srdivacky } 2096201361Srdivacky} 2097201361Srdivacky 2098201361SrdivackyVisibleDeclsRecord::ShadowMapEntry::iterator 2099201361SrdivackyVisibleDeclsRecord::ShadowMapEntry::begin() { 2100201361Srdivacky if (DeclOrVector.isNull()) 2101201361Srdivacky return 0; 2102201361Srdivacky 2103201361Srdivacky if (DeclOrVector.dyn_cast<NamedDecl *>()) 2104201361Srdivacky return &reinterpret_cast<NamedDecl*&>(DeclOrVector); 2105201361Srdivacky 2106201361Srdivacky return DeclOrVector.get<DeclVector *>()->begin(); 2107201361Srdivacky} 2108201361Srdivacky 2109201361SrdivackyVisibleDeclsRecord::ShadowMapEntry::iterator 2110201361SrdivackyVisibleDeclsRecord::ShadowMapEntry::end() { 2111201361Srdivacky if (DeclOrVector.isNull()) 2112201361Srdivacky return 0; 2113201361Srdivacky 2114201361Srdivacky if (DeclOrVector.dyn_cast<NamedDecl *>()) 2115201361Srdivacky return &reinterpret_cast<NamedDecl*&>(DeclOrVector) + 1; 2116201361Srdivacky 2117201361Srdivacky return DeclOrVector.get<DeclVector *>()->end(); 2118201361Srdivacky} 2119201361Srdivacky 2120201361SrdivackyNamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { 2121202379Srdivacky // Look through using declarations. 2122202379Srdivacky ND = ND->getUnderlyingDecl(); 2123202379Srdivacky 2124201361Srdivacky unsigned IDNS = ND->getIdentifierNamespace(); 2125201361Srdivacky std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); 2126201361Srdivacky for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); 2127201361Srdivacky SM != SMEnd; ++SM) { 2128201361Srdivacky ShadowMap::iterator Pos = SM->find(ND->getDeclName()); 2129201361Srdivacky if (Pos == SM->end()) 2130201361Srdivacky continue; 2131201361Srdivacky 2132201361Srdivacky for (ShadowMapEntry::iterator I = Pos->second.begin(), 2133201361Srdivacky IEnd = Pos->second.end(); 2134201361Srdivacky I != IEnd; ++I) { 2135201361Srdivacky // A tag declaration does not hide a non-tag declaration. 2136207619Srdivacky if ((*I)->hasTagIdentifierNamespace() && 2137201361Srdivacky (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | 2138201361Srdivacky Decl::IDNS_ObjCProtocol))) 2139201361Srdivacky continue; 2140201361Srdivacky 2141201361Srdivacky // Protocols are in distinct namespaces from everything else. 2142201361Srdivacky if ((((*I)->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) 2143201361Srdivacky || (IDNS & Decl::IDNS_ObjCProtocol)) && 2144201361Srdivacky (*I)->getIdentifierNamespace() != IDNS) 2145201361Srdivacky continue; 2146201361Srdivacky 2147202379Srdivacky // Functions and function templates in the same scope overload 2148202379Srdivacky // rather than hide. FIXME: Look for hiding based on function 2149202379Srdivacky // signatures! 2150202379Srdivacky if ((*I)->isFunctionOrFunctionTemplate() && 2151202379Srdivacky ND->isFunctionOrFunctionTemplate() && 2152202379Srdivacky SM == ShadowMaps.rbegin()) 2153202379Srdivacky continue; 2154202379Srdivacky 2155201361Srdivacky // We've found a declaration that hides this one. 2156201361Srdivacky return *I; 2157201361Srdivacky } 2158201361Srdivacky } 2159201361Srdivacky 2160201361Srdivacky return 0; 2161201361Srdivacky} 2162201361Srdivacky 2163201361Srdivackystatic void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, 2164201361Srdivacky bool QualifiedNameLookup, 2165202379Srdivacky bool InBaseClass, 2166201361Srdivacky VisibleDeclConsumer &Consumer, 2167201361Srdivacky VisibleDeclsRecord &Visited) { 2168203955Srdivacky if (!Ctx) 2169203955Srdivacky return; 2170203955Srdivacky 2171201361Srdivacky // Make sure we don't visit the same context twice. 2172201361Srdivacky if (Visited.visitedContext(Ctx->getPrimaryContext())) 2173201361Srdivacky return; 2174201361Srdivacky 2175201361Srdivacky // Enumerate all of the results in this context. 2176201361Srdivacky for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; 2177201361Srdivacky CurCtx = CurCtx->getNextContext()) { 2178201361Srdivacky for (DeclContext::decl_iterator D = CurCtx->decls_begin(), 2179201361Srdivacky DEnd = CurCtx->decls_end(); 2180201361Srdivacky D != DEnd; ++D) { 2181201361Srdivacky if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) 2182201361Srdivacky if (Result.isAcceptableDecl(ND)) { 2183202379Srdivacky Consumer.FoundDecl(ND, Visited.checkHidden(ND), InBaseClass); 2184201361Srdivacky Visited.add(ND); 2185201361Srdivacky } 2186201361Srdivacky 2187201361Srdivacky // Visit transparent contexts inside this context. 2188201361Srdivacky if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) { 2189201361Srdivacky if (InnerCtx->isTransparentContext()) 2190202379Srdivacky LookupVisibleDecls(InnerCtx, Result, QualifiedNameLookup, InBaseClass, 2191201361Srdivacky Consumer, Visited); 2192201361Srdivacky } 2193201361Srdivacky } 2194201361Srdivacky } 2195201361Srdivacky 2196201361Srdivacky // Traverse using directives for qualified name lookup. 2197201361Srdivacky if (QualifiedNameLookup) { 2198201361Srdivacky ShadowContextRAII Shadow(Visited); 2199201361Srdivacky DeclContext::udir_iterator I, E; 2200201361Srdivacky for (llvm::tie(I, E) = Ctx->getUsingDirectives(); I != E; ++I) { 2201201361Srdivacky LookupVisibleDecls((*I)->getNominatedNamespace(), Result, 2202202379Srdivacky QualifiedNameLookup, InBaseClass, Consumer, Visited); 2203201361Srdivacky } 2204201361Srdivacky } 2205201361Srdivacky 2206202379Srdivacky // Traverse the contexts of inherited C++ classes. 2207201361Srdivacky if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { 2208203955Srdivacky if (!Record->hasDefinition()) 2209203955Srdivacky return; 2210203955Srdivacky 2211201361Srdivacky for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), 2212201361Srdivacky BEnd = Record->bases_end(); 2213201361Srdivacky B != BEnd; ++B) { 2214201361Srdivacky QualType BaseType = B->getType(); 2215201361Srdivacky 2216201361Srdivacky // Don't look into dependent bases, because name lookup can't look 2217201361Srdivacky // there anyway. 2218201361Srdivacky if (BaseType->isDependentType()) 2219201361Srdivacky continue; 2220201361Srdivacky 2221201361Srdivacky const RecordType *Record = BaseType->getAs<RecordType>(); 2222201361Srdivacky if (!Record) 2223201361Srdivacky continue; 2224201361Srdivacky 2225201361Srdivacky // FIXME: It would be nice to be able to determine whether referencing 2226201361Srdivacky // a particular member would be ambiguous. For example, given 2227201361Srdivacky // 2228201361Srdivacky // struct A { int member; }; 2229201361Srdivacky // struct B { int member; }; 2230201361Srdivacky // struct C : A, B { }; 2231201361Srdivacky // 2232201361Srdivacky // void f(C *c) { c->### } 2233201361Srdivacky // 2234201361Srdivacky // accessing 'member' would result in an ambiguity. However, we 2235201361Srdivacky // could be smart enough to qualify the member with the base 2236201361Srdivacky // class, e.g., 2237201361Srdivacky // 2238201361Srdivacky // c->B::member 2239201361Srdivacky // 2240201361Srdivacky // or 2241201361Srdivacky // 2242201361Srdivacky // c->A::member 2243201361Srdivacky 2244201361Srdivacky // Find results in this base class (and its bases). 2245201361Srdivacky ShadowContextRAII Shadow(Visited); 2246201361Srdivacky LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, 2247202379Srdivacky true, Consumer, Visited); 2248202379Srdivacky } 2249202379Srdivacky } 2250202379Srdivacky 2251202379Srdivacky // Traverse the contexts of Objective-C classes. 2252202379Srdivacky if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { 2253202379Srdivacky // Traverse categories. 2254202379Srdivacky for (ObjCCategoryDecl *Category = IFace->getCategoryList(); 2255202379Srdivacky Category; Category = Category->getNextClassCategory()) { 2256202379Srdivacky ShadowContextRAII Shadow(Visited); 2257202379Srdivacky LookupVisibleDecls(Category, Result, QualifiedNameLookup, false, 2258201361Srdivacky Consumer, Visited); 2259201361Srdivacky } 2260202379Srdivacky 2261202379Srdivacky // Traverse protocols. 2262202379Srdivacky for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(), 2263202379Srdivacky E = IFace->protocol_end(); I != E; ++I) { 2264202379Srdivacky ShadowContextRAII Shadow(Visited); 2265202379Srdivacky LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 2266202379Srdivacky Visited); 2267202379Srdivacky } 2268202379Srdivacky 2269202379Srdivacky // Traverse the superclass. 2270202379Srdivacky if (IFace->getSuperClass()) { 2271202379Srdivacky ShadowContextRAII Shadow(Visited); 2272202379Srdivacky LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, 2273202379Srdivacky true, Consumer, Visited); 2274202379Srdivacky } 2275207619Srdivacky 2276207619Srdivacky // If there is an implementation, traverse it. We do this to find 2277207619Srdivacky // synthesized ivars. 2278207619Srdivacky if (IFace->getImplementation()) { 2279207619Srdivacky ShadowContextRAII Shadow(Visited); 2280207619Srdivacky LookupVisibleDecls(IFace->getImplementation(), Result, 2281207619Srdivacky QualifiedNameLookup, true, Consumer, Visited); 2282207619Srdivacky } 2283202379Srdivacky } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { 2284202379Srdivacky for (ObjCProtocolDecl::protocol_iterator I = Protocol->protocol_begin(), 2285202379Srdivacky E = Protocol->protocol_end(); I != E; ++I) { 2286202379Srdivacky ShadowContextRAII Shadow(Visited); 2287202379Srdivacky LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 2288202379Srdivacky Visited); 2289202379Srdivacky } 2290202379Srdivacky } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { 2291202379Srdivacky for (ObjCCategoryDecl::protocol_iterator I = Category->protocol_begin(), 2292202379Srdivacky E = Category->protocol_end(); I != E; ++I) { 2293202379Srdivacky ShadowContextRAII Shadow(Visited); 2294202379Srdivacky LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 2295202379Srdivacky Visited); 2296202379Srdivacky } 2297207619Srdivacky 2298207619Srdivacky // If there is an implementation, traverse it. 2299207619Srdivacky if (Category->getImplementation()) { 2300207619Srdivacky ShadowContextRAII Shadow(Visited); 2301207619Srdivacky LookupVisibleDecls(Category->getImplementation(), Result, 2302207619Srdivacky QualifiedNameLookup, true, Consumer, Visited); 2303207619Srdivacky } 2304201361Srdivacky } 2305201361Srdivacky} 2306201361Srdivacky 2307201361Srdivackystatic void LookupVisibleDecls(Scope *S, LookupResult &Result, 2308201361Srdivacky UnqualUsingDirectiveSet &UDirs, 2309201361Srdivacky VisibleDeclConsumer &Consumer, 2310201361Srdivacky VisibleDeclsRecord &Visited) { 2311201361Srdivacky if (!S) 2312201361Srdivacky return; 2313201361Srdivacky 2314202379Srdivacky if (!S->getEntity() || !S->getParent() || 2315202379Srdivacky ((DeclContext *)S->getEntity())->isFunctionOrMethod()) { 2316202379Srdivacky // Walk through the declarations in this Scope. 2317202379Srdivacky for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 2318202379Srdivacky D != DEnd; ++D) { 2319202379Srdivacky if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) 2320202379Srdivacky if (Result.isAcceptableDecl(ND)) { 2321202379Srdivacky Consumer.FoundDecl(ND, Visited.checkHidden(ND), false); 2322202379Srdivacky Visited.add(ND); 2323202379Srdivacky } 2324202379Srdivacky } 2325202379Srdivacky } 2326202379Srdivacky 2327205219Srdivacky // FIXME: C++ [temp.local]p8 2328201361Srdivacky DeclContext *Entity = 0; 2329202379Srdivacky if (S->getEntity()) { 2330201361Srdivacky // Look into this scope's declaration context, along with any of its 2331201361Srdivacky // parent lookup contexts (e.g., enclosing classes), up to the point 2332201361Srdivacky // where we hit the context stored in the next outer scope. 2333201361Srdivacky Entity = (DeclContext *)S->getEntity(); 2334205219Srdivacky DeclContext *OuterCtx = findOuterContext(S).first; // FIXME 2335201361Srdivacky 2336205219Srdivacky for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); 2337201361Srdivacky Ctx = Ctx->getLookupParent()) { 2338202379Srdivacky if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 2339202379Srdivacky if (Method->isInstanceMethod()) { 2340202379Srdivacky // For instance methods, look for ivars in the method's interface. 2341202379Srdivacky LookupResult IvarResult(Result.getSema(), Result.getLookupName(), 2342202379Srdivacky Result.getNameLoc(), Sema::LookupMemberName); 2343203955Srdivacky if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) 2344203955Srdivacky LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, 2345203955Srdivacky /*InBaseClass=*/false, Consumer, Visited); 2346202379Srdivacky } 2347202379Srdivacky 2348202379Srdivacky // We've already performed all of the name lookup that we need 2349202379Srdivacky // to for Objective-C methods; the next context will be the 2350202379Srdivacky // outer scope. 2351202379Srdivacky break; 2352202379Srdivacky } 2353202379Srdivacky 2354201361Srdivacky if (Ctx->isFunctionOrMethod()) 2355201361Srdivacky continue; 2356201361Srdivacky 2357201361Srdivacky LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, 2358202379Srdivacky /*InBaseClass=*/false, Consumer, Visited); 2359201361Srdivacky } 2360201361Srdivacky } else if (!S->getParent()) { 2361201361Srdivacky // Look into the translation unit scope. We walk through the translation 2362201361Srdivacky // unit's declaration context, because the Scope itself won't have all of 2363201361Srdivacky // the declarations if we loaded a precompiled header. 2364201361Srdivacky // FIXME: We would like the translation unit's Scope object to point to the 2365201361Srdivacky // translation unit, so we don't need this special "if" branch. However, 2366201361Srdivacky // doing so would force the normal C++ name-lookup code to look into the 2367201361Srdivacky // translation unit decl when the IdentifierInfo chains would suffice. 2368201361Srdivacky // Once we fix that problem (which is part of a more general "don't look 2369202379Srdivacky // in DeclContexts unless we have to" optimization), we can eliminate this. 2370201361Srdivacky Entity = Result.getSema().Context.getTranslationUnitDecl(); 2371201361Srdivacky LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, 2372202379Srdivacky /*InBaseClass=*/false, Consumer, Visited); 2373202379Srdivacky } 2374201361Srdivacky 2375201361Srdivacky if (Entity) { 2376201361Srdivacky // Lookup visible declarations in any namespaces found by using 2377201361Srdivacky // directives. 2378201361Srdivacky UnqualUsingDirectiveSet::const_iterator UI, UEnd; 2379201361Srdivacky llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity); 2380201361Srdivacky for (; UI != UEnd; ++UI) 2381201361Srdivacky LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()), 2382202379Srdivacky Result, /*QualifiedNameLookup=*/false, 2383202379Srdivacky /*InBaseClass=*/false, Consumer, Visited); 2384201361Srdivacky } 2385201361Srdivacky 2386201361Srdivacky // Lookup names in the parent scope. 2387201361Srdivacky ShadowContextRAII Shadow(Visited); 2388201361Srdivacky LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); 2389201361Srdivacky} 2390201361Srdivacky 2391201361Srdivackyvoid Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, 2392201361Srdivacky VisibleDeclConsumer &Consumer) { 2393201361Srdivacky // Determine the set of using directives available during 2394201361Srdivacky // unqualified name lookup. 2395201361Srdivacky Scope *Initial = S; 2396201361Srdivacky UnqualUsingDirectiveSet UDirs; 2397201361Srdivacky if (getLangOptions().CPlusPlus) { 2398201361Srdivacky // Find the first namespace or translation-unit scope. 2399201361Srdivacky while (S && !isNamespaceOrTranslationUnitScope(S)) 2400201361Srdivacky S = S->getParent(); 2401201361Srdivacky 2402201361Srdivacky UDirs.visitScopeChain(Initial, S); 2403201361Srdivacky } 2404201361Srdivacky UDirs.done(); 2405201361Srdivacky 2406201361Srdivacky // Look for visible declarations. 2407201361Srdivacky LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 2408201361Srdivacky VisibleDeclsRecord Visited; 2409201361Srdivacky ShadowContextRAII Shadow(Visited); 2410201361Srdivacky ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); 2411201361Srdivacky} 2412201361Srdivacky 2413201361Srdivackyvoid Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, 2414201361Srdivacky VisibleDeclConsumer &Consumer) { 2415201361Srdivacky LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 2416201361Srdivacky VisibleDeclsRecord Visited; 2417201361Srdivacky ShadowContextRAII Shadow(Visited); 2418202379Srdivacky ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, 2419202379Srdivacky /*InBaseClass=*/false, Consumer, Visited); 2420201361Srdivacky} 2421201361Srdivacky 2422201361Srdivacky//---------------------------------------------------------------------------- 2423201361Srdivacky// Typo correction 2424201361Srdivacky//---------------------------------------------------------------------------- 2425201361Srdivacky 2426201361Srdivackynamespace { 2427201361Srdivackyclass TypoCorrectionConsumer : public VisibleDeclConsumer { 2428201361Srdivacky /// \brief The name written that is a typo in the source. 2429201361Srdivacky llvm::StringRef Typo; 2430201361Srdivacky 2431201361Srdivacky /// \brief The results found that have the smallest edit distance 2432201361Srdivacky /// found (so far) with the typo name. 2433201361Srdivacky llvm::SmallVector<NamedDecl *, 4> BestResults; 2434201361Srdivacky 2435207619Srdivacky /// \brief The keywords that have the smallest edit distance. 2436207619Srdivacky llvm::SmallVector<IdentifierInfo *, 4> BestKeywords; 2437207619Srdivacky 2438201361Srdivacky /// \brief The best edit distance found so far. 2439201361Srdivacky unsigned BestEditDistance; 2440201361Srdivacky 2441201361Srdivackypublic: 2442201361Srdivacky explicit TypoCorrectionConsumer(IdentifierInfo *Typo) 2443201361Srdivacky : Typo(Typo->getName()) { } 2444201361Srdivacky 2445202379Srdivacky virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass); 2446207619Srdivacky void addKeywordResult(ASTContext &Context, llvm::StringRef Keyword); 2447201361Srdivacky 2448201361Srdivacky typedef llvm::SmallVector<NamedDecl *, 4>::const_iterator iterator; 2449201361Srdivacky iterator begin() const { return BestResults.begin(); } 2450201361Srdivacky iterator end() const { return BestResults.end(); } 2451207619Srdivacky void clear_decls() { BestResults.clear(); } 2452207619Srdivacky 2453207619Srdivacky bool empty() const { return BestResults.empty() && BestKeywords.empty(); } 2454201361Srdivacky 2455207619Srdivacky typedef llvm::SmallVector<IdentifierInfo *, 4>::const_iterator 2456207619Srdivacky keyword_iterator; 2457207619Srdivacky keyword_iterator keyword_begin() const { return BestKeywords.begin(); } 2458207619Srdivacky keyword_iterator keyword_end() const { return BestKeywords.end(); } 2459207619Srdivacky bool keyword_empty() const { return BestKeywords.empty(); } 2460207619Srdivacky unsigned keyword_size() const { return BestKeywords.size(); } 2461207619Srdivacky 2462207619Srdivacky unsigned getBestEditDistance() const { return BestEditDistance; } 2463201361Srdivacky}; 2464201361Srdivacky 2465201361Srdivacky} 2466201361Srdivacky 2467202379Srdivackyvoid TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, 2468202379Srdivacky bool InBaseClass) { 2469201361Srdivacky // Don't consider hidden names for typo correction. 2470201361Srdivacky if (Hiding) 2471201361Srdivacky return; 2472201361Srdivacky 2473201361Srdivacky // Only consider entities with identifiers for names, ignoring 2474201361Srdivacky // special names (constructors, overloaded operators, selectors, 2475201361Srdivacky // etc.). 2476201361Srdivacky IdentifierInfo *Name = ND->getIdentifier(); 2477201361Srdivacky if (!Name) 2478201361Srdivacky return; 2479201361Srdivacky 2480201361Srdivacky // Compute the edit distance between the typo and the name of this 2481201361Srdivacky // entity. If this edit distance is not worse than the best edit 2482201361Srdivacky // distance we've seen so far, add it to the list of results. 2483201361Srdivacky unsigned ED = Typo.edit_distance(Name->getName()); 2484207619Srdivacky if (!BestResults.empty() || !BestKeywords.empty()) { 2485201361Srdivacky if (ED < BestEditDistance) { 2486201361Srdivacky // This result is better than any we've seen before; clear out 2487201361Srdivacky // the previous results. 2488201361Srdivacky BestResults.clear(); 2489207619Srdivacky BestKeywords.clear(); 2490201361Srdivacky BestEditDistance = ED; 2491201361Srdivacky } else if (ED > BestEditDistance) { 2492201361Srdivacky // This result is worse than the best results we've seen so far; 2493201361Srdivacky // ignore it. 2494201361Srdivacky return; 2495201361Srdivacky } 2496201361Srdivacky } else 2497201361Srdivacky BestEditDistance = ED; 2498201361Srdivacky 2499201361Srdivacky BestResults.push_back(ND); 2500201361Srdivacky} 2501201361Srdivacky 2502207619Srdivackyvoid TypoCorrectionConsumer::addKeywordResult(ASTContext &Context, 2503207619Srdivacky llvm::StringRef Keyword) { 2504207619Srdivacky // Compute the edit distance between the typo and this keyword. 2505207619Srdivacky // If this edit distance is not worse than the best edit 2506207619Srdivacky // distance we've seen so far, add it to the list of results. 2507207619Srdivacky unsigned ED = Typo.edit_distance(Keyword); 2508207619Srdivacky if (!BestResults.empty() || !BestKeywords.empty()) { 2509207619Srdivacky if (ED < BestEditDistance) { 2510207619Srdivacky BestResults.clear(); 2511207619Srdivacky BestKeywords.clear(); 2512207619Srdivacky BestEditDistance = ED; 2513207619Srdivacky } else if (ED > BestEditDistance) { 2514207619Srdivacky // This result is worse than the best results we've seen so far; 2515207619Srdivacky // ignore it. 2516207619Srdivacky return; 2517207619Srdivacky } 2518207619Srdivacky } else 2519207619Srdivacky BestEditDistance = ED; 2520207619Srdivacky 2521207619Srdivacky BestKeywords.push_back(&Context.Idents.get(Keyword)); 2522207619Srdivacky} 2523207619Srdivacky 2524201361Srdivacky/// \brief Try to "correct" a typo in the source code by finding 2525201361Srdivacky/// visible declarations whose names are similar to the name that was 2526201361Srdivacky/// present in the source code. 2527201361Srdivacky/// 2528201361Srdivacky/// \param Res the \c LookupResult structure that contains the name 2529201361Srdivacky/// that was present in the source code along with the name-lookup 2530201361Srdivacky/// criteria used to search for the name. On success, this structure 2531201361Srdivacky/// will contain the results of name lookup. 2532201361Srdivacky/// 2533201361Srdivacky/// \param S the scope in which name lookup occurs. 2534201361Srdivacky/// 2535201361Srdivacky/// \param SS the nested-name-specifier that precedes the name we're 2536201361Srdivacky/// looking for, if present. 2537201361Srdivacky/// 2538201361Srdivacky/// \param MemberContext if non-NULL, the context in which to look for 2539201361Srdivacky/// a member access expression. 2540201361Srdivacky/// 2541201361Srdivacky/// \param EnteringContext whether we're entering the context described by 2542201361Srdivacky/// the nested-name-specifier SS. 2543201361Srdivacky/// 2544207619Srdivacky/// \param CTC The context in which typo correction occurs, which impacts the 2545207619Srdivacky/// set of keywords permitted. 2546207619Srdivacky/// 2547202379Srdivacky/// \param OPT when non-NULL, the search for visible declarations will 2548202379Srdivacky/// also walk the protocols in the qualified interfaces of \p OPT. 2549202379Srdivacky/// 2550207619Srdivacky/// \returns the corrected name if the typo was corrected, otherwise returns an 2551207619Srdivacky/// empty \c DeclarationName. When a typo was corrected, the result structure 2552207619Srdivacky/// may contain the results of name lookup for the correct name or it may be 2553207619Srdivacky/// empty. 2554207619SrdivackyDeclarationName Sema::CorrectTypo(LookupResult &Res, Scope *S, CXXScopeSpec *SS, 2555207619Srdivacky DeclContext *MemberContext, 2556207619Srdivacky bool EnteringContext, 2557207619Srdivacky CorrectTypoContext CTC, 2558207619Srdivacky const ObjCObjectPointerType *OPT) { 2559202379Srdivacky if (Diags.hasFatalErrorOccurred()) 2560207619Srdivacky return DeclarationName(); 2561203955Srdivacky 2562203955Srdivacky // Provide a stop gap for files that are just seriously broken. Trying 2563203955Srdivacky // to correct all typos can turn into a HUGE performance penalty, causing 2564203955Srdivacky // some files to take minutes to get rejected by the parser. 2565203955Srdivacky // FIXME: Is this the right solution? 2566203955Srdivacky if (TyposCorrected == 20) 2567207619Srdivacky return DeclarationName(); 2568203955Srdivacky ++TyposCorrected; 2569202379Srdivacky 2570201361Srdivacky // We only attempt to correct typos for identifiers. 2571201361Srdivacky IdentifierInfo *Typo = Res.getLookupName().getAsIdentifierInfo(); 2572201361Srdivacky if (!Typo) 2573207619Srdivacky return DeclarationName(); 2574201361Srdivacky 2575201361Srdivacky // If the scope specifier itself was invalid, don't try to correct 2576201361Srdivacky // typos. 2577201361Srdivacky if (SS && SS->isInvalid()) 2578207619Srdivacky return DeclarationName(); 2579201361Srdivacky 2580201361Srdivacky // Never try to correct typos during template deduction or 2581201361Srdivacky // instantiation. 2582201361Srdivacky if (!ActiveTemplateInstantiations.empty()) 2583207619Srdivacky return DeclarationName(); 2584207619Srdivacky 2585201361Srdivacky TypoCorrectionConsumer Consumer(Typo); 2586207619Srdivacky 2587207619Srdivacky // Perform name lookup to find visible, similarly-named entities. 2588202379Srdivacky if (MemberContext) { 2589201361Srdivacky LookupVisibleDecls(MemberContext, Res.getLookupKind(), Consumer); 2590202379Srdivacky 2591202379Srdivacky // Look in qualified interfaces. 2592202379Srdivacky if (OPT) { 2593202379Srdivacky for (ObjCObjectPointerType::qual_iterator 2594202379Srdivacky I = OPT->qual_begin(), E = OPT->qual_end(); 2595202379Srdivacky I != E; ++I) 2596202379Srdivacky LookupVisibleDecls(*I, Res.getLookupKind(), Consumer); 2597202379Srdivacky } 2598202379Srdivacky } else if (SS && SS->isSet()) { 2599201361Srdivacky DeclContext *DC = computeDeclContext(*SS, EnteringContext); 2600201361Srdivacky if (!DC) 2601207619Srdivacky return DeclarationName(); 2602201361Srdivacky 2603201361Srdivacky LookupVisibleDecls(DC, Res.getLookupKind(), Consumer); 2604201361Srdivacky } else { 2605201361Srdivacky LookupVisibleDecls(S, Res.getLookupKind(), Consumer); 2606201361Srdivacky } 2607201361Srdivacky 2608207619Srdivacky // Add context-dependent keywords. 2609207619Srdivacky bool WantTypeSpecifiers = false; 2610207619Srdivacky bool WantExpressionKeywords = false; 2611207619Srdivacky bool WantCXXNamedCasts = false; 2612207619Srdivacky bool WantRemainingKeywords = false; 2613207619Srdivacky switch (CTC) { 2614207619Srdivacky case CTC_Unknown: 2615207619Srdivacky WantTypeSpecifiers = true; 2616207619Srdivacky WantExpressionKeywords = true; 2617207619Srdivacky WantCXXNamedCasts = true; 2618207619Srdivacky WantRemainingKeywords = true; 2619208600Srdivacky 2620208600Srdivacky if (ObjCMethodDecl *Method = getCurMethodDecl()) 2621208600Srdivacky if (Method->getClassInterface() && 2622208600Srdivacky Method->getClassInterface()->getSuperClass()) 2623208600Srdivacky Consumer.addKeywordResult(Context, "super"); 2624208600Srdivacky 2625207619Srdivacky break; 2626207619Srdivacky 2627207619Srdivacky case CTC_NoKeywords: 2628207619Srdivacky break; 2629207619Srdivacky 2630207619Srdivacky case CTC_Type: 2631207619Srdivacky WantTypeSpecifiers = true; 2632207619Srdivacky break; 2633207619Srdivacky 2634207619Srdivacky case CTC_ObjCMessageReceiver: 2635207619Srdivacky Consumer.addKeywordResult(Context, "super"); 2636207619Srdivacky // Fall through to handle message receivers like expressions. 2637207619Srdivacky 2638207619Srdivacky case CTC_Expression: 2639207619Srdivacky if (getLangOptions().CPlusPlus) 2640207619Srdivacky WantTypeSpecifiers = true; 2641207619Srdivacky WantExpressionKeywords = true; 2642207619Srdivacky // Fall through to get C++ named casts. 2643207619Srdivacky 2644207619Srdivacky case CTC_CXXCasts: 2645207619Srdivacky WantCXXNamedCasts = true; 2646207619Srdivacky break; 2647207619Srdivacky 2648207619Srdivacky case CTC_MemberLookup: 2649207619Srdivacky if (getLangOptions().CPlusPlus) 2650207619Srdivacky Consumer.addKeywordResult(Context, "template"); 2651207619Srdivacky break; 2652207619Srdivacky } 2653207619Srdivacky 2654207619Srdivacky if (WantTypeSpecifiers) { 2655207619Srdivacky // Add type-specifier keywords to the set of results. 2656207619Srdivacky const char *CTypeSpecs[] = { 2657207619Srdivacky "char", "const", "double", "enum", "float", "int", "long", "short", 2658207619Srdivacky "signed", "struct", "union", "unsigned", "void", "volatile", "_Bool", 2659207619Srdivacky "_Complex", "_Imaginary", 2660207619Srdivacky // storage-specifiers as well 2661207619Srdivacky "extern", "inline", "static", "typedef" 2662207619Srdivacky }; 2663207619Srdivacky 2664207619Srdivacky const unsigned NumCTypeSpecs = sizeof(CTypeSpecs) / sizeof(CTypeSpecs[0]); 2665207619Srdivacky for (unsigned I = 0; I != NumCTypeSpecs; ++I) 2666207619Srdivacky Consumer.addKeywordResult(Context, CTypeSpecs[I]); 2667207619Srdivacky 2668207619Srdivacky if (getLangOptions().C99) 2669207619Srdivacky Consumer.addKeywordResult(Context, "restrict"); 2670207619Srdivacky if (getLangOptions().Bool || getLangOptions().CPlusPlus) 2671207619Srdivacky Consumer.addKeywordResult(Context, "bool"); 2672207619Srdivacky 2673207619Srdivacky if (getLangOptions().CPlusPlus) { 2674207619Srdivacky Consumer.addKeywordResult(Context, "class"); 2675207619Srdivacky Consumer.addKeywordResult(Context, "typename"); 2676207619Srdivacky Consumer.addKeywordResult(Context, "wchar_t"); 2677207619Srdivacky 2678207619Srdivacky if (getLangOptions().CPlusPlus0x) { 2679207619Srdivacky Consumer.addKeywordResult(Context, "char16_t"); 2680207619Srdivacky Consumer.addKeywordResult(Context, "char32_t"); 2681207619Srdivacky Consumer.addKeywordResult(Context, "constexpr"); 2682207619Srdivacky Consumer.addKeywordResult(Context, "decltype"); 2683207619Srdivacky Consumer.addKeywordResult(Context, "thread_local"); 2684207619Srdivacky } 2685207619Srdivacky } 2686207619Srdivacky 2687207619Srdivacky if (getLangOptions().GNUMode) 2688207619Srdivacky Consumer.addKeywordResult(Context, "typeof"); 2689207619Srdivacky } 2690207619Srdivacky 2691208600Srdivacky if (WantCXXNamedCasts && getLangOptions().CPlusPlus) { 2692207619Srdivacky Consumer.addKeywordResult(Context, "const_cast"); 2693207619Srdivacky Consumer.addKeywordResult(Context, "dynamic_cast"); 2694207619Srdivacky Consumer.addKeywordResult(Context, "reinterpret_cast"); 2695207619Srdivacky Consumer.addKeywordResult(Context, "static_cast"); 2696207619Srdivacky } 2697207619Srdivacky 2698207619Srdivacky if (WantExpressionKeywords) { 2699207619Srdivacky Consumer.addKeywordResult(Context, "sizeof"); 2700207619Srdivacky if (getLangOptions().Bool || getLangOptions().CPlusPlus) { 2701207619Srdivacky Consumer.addKeywordResult(Context, "false"); 2702207619Srdivacky Consumer.addKeywordResult(Context, "true"); 2703207619Srdivacky } 2704207619Srdivacky 2705207619Srdivacky if (getLangOptions().CPlusPlus) { 2706207619Srdivacky const char *CXXExprs[] = { 2707207619Srdivacky "delete", "new", "operator", "throw", "typeid" 2708207619Srdivacky }; 2709207619Srdivacky const unsigned NumCXXExprs = sizeof(CXXExprs) / sizeof(CXXExprs[0]); 2710207619Srdivacky for (unsigned I = 0; I != NumCXXExprs; ++I) 2711207619Srdivacky Consumer.addKeywordResult(Context, CXXExprs[I]); 2712207619Srdivacky 2713207619Srdivacky if (isa<CXXMethodDecl>(CurContext) && 2714207619Srdivacky cast<CXXMethodDecl>(CurContext)->isInstance()) 2715207619Srdivacky Consumer.addKeywordResult(Context, "this"); 2716207619Srdivacky 2717207619Srdivacky if (getLangOptions().CPlusPlus0x) { 2718207619Srdivacky Consumer.addKeywordResult(Context, "alignof"); 2719207619Srdivacky Consumer.addKeywordResult(Context, "nullptr"); 2720207619Srdivacky } 2721207619Srdivacky } 2722207619Srdivacky } 2723207619Srdivacky 2724207619Srdivacky if (WantRemainingKeywords) { 2725207619Srdivacky if (getCurFunctionOrMethodDecl() || getCurBlock()) { 2726207619Srdivacky // Statements. 2727207619Srdivacky const char *CStmts[] = { 2728207619Srdivacky "do", "else", "for", "goto", "if", "return", "switch", "while" }; 2729207619Srdivacky const unsigned NumCStmts = sizeof(CStmts) / sizeof(CStmts[0]); 2730207619Srdivacky for (unsigned I = 0; I != NumCStmts; ++I) 2731207619Srdivacky Consumer.addKeywordResult(Context, CStmts[I]); 2732207619Srdivacky 2733207619Srdivacky if (getLangOptions().CPlusPlus) { 2734207619Srdivacky Consumer.addKeywordResult(Context, "catch"); 2735207619Srdivacky Consumer.addKeywordResult(Context, "try"); 2736207619Srdivacky } 2737207619Srdivacky 2738207619Srdivacky if (S && S->getBreakParent()) 2739207619Srdivacky Consumer.addKeywordResult(Context, "break"); 2740207619Srdivacky 2741207619Srdivacky if (S && S->getContinueParent()) 2742207619Srdivacky Consumer.addKeywordResult(Context, "continue"); 2743207619Srdivacky 2744207619Srdivacky if (!getSwitchStack().empty()) { 2745207619Srdivacky Consumer.addKeywordResult(Context, "case"); 2746207619Srdivacky Consumer.addKeywordResult(Context, "default"); 2747207619Srdivacky } 2748207619Srdivacky } else { 2749207619Srdivacky if (getLangOptions().CPlusPlus) { 2750207619Srdivacky Consumer.addKeywordResult(Context, "namespace"); 2751207619Srdivacky Consumer.addKeywordResult(Context, "template"); 2752207619Srdivacky } 2753207619Srdivacky 2754207619Srdivacky if (S && S->isClassScope()) { 2755207619Srdivacky Consumer.addKeywordResult(Context, "explicit"); 2756207619Srdivacky Consumer.addKeywordResult(Context, "friend"); 2757207619Srdivacky Consumer.addKeywordResult(Context, "mutable"); 2758207619Srdivacky Consumer.addKeywordResult(Context, "private"); 2759207619Srdivacky Consumer.addKeywordResult(Context, "protected"); 2760207619Srdivacky Consumer.addKeywordResult(Context, "public"); 2761207619Srdivacky Consumer.addKeywordResult(Context, "virtual"); 2762207619Srdivacky } 2763207619Srdivacky } 2764207619Srdivacky 2765207619Srdivacky if (getLangOptions().CPlusPlus) { 2766207619Srdivacky Consumer.addKeywordResult(Context, "using"); 2767207619Srdivacky 2768207619Srdivacky if (getLangOptions().CPlusPlus0x) 2769207619Srdivacky Consumer.addKeywordResult(Context, "static_assert"); 2770207619Srdivacky } 2771207619Srdivacky } 2772207619Srdivacky 2773207619Srdivacky // If we haven't found anything, we're done. 2774201361Srdivacky if (Consumer.empty()) 2775207619Srdivacky return DeclarationName(); 2776201361Srdivacky 2777201361Srdivacky // Only allow a single, closest name in the result set (it's okay to 2778201361Srdivacky // have overloads of that name, though). 2779207619Srdivacky DeclarationName BestName; 2780207619Srdivacky NamedDecl *BestIvarOrPropertyDecl = 0; 2781207619Srdivacky bool FoundIvarOrPropertyDecl = false; 2782207619Srdivacky 2783207619Srdivacky // Check all of the declaration results to find the best name so far. 2784207619Srdivacky for (TypoCorrectionConsumer::iterator I = Consumer.begin(), 2785207619Srdivacky IEnd = Consumer.end(); 2786207619Srdivacky I != IEnd; ++I) { 2787207619Srdivacky if (!BestName) 2788207619Srdivacky BestName = (*I)->getDeclName(); 2789207619Srdivacky else if (BestName != (*I)->getDeclName()) 2790207619Srdivacky return DeclarationName(); 2791202379Srdivacky 2792207619Srdivacky // \brief Keep track of either an Objective-C ivar or a property, but not 2793207619Srdivacky // both. 2794207619Srdivacky if (isa<ObjCIvarDecl>(*I) || isa<ObjCPropertyDecl>(*I)) { 2795207619Srdivacky if (FoundIvarOrPropertyDecl) 2796207619Srdivacky BestIvarOrPropertyDecl = 0; 2797207619Srdivacky else { 2798207619Srdivacky BestIvarOrPropertyDecl = *I; 2799207619Srdivacky FoundIvarOrPropertyDecl = true; 2800207619Srdivacky } 2801207619Srdivacky } 2802207619Srdivacky } 2803202379Srdivacky 2804207619Srdivacky // Now check all of the keyword results to find the best name. 2805207619Srdivacky switch (Consumer.keyword_size()) { 2806207619Srdivacky case 0: 2807207619Srdivacky // No keywords matched. 2808207619Srdivacky break; 2809207619Srdivacky 2810207619Srdivacky case 1: 2811207619Srdivacky // If we already have a name 2812207619Srdivacky if (!BestName) { 2813207619Srdivacky // We did not have anything previously, 2814207619Srdivacky BestName = *Consumer.keyword_begin(); 2815207619Srdivacky } else if (BestName.getAsIdentifierInfo() == *Consumer.keyword_begin()) { 2816207619Srdivacky // We have a declaration with the same name as a context-sensitive 2817207619Srdivacky // keyword. The keyword takes precedence. 2818207619Srdivacky BestIvarOrPropertyDecl = 0; 2819207619Srdivacky FoundIvarOrPropertyDecl = false; 2820207619Srdivacky Consumer.clear_decls(); 2821208600Srdivacky } else if (CTC == CTC_ObjCMessageReceiver && 2822208600Srdivacky (*Consumer.keyword_begin())->isStr("super")) { 2823208600Srdivacky // In an Objective-C message send, give the "super" keyword a slight 2824208600Srdivacky // edge over entities not in function or method scope. 2825208600Srdivacky for (TypoCorrectionConsumer::iterator I = Consumer.begin(), 2826208600Srdivacky IEnd = Consumer.end(); 2827208600Srdivacky I != IEnd; ++I) { 2828208600Srdivacky if ((*I)->getDeclName() == BestName) { 2829208600Srdivacky if ((*I)->getDeclContext()->isFunctionOrMethod()) 2830208600Srdivacky return DeclarationName(); 2831208600Srdivacky } 2832208600Srdivacky } 2833208600Srdivacky 2834208600Srdivacky // Everything found was outside a function or method; the 'super' 2835208600Srdivacky // keyword takes precedence. 2836208600Srdivacky BestIvarOrPropertyDecl = 0; 2837208600Srdivacky FoundIvarOrPropertyDecl = false; 2838208600Srdivacky Consumer.clear_decls(); 2839208600Srdivacky BestName = *Consumer.keyword_begin(); 2840207619Srdivacky } else { 2841207619Srdivacky // Name collision; we will not correct typos. 2842207619Srdivacky return DeclarationName(); 2843207619Srdivacky } 2844207619Srdivacky break; 2845207619Srdivacky 2846207619Srdivacky default: 2847207619Srdivacky // Name collision; we will not correct typos. 2848207619Srdivacky return DeclarationName(); 2849201361Srdivacky } 2850207619Srdivacky 2851201361Srdivacky // BestName is the closest viable name to what the user 2852201361Srdivacky // typed. However, to make sure that we don't pick something that's 2853201361Srdivacky // way off, make sure that the user typed at least 3 characters for 2854201361Srdivacky // each correction. 2855201361Srdivacky unsigned ED = Consumer.getBestEditDistance(); 2856207619Srdivacky if (ED == 0 || !BestName.getAsIdentifierInfo() || 2857207619Srdivacky (BestName.getAsIdentifierInfo()->getName().size() / ED) < 3) 2858207619Srdivacky return DeclarationName(); 2859201361Srdivacky 2860201361Srdivacky // Perform name lookup again with the name we chose, and declare 2861201361Srdivacky // success if we found something that was not ambiguous. 2862201361Srdivacky Res.clear(); 2863201361Srdivacky Res.setLookupName(BestName); 2864202379Srdivacky 2865202379Srdivacky // If we found an ivar or property, add that result; no further 2866202379Srdivacky // lookup is required. 2867207619Srdivacky if (BestIvarOrPropertyDecl) 2868207619Srdivacky Res.addDecl(BestIvarOrPropertyDecl); 2869202379Srdivacky // If we're looking into the context of a member, perform qualified 2870202379Srdivacky // name lookup on the best name. 2871207619Srdivacky else if (!Consumer.keyword_empty()) { 2872207619Srdivacky // The best match was a keyword. Return it. 2873207619Srdivacky return BestName; 2874207619Srdivacky } else if (MemberContext) 2875201361Srdivacky LookupQualifiedName(Res, MemberContext); 2876202379Srdivacky // Perform lookup as if we had just parsed the best name. 2877201361Srdivacky else 2878201361Srdivacky LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, 2879201361Srdivacky EnteringContext); 2880201361Srdivacky 2881201361Srdivacky if (Res.isAmbiguous()) { 2882201361Srdivacky Res.suppressDiagnostics(); 2883207619Srdivacky return DeclarationName(); 2884201361Srdivacky } 2885201361Srdivacky 2886207619Srdivacky if (Res.getResultKind() != LookupResult::NotFound) 2887207619Srdivacky return BestName; 2888207619Srdivacky 2889207619Srdivacky return DeclarationName(); 2890201361Srdivacky} 2891