1*f4a2713aSLionel Sambuc //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements name lookup for C, C++, Objective-C, and 11*f4a2713aSLionel Sambuc // Objective-C++. 12*f4a2713aSLionel Sambuc // 13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 14*f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h" 15*f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/CXXInheritance.h" 17*f4a2713aSLionel Sambuc #include "clang/AST/Decl.h" 18*f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h" 19*f4a2713aSLionel Sambuc #include "clang/AST/DeclLookups.h" 20*f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h" 21*f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h" 22*f4a2713aSLionel Sambuc #include "clang/AST/Expr.h" 23*f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h" 24*f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.h" 25*f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h" 26*f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h" 27*f4a2713aSLionel Sambuc #include "clang/Sema/ExternalSemaSource.h" 28*f4a2713aSLionel Sambuc #include "clang/Sema/Overload.h" 29*f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h" 30*f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h" 31*f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h" 32*f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h" 33*f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h" 34*f4a2713aSLionel Sambuc #include "clang/Sema/TypoCorrection.h" 35*f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h" 36*f4a2713aSLionel Sambuc #include "llvm/ADT/SetVector.h" 37*f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h" 38*f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h" 39*f4a2713aSLionel Sambuc #include "llvm/ADT/TinyPtrVector.h" 40*f4a2713aSLionel Sambuc #include "llvm/ADT/edit_distance.h" 41*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h" 42*f4a2713aSLionel Sambuc #include <algorithm> 43*f4a2713aSLionel Sambuc #include <iterator> 44*f4a2713aSLionel Sambuc #include <limits> 45*f4a2713aSLionel Sambuc #include <list> 46*f4a2713aSLionel Sambuc #include <map> 47*f4a2713aSLionel Sambuc #include <set> 48*f4a2713aSLionel Sambuc #include <utility> 49*f4a2713aSLionel Sambuc #include <vector> 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc using namespace clang; 52*f4a2713aSLionel Sambuc using namespace sema; 53*f4a2713aSLionel Sambuc 54*f4a2713aSLionel Sambuc namespace { 55*f4a2713aSLionel Sambuc class UnqualUsingEntry { 56*f4a2713aSLionel Sambuc const DeclContext *Nominated; 57*f4a2713aSLionel Sambuc const DeclContext *CommonAncestor; 58*f4a2713aSLionel Sambuc 59*f4a2713aSLionel Sambuc public: 60*f4a2713aSLionel Sambuc UnqualUsingEntry(const DeclContext *Nominated, 61*f4a2713aSLionel Sambuc const DeclContext *CommonAncestor) 62*f4a2713aSLionel Sambuc : Nominated(Nominated), CommonAncestor(CommonAncestor) { 63*f4a2713aSLionel Sambuc } 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel Sambuc const DeclContext *getCommonAncestor() const { 66*f4a2713aSLionel Sambuc return CommonAncestor; 67*f4a2713aSLionel Sambuc } 68*f4a2713aSLionel Sambuc 69*f4a2713aSLionel Sambuc const DeclContext *getNominatedNamespace() const { 70*f4a2713aSLionel Sambuc return Nominated; 71*f4a2713aSLionel Sambuc } 72*f4a2713aSLionel Sambuc 73*f4a2713aSLionel Sambuc // Sort by the pointer value of the common ancestor. 74*f4a2713aSLionel Sambuc struct Comparator { 75*f4a2713aSLionel Sambuc bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { 76*f4a2713aSLionel Sambuc return L.getCommonAncestor() < R.getCommonAncestor(); 77*f4a2713aSLionel Sambuc } 78*f4a2713aSLionel Sambuc 79*f4a2713aSLionel Sambuc bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { 80*f4a2713aSLionel Sambuc return E.getCommonAncestor() < DC; 81*f4a2713aSLionel Sambuc } 82*f4a2713aSLionel Sambuc 83*f4a2713aSLionel Sambuc bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { 84*f4a2713aSLionel Sambuc return DC < E.getCommonAncestor(); 85*f4a2713aSLionel Sambuc } 86*f4a2713aSLionel Sambuc }; 87*f4a2713aSLionel Sambuc }; 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel Sambuc /// A collection of using directives, as used by C++ unqualified 90*f4a2713aSLionel Sambuc /// lookup. 91*f4a2713aSLionel Sambuc class UnqualUsingDirectiveSet { 92*f4a2713aSLionel Sambuc typedef SmallVector<UnqualUsingEntry, 8> ListTy; 93*f4a2713aSLionel Sambuc 94*f4a2713aSLionel Sambuc ListTy list; 95*f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext*, 8> visited; 96*f4a2713aSLionel Sambuc 97*f4a2713aSLionel Sambuc public: 98*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet() {} 99*f4a2713aSLionel Sambuc 100*f4a2713aSLionel Sambuc void visitScopeChain(Scope *S, Scope *InnermostFileScope) { 101*f4a2713aSLionel Sambuc // C++ [namespace.udir]p1: 102*f4a2713aSLionel Sambuc // During unqualified name lookup, the names appear as if they 103*f4a2713aSLionel Sambuc // were declared in the nearest enclosing namespace which contains 104*f4a2713aSLionel Sambuc // both the using-directive and the nominated namespace. 105*f4a2713aSLionel Sambuc DeclContext *InnermostFileDC = InnermostFileScope->getEntity(); 106*f4a2713aSLionel Sambuc assert(InnermostFileDC && InnermostFileDC->isFileContext()); 107*f4a2713aSLionel Sambuc 108*f4a2713aSLionel Sambuc for (; S; S = S->getParent()) { 109*f4a2713aSLionel Sambuc // C++ [namespace.udir]p1: 110*f4a2713aSLionel Sambuc // A using-directive shall not appear in class scope, but may 111*f4a2713aSLionel Sambuc // appear in namespace scope or in block scope. 112*f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity(); 113*f4a2713aSLionel Sambuc if (Ctx && Ctx->isFileContext()) { 114*f4a2713aSLionel Sambuc visit(Ctx, Ctx); 115*f4a2713aSLionel Sambuc } else if (!Ctx || Ctx->isFunctionOrMethod()) { 116*f4a2713aSLionel Sambuc Scope::udir_iterator I = S->using_directives_begin(), 117*f4a2713aSLionel Sambuc End = S->using_directives_end(); 118*f4a2713aSLionel Sambuc for (; I != End; ++I) 119*f4a2713aSLionel Sambuc visit(*I, InnermostFileDC); 120*f4a2713aSLionel Sambuc } 121*f4a2713aSLionel Sambuc } 122*f4a2713aSLionel Sambuc } 123*f4a2713aSLionel Sambuc 124*f4a2713aSLionel Sambuc // Visits a context and collect all of its using directives 125*f4a2713aSLionel Sambuc // recursively. Treats all using directives as if they were 126*f4a2713aSLionel Sambuc // declared in the context. 127*f4a2713aSLionel Sambuc // 128*f4a2713aSLionel Sambuc // A given context is only every visited once, so it is important 129*f4a2713aSLionel Sambuc // that contexts be visited from the inside out in order to get 130*f4a2713aSLionel Sambuc // the effective DCs right. 131*f4a2713aSLionel Sambuc void visit(DeclContext *DC, DeclContext *EffectiveDC) { 132*f4a2713aSLionel Sambuc if (!visited.insert(DC)) 133*f4a2713aSLionel Sambuc return; 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc addUsingDirectives(DC, EffectiveDC); 136*f4a2713aSLionel Sambuc } 137*f4a2713aSLionel Sambuc 138*f4a2713aSLionel Sambuc // Visits a using directive and collects all of its using 139*f4a2713aSLionel Sambuc // directives recursively. Treats all using directives as if they 140*f4a2713aSLionel Sambuc // were declared in the effective DC. 141*f4a2713aSLionel Sambuc void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 142*f4a2713aSLionel Sambuc DeclContext *NS = UD->getNominatedNamespace(); 143*f4a2713aSLionel Sambuc if (!visited.insert(NS)) 144*f4a2713aSLionel Sambuc return; 145*f4a2713aSLionel Sambuc 146*f4a2713aSLionel Sambuc addUsingDirective(UD, EffectiveDC); 147*f4a2713aSLionel Sambuc addUsingDirectives(NS, EffectiveDC); 148*f4a2713aSLionel Sambuc } 149*f4a2713aSLionel Sambuc 150*f4a2713aSLionel Sambuc // Adds all the using directives in a context (and those nominated 151*f4a2713aSLionel Sambuc // by its using directives, transitively) as if they appeared in 152*f4a2713aSLionel Sambuc // the given effective context. 153*f4a2713aSLionel Sambuc void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { 154*f4a2713aSLionel Sambuc SmallVector<DeclContext*,4> queue; 155*f4a2713aSLionel Sambuc while (true) { 156*f4a2713aSLionel Sambuc DeclContext::udir_iterator I, End; 157*f4a2713aSLionel Sambuc for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) { 158*f4a2713aSLionel Sambuc UsingDirectiveDecl *UD = *I; 159*f4a2713aSLionel Sambuc DeclContext *NS = UD->getNominatedNamespace(); 160*f4a2713aSLionel Sambuc if (visited.insert(NS)) { 161*f4a2713aSLionel Sambuc addUsingDirective(UD, EffectiveDC); 162*f4a2713aSLionel Sambuc queue.push_back(NS); 163*f4a2713aSLionel Sambuc } 164*f4a2713aSLionel Sambuc } 165*f4a2713aSLionel Sambuc 166*f4a2713aSLionel Sambuc if (queue.empty()) 167*f4a2713aSLionel Sambuc return; 168*f4a2713aSLionel Sambuc 169*f4a2713aSLionel Sambuc DC = queue.pop_back_val(); 170*f4a2713aSLionel Sambuc } 171*f4a2713aSLionel Sambuc } 172*f4a2713aSLionel Sambuc 173*f4a2713aSLionel Sambuc // Add a using directive as if it had been declared in the given 174*f4a2713aSLionel Sambuc // context. This helps implement C++ [namespace.udir]p3: 175*f4a2713aSLionel Sambuc // The using-directive is transitive: if a scope contains a 176*f4a2713aSLionel Sambuc // using-directive that nominates a second namespace that itself 177*f4a2713aSLionel Sambuc // contains using-directives, the effect is as if the 178*f4a2713aSLionel Sambuc // using-directives from the second namespace also appeared in 179*f4a2713aSLionel Sambuc // the first. 180*f4a2713aSLionel Sambuc void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 181*f4a2713aSLionel Sambuc // Find the common ancestor between the effective context and 182*f4a2713aSLionel Sambuc // the nominated namespace. 183*f4a2713aSLionel Sambuc DeclContext *Common = UD->getNominatedNamespace(); 184*f4a2713aSLionel Sambuc while (!Common->Encloses(EffectiveDC)) 185*f4a2713aSLionel Sambuc Common = Common->getParent(); 186*f4a2713aSLionel Sambuc Common = Common->getPrimaryContext(); 187*f4a2713aSLionel Sambuc 188*f4a2713aSLionel Sambuc list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); 189*f4a2713aSLionel Sambuc } 190*f4a2713aSLionel Sambuc 191*f4a2713aSLionel Sambuc void done() { 192*f4a2713aSLionel Sambuc std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); 193*f4a2713aSLionel Sambuc } 194*f4a2713aSLionel Sambuc 195*f4a2713aSLionel Sambuc typedef ListTy::const_iterator const_iterator; 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc const_iterator begin() const { return list.begin(); } 198*f4a2713aSLionel Sambuc const_iterator end() const { return list.end(); } 199*f4a2713aSLionel Sambuc 200*f4a2713aSLionel Sambuc std::pair<const_iterator,const_iterator> 201*f4a2713aSLionel Sambuc getNamespacesFor(DeclContext *DC) const { 202*f4a2713aSLionel Sambuc return std::equal_range(begin(), end(), DC->getPrimaryContext(), 203*f4a2713aSLionel Sambuc UnqualUsingEntry::Comparator()); 204*f4a2713aSLionel Sambuc } 205*f4a2713aSLionel Sambuc }; 206*f4a2713aSLionel Sambuc } 207*f4a2713aSLionel Sambuc 208*f4a2713aSLionel Sambuc // Retrieve the set of identifier namespaces that correspond to a 209*f4a2713aSLionel Sambuc // specific kind of name lookup. 210*f4a2713aSLionel Sambuc static inline unsigned getIDNS(Sema::LookupNameKind NameKind, 211*f4a2713aSLionel Sambuc bool CPlusPlus, 212*f4a2713aSLionel Sambuc bool Redeclaration) { 213*f4a2713aSLionel Sambuc unsigned IDNS = 0; 214*f4a2713aSLionel Sambuc switch (NameKind) { 215*f4a2713aSLionel Sambuc case Sema::LookupObjCImplicitSelfParam: 216*f4a2713aSLionel Sambuc case Sema::LookupOrdinaryName: 217*f4a2713aSLionel Sambuc case Sema::LookupRedeclarationWithLinkage: 218*f4a2713aSLionel Sambuc case Sema::LookupLocalFriendName: 219*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Ordinary; 220*f4a2713aSLionel Sambuc if (CPlusPlus) { 221*f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; 222*f4a2713aSLionel Sambuc if (Redeclaration) 223*f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; 224*f4a2713aSLionel Sambuc } 225*f4a2713aSLionel Sambuc if (Redeclaration) 226*f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_LocalExtern; 227*f4a2713aSLionel Sambuc break; 228*f4a2713aSLionel Sambuc 229*f4a2713aSLionel Sambuc case Sema::LookupOperatorName: 230*f4a2713aSLionel Sambuc // Operator lookup is its own crazy thing; it is not the same 231*f4a2713aSLionel Sambuc // as (e.g.) looking up an operator name for redeclaration. 232*f4a2713aSLionel Sambuc assert(!Redeclaration && "cannot do redeclaration operator lookup"); 233*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_NonMemberOperator; 234*f4a2713aSLionel Sambuc break; 235*f4a2713aSLionel Sambuc 236*f4a2713aSLionel Sambuc case Sema::LookupTagName: 237*f4a2713aSLionel Sambuc if (CPlusPlus) { 238*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Type; 239*f4a2713aSLionel Sambuc 240*f4a2713aSLionel Sambuc // When looking for a redeclaration of a tag name, we add: 241*f4a2713aSLionel Sambuc // 1) TagFriend to find undeclared friend decls 242*f4a2713aSLionel Sambuc // 2) Namespace because they can't "overload" with tag decls. 243*f4a2713aSLionel Sambuc // 3) Tag because it includes class templates, which can't 244*f4a2713aSLionel Sambuc // "overload" with tag decls. 245*f4a2713aSLionel Sambuc if (Redeclaration) 246*f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; 247*f4a2713aSLionel Sambuc } else { 248*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Tag; 249*f4a2713aSLionel Sambuc } 250*f4a2713aSLionel Sambuc break; 251*f4a2713aSLionel Sambuc case Sema::LookupLabel: 252*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Label; 253*f4a2713aSLionel Sambuc break; 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc case Sema::LookupMemberName: 256*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Member; 257*f4a2713aSLionel Sambuc if (CPlusPlus) 258*f4a2713aSLionel Sambuc IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; 259*f4a2713aSLionel Sambuc break; 260*f4a2713aSLionel Sambuc 261*f4a2713aSLionel Sambuc case Sema::LookupNestedNameSpecifierName: 262*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; 263*f4a2713aSLionel Sambuc break; 264*f4a2713aSLionel Sambuc 265*f4a2713aSLionel Sambuc case Sema::LookupNamespaceName: 266*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Namespace; 267*f4a2713aSLionel Sambuc break; 268*f4a2713aSLionel Sambuc 269*f4a2713aSLionel Sambuc case Sema::LookupUsingDeclName: 270*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag 271*f4a2713aSLionel Sambuc | Decl::IDNS_Member | Decl::IDNS_Using; 272*f4a2713aSLionel Sambuc break; 273*f4a2713aSLionel Sambuc 274*f4a2713aSLionel Sambuc case Sema::LookupObjCProtocolName: 275*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_ObjCProtocol; 276*f4a2713aSLionel Sambuc break; 277*f4a2713aSLionel Sambuc 278*f4a2713aSLionel Sambuc case Sema::LookupAnyName: 279*f4a2713aSLionel Sambuc IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member 280*f4a2713aSLionel Sambuc | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol 281*f4a2713aSLionel Sambuc | Decl::IDNS_Type; 282*f4a2713aSLionel Sambuc break; 283*f4a2713aSLionel Sambuc } 284*f4a2713aSLionel Sambuc return IDNS; 285*f4a2713aSLionel Sambuc } 286*f4a2713aSLionel Sambuc 287*f4a2713aSLionel Sambuc void LookupResult::configure() { 288*f4a2713aSLionel Sambuc IDNS = getIDNS(LookupKind, SemaRef.getLangOpts().CPlusPlus, 289*f4a2713aSLionel Sambuc isForRedeclaration()); 290*f4a2713aSLionel Sambuc 291*f4a2713aSLionel Sambuc if (!isForRedeclaration()) { 292*f4a2713aSLionel Sambuc // If we're looking for one of the allocation or deallocation 293*f4a2713aSLionel Sambuc // operators, make sure that the implicitly-declared new and delete 294*f4a2713aSLionel Sambuc // operators can be found. 295*f4a2713aSLionel Sambuc switch (NameInfo.getName().getCXXOverloadedOperator()) { 296*f4a2713aSLionel Sambuc case OO_New: 297*f4a2713aSLionel Sambuc case OO_Delete: 298*f4a2713aSLionel Sambuc case OO_Array_New: 299*f4a2713aSLionel Sambuc case OO_Array_Delete: 300*f4a2713aSLionel Sambuc SemaRef.DeclareGlobalNewDelete(); 301*f4a2713aSLionel Sambuc break; 302*f4a2713aSLionel Sambuc 303*f4a2713aSLionel Sambuc default: 304*f4a2713aSLionel Sambuc break; 305*f4a2713aSLionel Sambuc } 306*f4a2713aSLionel Sambuc 307*f4a2713aSLionel Sambuc // Compiler builtins are always visible, regardless of where they end 308*f4a2713aSLionel Sambuc // up being declared. 309*f4a2713aSLionel Sambuc if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) { 310*f4a2713aSLionel Sambuc if (unsigned BuiltinID = Id->getBuiltinID()) { 311*f4a2713aSLionel Sambuc if (!SemaRef.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 312*f4a2713aSLionel Sambuc AllowHidden = true; 313*f4a2713aSLionel Sambuc } 314*f4a2713aSLionel Sambuc } 315*f4a2713aSLionel Sambuc } 316*f4a2713aSLionel Sambuc } 317*f4a2713aSLionel Sambuc 318*f4a2713aSLionel Sambuc void LookupResult::sanityImpl() const { 319*f4a2713aSLionel Sambuc // Note that this function is never called by NDEBUG builds. See 320*f4a2713aSLionel Sambuc // LookupResult::sanity(). 321*f4a2713aSLionel Sambuc assert(ResultKind != NotFound || Decls.size() == 0); 322*f4a2713aSLionel Sambuc assert(ResultKind != Found || Decls.size() == 1); 323*f4a2713aSLionel Sambuc assert(ResultKind != FoundOverloaded || Decls.size() > 1 || 324*f4a2713aSLionel Sambuc (Decls.size() == 1 && 325*f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); 326*f4a2713aSLionel Sambuc assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved()); 327*f4a2713aSLionel Sambuc assert(ResultKind != Ambiguous || Decls.size() > 1 || 328*f4a2713aSLionel Sambuc (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || 329*f4a2713aSLionel Sambuc Ambiguity == AmbiguousBaseSubobjectTypes))); 330*f4a2713aSLionel Sambuc assert((Paths != NULL) == (ResultKind == Ambiguous && 331*f4a2713aSLionel Sambuc (Ambiguity == AmbiguousBaseSubobjectTypes || 332*f4a2713aSLionel Sambuc Ambiguity == AmbiguousBaseSubobjects))); 333*f4a2713aSLionel Sambuc } 334*f4a2713aSLionel Sambuc 335*f4a2713aSLionel Sambuc // Necessary because CXXBasePaths is not complete in Sema.h 336*f4a2713aSLionel Sambuc void LookupResult::deletePaths(CXXBasePaths *Paths) { 337*f4a2713aSLionel Sambuc delete Paths; 338*f4a2713aSLionel Sambuc } 339*f4a2713aSLionel Sambuc 340*f4a2713aSLionel Sambuc /// Get a representative context for a declaration such that two declarations 341*f4a2713aSLionel Sambuc /// will have the same context if they were found within the same scope. 342*f4a2713aSLionel Sambuc static DeclContext *getContextForScopeMatching(Decl *D) { 343*f4a2713aSLionel Sambuc // For function-local declarations, use that function as the context. This 344*f4a2713aSLionel Sambuc // doesn't account for scopes within the function; the caller must deal with 345*f4a2713aSLionel Sambuc // those. 346*f4a2713aSLionel Sambuc DeclContext *DC = D->getLexicalDeclContext(); 347*f4a2713aSLionel Sambuc if (DC->isFunctionOrMethod()) 348*f4a2713aSLionel Sambuc return DC; 349*f4a2713aSLionel Sambuc 350*f4a2713aSLionel Sambuc // Otherwise, look at the semantic context of the declaration. The 351*f4a2713aSLionel Sambuc // declaration must have been found there. 352*f4a2713aSLionel Sambuc return D->getDeclContext()->getRedeclContext(); 353*f4a2713aSLionel Sambuc } 354*f4a2713aSLionel Sambuc 355*f4a2713aSLionel Sambuc /// Resolves the result kind of this lookup. 356*f4a2713aSLionel Sambuc void LookupResult::resolveKind() { 357*f4a2713aSLionel Sambuc unsigned N = Decls.size(); 358*f4a2713aSLionel Sambuc 359*f4a2713aSLionel Sambuc // Fast case: no possible ambiguity. 360*f4a2713aSLionel Sambuc if (N == 0) { 361*f4a2713aSLionel Sambuc assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); 362*f4a2713aSLionel Sambuc return; 363*f4a2713aSLionel Sambuc } 364*f4a2713aSLionel Sambuc 365*f4a2713aSLionel Sambuc // If there's a single decl, we need to examine it to decide what 366*f4a2713aSLionel Sambuc // kind of lookup this is. 367*f4a2713aSLionel Sambuc if (N == 1) { 368*f4a2713aSLionel Sambuc NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); 369*f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(D)) 370*f4a2713aSLionel Sambuc ResultKind = FoundOverloaded; 371*f4a2713aSLionel Sambuc else if (isa<UnresolvedUsingValueDecl>(D)) 372*f4a2713aSLionel Sambuc ResultKind = FoundUnresolvedValue; 373*f4a2713aSLionel Sambuc return; 374*f4a2713aSLionel Sambuc } 375*f4a2713aSLionel Sambuc 376*f4a2713aSLionel Sambuc // Don't do any extra resolution if we've already resolved as ambiguous. 377*f4a2713aSLionel Sambuc if (ResultKind == Ambiguous) return; 378*f4a2713aSLionel Sambuc 379*f4a2713aSLionel Sambuc llvm::SmallPtrSet<NamedDecl*, 16> Unique; 380*f4a2713aSLionel Sambuc llvm::SmallPtrSet<QualType, 16> UniqueTypes; 381*f4a2713aSLionel Sambuc 382*f4a2713aSLionel Sambuc bool Ambiguous = false; 383*f4a2713aSLionel Sambuc bool HasTag = false, HasFunction = false, HasNonFunction = false; 384*f4a2713aSLionel Sambuc bool HasFunctionTemplate = false, HasUnresolved = false; 385*f4a2713aSLionel Sambuc 386*f4a2713aSLionel Sambuc unsigned UniqueTagIndex = 0; 387*f4a2713aSLionel Sambuc 388*f4a2713aSLionel Sambuc unsigned I = 0; 389*f4a2713aSLionel Sambuc while (I < N) { 390*f4a2713aSLionel Sambuc NamedDecl *D = Decls[I]->getUnderlyingDecl(); 391*f4a2713aSLionel Sambuc D = cast<NamedDecl>(D->getCanonicalDecl()); 392*f4a2713aSLionel Sambuc 393*f4a2713aSLionel Sambuc // Ignore an invalid declaration unless it's the only one left. 394*f4a2713aSLionel Sambuc if (D->isInvalidDecl() && I < N-1) { 395*f4a2713aSLionel Sambuc Decls[I] = Decls[--N]; 396*f4a2713aSLionel Sambuc continue; 397*f4a2713aSLionel Sambuc } 398*f4a2713aSLionel Sambuc 399*f4a2713aSLionel Sambuc // Redeclarations of types via typedef can occur both within a scope 400*f4a2713aSLionel Sambuc // and, through using declarations and directives, across scopes. There is 401*f4a2713aSLionel Sambuc // no ambiguity if they all refer to the same type, so unique based on the 402*f4a2713aSLionel Sambuc // canonical type. 403*f4a2713aSLionel Sambuc if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { 404*f4a2713aSLionel Sambuc if (!TD->getDeclContext()->isRecord()) { 405*f4a2713aSLionel Sambuc QualType T = SemaRef.Context.getTypeDeclType(TD); 406*f4a2713aSLionel Sambuc if (!UniqueTypes.insert(SemaRef.Context.getCanonicalType(T))) { 407*f4a2713aSLionel Sambuc // The type is not unique; pull something off the back and continue 408*f4a2713aSLionel Sambuc // at this index. 409*f4a2713aSLionel Sambuc Decls[I] = Decls[--N]; 410*f4a2713aSLionel Sambuc continue; 411*f4a2713aSLionel Sambuc } 412*f4a2713aSLionel Sambuc } 413*f4a2713aSLionel Sambuc } 414*f4a2713aSLionel Sambuc 415*f4a2713aSLionel Sambuc if (!Unique.insert(D)) { 416*f4a2713aSLionel Sambuc // If it's not unique, pull something off the back (and 417*f4a2713aSLionel Sambuc // continue at this index). 418*f4a2713aSLionel Sambuc Decls[I] = Decls[--N]; 419*f4a2713aSLionel Sambuc continue; 420*f4a2713aSLionel Sambuc } 421*f4a2713aSLionel Sambuc 422*f4a2713aSLionel Sambuc // Otherwise, do some decl type analysis and then continue. 423*f4a2713aSLionel Sambuc 424*f4a2713aSLionel Sambuc if (isa<UnresolvedUsingValueDecl>(D)) { 425*f4a2713aSLionel Sambuc HasUnresolved = true; 426*f4a2713aSLionel Sambuc } else if (isa<TagDecl>(D)) { 427*f4a2713aSLionel Sambuc if (HasTag) 428*f4a2713aSLionel Sambuc Ambiguous = true; 429*f4a2713aSLionel Sambuc UniqueTagIndex = I; 430*f4a2713aSLionel Sambuc HasTag = true; 431*f4a2713aSLionel Sambuc } else if (isa<FunctionTemplateDecl>(D)) { 432*f4a2713aSLionel Sambuc HasFunction = true; 433*f4a2713aSLionel Sambuc HasFunctionTemplate = true; 434*f4a2713aSLionel Sambuc } else if (isa<FunctionDecl>(D)) { 435*f4a2713aSLionel Sambuc HasFunction = true; 436*f4a2713aSLionel Sambuc } else { 437*f4a2713aSLionel Sambuc if (HasNonFunction) 438*f4a2713aSLionel Sambuc Ambiguous = true; 439*f4a2713aSLionel Sambuc HasNonFunction = true; 440*f4a2713aSLionel Sambuc } 441*f4a2713aSLionel Sambuc I++; 442*f4a2713aSLionel Sambuc } 443*f4a2713aSLionel Sambuc 444*f4a2713aSLionel Sambuc // C++ [basic.scope.hiding]p2: 445*f4a2713aSLionel Sambuc // A class name or enumeration name can be hidden by the name of 446*f4a2713aSLionel Sambuc // an object, function, or enumerator declared in the same 447*f4a2713aSLionel Sambuc // scope. If a class or enumeration name and an object, function, 448*f4a2713aSLionel Sambuc // or enumerator are declared in the same scope (in any order) 449*f4a2713aSLionel Sambuc // with the same name, the class or enumeration name is hidden 450*f4a2713aSLionel Sambuc // wherever the object, function, or enumerator name is visible. 451*f4a2713aSLionel Sambuc // But it's still an error if there are distinct tag types found, 452*f4a2713aSLionel Sambuc // even if they're not visible. (ref?) 453*f4a2713aSLionel Sambuc if (HideTags && HasTag && !Ambiguous && 454*f4a2713aSLionel Sambuc (HasFunction || HasNonFunction || HasUnresolved)) { 455*f4a2713aSLionel Sambuc if (getContextForScopeMatching(Decls[UniqueTagIndex])->Equals( 456*f4a2713aSLionel Sambuc getContextForScopeMatching(Decls[UniqueTagIndex ? 0 : N - 1]))) 457*f4a2713aSLionel Sambuc Decls[UniqueTagIndex] = Decls[--N]; 458*f4a2713aSLionel Sambuc else 459*f4a2713aSLionel Sambuc Ambiguous = true; 460*f4a2713aSLionel Sambuc } 461*f4a2713aSLionel Sambuc 462*f4a2713aSLionel Sambuc Decls.set_size(N); 463*f4a2713aSLionel Sambuc 464*f4a2713aSLionel Sambuc if (HasNonFunction && (HasFunction || HasUnresolved)) 465*f4a2713aSLionel Sambuc Ambiguous = true; 466*f4a2713aSLionel Sambuc 467*f4a2713aSLionel Sambuc if (Ambiguous) 468*f4a2713aSLionel Sambuc setAmbiguous(LookupResult::AmbiguousReference); 469*f4a2713aSLionel Sambuc else if (HasUnresolved) 470*f4a2713aSLionel Sambuc ResultKind = LookupResult::FoundUnresolvedValue; 471*f4a2713aSLionel Sambuc else if (N > 1 || HasFunctionTemplate) 472*f4a2713aSLionel Sambuc ResultKind = LookupResult::FoundOverloaded; 473*f4a2713aSLionel Sambuc else 474*f4a2713aSLionel Sambuc ResultKind = LookupResult::Found; 475*f4a2713aSLionel Sambuc } 476*f4a2713aSLionel Sambuc 477*f4a2713aSLionel Sambuc void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { 478*f4a2713aSLionel Sambuc CXXBasePaths::const_paths_iterator I, E; 479*f4a2713aSLionel Sambuc for (I = P.begin(), E = P.end(); I != E; ++I) 480*f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator DI = I->Decls.begin(), 481*f4a2713aSLionel Sambuc DE = I->Decls.end(); DI != DE; ++DI) 482*f4a2713aSLionel Sambuc addDecl(*DI); 483*f4a2713aSLionel Sambuc } 484*f4a2713aSLionel Sambuc 485*f4a2713aSLionel Sambuc void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { 486*f4a2713aSLionel Sambuc Paths = new CXXBasePaths; 487*f4a2713aSLionel Sambuc Paths->swap(P); 488*f4a2713aSLionel Sambuc addDeclsFromBasePaths(*Paths); 489*f4a2713aSLionel Sambuc resolveKind(); 490*f4a2713aSLionel Sambuc setAmbiguous(AmbiguousBaseSubobjects); 491*f4a2713aSLionel Sambuc } 492*f4a2713aSLionel Sambuc 493*f4a2713aSLionel Sambuc void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { 494*f4a2713aSLionel Sambuc Paths = new CXXBasePaths; 495*f4a2713aSLionel Sambuc Paths->swap(P); 496*f4a2713aSLionel Sambuc addDeclsFromBasePaths(*Paths); 497*f4a2713aSLionel Sambuc resolveKind(); 498*f4a2713aSLionel Sambuc setAmbiguous(AmbiguousBaseSubobjectTypes); 499*f4a2713aSLionel Sambuc } 500*f4a2713aSLionel Sambuc 501*f4a2713aSLionel Sambuc void LookupResult::print(raw_ostream &Out) { 502*f4a2713aSLionel Sambuc Out << Decls.size() << " result(s)"; 503*f4a2713aSLionel Sambuc if (isAmbiguous()) Out << ", ambiguous"; 504*f4a2713aSLionel Sambuc if (Paths) Out << ", base paths present"; 505*f4a2713aSLionel Sambuc 506*f4a2713aSLionel Sambuc for (iterator I = begin(), E = end(); I != E; ++I) { 507*f4a2713aSLionel Sambuc Out << "\n"; 508*f4a2713aSLionel Sambuc (*I)->print(Out, 2); 509*f4a2713aSLionel Sambuc } 510*f4a2713aSLionel Sambuc } 511*f4a2713aSLionel Sambuc 512*f4a2713aSLionel Sambuc /// \brief Lookup a builtin function, when name lookup would otherwise 513*f4a2713aSLionel Sambuc /// fail. 514*f4a2713aSLionel Sambuc static bool LookupBuiltin(Sema &S, LookupResult &R) { 515*f4a2713aSLionel Sambuc Sema::LookupNameKind NameKind = R.getLookupKind(); 516*f4a2713aSLionel Sambuc 517*f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, and if the identifier 518*f4a2713aSLionel Sambuc // corresponds to a compiler builtin, create the decl object for the builtin 519*f4a2713aSLionel Sambuc // now, injecting it into translation unit scope, and return it. 520*f4a2713aSLionel Sambuc if (NameKind == Sema::LookupOrdinaryName || 521*f4a2713aSLionel Sambuc NameKind == Sema::LookupRedeclarationWithLinkage) { 522*f4a2713aSLionel Sambuc IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); 523*f4a2713aSLionel Sambuc if (II) { 524*f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && S.getLangOpts().GNUMode && 525*f4a2713aSLionel Sambuc II == S.getFloat128Identifier()) { 526*f4a2713aSLionel Sambuc // libstdc++4.7's type_traits expects type __float128 to exist, so 527*f4a2713aSLionel Sambuc // insert a dummy type to make that header build in gnu++11 mode. 528*f4a2713aSLionel Sambuc R.addDecl(S.getASTContext().getFloat128StubType()); 529*f4a2713aSLionel Sambuc return true; 530*f4a2713aSLionel Sambuc } 531*f4a2713aSLionel Sambuc 532*f4a2713aSLionel Sambuc // If this is a builtin on this (or all) targets, create the decl. 533*f4a2713aSLionel Sambuc if (unsigned BuiltinID = II->getBuiltinID()) { 534*f4a2713aSLionel Sambuc // In C++, we don't have any predefined library functions like 535*f4a2713aSLionel Sambuc // 'malloc'. Instead, we'll just error. 536*f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus && 537*f4a2713aSLionel Sambuc S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 538*f4a2713aSLionel Sambuc return false; 539*f4a2713aSLionel Sambuc 540*f4a2713aSLionel Sambuc if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, 541*f4a2713aSLionel Sambuc BuiltinID, S.TUScope, 542*f4a2713aSLionel Sambuc R.isForRedeclaration(), 543*f4a2713aSLionel Sambuc R.getNameLoc())) { 544*f4a2713aSLionel Sambuc R.addDecl(D); 545*f4a2713aSLionel Sambuc return true; 546*f4a2713aSLionel Sambuc } 547*f4a2713aSLionel Sambuc 548*f4a2713aSLionel Sambuc if (R.isForRedeclaration()) { 549*f4a2713aSLionel Sambuc // If we're redeclaring this function anyway, forget that 550*f4a2713aSLionel Sambuc // this was a builtin at all. 551*f4a2713aSLionel Sambuc S.Context.BuiltinInfo.ForgetBuiltin(BuiltinID, S.Context.Idents); 552*f4a2713aSLionel Sambuc } 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc return false; 555*f4a2713aSLionel Sambuc } 556*f4a2713aSLionel Sambuc } 557*f4a2713aSLionel Sambuc } 558*f4a2713aSLionel Sambuc 559*f4a2713aSLionel Sambuc return false; 560*f4a2713aSLionel Sambuc } 561*f4a2713aSLionel Sambuc 562*f4a2713aSLionel Sambuc /// \brief Determine whether we can declare a special member function within 563*f4a2713aSLionel Sambuc /// the class at this point. 564*f4a2713aSLionel Sambuc static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) { 565*f4a2713aSLionel Sambuc // We need to have a definition for the class. 566*f4a2713aSLionel Sambuc if (!Class->getDefinition() || Class->isDependentContext()) 567*f4a2713aSLionel Sambuc return false; 568*f4a2713aSLionel Sambuc 569*f4a2713aSLionel Sambuc // We can't be in the middle of defining the class. 570*f4a2713aSLionel Sambuc return !Class->isBeingDefined(); 571*f4a2713aSLionel Sambuc } 572*f4a2713aSLionel Sambuc 573*f4a2713aSLionel Sambuc void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { 574*f4a2713aSLionel Sambuc if (!CanDeclareSpecialMemberFunction(Class)) 575*f4a2713aSLionel Sambuc return; 576*f4a2713aSLionel Sambuc 577*f4a2713aSLionel Sambuc // If the default constructor has not yet been declared, do so now. 578*f4a2713aSLionel Sambuc if (Class->needsImplicitDefaultConstructor()) 579*f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(Class); 580*f4a2713aSLionel Sambuc 581*f4a2713aSLionel Sambuc // If the copy constructor has not yet been declared, do so now. 582*f4a2713aSLionel Sambuc if (Class->needsImplicitCopyConstructor()) 583*f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(Class); 584*f4a2713aSLionel Sambuc 585*f4a2713aSLionel Sambuc // If the copy assignment operator has not yet been declared, do so now. 586*f4a2713aSLionel Sambuc if (Class->needsImplicitCopyAssignment()) 587*f4a2713aSLionel Sambuc DeclareImplicitCopyAssignment(Class); 588*f4a2713aSLionel Sambuc 589*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11) { 590*f4a2713aSLionel Sambuc // If the move constructor has not yet been declared, do so now. 591*f4a2713aSLionel Sambuc if (Class->needsImplicitMoveConstructor()) 592*f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(Class); // might not actually do it 593*f4a2713aSLionel Sambuc 594*f4a2713aSLionel Sambuc // If the move assignment operator has not yet been declared, do so now. 595*f4a2713aSLionel Sambuc if (Class->needsImplicitMoveAssignment()) 596*f4a2713aSLionel Sambuc DeclareImplicitMoveAssignment(Class); // might not actually do it 597*f4a2713aSLionel Sambuc } 598*f4a2713aSLionel Sambuc 599*f4a2713aSLionel Sambuc // If the destructor has not yet been declared, do so now. 600*f4a2713aSLionel Sambuc if (Class->needsImplicitDestructor()) 601*f4a2713aSLionel Sambuc DeclareImplicitDestructor(Class); 602*f4a2713aSLionel Sambuc } 603*f4a2713aSLionel Sambuc 604*f4a2713aSLionel Sambuc /// \brief Determine whether this is the name of an implicitly-declared 605*f4a2713aSLionel Sambuc /// special member function. 606*f4a2713aSLionel Sambuc static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { 607*f4a2713aSLionel Sambuc switch (Name.getNameKind()) { 608*f4a2713aSLionel Sambuc case DeclarationName::CXXConstructorName: 609*f4a2713aSLionel Sambuc case DeclarationName::CXXDestructorName: 610*f4a2713aSLionel Sambuc return true; 611*f4a2713aSLionel Sambuc 612*f4a2713aSLionel Sambuc case DeclarationName::CXXOperatorName: 613*f4a2713aSLionel Sambuc return Name.getCXXOverloadedOperator() == OO_Equal; 614*f4a2713aSLionel Sambuc 615*f4a2713aSLionel Sambuc default: 616*f4a2713aSLionel Sambuc break; 617*f4a2713aSLionel Sambuc } 618*f4a2713aSLionel Sambuc 619*f4a2713aSLionel Sambuc return false; 620*f4a2713aSLionel Sambuc } 621*f4a2713aSLionel Sambuc 622*f4a2713aSLionel Sambuc /// \brief If there are any implicit member functions with the given name 623*f4a2713aSLionel Sambuc /// that need to be declared in the given declaration context, do so. 624*f4a2713aSLionel Sambuc static void DeclareImplicitMemberFunctionsWithName(Sema &S, 625*f4a2713aSLionel Sambuc DeclarationName Name, 626*f4a2713aSLionel Sambuc const DeclContext *DC) { 627*f4a2713aSLionel Sambuc if (!DC) 628*f4a2713aSLionel Sambuc return; 629*f4a2713aSLionel Sambuc 630*f4a2713aSLionel Sambuc switch (Name.getNameKind()) { 631*f4a2713aSLionel Sambuc case DeclarationName::CXXConstructorName: 632*f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) 633*f4a2713aSLionel Sambuc if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { 634*f4a2713aSLionel Sambuc CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); 635*f4a2713aSLionel Sambuc if (Record->needsImplicitDefaultConstructor()) 636*f4a2713aSLionel Sambuc S.DeclareImplicitDefaultConstructor(Class); 637*f4a2713aSLionel Sambuc if (Record->needsImplicitCopyConstructor()) 638*f4a2713aSLionel Sambuc S.DeclareImplicitCopyConstructor(Class); 639*f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && 640*f4a2713aSLionel Sambuc Record->needsImplicitMoveConstructor()) 641*f4a2713aSLionel Sambuc S.DeclareImplicitMoveConstructor(Class); 642*f4a2713aSLionel Sambuc } 643*f4a2713aSLionel Sambuc break; 644*f4a2713aSLionel Sambuc 645*f4a2713aSLionel Sambuc case DeclarationName::CXXDestructorName: 646*f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) 647*f4a2713aSLionel Sambuc if (Record->getDefinition() && Record->needsImplicitDestructor() && 648*f4a2713aSLionel Sambuc CanDeclareSpecialMemberFunction(Record)) 649*f4a2713aSLionel Sambuc S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); 650*f4a2713aSLionel Sambuc break; 651*f4a2713aSLionel Sambuc 652*f4a2713aSLionel Sambuc case DeclarationName::CXXOperatorName: 653*f4a2713aSLionel Sambuc if (Name.getCXXOverloadedOperator() != OO_Equal) 654*f4a2713aSLionel Sambuc break; 655*f4a2713aSLionel Sambuc 656*f4a2713aSLionel Sambuc if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) { 657*f4a2713aSLionel Sambuc if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { 658*f4a2713aSLionel Sambuc CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); 659*f4a2713aSLionel Sambuc if (Record->needsImplicitCopyAssignment()) 660*f4a2713aSLionel Sambuc S.DeclareImplicitCopyAssignment(Class); 661*f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && 662*f4a2713aSLionel Sambuc Record->needsImplicitMoveAssignment()) 663*f4a2713aSLionel Sambuc S.DeclareImplicitMoveAssignment(Class); 664*f4a2713aSLionel Sambuc } 665*f4a2713aSLionel Sambuc } 666*f4a2713aSLionel Sambuc break; 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc default: 669*f4a2713aSLionel Sambuc break; 670*f4a2713aSLionel Sambuc } 671*f4a2713aSLionel Sambuc } 672*f4a2713aSLionel Sambuc 673*f4a2713aSLionel Sambuc // Adds all qualifying matches for a name within a decl context to the 674*f4a2713aSLionel Sambuc // given lookup result. Returns true if any matches were found. 675*f4a2713aSLionel Sambuc static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { 676*f4a2713aSLionel Sambuc bool Found = false; 677*f4a2713aSLionel Sambuc 678*f4a2713aSLionel Sambuc // Lazily declare C++ special member functions. 679*f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus) 680*f4a2713aSLionel Sambuc DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC); 681*f4a2713aSLionel Sambuc 682*f4a2713aSLionel Sambuc // Perform lookup into this declaration context. 683*f4a2713aSLionel Sambuc DeclContext::lookup_const_result DR = DC->lookup(R.getLookupName()); 684*f4a2713aSLionel Sambuc for (DeclContext::lookup_const_iterator I = DR.begin(), E = DR.end(); I != E; 685*f4a2713aSLionel Sambuc ++I) { 686*f4a2713aSLionel Sambuc NamedDecl *D = *I; 687*f4a2713aSLionel Sambuc if ((D = R.getAcceptableDecl(D))) { 688*f4a2713aSLionel Sambuc R.addDecl(D); 689*f4a2713aSLionel Sambuc Found = true; 690*f4a2713aSLionel Sambuc } 691*f4a2713aSLionel Sambuc } 692*f4a2713aSLionel Sambuc 693*f4a2713aSLionel Sambuc if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) 694*f4a2713aSLionel Sambuc return true; 695*f4a2713aSLionel Sambuc 696*f4a2713aSLionel Sambuc if (R.getLookupName().getNameKind() 697*f4a2713aSLionel Sambuc != DeclarationName::CXXConversionFunctionName || 698*f4a2713aSLionel Sambuc R.getLookupName().getCXXNameType()->isDependentType() || 699*f4a2713aSLionel Sambuc !isa<CXXRecordDecl>(DC)) 700*f4a2713aSLionel Sambuc return Found; 701*f4a2713aSLionel Sambuc 702*f4a2713aSLionel Sambuc // C++ [temp.mem]p6: 703*f4a2713aSLionel Sambuc // A specialization of a conversion function template is not found by 704*f4a2713aSLionel Sambuc // name lookup. Instead, any conversion function templates visible in the 705*f4a2713aSLionel Sambuc // context of the use are considered. [...] 706*f4a2713aSLionel Sambuc const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); 707*f4a2713aSLionel Sambuc if (!Record->isCompleteDefinition()) 708*f4a2713aSLionel Sambuc return Found; 709*f4a2713aSLionel Sambuc 710*f4a2713aSLionel Sambuc for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(), 711*f4a2713aSLionel Sambuc UEnd = Record->conversion_end(); U != UEnd; ++U) { 712*f4a2713aSLionel Sambuc FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); 713*f4a2713aSLionel Sambuc if (!ConvTemplate) 714*f4a2713aSLionel Sambuc continue; 715*f4a2713aSLionel Sambuc 716*f4a2713aSLionel Sambuc // When we're performing lookup for the purposes of redeclaration, just 717*f4a2713aSLionel Sambuc // add the conversion function template. When we deduce template 718*f4a2713aSLionel Sambuc // arguments for specializations, we'll end up unifying the return 719*f4a2713aSLionel Sambuc // type of the new declaration with the type of the function template. 720*f4a2713aSLionel Sambuc if (R.isForRedeclaration()) { 721*f4a2713aSLionel Sambuc R.addDecl(ConvTemplate); 722*f4a2713aSLionel Sambuc Found = true; 723*f4a2713aSLionel Sambuc continue; 724*f4a2713aSLionel Sambuc } 725*f4a2713aSLionel Sambuc 726*f4a2713aSLionel Sambuc // C++ [temp.mem]p6: 727*f4a2713aSLionel Sambuc // [...] For each such operator, if argument deduction succeeds 728*f4a2713aSLionel Sambuc // (14.9.2.3), the resulting specialization is used as if found by 729*f4a2713aSLionel Sambuc // name lookup. 730*f4a2713aSLionel Sambuc // 731*f4a2713aSLionel Sambuc // When referencing a conversion function for any purpose other than 732*f4a2713aSLionel Sambuc // a redeclaration (such that we'll be building an expression with the 733*f4a2713aSLionel Sambuc // result), perform template argument deduction and place the 734*f4a2713aSLionel Sambuc // specialization into the result set. We do this to avoid forcing all 735*f4a2713aSLionel Sambuc // callers to perform special deduction for conversion functions. 736*f4a2713aSLionel Sambuc TemplateDeductionInfo Info(R.getNameLoc()); 737*f4a2713aSLionel Sambuc FunctionDecl *Specialization = 0; 738*f4a2713aSLionel Sambuc 739*f4a2713aSLionel Sambuc const FunctionProtoType *ConvProto 740*f4a2713aSLionel Sambuc = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); 741*f4a2713aSLionel Sambuc assert(ConvProto && "Nonsensical conversion function template type"); 742*f4a2713aSLionel Sambuc 743*f4a2713aSLionel Sambuc // Compute the type of the function that we would expect the conversion 744*f4a2713aSLionel Sambuc // function to have, if it were to match the name given. 745*f4a2713aSLionel Sambuc // FIXME: Calling convention! 746*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo(); 747*f4a2713aSLionel Sambuc EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C); 748*f4a2713aSLionel Sambuc EPI.ExceptionSpecType = EST_None; 749*f4a2713aSLionel Sambuc EPI.NumExceptions = 0; 750*f4a2713aSLionel Sambuc QualType ExpectedType 751*f4a2713aSLionel Sambuc = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), 752*f4a2713aSLionel Sambuc None, EPI); 753*f4a2713aSLionel Sambuc 754*f4a2713aSLionel Sambuc // Perform template argument deduction against the type that we would 755*f4a2713aSLionel Sambuc // expect the function to have. 756*f4a2713aSLionel Sambuc if (R.getSema().DeduceTemplateArguments(ConvTemplate, 0, ExpectedType, 757*f4a2713aSLionel Sambuc Specialization, Info) 758*f4a2713aSLionel Sambuc == Sema::TDK_Success) { 759*f4a2713aSLionel Sambuc R.addDecl(Specialization); 760*f4a2713aSLionel Sambuc Found = true; 761*f4a2713aSLionel Sambuc } 762*f4a2713aSLionel Sambuc } 763*f4a2713aSLionel Sambuc 764*f4a2713aSLionel Sambuc return Found; 765*f4a2713aSLionel Sambuc } 766*f4a2713aSLionel Sambuc 767*f4a2713aSLionel Sambuc // Performs C++ unqualified lookup into the given file context. 768*f4a2713aSLionel Sambuc static bool 769*f4a2713aSLionel Sambuc CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, 770*f4a2713aSLionel Sambuc DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { 771*f4a2713aSLionel Sambuc 772*f4a2713aSLionel Sambuc assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); 773*f4a2713aSLionel Sambuc 774*f4a2713aSLionel Sambuc // Perform direct name lookup into the LookupCtx. 775*f4a2713aSLionel Sambuc bool Found = LookupDirect(S, R, NS); 776*f4a2713aSLionel Sambuc 777*f4a2713aSLionel Sambuc // Perform direct name lookup into the namespaces nominated by the 778*f4a2713aSLionel Sambuc // using directives whose common ancestor is this namespace. 779*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet::const_iterator UI, UEnd; 780*f4a2713aSLionel Sambuc llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); 781*f4a2713aSLionel Sambuc 782*f4a2713aSLionel Sambuc for (; UI != UEnd; ++UI) 783*f4a2713aSLionel Sambuc if (LookupDirect(S, R, UI->getNominatedNamespace())) 784*f4a2713aSLionel Sambuc Found = true; 785*f4a2713aSLionel Sambuc 786*f4a2713aSLionel Sambuc R.resolveKind(); 787*f4a2713aSLionel Sambuc 788*f4a2713aSLionel Sambuc return Found; 789*f4a2713aSLionel Sambuc } 790*f4a2713aSLionel Sambuc 791*f4a2713aSLionel Sambuc static bool isNamespaceOrTranslationUnitScope(Scope *S) { 792*f4a2713aSLionel Sambuc if (DeclContext *Ctx = S->getEntity()) 793*f4a2713aSLionel Sambuc return Ctx->isFileContext(); 794*f4a2713aSLionel Sambuc return false; 795*f4a2713aSLionel Sambuc } 796*f4a2713aSLionel Sambuc 797*f4a2713aSLionel Sambuc // Find the next outer declaration context from this scope. This 798*f4a2713aSLionel Sambuc // routine actually returns the semantic outer context, which may 799*f4a2713aSLionel Sambuc // differ from the lexical context (encoded directly in the Scope 800*f4a2713aSLionel Sambuc // stack) when we are parsing a member of a class template. In this 801*f4a2713aSLionel Sambuc // case, the second element of the pair will be true, to indicate that 802*f4a2713aSLionel Sambuc // name lookup should continue searching in this semantic context when 803*f4a2713aSLionel Sambuc // it leaves the current template parameter scope. 804*f4a2713aSLionel Sambuc static std::pair<DeclContext *, bool> findOuterContext(Scope *S) { 805*f4a2713aSLionel Sambuc DeclContext *DC = S->getEntity(); 806*f4a2713aSLionel Sambuc DeclContext *Lexical = 0; 807*f4a2713aSLionel Sambuc for (Scope *OuterS = S->getParent(); OuterS; 808*f4a2713aSLionel Sambuc OuterS = OuterS->getParent()) { 809*f4a2713aSLionel Sambuc if (OuterS->getEntity()) { 810*f4a2713aSLionel Sambuc Lexical = OuterS->getEntity(); 811*f4a2713aSLionel Sambuc break; 812*f4a2713aSLionel Sambuc } 813*f4a2713aSLionel Sambuc } 814*f4a2713aSLionel Sambuc 815*f4a2713aSLionel Sambuc // C++ [temp.local]p8: 816*f4a2713aSLionel Sambuc // In the definition of a member of a class template that appears 817*f4a2713aSLionel Sambuc // outside of the namespace containing the class template 818*f4a2713aSLionel Sambuc // definition, the name of a template-parameter hides the name of 819*f4a2713aSLionel Sambuc // a member of this namespace. 820*f4a2713aSLionel Sambuc // 821*f4a2713aSLionel Sambuc // Example: 822*f4a2713aSLionel Sambuc // 823*f4a2713aSLionel Sambuc // namespace N { 824*f4a2713aSLionel Sambuc // class C { }; 825*f4a2713aSLionel Sambuc // 826*f4a2713aSLionel Sambuc // template<class T> class B { 827*f4a2713aSLionel Sambuc // void f(T); 828*f4a2713aSLionel Sambuc // }; 829*f4a2713aSLionel Sambuc // } 830*f4a2713aSLionel Sambuc // 831*f4a2713aSLionel Sambuc // template<class C> void N::B<C>::f(C) { 832*f4a2713aSLionel Sambuc // C b; // C is the template parameter, not N::C 833*f4a2713aSLionel Sambuc // } 834*f4a2713aSLionel Sambuc // 835*f4a2713aSLionel Sambuc // In this example, the lexical context we return is the 836*f4a2713aSLionel Sambuc // TranslationUnit, while the semantic context is the namespace N. 837*f4a2713aSLionel Sambuc if (!Lexical || !DC || !S->getParent() || 838*f4a2713aSLionel Sambuc !S->getParent()->isTemplateParamScope()) 839*f4a2713aSLionel Sambuc return std::make_pair(Lexical, false); 840*f4a2713aSLionel Sambuc 841*f4a2713aSLionel Sambuc // Find the outermost template parameter scope. 842*f4a2713aSLionel Sambuc // For the example, this is the scope for the template parameters of 843*f4a2713aSLionel Sambuc // template<class C>. 844*f4a2713aSLionel Sambuc Scope *OutermostTemplateScope = S->getParent(); 845*f4a2713aSLionel Sambuc while (OutermostTemplateScope->getParent() && 846*f4a2713aSLionel Sambuc OutermostTemplateScope->getParent()->isTemplateParamScope()) 847*f4a2713aSLionel Sambuc OutermostTemplateScope = OutermostTemplateScope->getParent(); 848*f4a2713aSLionel Sambuc 849*f4a2713aSLionel Sambuc // Find the namespace context in which the original scope occurs. In 850*f4a2713aSLionel Sambuc // the example, this is namespace N. 851*f4a2713aSLionel Sambuc DeclContext *Semantic = DC; 852*f4a2713aSLionel Sambuc while (!Semantic->isFileContext()) 853*f4a2713aSLionel Sambuc Semantic = Semantic->getParent(); 854*f4a2713aSLionel Sambuc 855*f4a2713aSLionel Sambuc // Find the declaration context just outside of the template 856*f4a2713aSLionel Sambuc // parameter scope. This is the context in which the template is 857*f4a2713aSLionel Sambuc // being lexically declaration (a namespace context). In the 858*f4a2713aSLionel Sambuc // example, this is the global scope. 859*f4a2713aSLionel Sambuc if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && 860*f4a2713aSLionel Sambuc Lexical->Encloses(Semantic)) 861*f4a2713aSLionel Sambuc return std::make_pair(Semantic, true); 862*f4a2713aSLionel Sambuc 863*f4a2713aSLionel Sambuc return std::make_pair(Lexical, false); 864*f4a2713aSLionel Sambuc } 865*f4a2713aSLionel Sambuc 866*f4a2713aSLionel Sambuc namespace { 867*f4a2713aSLionel Sambuc /// An RAII object to specify that we want to find block scope extern 868*f4a2713aSLionel Sambuc /// declarations. 869*f4a2713aSLionel Sambuc struct FindLocalExternScope { 870*f4a2713aSLionel Sambuc FindLocalExternScope(LookupResult &R) 871*f4a2713aSLionel Sambuc : R(R), OldFindLocalExtern(R.getIdentifierNamespace() & 872*f4a2713aSLionel Sambuc Decl::IDNS_LocalExtern) { 873*f4a2713aSLionel Sambuc R.setFindLocalExtern(R.getIdentifierNamespace() & Decl::IDNS_Ordinary); 874*f4a2713aSLionel Sambuc } 875*f4a2713aSLionel Sambuc void restore() { 876*f4a2713aSLionel Sambuc R.setFindLocalExtern(OldFindLocalExtern); 877*f4a2713aSLionel Sambuc } 878*f4a2713aSLionel Sambuc ~FindLocalExternScope() { 879*f4a2713aSLionel Sambuc restore(); 880*f4a2713aSLionel Sambuc } 881*f4a2713aSLionel Sambuc LookupResult &R; 882*f4a2713aSLionel Sambuc bool OldFindLocalExtern; 883*f4a2713aSLionel Sambuc }; 884*f4a2713aSLionel Sambuc } 885*f4a2713aSLionel Sambuc 886*f4a2713aSLionel Sambuc bool Sema::CppLookupName(LookupResult &R, Scope *S) { 887*f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup"); 888*f4a2713aSLionel Sambuc 889*f4a2713aSLionel Sambuc DeclarationName Name = R.getLookupName(); 890*f4a2713aSLionel Sambuc Sema::LookupNameKind NameKind = R.getLookupKind(); 891*f4a2713aSLionel Sambuc 892*f4a2713aSLionel Sambuc // If this is the name of an implicitly-declared special member function, 893*f4a2713aSLionel Sambuc // go through the scope stack to implicitly declare 894*f4a2713aSLionel Sambuc if (isImplicitlyDeclaredMemberFunctionName(Name)) { 895*f4a2713aSLionel Sambuc for (Scope *PreS = S; PreS; PreS = PreS->getParent()) 896*f4a2713aSLionel Sambuc if (DeclContext *DC = PreS->getEntity()) 897*f4a2713aSLionel Sambuc DeclareImplicitMemberFunctionsWithName(*this, Name, DC); 898*f4a2713aSLionel Sambuc } 899*f4a2713aSLionel Sambuc 900*f4a2713aSLionel Sambuc // Implicitly declare member functions with the name we're looking for, if in 901*f4a2713aSLionel Sambuc // fact we are in a scope where it matters. 902*f4a2713aSLionel Sambuc 903*f4a2713aSLionel Sambuc Scope *Initial = S; 904*f4a2713aSLionel Sambuc IdentifierResolver::iterator 905*f4a2713aSLionel Sambuc I = IdResolver.begin(Name), 906*f4a2713aSLionel Sambuc IEnd = IdResolver.end(); 907*f4a2713aSLionel Sambuc 908*f4a2713aSLionel Sambuc // First we lookup local scope. 909*f4a2713aSLionel Sambuc // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] 910*f4a2713aSLionel Sambuc // ...During unqualified name lookup (3.4.1), the names appear as if 911*f4a2713aSLionel Sambuc // they were declared in the nearest enclosing namespace which contains 912*f4a2713aSLionel Sambuc // both the using-directive and the nominated namespace. 913*f4a2713aSLionel Sambuc // [Note: in this context, "contains" means "contains directly or 914*f4a2713aSLionel Sambuc // indirectly". 915*f4a2713aSLionel Sambuc // 916*f4a2713aSLionel Sambuc // For example: 917*f4a2713aSLionel Sambuc // namespace A { int i; } 918*f4a2713aSLionel Sambuc // void foo() { 919*f4a2713aSLionel Sambuc // int i; 920*f4a2713aSLionel Sambuc // { 921*f4a2713aSLionel Sambuc // using namespace A; 922*f4a2713aSLionel Sambuc // ++i; // finds local 'i', A::i appears at global scope 923*f4a2713aSLionel Sambuc // } 924*f4a2713aSLionel Sambuc // } 925*f4a2713aSLionel Sambuc // 926*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet UDirs; 927*f4a2713aSLionel Sambuc bool VisitedUsingDirectives = false; 928*f4a2713aSLionel Sambuc bool LeftStartingScope = false; 929*f4a2713aSLionel Sambuc DeclContext *OutsideOfTemplateParamDC = 0; 930*f4a2713aSLionel Sambuc 931*f4a2713aSLionel Sambuc // When performing a scope lookup, we want to find local extern decls. 932*f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(R); 933*f4a2713aSLionel Sambuc 934*f4a2713aSLionel Sambuc for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { 935*f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity(); 936*f4a2713aSLionel Sambuc 937*f4a2713aSLionel Sambuc // Check whether the IdResolver has anything in this scope. 938*f4a2713aSLionel Sambuc bool Found = false; 939*f4a2713aSLionel Sambuc for (; I != IEnd && S->isDeclScope(*I); ++I) { 940*f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(*I)) { 941*f4a2713aSLionel Sambuc if (NameKind == LookupRedeclarationWithLinkage) { 942*f4a2713aSLionel Sambuc // Determine whether this (or a previous) declaration is 943*f4a2713aSLionel Sambuc // out-of-scope. 944*f4a2713aSLionel Sambuc if (!LeftStartingScope && !Initial->isDeclScope(*I)) 945*f4a2713aSLionel Sambuc LeftStartingScope = true; 946*f4a2713aSLionel Sambuc 947*f4a2713aSLionel Sambuc // If we found something outside of our starting scope that 948*f4a2713aSLionel Sambuc // does not have linkage, skip it. If it's a template parameter, 949*f4a2713aSLionel Sambuc // we still find it, so we can diagnose the invalid redeclaration. 950*f4a2713aSLionel Sambuc if (LeftStartingScope && !((*I)->hasLinkage()) && 951*f4a2713aSLionel Sambuc !(*I)->isTemplateParameter()) { 952*f4a2713aSLionel Sambuc R.setShadowed(); 953*f4a2713aSLionel Sambuc continue; 954*f4a2713aSLionel Sambuc } 955*f4a2713aSLionel Sambuc } 956*f4a2713aSLionel Sambuc 957*f4a2713aSLionel Sambuc Found = true; 958*f4a2713aSLionel Sambuc R.addDecl(ND); 959*f4a2713aSLionel Sambuc } 960*f4a2713aSLionel Sambuc } 961*f4a2713aSLionel Sambuc if (Found) { 962*f4a2713aSLionel Sambuc R.resolveKind(); 963*f4a2713aSLionel Sambuc if (S->isClassScope()) 964*f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) 965*f4a2713aSLionel Sambuc R.setNamingClass(Record); 966*f4a2713aSLionel Sambuc return true; 967*f4a2713aSLionel Sambuc } 968*f4a2713aSLionel Sambuc 969*f4a2713aSLionel Sambuc if (NameKind == LookupLocalFriendName && !S->isClassScope()) { 970*f4a2713aSLionel Sambuc // C++11 [class.friend]p11: 971*f4a2713aSLionel Sambuc // If a friend declaration appears in a local class and the name 972*f4a2713aSLionel Sambuc // specified is an unqualified name, a prior declaration is 973*f4a2713aSLionel Sambuc // looked up without considering scopes that are outside the 974*f4a2713aSLionel Sambuc // innermost enclosing non-class scope. 975*f4a2713aSLionel Sambuc return false; 976*f4a2713aSLionel Sambuc } 977*f4a2713aSLionel Sambuc 978*f4a2713aSLionel Sambuc if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 979*f4a2713aSLionel Sambuc S->getParent() && !S->getParent()->isTemplateParamScope()) { 980*f4a2713aSLionel Sambuc // We've just searched the last template parameter scope and 981*f4a2713aSLionel Sambuc // found nothing, so look into the contexts between the 982*f4a2713aSLionel Sambuc // lexical and semantic declaration contexts returned by 983*f4a2713aSLionel Sambuc // findOuterContext(). This implements the name lookup behavior 984*f4a2713aSLionel Sambuc // of C++ [temp.local]p8. 985*f4a2713aSLionel Sambuc Ctx = OutsideOfTemplateParamDC; 986*f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = 0; 987*f4a2713aSLionel Sambuc } 988*f4a2713aSLionel Sambuc 989*f4a2713aSLionel Sambuc if (Ctx) { 990*f4a2713aSLionel Sambuc DeclContext *OuterCtx; 991*f4a2713aSLionel Sambuc bool SearchAfterTemplateScope; 992*f4a2713aSLionel Sambuc llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 993*f4a2713aSLionel Sambuc if (SearchAfterTemplateScope) 994*f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = OuterCtx; 995*f4a2713aSLionel Sambuc 996*f4a2713aSLionel Sambuc for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 997*f4a2713aSLionel Sambuc // We do not directly look into transparent contexts, since 998*f4a2713aSLionel Sambuc // those entities will be found in the nearest enclosing 999*f4a2713aSLionel Sambuc // non-transparent context. 1000*f4a2713aSLionel Sambuc if (Ctx->isTransparentContext()) 1001*f4a2713aSLionel Sambuc continue; 1002*f4a2713aSLionel Sambuc 1003*f4a2713aSLionel Sambuc // We do not look directly into function or method contexts, 1004*f4a2713aSLionel Sambuc // since all of the local variables and parameters of the 1005*f4a2713aSLionel Sambuc // function/method are present within the Scope. 1006*f4a2713aSLionel Sambuc if (Ctx->isFunctionOrMethod()) { 1007*f4a2713aSLionel Sambuc // If we have an Objective-C instance method, look for ivars 1008*f4a2713aSLionel Sambuc // in the corresponding interface. 1009*f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 1010*f4a2713aSLionel Sambuc if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) 1011*f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { 1012*f4a2713aSLionel Sambuc ObjCInterfaceDecl *ClassDeclared; 1013*f4a2713aSLionel Sambuc if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( 1014*f4a2713aSLionel Sambuc Name.getAsIdentifierInfo(), 1015*f4a2713aSLionel Sambuc ClassDeclared)) { 1016*f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) { 1017*f4a2713aSLionel Sambuc R.addDecl(ND); 1018*f4a2713aSLionel Sambuc R.resolveKind(); 1019*f4a2713aSLionel Sambuc return true; 1020*f4a2713aSLionel Sambuc } 1021*f4a2713aSLionel Sambuc } 1022*f4a2713aSLionel Sambuc } 1023*f4a2713aSLionel Sambuc } 1024*f4a2713aSLionel Sambuc 1025*f4a2713aSLionel Sambuc continue; 1026*f4a2713aSLionel Sambuc } 1027*f4a2713aSLionel Sambuc 1028*f4a2713aSLionel Sambuc // If this is a file context, we need to perform unqualified name 1029*f4a2713aSLionel Sambuc // lookup considering using directives. 1030*f4a2713aSLionel Sambuc if (Ctx->isFileContext()) { 1031*f4a2713aSLionel Sambuc // If we haven't handled using directives yet, do so now. 1032*f4a2713aSLionel Sambuc if (!VisitedUsingDirectives) { 1033*f4a2713aSLionel Sambuc // Add using directives from this context up to the top level. 1034*f4a2713aSLionel Sambuc for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) { 1035*f4a2713aSLionel Sambuc if (UCtx->isTransparentContext()) 1036*f4a2713aSLionel Sambuc continue; 1037*f4a2713aSLionel Sambuc 1038*f4a2713aSLionel Sambuc UDirs.visit(UCtx, UCtx); 1039*f4a2713aSLionel Sambuc } 1040*f4a2713aSLionel Sambuc 1041*f4a2713aSLionel Sambuc // Find the innermost file scope, so we can add using directives 1042*f4a2713aSLionel Sambuc // from local scopes. 1043*f4a2713aSLionel Sambuc Scope *InnermostFileScope = S; 1044*f4a2713aSLionel Sambuc while (InnermostFileScope && 1045*f4a2713aSLionel Sambuc !isNamespaceOrTranslationUnitScope(InnermostFileScope)) 1046*f4a2713aSLionel Sambuc InnermostFileScope = InnermostFileScope->getParent(); 1047*f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, InnermostFileScope); 1048*f4a2713aSLionel Sambuc 1049*f4a2713aSLionel Sambuc UDirs.done(); 1050*f4a2713aSLionel Sambuc 1051*f4a2713aSLionel Sambuc VisitedUsingDirectives = true; 1052*f4a2713aSLionel Sambuc } 1053*f4a2713aSLionel Sambuc 1054*f4a2713aSLionel Sambuc if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) { 1055*f4a2713aSLionel Sambuc R.resolveKind(); 1056*f4a2713aSLionel Sambuc return true; 1057*f4a2713aSLionel Sambuc } 1058*f4a2713aSLionel Sambuc 1059*f4a2713aSLionel Sambuc continue; 1060*f4a2713aSLionel Sambuc } 1061*f4a2713aSLionel Sambuc 1062*f4a2713aSLionel Sambuc // Perform qualified name lookup into this context. 1063*f4a2713aSLionel Sambuc // FIXME: In some cases, we know that every name that could be found by 1064*f4a2713aSLionel Sambuc // this qualified name lookup will also be on the identifier chain. For 1065*f4a2713aSLionel Sambuc // example, inside a class without any base classes, we never need to 1066*f4a2713aSLionel Sambuc // perform qualified lookup because all of the members are on top of the 1067*f4a2713aSLionel Sambuc // identifier chain. 1068*f4a2713aSLionel Sambuc if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) 1069*f4a2713aSLionel Sambuc return true; 1070*f4a2713aSLionel Sambuc } 1071*f4a2713aSLionel Sambuc } 1072*f4a2713aSLionel Sambuc } 1073*f4a2713aSLionel Sambuc 1074*f4a2713aSLionel Sambuc // Stop if we ran out of scopes. 1075*f4a2713aSLionel Sambuc // FIXME: This really, really shouldn't be happening. 1076*f4a2713aSLionel Sambuc if (!S) return false; 1077*f4a2713aSLionel Sambuc 1078*f4a2713aSLionel Sambuc // If we are looking for members, no need to look into global/namespace scope. 1079*f4a2713aSLionel Sambuc if (NameKind == LookupMemberName) 1080*f4a2713aSLionel Sambuc return false; 1081*f4a2713aSLionel Sambuc 1082*f4a2713aSLionel Sambuc // Collect UsingDirectiveDecls in all scopes, and recursively all 1083*f4a2713aSLionel Sambuc // nominated namespaces by those using-directives. 1084*f4a2713aSLionel Sambuc // 1085*f4a2713aSLionel Sambuc // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we 1086*f4a2713aSLionel Sambuc // don't build it for each lookup! 1087*f4a2713aSLionel Sambuc if (!VisitedUsingDirectives) { 1088*f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, S); 1089*f4a2713aSLionel Sambuc UDirs.done(); 1090*f4a2713aSLionel Sambuc } 1091*f4a2713aSLionel Sambuc 1092*f4a2713aSLionel Sambuc // If we're not performing redeclaration lookup, do not look for local 1093*f4a2713aSLionel Sambuc // extern declarations outside of a function scope. 1094*f4a2713aSLionel Sambuc if (!R.isForRedeclaration()) 1095*f4a2713aSLionel Sambuc FindLocals.restore(); 1096*f4a2713aSLionel Sambuc 1097*f4a2713aSLionel Sambuc // Lookup namespace scope, and global scope. 1098*f4a2713aSLionel Sambuc // Unqualified name lookup in C++ requires looking into scopes 1099*f4a2713aSLionel Sambuc // that aren't strictly lexical, and therefore we walk through the 1100*f4a2713aSLionel Sambuc // context as well as walking through the scopes. 1101*f4a2713aSLionel Sambuc for (; S; S = S->getParent()) { 1102*f4a2713aSLionel Sambuc // Check whether the IdResolver has anything in this scope. 1103*f4a2713aSLionel Sambuc bool Found = false; 1104*f4a2713aSLionel Sambuc for (; I != IEnd && S->isDeclScope(*I); ++I) { 1105*f4a2713aSLionel Sambuc if (NamedDecl *ND = R.getAcceptableDecl(*I)) { 1106*f4a2713aSLionel Sambuc // We found something. Look for anything else in our scope 1107*f4a2713aSLionel Sambuc // with this same name and in an acceptable identifier 1108*f4a2713aSLionel Sambuc // namespace, so that we can construct an overload set if we 1109*f4a2713aSLionel Sambuc // need to. 1110*f4a2713aSLionel Sambuc Found = true; 1111*f4a2713aSLionel Sambuc R.addDecl(ND); 1112*f4a2713aSLionel Sambuc } 1113*f4a2713aSLionel Sambuc } 1114*f4a2713aSLionel Sambuc 1115*f4a2713aSLionel Sambuc if (Found && S->isTemplateParamScope()) { 1116*f4a2713aSLionel Sambuc R.resolveKind(); 1117*f4a2713aSLionel Sambuc return true; 1118*f4a2713aSLionel Sambuc } 1119*f4a2713aSLionel Sambuc 1120*f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity(); 1121*f4a2713aSLionel Sambuc if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 1122*f4a2713aSLionel Sambuc S->getParent() && !S->getParent()->isTemplateParamScope()) { 1123*f4a2713aSLionel Sambuc // We've just searched the last template parameter scope and 1124*f4a2713aSLionel Sambuc // found nothing, so look into the contexts between the 1125*f4a2713aSLionel Sambuc // lexical and semantic declaration contexts returned by 1126*f4a2713aSLionel Sambuc // findOuterContext(). This implements the name lookup behavior 1127*f4a2713aSLionel Sambuc // of C++ [temp.local]p8. 1128*f4a2713aSLionel Sambuc Ctx = OutsideOfTemplateParamDC; 1129*f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = 0; 1130*f4a2713aSLionel Sambuc } 1131*f4a2713aSLionel Sambuc 1132*f4a2713aSLionel Sambuc if (Ctx) { 1133*f4a2713aSLionel Sambuc DeclContext *OuterCtx; 1134*f4a2713aSLionel Sambuc bool SearchAfterTemplateScope; 1135*f4a2713aSLionel Sambuc llvm::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 1136*f4a2713aSLionel Sambuc if (SearchAfterTemplateScope) 1137*f4a2713aSLionel Sambuc OutsideOfTemplateParamDC = OuterCtx; 1138*f4a2713aSLionel Sambuc 1139*f4a2713aSLionel Sambuc for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 1140*f4a2713aSLionel Sambuc // We do not directly look into transparent contexts, since 1141*f4a2713aSLionel Sambuc // those entities will be found in the nearest enclosing 1142*f4a2713aSLionel Sambuc // non-transparent context. 1143*f4a2713aSLionel Sambuc if (Ctx->isTransparentContext()) 1144*f4a2713aSLionel Sambuc continue; 1145*f4a2713aSLionel Sambuc 1146*f4a2713aSLionel Sambuc // If we have a context, and it's not a context stashed in the 1147*f4a2713aSLionel Sambuc // template parameter scope for an out-of-line definition, also 1148*f4a2713aSLionel Sambuc // look into that context. 1149*f4a2713aSLionel Sambuc if (!(Found && S && S->isTemplateParamScope())) { 1150*f4a2713aSLionel Sambuc assert(Ctx->isFileContext() && 1151*f4a2713aSLionel Sambuc "We should have been looking only at file context here already."); 1152*f4a2713aSLionel Sambuc 1153*f4a2713aSLionel Sambuc // Look into context considering using-directives. 1154*f4a2713aSLionel Sambuc if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) 1155*f4a2713aSLionel Sambuc Found = true; 1156*f4a2713aSLionel Sambuc } 1157*f4a2713aSLionel Sambuc 1158*f4a2713aSLionel Sambuc if (Found) { 1159*f4a2713aSLionel Sambuc R.resolveKind(); 1160*f4a2713aSLionel Sambuc return true; 1161*f4a2713aSLionel Sambuc } 1162*f4a2713aSLionel Sambuc 1163*f4a2713aSLionel Sambuc if (R.isForRedeclaration() && !Ctx->isTransparentContext()) 1164*f4a2713aSLionel Sambuc return false; 1165*f4a2713aSLionel Sambuc } 1166*f4a2713aSLionel Sambuc } 1167*f4a2713aSLionel Sambuc 1168*f4a2713aSLionel Sambuc if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) 1169*f4a2713aSLionel Sambuc return false; 1170*f4a2713aSLionel Sambuc } 1171*f4a2713aSLionel Sambuc 1172*f4a2713aSLionel Sambuc return !R.empty(); 1173*f4a2713aSLionel Sambuc } 1174*f4a2713aSLionel Sambuc 1175*f4a2713aSLionel Sambuc /// \brief Find the declaration that a class temploid member specialization was 1176*f4a2713aSLionel Sambuc /// instantiated from, or the member itself if it is an explicit specialization. 1177*f4a2713aSLionel Sambuc static Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) { 1178*f4a2713aSLionel Sambuc return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom(); 1179*f4a2713aSLionel Sambuc } 1180*f4a2713aSLionel Sambuc 1181*f4a2713aSLionel Sambuc /// \brief Find the module in which the given declaration was defined. 1182*f4a2713aSLionel Sambuc static Module *getDefiningModule(Decl *Entity) { 1183*f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) { 1184*f4a2713aSLionel Sambuc // If this function was instantiated from a template, the defining module is 1185*f4a2713aSLionel Sambuc // the module containing the pattern. 1186*f4a2713aSLionel Sambuc if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) 1187*f4a2713aSLionel Sambuc Entity = Pattern; 1188*f4a2713aSLionel Sambuc } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) { 1189*f4a2713aSLionel Sambuc // If it's a class template specialization, find the template or partial 1190*f4a2713aSLionel Sambuc // specialization from which it was instantiated. 1191*f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *SpecRD = 1192*f4a2713aSLionel Sambuc dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 1193*f4a2713aSLionel Sambuc llvm::PointerUnion<ClassTemplateDecl*, 1194*f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl*> From = 1195*f4a2713aSLionel Sambuc SpecRD->getInstantiatedFrom(); 1196*f4a2713aSLionel Sambuc if (ClassTemplateDecl *FromTemplate = From.dyn_cast<ClassTemplateDecl*>()) 1197*f4a2713aSLionel Sambuc Entity = FromTemplate->getTemplatedDecl(); 1198*f4a2713aSLionel Sambuc else if (From) 1199*f4a2713aSLionel Sambuc Entity = From.get<ClassTemplatePartialSpecializationDecl*>(); 1200*f4a2713aSLionel Sambuc // Otherwise, it's an explicit specialization. 1201*f4a2713aSLionel Sambuc } else if (MemberSpecializationInfo *MSInfo = 1202*f4a2713aSLionel Sambuc RD->getMemberSpecializationInfo()) 1203*f4a2713aSLionel Sambuc Entity = getInstantiatedFrom(RD, MSInfo); 1204*f4a2713aSLionel Sambuc } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) { 1205*f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo()) 1206*f4a2713aSLionel Sambuc Entity = getInstantiatedFrom(ED, MSInfo); 1207*f4a2713aSLionel Sambuc } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) { 1208*f4a2713aSLionel Sambuc // FIXME: Map from variable template specializations back to the template. 1209*f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) 1210*f4a2713aSLionel Sambuc Entity = getInstantiatedFrom(VD, MSInfo); 1211*f4a2713aSLionel Sambuc } 1212*f4a2713aSLionel Sambuc 1213*f4a2713aSLionel Sambuc // Walk up to the containing context. That might also have been instantiated 1214*f4a2713aSLionel Sambuc // from a template. 1215*f4a2713aSLionel Sambuc DeclContext *Context = Entity->getDeclContext(); 1216*f4a2713aSLionel Sambuc if (Context->isFileContext()) 1217*f4a2713aSLionel Sambuc return Entity->getOwningModule(); 1218*f4a2713aSLionel Sambuc return getDefiningModule(cast<Decl>(Context)); 1219*f4a2713aSLionel Sambuc } 1220*f4a2713aSLionel Sambuc 1221*f4a2713aSLionel Sambuc llvm::DenseSet<Module*> &Sema::getLookupModules() { 1222*f4a2713aSLionel Sambuc unsigned N = ActiveTemplateInstantiations.size(); 1223*f4a2713aSLionel Sambuc for (unsigned I = ActiveTemplateInstantiationLookupModules.size(); 1224*f4a2713aSLionel Sambuc I != N; ++I) { 1225*f4a2713aSLionel Sambuc Module *M = getDefiningModule(ActiveTemplateInstantiations[I].Entity); 1226*f4a2713aSLionel Sambuc if (M && !LookupModulesCache.insert(M).second) 1227*f4a2713aSLionel Sambuc M = 0; 1228*f4a2713aSLionel Sambuc ActiveTemplateInstantiationLookupModules.push_back(M); 1229*f4a2713aSLionel Sambuc } 1230*f4a2713aSLionel Sambuc return LookupModulesCache; 1231*f4a2713aSLionel Sambuc } 1232*f4a2713aSLionel Sambuc 1233*f4a2713aSLionel Sambuc /// \brief Determine whether a declaration is visible to name lookup. 1234*f4a2713aSLionel Sambuc /// 1235*f4a2713aSLionel Sambuc /// This routine determines whether the declaration D is visible in the current 1236*f4a2713aSLionel Sambuc /// lookup context, taking into account the current template instantiation 1237*f4a2713aSLionel Sambuc /// stack. During template instantiation, a declaration is visible if it is 1238*f4a2713aSLionel Sambuc /// visible from a module containing any entity on the template instantiation 1239*f4a2713aSLionel Sambuc /// path (by instantiating a template, you allow it to see the declarations that 1240*f4a2713aSLionel Sambuc /// your module can see, including those later on in your module). 1241*f4a2713aSLionel Sambuc bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) { 1242*f4a2713aSLionel Sambuc assert(D->isHidden() && !SemaRef.ActiveTemplateInstantiations.empty() && 1243*f4a2713aSLionel Sambuc "should not call this: not in slow case"); 1244*f4a2713aSLionel Sambuc Module *DeclModule = D->getOwningModule(); 1245*f4a2713aSLionel Sambuc assert(DeclModule && "hidden decl not from a module"); 1246*f4a2713aSLionel Sambuc 1247*f4a2713aSLionel Sambuc // Find the extra places where we need to look. 1248*f4a2713aSLionel Sambuc llvm::DenseSet<Module*> &LookupModules = SemaRef.getLookupModules(); 1249*f4a2713aSLionel Sambuc if (LookupModules.empty()) 1250*f4a2713aSLionel Sambuc return false; 1251*f4a2713aSLionel Sambuc 1252*f4a2713aSLionel Sambuc // If our lookup set contains the decl's module, it's visible. 1253*f4a2713aSLionel Sambuc if (LookupModules.count(DeclModule)) 1254*f4a2713aSLionel Sambuc return true; 1255*f4a2713aSLionel Sambuc 1256*f4a2713aSLionel Sambuc // If the declaration isn't exported, it's not visible in any other module. 1257*f4a2713aSLionel Sambuc if (D->isModulePrivate()) 1258*f4a2713aSLionel Sambuc return false; 1259*f4a2713aSLionel Sambuc 1260*f4a2713aSLionel Sambuc // Check whether DeclModule is transitively exported to an import of 1261*f4a2713aSLionel Sambuc // the lookup set. 1262*f4a2713aSLionel Sambuc for (llvm::DenseSet<Module *>::iterator I = LookupModules.begin(), 1263*f4a2713aSLionel Sambuc E = LookupModules.end(); 1264*f4a2713aSLionel Sambuc I != E; ++I) 1265*f4a2713aSLionel Sambuc if ((*I)->isModuleVisible(DeclModule)) 1266*f4a2713aSLionel Sambuc return true; 1267*f4a2713aSLionel Sambuc return false; 1268*f4a2713aSLionel Sambuc } 1269*f4a2713aSLionel Sambuc 1270*f4a2713aSLionel Sambuc /// \brief Retrieve the visible declaration corresponding to D, if any. 1271*f4a2713aSLionel Sambuc /// 1272*f4a2713aSLionel Sambuc /// This routine determines whether the declaration D is visible in the current 1273*f4a2713aSLionel Sambuc /// module, with the current imports. If not, it checks whether any 1274*f4a2713aSLionel Sambuc /// redeclaration of D is visible, and if so, returns that declaration. 1275*f4a2713aSLionel Sambuc /// 1276*f4a2713aSLionel Sambuc /// \returns D, or a visible previous declaration of D, whichever is more recent 1277*f4a2713aSLionel Sambuc /// and visible. If no declaration of D is visible, returns null. 1278*f4a2713aSLionel Sambuc static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 1279*f4a2713aSLionel Sambuc assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 1280*f4a2713aSLionel Sambuc 1281*f4a2713aSLionel Sambuc for (Decl::redecl_iterator RD = D->redecls_begin(), RDEnd = D->redecls_end(); 1282*f4a2713aSLionel Sambuc RD != RDEnd; ++RD) { 1283*f4a2713aSLionel Sambuc if (NamedDecl *ND = dyn_cast<NamedDecl>(*RD)) { 1284*f4a2713aSLionel Sambuc if (LookupResult::isVisible(SemaRef, ND)) 1285*f4a2713aSLionel Sambuc return ND; 1286*f4a2713aSLionel Sambuc } 1287*f4a2713aSLionel Sambuc } 1288*f4a2713aSLionel Sambuc 1289*f4a2713aSLionel Sambuc return 0; 1290*f4a2713aSLionel Sambuc } 1291*f4a2713aSLionel Sambuc 1292*f4a2713aSLionel Sambuc NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const { 1293*f4a2713aSLionel Sambuc return findAcceptableDecl(SemaRef, D); 1294*f4a2713aSLionel Sambuc } 1295*f4a2713aSLionel Sambuc 1296*f4a2713aSLionel Sambuc /// @brief Perform unqualified name lookup starting from a given 1297*f4a2713aSLionel Sambuc /// scope. 1298*f4a2713aSLionel Sambuc /// 1299*f4a2713aSLionel Sambuc /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is 1300*f4a2713aSLionel Sambuc /// used to find names within the current scope. For example, 'x' in 1301*f4a2713aSLionel Sambuc /// @code 1302*f4a2713aSLionel Sambuc /// int x; 1303*f4a2713aSLionel Sambuc /// int f() { 1304*f4a2713aSLionel Sambuc /// return x; // unqualified name look finds 'x' in the global scope 1305*f4a2713aSLionel Sambuc /// } 1306*f4a2713aSLionel Sambuc /// @endcode 1307*f4a2713aSLionel Sambuc /// 1308*f4a2713aSLionel Sambuc /// Different lookup criteria can find different names. For example, a 1309*f4a2713aSLionel Sambuc /// particular scope can have both a struct and a function of the same 1310*f4a2713aSLionel Sambuc /// name, and each can be found by certain lookup criteria. For more 1311*f4a2713aSLionel Sambuc /// information about lookup criteria, see the documentation for the 1312*f4a2713aSLionel Sambuc /// class LookupCriteria. 1313*f4a2713aSLionel Sambuc /// 1314*f4a2713aSLionel Sambuc /// @param S The scope from which unqualified name lookup will 1315*f4a2713aSLionel Sambuc /// begin. If the lookup criteria permits, name lookup may also search 1316*f4a2713aSLionel Sambuc /// in the parent scopes. 1317*f4a2713aSLionel Sambuc /// 1318*f4a2713aSLionel Sambuc /// @param [in,out] R Specifies the lookup to perform (e.g., the name to 1319*f4a2713aSLionel Sambuc /// look up and the lookup kind), and is updated with the results of lookup 1320*f4a2713aSLionel Sambuc /// including zero or more declarations and possibly additional information 1321*f4a2713aSLionel Sambuc /// used to diagnose ambiguities. 1322*f4a2713aSLionel Sambuc /// 1323*f4a2713aSLionel Sambuc /// @returns \c true if lookup succeeded and false otherwise. 1324*f4a2713aSLionel Sambuc bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { 1325*f4a2713aSLionel Sambuc DeclarationName Name = R.getLookupName(); 1326*f4a2713aSLionel Sambuc if (!Name) return false; 1327*f4a2713aSLionel Sambuc 1328*f4a2713aSLionel Sambuc LookupNameKind NameKind = R.getLookupKind(); 1329*f4a2713aSLionel Sambuc 1330*f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus) { 1331*f4a2713aSLionel Sambuc // Unqualified name lookup in C/Objective-C is purely lexical, so 1332*f4a2713aSLionel Sambuc // search in the declarations attached to the name. 1333*f4a2713aSLionel Sambuc if (NameKind == Sema::LookupRedeclarationWithLinkage) { 1334*f4a2713aSLionel Sambuc // Find the nearest non-transparent declaration scope. 1335*f4a2713aSLionel Sambuc while (!(S->getFlags() & Scope::DeclScope) || 1336*f4a2713aSLionel Sambuc (S->getEntity() && S->getEntity()->isTransparentContext())) 1337*f4a2713aSLionel Sambuc S = S->getParent(); 1338*f4a2713aSLionel Sambuc } 1339*f4a2713aSLionel Sambuc 1340*f4a2713aSLionel Sambuc // When performing a scope lookup, we want to find local extern decls. 1341*f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(R); 1342*f4a2713aSLionel Sambuc 1343*f4a2713aSLionel Sambuc // Scan up the scope chain looking for a decl that matches this 1344*f4a2713aSLionel Sambuc // identifier that is in the appropriate namespace. This search 1345*f4a2713aSLionel Sambuc // should not take long, as shadowing of names is uncommon, and 1346*f4a2713aSLionel Sambuc // deep shadowing is extremely uncommon. 1347*f4a2713aSLionel Sambuc bool LeftStartingScope = false; 1348*f4a2713aSLionel Sambuc 1349*f4a2713aSLionel Sambuc for (IdentifierResolver::iterator I = IdResolver.begin(Name), 1350*f4a2713aSLionel Sambuc IEnd = IdResolver.end(); 1351*f4a2713aSLionel Sambuc I != IEnd; ++I) 1352*f4a2713aSLionel Sambuc if (NamedDecl *D = R.getAcceptableDecl(*I)) { 1353*f4a2713aSLionel Sambuc if (NameKind == LookupRedeclarationWithLinkage) { 1354*f4a2713aSLionel Sambuc // Determine whether this (or a previous) declaration is 1355*f4a2713aSLionel Sambuc // out-of-scope. 1356*f4a2713aSLionel Sambuc if (!LeftStartingScope && !S->isDeclScope(*I)) 1357*f4a2713aSLionel Sambuc LeftStartingScope = true; 1358*f4a2713aSLionel Sambuc 1359*f4a2713aSLionel Sambuc // If we found something outside of our starting scope that 1360*f4a2713aSLionel Sambuc // does not have linkage, skip it. 1361*f4a2713aSLionel Sambuc if (LeftStartingScope && !((*I)->hasLinkage())) { 1362*f4a2713aSLionel Sambuc R.setShadowed(); 1363*f4a2713aSLionel Sambuc continue; 1364*f4a2713aSLionel Sambuc } 1365*f4a2713aSLionel Sambuc } 1366*f4a2713aSLionel Sambuc else if (NameKind == LookupObjCImplicitSelfParam && 1367*f4a2713aSLionel Sambuc !isa<ImplicitParamDecl>(*I)) 1368*f4a2713aSLionel Sambuc continue; 1369*f4a2713aSLionel Sambuc 1370*f4a2713aSLionel Sambuc R.addDecl(D); 1371*f4a2713aSLionel Sambuc 1372*f4a2713aSLionel Sambuc // Check whether there are any other declarations with the same name 1373*f4a2713aSLionel Sambuc // and in the same scope. 1374*f4a2713aSLionel Sambuc if (I != IEnd) { 1375*f4a2713aSLionel Sambuc // Find the scope in which this declaration was declared (if it 1376*f4a2713aSLionel Sambuc // actually exists in a Scope). 1377*f4a2713aSLionel Sambuc while (S && !S->isDeclScope(D)) 1378*f4a2713aSLionel Sambuc S = S->getParent(); 1379*f4a2713aSLionel Sambuc 1380*f4a2713aSLionel Sambuc // If the scope containing the declaration is the translation unit, 1381*f4a2713aSLionel Sambuc // then we'll need to perform our checks based on the matching 1382*f4a2713aSLionel Sambuc // DeclContexts rather than matching scopes. 1383*f4a2713aSLionel Sambuc if (S && isNamespaceOrTranslationUnitScope(S)) 1384*f4a2713aSLionel Sambuc S = 0; 1385*f4a2713aSLionel Sambuc 1386*f4a2713aSLionel Sambuc // Compute the DeclContext, if we need it. 1387*f4a2713aSLionel Sambuc DeclContext *DC = 0; 1388*f4a2713aSLionel Sambuc if (!S) 1389*f4a2713aSLionel Sambuc DC = (*I)->getDeclContext()->getRedeclContext(); 1390*f4a2713aSLionel Sambuc 1391*f4a2713aSLionel Sambuc IdentifierResolver::iterator LastI = I; 1392*f4a2713aSLionel Sambuc for (++LastI; LastI != IEnd; ++LastI) { 1393*f4a2713aSLionel Sambuc if (S) { 1394*f4a2713aSLionel Sambuc // Match based on scope. 1395*f4a2713aSLionel Sambuc if (!S->isDeclScope(*LastI)) 1396*f4a2713aSLionel Sambuc break; 1397*f4a2713aSLionel Sambuc } else { 1398*f4a2713aSLionel Sambuc // Match based on DeclContext. 1399*f4a2713aSLionel Sambuc DeclContext *LastDC 1400*f4a2713aSLionel Sambuc = (*LastI)->getDeclContext()->getRedeclContext(); 1401*f4a2713aSLionel Sambuc if (!LastDC->Equals(DC)) 1402*f4a2713aSLionel Sambuc break; 1403*f4a2713aSLionel Sambuc } 1404*f4a2713aSLionel Sambuc 1405*f4a2713aSLionel Sambuc // If the declaration is in the right namespace and visible, add it. 1406*f4a2713aSLionel Sambuc if (NamedDecl *LastD = R.getAcceptableDecl(*LastI)) 1407*f4a2713aSLionel Sambuc R.addDecl(LastD); 1408*f4a2713aSLionel Sambuc } 1409*f4a2713aSLionel Sambuc 1410*f4a2713aSLionel Sambuc R.resolveKind(); 1411*f4a2713aSLionel Sambuc } 1412*f4a2713aSLionel Sambuc 1413*f4a2713aSLionel Sambuc return true; 1414*f4a2713aSLionel Sambuc } 1415*f4a2713aSLionel Sambuc } else { 1416*f4a2713aSLionel Sambuc // Perform C++ unqualified name lookup. 1417*f4a2713aSLionel Sambuc if (CppLookupName(R, S)) 1418*f4a2713aSLionel Sambuc return true; 1419*f4a2713aSLionel Sambuc } 1420*f4a2713aSLionel Sambuc 1421*f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, and if the identifier 1422*f4a2713aSLionel Sambuc // corresponds to a compiler builtin, create the decl object for the builtin 1423*f4a2713aSLionel Sambuc // now, injecting it into translation unit scope, and return it. 1424*f4a2713aSLionel Sambuc if (AllowBuiltinCreation && LookupBuiltin(*this, R)) 1425*f4a2713aSLionel Sambuc return true; 1426*f4a2713aSLionel Sambuc 1427*f4a2713aSLionel Sambuc // If we didn't find a use of this identifier, the ExternalSource 1428*f4a2713aSLionel Sambuc // may be able to handle the situation. 1429*f4a2713aSLionel Sambuc // Note: some lookup failures are expected! 1430*f4a2713aSLionel Sambuc // See e.g. R.isForRedeclaration(). 1431*f4a2713aSLionel Sambuc return (ExternalSource && ExternalSource->LookupUnqualified(R, S)); 1432*f4a2713aSLionel Sambuc } 1433*f4a2713aSLionel Sambuc 1434*f4a2713aSLionel Sambuc /// @brief Perform qualified name lookup in the namespaces nominated by 1435*f4a2713aSLionel Sambuc /// using directives by the given context. 1436*f4a2713aSLionel Sambuc /// 1437*f4a2713aSLionel Sambuc /// C++98 [namespace.qual]p2: 1438*f4a2713aSLionel Sambuc /// Given X::m (where X is a user-declared namespace), or given \::m 1439*f4a2713aSLionel Sambuc /// (where X is the global namespace), let S be the set of all 1440*f4a2713aSLionel Sambuc /// declarations of m in X and in the transitive closure of all 1441*f4a2713aSLionel Sambuc /// namespaces nominated by using-directives in X and its used 1442*f4a2713aSLionel Sambuc /// namespaces, except that using-directives are ignored in any 1443*f4a2713aSLionel Sambuc /// namespace, including X, directly containing one or more 1444*f4a2713aSLionel Sambuc /// declarations of m. No namespace is searched more than once in 1445*f4a2713aSLionel Sambuc /// the lookup of a name. If S is the empty set, the program is 1446*f4a2713aSLionel Sambuc /// ill-formed. Otherwise, if S has exactly one member, or if the 1447*f4a2713aSLionel Sambuc /// context of the reference is a using-declaration 1448*f4a2713aSLionel Sambuc /// (namespace.udecl), S is the required set of declarations of 1449*f4a2713aSLionel Sambuc /// m. Otherwise if the use of m is not one that allows a unique 1450*f4a2713aSLionel Sambuc /// declaration to be chosen from S, the program is ill-formed. 1451*f4a2713aSLionel Sambuc /// 1452*f4a2713aSLionel Sambuc /// C++98 [namespace.qual]p5: 1453*f4a2713aSLionel Sambuc /// During the lookup of a qualified namespace member name, if the 1454*f4a2713aSLionel Sambuc /// lookup finds more than one declaration of the member, and if one 1455*f4a2713aSLionel Sambuc /// declaration introduces a class name or enumeration name and the 1456*f4a2713aSLionel Sambuc /// other declarations either introduce the same object, the same 1457*f4a2713aSLionel Sambuc /// enumerator or a set of functions, the non-type name hides the 1458*f4a2713aSLionel Sambuc /// class or enumeration name if and only if the declarations are 1459*f4a2713aSLionel Sambuc /// from the same namespace; otherwise (the declarations are from 1460*f4a2713aSLionel Sambuc /// different namespaces), the program is ill-formed. 1461*f4a2713aSLionel Sambuc static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, 1462*f4a2713aSLionel Sambuc DeclContext *StartDC) { 1463*f4a2713aSLionel Sambuc assert(StartDC->isFileContext() && "start context is not a file context"); 1464*f4a2713aSLionel Sambuc 1465*f4a2713aSLionel Sambuc DeclContext::udir_iterator I = StartDC->using_directives_begin(); 1466*f4a2713aSLionel Sambuc DeclContext::udir_iterator E = StartDC->using_directives_end(); 1467*f4a2713aSLionel Sambuc 1468*f4a2713aSLionel Sambuc if (I == E) return false; 1469*f4a2713aSLionel Sambuc 1470*f4a2713aSLionel Sambuc // We have at least added all these contexts to the queue. 1471*f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext*, 8> Visited; 1472*f4a2713aSLionel Sambuc Visited.insert(StartDC); 1473*f4a2713aSLionel Sambuc 1474*f4a2713aSLionel Sambuc // We have not yet looked into these namespaces, much less added 1475*f4a2713aSLionel Sambuc // their "using-children" to the queue. 1476*f4a2713aSLionel Sambuc SmallVector<NamespaceDecl*, 8> Queue; 1477*f4a2713aSLionel Sambuc 1478*f4a2713aSLionel Sambuc // We have already looked into the initial namespace; seed the queue 1479*f4a2713aSLionel Sambuc // with its using-children. 1480*f4a2713aSLionel Sambuc for (; I != E; ++I) { 1481*f4a2713aSLionel Sambuc NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace(); 1482*f4a2713aSLionel Sambuc if (Visited.insert(ND)) 1483*f4a2713aSLionel Sambuc Queue.push_back(ND); 1484*f4a2713aSLionel Sambuc } 1485*f4a2713aSLionel Sambuc 1486*f4a2713aSLionel Sambuc // The easiest way to implement the restriction in [namespace.qual]p5 1487*f4a2713aSLionel Sambuc // is to check whether any of the individual results found a tag 1488*f4a2713aSLionel Sambuc // and, if so, to declare an ambiguity if the final result is not 1489*f4a2713aSLionel Sambuc // a tag. 1490*f4a2713aSLionel Sambuc bool FoundTag = false; 1491*f4a2713aSLionel Sambuc bool FoundNonTag = false; 1492*f4a2713aSLionel Sambuc 1493*f4a2713aSLionel Sambuc LookupResult LocalR(LookupResult::Temporary, R); 1494*f4a2713aSLionel Sambuc 1495*f4a2713aSLionel Sambuc bool Found = false; 1496*f4a2713aSLionel Sambuc while (!Queue.empty()) { 1497*f4a2713aSLionel Sambuc NamespaceDecl *ND = Queue.pop_back_val(); 1498*f4a2713aSLionel Sambuc 1499*f4a2713aSLionel Sambuc // We go through some convolutions here to avoid copying results 1500*f4a2713aSLionel Sambuc // between LookupResults. 1501*f4a2713aSLionel Sambuc bool UseLocal = !R.empty(); 1502*f4a2713aSLionel Sambuc LookupResult &DirectR = UseLocal ? LocalR : R; 1503*f4a2713aSLionel Sambuc bool FoundDirect = LookupDirect(S, DirectR, ND); 1504*f4a2713aSLionel Sambuc 1505*f4a2713aSLionel Sambuc if (FoundDirect) { 1506*f4a2713aSLionel Sambuc // First do any local hiding. 1507*f4a2713aSLionel Sambuc DirectR.resolveKind(); 1508*f4a2713aSLionel Sambuc 1509*f4a2713aSLionel Sambuc // If the local result is a tag, remember that. 1510*f4a2713aSLionel Sambuc if (DirectR.isSingleTagDecl()) 1511*f4a2713aSLionel Sambuc FoundTag = true; 1512*f4a2713aSLionel Sambuc else 1513*f4a2713aSLionel Sambuc FoundNonTag = true; 1514*f4a2713aSLionel Sambuc 1515*f4a2713aSLionel Sambuc // Append the local results to the total results if necessary. 1516*f4a2713aSLionel Sambuc if (UseLocal) { 1517*f4a2713aSLionel Sambuc R.addAllDecls(LocalR); 1518*f4a2713aSLionel Sambuc LocalR.clear(); 1519*f4a2713aSLionel Sambuc } 1520*f4a2713aSLionel Sambuc } 1521*f4a2713aSLionel Sambuc 1522*f4a2713aSLionel Sambuc // If we find names in this namespace, ignore its using directives. 1523*f4a2713aSLionel Sambuc if (FoundDirect) { 1524*f4a2713aSLionel Sambuc Found = true; 1525*f4a2713aSLionel Sambuc continue; 1526*f4a2713aSLionel Sambuc } 1527*f4a2713aSLionel Sambuc 1528*f4a2713aSLionel Sambuc for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) { 1529*f4a2713aSLionel Sambuc NamespaceDecl *Nom = (*I)->getNominatedNamespace(); 1530*f4a2713aSLionel Sambuc if (Visited.insert(Nom)) 1531*f4a2713aSLionel Sambuc Queue.push_back(Nom); 1532*f4a2713aSLionel Sambuc } 1533*f4a2713aSLionel Sambuc } 1534*f4a2713aSLionel Sambuc 1535*f4a2713aSLionel Sambuc if (Found) { 1536*f4a2713aSLionel Sambuc if (FoundTag && FoundNonTag) 1537*f4a2713aSLionel Sambuc R.setAmbiguousQualifiedTagHiding(); 1538*f4a2713aSLionel Sambuc else 1539*f4a2713aSLionel Sambuc R.resolveKind(); 1540*f4a2713aSLionel Sambuc } 1541*f4a2713aSLionel Sambuc 1542*f4a2713aSLionel Sambuc return Found; 1543*f4a2713aSLionel Sambuc } 1544*f4a2713aSLionel Sambuc 1545*f4a2713aSLionel Sambuc /// \brief Callback that looks for any member of a class with the given name. 1546*f4a2713aSLionel Sambuc static bool LookupAnyMember(const CXXBaseSpecifier *Specifier, 1547*f4a2713aSLionel Sambuc CXXBasePath &Path, 1548*f4a2713aSLionel Sambuc void *Name) { 1549*f4a2713aSLionel Sambuc RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); 1550*f4a2713aSLionel Sambuc 1551*f4a2713aSLionel Sambuc DeclarationName N = DeclarationName::getFromOpaquePtr(Name); 1552*f4a2713aSLionel Sambuc Path.Decls = BaseRecord->lookup(N); 1553*f4a2713aSLionel Sambuc return !Path.Decls.empty(); 1554*f4a2713aSLionel Sambuc } 1555*f4a2713aSLionel Sambuc 1556*f4a2713aSLionel Sambuc /// \brief Determine whether the given set of member declarations contains only 1557*f4a2713aSLionel Sambuc /// static members, nested types, and enumerators. 1558*f4a2713aSLionel Sambuc template<typename InputIterator> 1559*f4a2713aSLionel Sambuc static bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) { 1560*f4a2713aSLionel Sambuc Decl *D = (*First)->getUnderlyingDecl(); 1561*f4a2713aSLionel Sambuc if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D)) 1562*f4a2713aSLionel Sambuc return true; 1563*f4a2713aSLionel Sambuc 1564*f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(D)) { 1565*f4a2713aSLionel Sambuc // Determine whether all of the methods are static. 1566*f4a2713aSLionel Sambuc bool AllMethodsAreStatic = true; 1567*f4a2713aSLionel Sambuc for(; First != Last; ++First) { 1568*f4a2713aSLionel Sambuc D = (*First)->getUnderlyingDecl(); 1569*f4a2713aSLionel Sambuc 1570*f4a2713aSLionel Sambuc if (!isa<CXXMethodDecl>(D)) { 1571*f4a2713aSLionel Sambuc assert(isa<TagDecl>(D) && "Non-function must be a tag decl"); 1572*f4a2713aSLionel Sambuc break; 1573*f4a2713aSLionel Sambuc } 1574*f4a2713aSLionel Sambuc 1575*f4a2713aSLionel Sambuc if (!cast<CXXMethodDecl>(D)->isStatic()) { 1576*f4a2713aSLionel Sambuc AllMethodsAreStatic = false; 1577*f4a2713aSLionel Sambuc break; 1578*f4a2713aSLionel Sambuc } 1579*f4a2713aSLionel Sambuc } 1580*f4a2713aSLionel Sambuc 1581*f4a2713aSLionel Sambuc if (AllMethodsAreStatic) 1582*f4a2713aSLionel Sambuc return true; 1583*f4a2713aSLionel Sambuc } 1584*f4a2713aSLionel Sambuc 1585*f4a2713aSLionel Sambuc return false; 1586*f4a2713aSLionel Sambuc } 1587*f4a2713aSLionel Sambuc 1588*f4a2713aSLionel Sambuc /// \brief Perform qualified name lookup into a given context. 1589*f4a2713aSLionel Sambuc /// 1590*f4a2713aSLionel Sambuc /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find 1591*f4a2713aSLionel Sambuc /// names when the context of those names is explicit specified, e.g., 1592*f4a2713aSLionel Sambuc /// "std::vector" or "x->member", or as part of unqualified name lookup. 1593*f4a2713aSLionel Sambuc /// 1594*f4a2713aSLionel Sambuc /// Different lookup criteria can find different names. For example, a 1595*f4a2713aSLionel Sambuc /// particular scope can have both a struct and a function of the same 1596*f4a2713aSLionel Sambuc /// name, and each can be found by certain lookup criteria. For more 1597*f4a2713aSLionel Sambuc /// information about lookup criteria, see the documentation for the 1598*f4a2713aSLionel Sambuc /// class LookupCriteria. 1599*f4a2713aSLionel Sambuc /// 1600*f4a2713aSLionel Sambuc /// \param R captures both the lookup criteria and any lookup results found. 1601*f4a2713aSLionel Sambuc /// 1602*f4a2713aSLionel Sambuc /// \param LookupCtx The context in which qualified name lookup will 1603*f4a2713aSLionel Sambuc /// search. If the lookup criteria permits, name lookup may also search 1604*f4a2713aSLionel Sambuc /// in the parent contexts or (for C++ classes) base classes. 1605*f4a2713aSLionel Sambuc /// 1606*f4a2713aSLionel Sambuc /// \param InUnqualifiedLookup true if this is qualified name lookup that 1607*f4a2713aSLionel Sambuc /// occurs as part of unqualified name lookup. 1608*f4a2713aSLionel Sambuc /// 1609*f4a2713aSLionel Sambuc /// \returns true if lookup succeeded, false if it failed. 1610*f4a2713aSLionel Sambuc bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, 1611*f4a2713aSLionel Sambuc bool InUnqualifiedLookup) { 1612*f4a2713aSLionel Sambuc assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); 1613*f4a2713aSLionel Sambuc 1614*f4a2713aSLionel Sambuc if (!R.getLookupName()) 1615*f4a2713aSLionel Sambuc return false; 1616*f4a2713aSLionel Sambuc 1617*f4a2713aSLionel Sambuc // Make sure that the declaration context is complete. 1618*f4a2713aSLionel Sambuc assert((!isa<TagDecl>(LookupCtx) || 1619*f4a2713aSLionel Sambuc LookupCtx->isDependentContext() || 1620*f4a2713aSLionel Sambuc cast<TagDecl>(LookupCtx)->isCompleteDefinition() || 1621*f4a2713aSLionel Sambuc cast<TagDecl>(LookupCtx)->isBeingDefined()) && 1622*f4a2713aSLionel Sambuc "Declaration context must already be complete!"); 1623*f4a2713aSLionel Sambuc 1624*f4a2713aSLionel Sambuc // Perform qualified name lookup into the LookupCtx. 1625*f4a2713aSLionel Sambuc if (LookupDirect(*this, R, LookupCtx)) { 1626*f4a2713aSLionel Sambuc R.resolveKind(); 1627*f4a2713aSLionel Sambuc if (isa<CXXRecordDecl>(LookupCtx)) 1628*f4a2713aSLionel Sambuc R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); 1629*f4a2713aSLionel Sambuc return true; 1630*f4a2713aSLionel Sambuc } 1631*f4a2713aSLionel Sambuc 1632*f4a2713aSLionel Sambuc // Don't descend into implied contexts for redeclarations. 1633*f4a2713aSLionel Sambuc // C++98 [namespace.qual]p6: 1634*f4a2713aSLionel Sambuc // In a declaration for a namespace member in which the 1635*f4a2713aSLionel Sambuc // declarator-id is a qualified-id, given that the qualified-id 1636*f4a2713aSLionel Sambuc // for the namespace member has the form 1637*f4a2713aSLionel Sambuc // nested-name-specifier unqualified-id 1638*f4a2713aSLionel Sambuc // the unqualified-id shall name a member of the namespace 1639*f4a2713aSLionel Sambuc // designated by the nested-name-specifier. 1640*f4a2713aSLionel Sambuc // See also [class.mfct]p5 and [class.static.data]p2. 1641*f4a2713aSLionel Sambuc if (R.isForRedeclaration()) 1642*f4a2713aSLionel Sambuc return false; 1643*f4a2713aSLionel Sambuc 1644*f4a2713aSLionel Sambuc // If this is a namespace, look it up in the implied namespaces. 1645*f4a2713aSLionel Sambuc if (LookupCtx->isFileContext()) 1646*f4a2713aSLionel Sambuc return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); 1647*f4a2713aSLionel Sambuc 1648*f4a2713aSLionel Sambuc // If this isn't a C++ class, we aren't allowed to look into base 1649*f4a2713aSLionel Sambuc // classes, we're done. 1650*f4a2713aSLionel Sambuc CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); 1651*f4a2713aSLionel Sambuc if (!LookupRec || !LookupRec->getDefinition()) 1652*f4a2713aSLionel Sambuc return false; 1653*f4a2713aSLionel Sambuc 1654*f4a2713aSLionel Sambuc // If we're performing qualified name lookup into a dependent class, 1655*f4a2713aSLionel Sambuc // then we are actually looking into a current instantiation. If we have any 1656*f4a2713aSLionel Sambuc // dependent base classes, then we either have to delay lookup until 1657*f4a2713aSLionel Sambuc // template instantiation time (at which point all bases will be available) 1658*f4a2713aSLionel Sambuc // or we have to fail. 1659*f4a2713aSLionel Sambuc if (!InUnqualifiedLookup && LookupRec->isDependentContext() && 1660*f4a2713aSLionel Sambuc LookupRec->hasAnyDependentBases()) { 1661*f4a2713aSLionel Sambuc R.setNotFoundInCurrentInstantiation(); 1662*f4a2713aSLionel Sambuc return false; 1663*f4a2713aSLionel Sambuc } 1664*f4a2713aSLionel Sambuc 1665*f4a2713aSLionel Sambuc // Perform lookup into our base classes. 1666*f4a2713aSLionel Sambuc CXXBasePaths Paths; 1667*f4a2713aSLionel Sambuc Paths.setOrigin(LookupRec); 1668*f4a2713aSLionel Sambuc 1669*f4a2713aSLionel Sambuc // Look for this member in our base classes 1670*f4a2713aSLionel Sambuc CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0; 1671*f4a2713aSLionel Sambuc switch (R.getLookupKind()) { 1672*f4a2713aSLionel Sambuc case LookupObjCImplicitSelfParam: 1673*f4a2713aSLionel Sambuc case LookupOrdinaryName: 1674*f4a2713aSLionel Sambuc case LookupMemberName: 1675*f4a2713aSLionel Sambuc case LookupRedeclarationWithLinkage: 1676*f4a2713aSLionel Sambuc case LookupLocalFriendName: 1677*f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindOrdinaryMember; 1678*f4a2713aSLionel Sambuc break; 1679*f4a2713aSLionel Sambuc 1680*f4a2713aSLionel Sambuc case LookupTagName: 1681*f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindTagMember; 1682*f4a2713aSLionel Sambuc break; 1683*f4a2713aSLionel Sambuc 1684*f4a2713aSLionel Sambuc case LookupAnyName: 1685*f4a2713aSLionel Sambuc BaseCallback = &LookupAnyMember; 1686*f4a2713aSLionel Sambuc break; 1687*f4a2713aSLionel Sambuc 1688*f4a2713aSLionel Sambuc case LookupUsingDeclName: 1689*f4a2713aSLionel Sambuc // This lookup is for redeclarations only. 1690*f4a2713aSLionel Sambuc 1691*f4a2713aSLionel Sambuc case LookupOperatorName: 1692*f4a2713aSLionel Sambuc case LookupNamespaceName: 1693*f4a2713aSLionel Sambuc case LookupObjCProtocolName: 1694*f4a2713aSLionel Sambuc case LookupLabel: 1695*f4a2713aSLionel Sambuc // These lookups will never find a member in a C++ class (or base class). 1696*f4a2713aSLionel Sambuc return false; 1697*f4a2713aSLionel Sambuc 1698*f4a2713aSLionel Sambuc case LookupNestedNameSpecifierName: 1699*f4a2713aSLionel Sambuc BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; 1700*f4a2713aSLionel Sambuc break; 1701*f4a2713aSLionel Sambuc } 1702*f4a2713aSLionel Sambuc 1703*f4a2713aSLionel Sambuc if (!LookupRec->lookupInBases(BaseCallback, 1704*f4a2713aSLionel Sambuc R.getLookupName().getAsOpaquePtr(), Paths)) 1705*f4a2713aSLionel Sambuc return false; 1706*f4a2713aSLionel Sambuc 1707*f4a2713aSLionel Sambuc R.setNamingClass(LookupRec); 1708*f4a2713aSLionel Sambuc 1709*f4a2713aSLionel Sambuc // C++ [class.member.lookup]p2: 1710*f4a2713aSLionel Sambuc // [...] If the resulting set of declarations are not all from 1711*f4a2713aSLionel Sambuc // sub-objects of the same type, or the set has a nonstatic member 1712*f4a2713aSLionel Sambuc // and includes members from distinct sub-objects, there is an 1713*f4a2713aSLionel Sambuc // ambiguity and the program is ill-formed. Otherwise that set is 1714*f4a2713aSLionel Sambuc // the result of the lookup. 1715*f4a2713aSLionel Sambuc QualType SubobjectType; 1716*f4a2713aSLionel Sambuc int SubobjectNumber = 0; 1717*f4a2713aSLionel Sambuc AccessSpecifier SubobjectAccess = AS_none; 1718*f4a2713aSLionel Sambuc 1719*f4a2713aSLionel Sambuc for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); 1720*f4a2713aSLionel Sambuc Path != PathEnd; ++Path) { 1721*f4a2713aSLionel Sambuc const CXXBasePathElement &PathElement = Path->back(); 1722*f4a2713aSLionel Sambuc 1723*f4a2713aSLionel Sambuc // Pick the best (i.e. most permissive i.e. numerically lowest) access 1724*f4a2713aSLionel Sambuc // across all paths. 1725*f4a2713aSLionel Sambuc SubobjectAccess = std::min(SubobjectAccess, Path->Access); 1726*f4a2713aSLionel Sambuc 1727*f4a2713aSLionel Sambuc // Determine whether we're looking at a distinct sub-object or not. 1728*f4a2713aSLionel Sambuc if (SubobjectType.isNull()) { 1729*f4a2713aSLionel Sambuc // This is the first subobject we've looked at. Record its type. 1730*f4a2713aSLionel Sambuc SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); 1731*f4a2713aSLionel Sambuc SubobjectNumber = PathElement.SubobjectNumber; 1732*f4a2713aSLionel Sambuc continue; 1733*f4a2713aSLionel Sambuc } 1734*f4a2713aSLionel Sambuc 1735*f4a2713aSLionel Sambuc if (SubobjectType 1736*f4a2713aSLionel Sambuc != Context.getCanonicalType(PathElement.Base->getType())) { 1737*f4a2713aSLionel Sambuc // We found members of the given name in two subobjects of 1738*f4a2713aSLionel Sambuc // different types. If the declaration sets aren't the same, this 1739*f4a2713aSLionel Sambuc // this lookup is ambiguous. 1740*f4a2713aSLionel Sambuc if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) { 1741*f4a2713aSLionel Sambuc CXXBasePaths::paths_iterator FirstPath = Paths.begin(); 1742*f4a2713aSLionel Sambuc DeclContext::lookup_iterator FirstD = FirstPath->Decls.begin(); 1743*f4a2713aSLionel Sambuc DeclContext::lookup_iterator CurrentD = Path->Decls.begin(); 1744*f4a2713aSLionel Sambuc 1745*f4a2713aSLionel Sambuc while (FirstD != FirstPath->Decls.end() && 1746*f4a2713aSLionel Sambuc CurrentD != Path->Decls.end()) { 1747*f4a2713aSLionel Sambuc if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() != 1748*f4a2713aSLionel Sambuc (*CurrentD)->getUnderlyingDecl()->getCanonicalDecl()) 1749*f4a2713aSLionel Sambuc break; 1750*f4a2713aSLionel Sambuc 1751*f4a2713aSLionel Sambuc ++FirstD; 1752*f4a2713aSLionel Sambuc ++CurrentD; 1753*f4a2713aSLionel Sambuc } 1754*f4a2713aSLionel Sambuc 1755*f4a2713aSLionel Sambuc if (FirstD == FirstPath->Decls.end() && 1756*f4a2713aSLionel Sambuc CurrentD == Path->Decls.end()) 1757*f4a2713aSLionel Sambuc continue; 1758*f4a2713aSLionel Sambuc } 1759*f4a2713aSLionel Sambuc 1760*f4a2713aSLionel Sambuc R.setAmbiguousBaseSubobjectTypes(Paths); 1761*f4a2713aSLionel Sambuc return true; 1762*f4a2713aSLionel Sambuc } 1763*f4a2713aSLionel Sambuc 1764*f4a2713aSLionel Sambuc if (SubobjectNumber != PathElement.SubobjectNumber) { 1765*f4a2713aSLionel Sambuc // We have a different subobject of the same type. 1766*f4a2713aSLionel Sambuc 1767*f4a2713aSLionel Sambuc // C++ [class.member.lookup]p5: 1768*f4a2713aSLionel Sambuc // A static member, a nested type or an enumerator defined in 1769*f4a2713aSLionel Sambuc // a base class T can unambiguously be found even if an object 1770*f4a2713aSLionel Sambuc // has more than one base class subobject of type T. 1771*f4a2713aSLionel Sambuc if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) 1772*f4a2713aSLionel Sambuc continue; 1773*f4a2713aSLionel Sambuc 1774*f4a2713aSLionel Sambuc // We have found a nonstatic member name in multiple, distinct 1775*f4a2713aSLionel Sambuc // subobjects. Name lookup is ambiguous. 1776*f4a2713aSLionel Sambuc R.setAmbiguousBaseSubobjects(Paths); 1777*f4a2713aSLionel Sambuc return true; 1778*f4a2713aSLionel Sambuc } 1779*f4a2713aSLionel Sambuc } 1780*f4a2713aSLionel Sambuc 1781*f4a2713aSLionel Sambuc // Lookup in a base class succeeded; return these results. 1782*f4a2713aSLionel Sambuc 1783*f4a2713aSLionel Sambuc DeclContext::lookup_result DR = Paths.front().Decls; 1784*f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator I = DR.begin(), E = DR.end(); I != E; ++I) { 1785*f4a2713aSLionel Sambuc NamedDecl *D = *I; 1786*f4a2713aSLionel Sambuc AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, 1787*f4a2713aSLionel Sambuc D->getAccess()); 1788*f4a2713aSLionel Sambuc R.addDecl(D, AS); 1789*f4a2713aSLionel Sambuc } 1790*f4a2713aSLionel Sambuc R.resolveKind(); 1791*f4a2713aSLionel Sambuc return true; 1792*f4a2713aSLionel Sambuc } 1793*f4a2713aSLionel Sambuc 1794*f4a2713aSLionel Sambuc /// @brief Performs name lookup for a name that was parsed in the 1795*f4a2713aSLionel Sambuc /// source code, and may contain a C++ scope specifier. 1796*f4a2713aSLionel Sambuc /// 1797*f4a2713aSLionel Sambuc /// This routine is a convenience routine meant to be called from 1798*f4a2713aSLionel Sambuc /// contexts that receive a name and an optional C++ scope specifier 1799*f4a2713aSLionel Sambuc /// (e.g., "N::M::x"). It will then perform either qualified or 1800*f4a2713aSLionel Sambuc /// unqualified name lookup (with LookupQualifiedName or LookupName, 1801*f4a2713aSLionel Sambuc /// respectively) on the given name and return those results. 1802*f4a2713aSLionel Sambuc /// 1803*f4a2713aSLionel Sambuc /// @param S The scope from which unqualified name lookup will 1804*f4a2713aSLionel Sambuc /// begin. 1805*f4a2713aSLionel Sambuc /// 1806*f4a2713aSLionel Sambuc /// @param SS An optional C++ scope-specifier, e.g., "::N::M". 1807*f4a2713aSLionel Sambuc /// 1808*f4a2713aSLionel Sambuc /// @param EnteringContext Indicates whether we are going to enter the 1809*f4a2713aSLionel Sambuc /// context of the scope-specifier SS (if present). 1810*f4a2713aSLionel Sambuc /// 1811*f4a2713aSLionel Sambuc /// @returns True if any decls were found (but possibly ambiguous) 1812*f4a2713aSLionel Sambuc bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, 1813*f4a2713aSLionel Sambuc bool AllowBuiltinCreation, bool EnteringContext) { 1814*f4a2713aSLionel Sambuc if (SS && SS->isInvalid()) { 1815*f4a2713aSLionel Sambuc // When the scope specifier is invalid, don't even look for 1816*f4a2713aSLionel Sambuc // anything. 1817*f4a2713aSLionel Sambuc return false; 1818*f4a2713aSLionel Sambuc } 1819*f4a2713aSLionel Sambuc 1820*f4a2713aSLionel Sambuc if (SS && SS->isSet()) { 1821*f4a2713aSLionel Sambuc if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { 1822*f4a2713aSLionel Sambuc // We have resolved the scope specifier to a particular declaration 1823*f4a2713aSLionel Sambuc // contex, and will perform name lookup in that context. 1824*f4a2713aSLionel Sambuc if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) 1825*f4a2713aSLionel Sambuc return false; 1826*f4a2713aSLionel Sambuc 1827*f4a2713aSLionel Sambuc R.setContextRange(SS->getRange()); 1828*f4a2713aSLionel Sambuc return LookupQualifiedName(R, DC); 1829*f4a2713aSLionel Sambuc } 1830*f4a2713aSLionel Sambuc 1831*f4a2713aSLionel Sambuc // We could not resolve the scope specified to a specific declaration 1832*f4a2713aSLionel Sambuc // context, which means that SS refers to an unknown specialization. 1833*f4a2713aSLionel Sambuc // Name lookup can't find anything in this case. 1834*f4a2713aSLionel Sambuc R.setNotFoundInCurrentInstantiation(); 1835*f4a2713aSLionel Sambuc R.setContextRange(SS->getRange()); 1836*f4a2713aSLionel Sambuc return false; 1837*f4a2713aSLionel Sambuc } 1838*f4a2713aSLionel Sambuc 1839*f4a2713aSLionel Sambuc // Perform unqualified name lookup starting in the given scope. 1840*f4a2713aSLionel Sambuc return LookupName(R, S, AllowBuiltinCreation); 1841*f4a2713aSLionel Sambuc } 1842*f4a2713aSLionel Sambuc 1843*f4a2713aSLionel Sambuc 1844*f4a2713aSLionel Sambuc /// \brief Produce a diagnostic describing the ambiguity that resulted 1845*f4a2713aSLionel Sambuc /// from name lookup. 1846*f4a2713aSLionel Sambuc /// 1847*f4a2713aSLionel Sambuc /// \param Result The result of the ambiguous lookup to be diagnosed. 1848*f4a2713aSLionel Sambuc void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { 1849*f4a2713aSLionel Sambuc assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); 1850*f4a2713aSLionel Sambuc 1851*f4a2713aSLionel Sambuc DeclarationName Name = Result.getLookupName(); 1852*f4a2713aSLionel Sambuc SourceLocation NameLoc = Result.getNameLoc(); 1853*f4a2713aSLionel Sambuc SourceRange LookupRange = Result.getContextRange(); 1854*f4a2713aSLionel Sambuc 1855*f4a2713aSLionel Sambuc switch (Result.getAmbiguityKind()) { 1856*f4a2713aSLionel Sambuc case LookupResult::AmbiguousBaseSubobjects: { 1857*f4a2713aSLionel Sambuc CXXBasePaths *Paths = Result.getBasePaths(); 1858*f4a2713aSLionel Sambuc QualType SubobjectType = Paths->front().back().Base->getType(); 1859*f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) 1860*f4a2713aSLionel Sambuc << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) 1861*f4a2713aSLionel Sambuc << LookupRange; 1862*f4a2713aSLionel Sambuc 1863*f4a2713aSLionel Sambuc DeclContext::lookup_iterator Found = Paths->front().Decls.begin(); 1864*f4a2713aSLionel Sambuc while (isa<CXXMethodDecl>(*Found) && 1865*f4a2713aSLionel Sambuc cast<CXXMethodDecl>(*Found)->isStatic()) 1866*f4a2713aSLionel Sambuc ++Found; 1867*f4a2713aSLionel Sambuc 1868*f4a2713aSLionel Sambuc Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); 1869*f4a2713aSLionel Sambuc break; 1870*f4a2713aSLionel Sambuc } 1871*f4a2713aSLionel Sambuc 1872*f4a2713aSLionel Sambuc case LookupResult::AmbiguousBaseSubobjectTypes: { 1873*f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) 1874*f4a2713aSLionel Sambuc << Name << LookupRange; 1875*f4a2713aSLionel Sambuc 1876*f4a2713aSLionel Sambuc CXXBasePaths *Paths = Result.getBasePaths(); 1877*f4a2713aSLionel Sambuc std::set<Decl *> DeclsPrinted; 1878*f4a2713aSLionel Sambuc for (CXXBasePaths::paths_iterator Path = Paths->begin(), 1879*f4a2713aSLionel Sambuc PathEnd = Paths->end(); 1880*f4a2713aSLionel Sambuc Path != PathEnd; ++Path) { 1881*f4a2713aSLionel Sambuc Decl *D = Path->Decls.front(); 1882*f4a2713aSLionel Sambuc if (DeclsPrinted.insert(D).second) 1883*f4a2713aSLionel Sambuc Diag(D->getLocation(), diag::note_ambiguous_member_found); 1884*f4a2713aSLionel Sambuc } 1885*f4a2713aSLionel Sambuc break; 1886*f4a2713aSLionel Sambuc } 1887*f4a2713aSLionel Sambuc 1888*f4a2713aSLionel Sambuc case LookupResult::AmbiguousTagHiding: { 1889*f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; 1890*f4a2713aSLionel Sambuc 1891*f4a2713aSLionel Sambuc llvm::SmallPtrSet<NamedDecl*,8> TagDecls; 1892*f4a2713aSLionel Sambuc 1893*f4a2713aSLionel Sambuc LookupResult::iterator DI, DE = Result.end(); 1894*f4a2713aSLionel Sambuc for (DI = Result.begin(); DI != DE; ++DI) 1895*f4a2713aSLionel Sambuc if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { 1896*f4a2713aSLionel Sambuc TagDecls.insert(TD); 1897*f4a2713aSLionel Sambuc Diag(TD->getLocation(), diag::note_hidden_tag); 1898*f4a2713aSLionel Sambuc } 1899*f4a2713aSLionel Sambuc 1900*f4a2713aSLionel Sambuc for (DI = Result.begin(); DI != DE; ++DI) 1901*f4a2713aSLionel Sambuc if (!isa<TagDecl>(*DI)) 1902*f4a2713aSLionel Sambuc Diag((*DI)->getLocation(), diag::note_hiding_object); 1903*f4a2713aSLionel Sambuc 1904*f4a2713aSLionel Sambuc // For recovery purposes, go ahead and implement the hiding. 1905*f4a2713aSLionel Sambuc LookupResult::Filter F = Result.makeFilter(); 1906*f4a2713aSLionel Sambuc while (F.hasNext()) { 1907*f4a2713aSLionel Sambuc if (TagDecls.count(F.next())) 1908*f4a2713aSLionel Sambuc F.erase(); 1909*f4a2713aSLionel Sambuc } 1910*f4a2713aSLionel Sambuc F.done(); 1911*f4a2713aSLionel Sambuc break; 1912*f4a2713aSLionel Sambuc } 1913*f4a2713aSLionel Sambuc 1914*f4a2713aSLionel Sambuc case LookupResult::AmbiguousReference: { 1915*f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; 1916*f4a2713aSLionel Sambuc 1917*f4a2713aSLionel Sambuc LookupResult::iterator DI = Result.begin(), DE = Result.end(); 1918*f4a2713aSLionel Sambuc for (; DI != DE; ++DI) 1919*f4a2713aSLionel Sambuc Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; 1920*f4a2713aSLionel Sambuc break; 1921*f4a2713aSLionel Sambuc } 1922*f4a2713aSLionel Sambuc } 1923*f4a2713aSLionel Sambuc } 1924*f4a2713aSLionel Sambuc 1925*f4a2713aSLionel Sambuc namespace { 1926*f4a2713aSLionel Sambuc struct AssociatedLookup { 1927*f4a2713aSLionel Sambuc AssociatedLookup(Sema &S, SourceLocation InstantiationLoc, 1928*f4a2713aSLionel Sambuc Sema::AssociatedNamespaceSet &Namespaces, 1929*f4a2713aSLionel Sambuc Sema::AssociatedClassSet &Classes) 1930*f4a2713aSLionel Sambuc : S(S), Namespaces(Namespaces), Classes(Classes), 1931*f4a2713aSLionel Sambuc InstantiationLoc(InstantiationLoc) { 1932*f4a2713aSLionel Sambuc } 1933*f4a2713aSLionel Sambuc 1934*f4a2713aSLionel Sambuc Sema &S; 1935*f4a2713aSLionel Sambuc Sema::AssociatedNamespaceSet &Namespaces; 1936*f4a2713aSLionel Sambuc Sema::AssociatedClassSet &Classes; 1937*f4a2713aSLionel Sambuc SourceLocation InstantiationLoc; 1938*f4a2713aSLionel Sambuc }; 1939*f4a2713aSLionel Sambuc } 1940*f4a2713aSLionel Sambuc 1941*f4a2713aSLionel Sambuc static void 1942*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); 1943*f4a2713aSLionel Sambuc 1944*f4a2713aSLionel Sambuc static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, 1945*f4a2713aSLionel Sambuc DeclContext *Ctx) { 1946*f4a2713aSLionel Sambuc // Add the associated namespace for this class. 1947*f4a2713aSLionel Sambuc 1948*f4a2713aSLionel Sambuc // We don't use DeclContext::getEnclosingNamespaceContext() as this may 1949*f4a2713aSLionel Sambuc // be a locally scoped record. 1950*f4a2713aSLionel Sambuc 1951*f4a2713aSLionel Sambuc // We skip out of inline namespaces. The innermost non-inline namespace 1952*f4a2713aSLionel Sambuc // contains all names of all its nested inline namespaces anyway, so we can 1953*f4a2713aSLionel Sambuc // replace the entire inline namespace tree with its root. 1954*f4a2713aSLionel Sambuc while (Ctx->isRecord() || Ctx->isTransparentContext() || 1955*f4a2713aSLionel Sambuc Ctx->isInlineNamespace()) 1956*f4a2713aSLionel Sambuc Ctx = Ctx->getParent(); 1957*f4a2713aSLionel Sambuc 1958*f4a2713aSLionel Sambuc if (Ctx->isFileContext()) 1959*f4a2713aSLionel Sambuc Namespaces.insert(Ctx->getPrimaryContext()); 1960*f4a2713aSLionel Sambuc } 1961*f4a2713aSLionel Sambuc 1962*f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for argument-dependent 1963*f4a2713aSLionel Sambuc // lookup that involves a template argument (C++ [basic.lookup.koenig]p2). 1964*f4a2713aSLionel Sambuc static void 1965*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, 1966*f4a2713aSLionel Sambuc const TemplateArgument &Arg) { 1967*f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2, last bullet: 1968*f4a2713aSLionel Sambuc // -- [...] ; 1969*f4a2713aSLionel Sambuc switch (Arg.getKind()) { 1970*f4a2713aSLionel Sambuc case TemplateArgument::Null: 1971*f4a2713aSLionel Sambuc break; 1972*f4a2713aSLionel Sambuc 1973*f4a2713aSLionel Sambuc case TemplateArgument::Type: 1974*f4a2713aSLionel Sambuc // [...] the namespaces and classes associated with the types of the 1975*f4a2713aSLionel Sambuc // template arguments provided for template type parameters (excluding 1976*f4a2713aSLionel Sambuc // template template parameters) 1977*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); 1978*f4a2713aSLionel Sambuc break; 1979*f4a2713aSLionel Sambuc 1980*f4a2713aSLionel Sambuc case TemplateArgument::Template: 1981*f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion: { 1982*f4a2713aSLionel Sambuc // [...] the namespaces in which any template template arguments are 1983*f4a2713aSLionel Sambuc // defined; and the classes in which any member templates used as 1984*f4a2713aSLionel Sambuc // template template arguments are defined. 1985*f4a2713aSLionel Sambuc TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); 1986*f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTemplate 1987*f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { 1988*f4a2713aSLionel Sambuc DeclContext *Ctx = ClassTemplate->getDeclContext(); 1989*f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1990*f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass); 1991*f4a2713aSLionel Sambuc // Add the associated namespace for this class. 1992*f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx); 1993*f4a2713aSLionel Sambuc } 1994*f4a2713aSLionel Sambuc break; 1995*f4a2713aSLionel Sambuc } 1996*f4a2713aSLionel Sambuc 1997*f4a2713aSLionel Sambuc case TemplateArgument::Declaration: 1998*f4a2713aSLionel Sambuc case TemplateArgument::Integral: 1999*f4a2713aSLionel Sambuc case TemplateArgument::Expression: 2000*f4a2713aSLionel Sambuc case TemplateArgument::NullPtr: 2001*f4a2713aSLionel Sambuc // [Note: non-type template arguments do not contribute to the set of 2002*f4a2713aSLionel Sambuc // associated namespaces. ] 2003*f4a2713aSLionel Sambuc break; 2004*f4a2713aSLionel Sambuc 2005*f4a2713aSLionel Sambuc case TemplateArgument::Pack: 2006*f4a2713aSLionel Sambuc for (TemplateArgument::pack_iterator P = Arg.pack_begin(), 2007*f4a2713aSLionel Sambuc PEnd = Arg.pack_end(); 2008*f4a2713aSLionel Sambuc P != PEnd; ++P) 2009*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, *P); 2010*f4a2713aSLionel Sambuc break; 2011*f4a2713aSLionel Sambuc } 2012*f4a2713aSLionel Sambuc } 2013*f4a2713aSLionel Sambuc 2014*f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for 2015*f4a2713aSLionel Sambuc // argument-dependent lookup with an argument of class type 2016*f4a2713aSLionel Sambuc // (C++ [basic.lookup.koenig]p2). 2017*f4a2713aSLionel Sambuc static void 2018*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, 2019*f4a2713aSLionel Sambuc CXXRecordDecl *Class) { 2020*f4a2713aSLionel Sambuc 2021*f4a2713aSLionel Sambuc // Just silently ignore anything whose name is __va_list_tag. 2022*f4a2713aSLionel Sambuc if (Class->getDeclName() == Result.S.VAListTagName) 2023*f4a2713aSLionel Sambuc return; 2024*f4a2713aSLionel Sambuc 2025*f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2: 2026*f4a2713aSLionel Sambuc // [...] 2027*f4a2713aSLionel Sambuc // -- If T is a class type (including unions), its associated 2028*f4a2713aSLionel Sambuc // classes are: the class itself; the class of which it is a 2029*f4a2713aSLionel Sambuc // member, if any; and its direct and indirect base 2030*f4a2713aSLionel Sambuc // classes. Its associated namespaces are the namespaces in 2031*f4a2713aSLionel Sambuc // which its associated classes are defined. 2032*f4a2713aSLionel Sambuc 2033*f4a2713aSLionel Sambuc // Add the class of which it is a member, if any. 2034*f4a2713aSLionel Sambuc DeclContext *Ctx = Class->getDeclContext(); 2035*f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2036*f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass); 2037*f4a2713aSLionel Sambuc // Add the associated namespace for this class. 2038*f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx); 2039*f4a2713aSLionel Sambuc 2040*f4a2713aSLionel Sambuc // Add the class itself. If we've already seen this class, we don't 2041*f4a2713aSLionel Sambuc // need to visit base classes. 2042*f4a2713aSLionel Sambuc if (!Result.Classes.insert(Class)) 2043*f4a2713aSLionel Sambuc return; 2044*f4a2713aSLionel Sambuc 2045*f4a2713aSLionel Sambuc // -- If T is a template-id, its associated namespaces and classes are 2046*f4a2713aSLionel Sambuc // the namespace in which the template is defined; for member 2047*f4a2713aSLionel Sambuc // templates, the member template's class; the namespaces and classes 2048*f4a2713aSLionel Sambuc // associated with the types of the template arguments provided for 2049*f4a2713aSLionel Sambuc // template type parameters (excluding template template parameters); the 2050*f4a2713aSLionel Sambuc // namespaces in which any template template arguments are defined; and 2051*f4a2713aSLionel Sambuc // the classes in which any member templates used as template template 2052*f4a2713aSLionel Sambuc // arguments are defined. [Note: non-type template arguments do not 2053*f4a2713aSLionel Sambuc // contribute to the set of associated namespaces. ] 2054*f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec 2055*f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { 2056*f4a2713aSLionel Sambuc DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); 2057*f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2058*f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass); 2059*f4a2713aSLionel Sambuc // Add the associated namespace for this class. 2060*f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx); 2061*f4a2713aSLionel Sambuc 2062*f4a2713aSLionel Sambuc const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 2063*f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 2064*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); 2065*f4a2713aSLionel Sambuc } 2066*f4a2713aSLionel Sambuc 2067*f4a2713aSLionel Sambuc // Only recurse into base classes for complete types. 2068*f4a2713aSLionel Sambuc if (!Class->hasDefinition()) { 2069*f4a2713aSLionel Sambuc QualType type = Result.S.Context.getTypeDeclType(Class); 2070*f4a2713aSLionel Sambuc if (Result.S.RequireCompleteType(Result.InstantiationLoc, type, 2071*f4a2713aSLionel Sambuc /*no diagnostic*/ 0)) 2072*f4a2713aSLionel Sambuc return; 2073*f4a2713aSLionel Sambuc } 2074*f4a2713aSLionel Sambuc 2075*f4a2713aSLionel Sambuc // Add direct and indirect base classes along with their associated 2076*f4a2713aSLionel Sambuc // namespaces. 2077*f4a2713aSLionel Sambuc SmallVector<CXXRecordDecl *, 32> Bases; 2078*f4a2713aSLionel Sambuc Bases.push_back(Class); 2079*f4a2713aSLionel Sambuc while (!Bases.empty()) { 2080*f4a2713aSLionel Sambuc // Pop this class off the stack. 2081*f4a2713aSLionel Sambuc Class = Bases.pop_back_val(); 2082*f4a2713aSLionel Sambuc 2083*f4a2713aSLionel Sambuc // Visit the base classes. 2084*f4a2713aSLionel Sambuc for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), 2085*f4a2713aSLionel Sambuc BaseEnd = Class->bases_end(); 2086*f4a2713aSLionel Sambuc Base != BaseEnd; ++Base) { 2087*f4a2713aSLionel Sambuc const RecordType *BaseType = Base->getType()->getAs<RecordType>(); 2088*f4a2713aSLionel Sambuc // In dependent contexts, we do ADL twice, and the first time around, 2089*f4a2713aSLionel Sambuc // the base type might be a dependent TemplateSpecializationType, or a 2090*f4a2713aSLionel Sambuc // TemplateTypeParmType. If that happens, simply ignore it. 2091*f4a2713aSLionel Sambuc // FIXME: If we want to support export, we probably need to add the 2092*f4a2713aSLionel Sambuc // namespace of the template in a TemplateSpecializationType, or even 2093*f4a2713aSLionel Sambuc // the classes and namespaces of known non-dependent arguments. 2094*f4a2713aSLionel Sambuc if (!BaseType) 2095*f4a2713aSLionel Sambuc continue; 2096*f4a2713aSLionel Sambuc CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); 2097*f4a2713aSLionel Sambuc if (Result.Classes.insert(BaseDecl)) { 2098*f4a2713aSLionel Sambuc // Find the associated namespace for this base class. 2099*f4a2713aSLionel Sambuc DeclContext *BaseCtx = BaseDecl->getDeclContext(); 2100*f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, BaseCtx); 2101*f4a2713aSLionel Sambuc 2102*f4a2713aSLionel Sambuc // Make sure we visit the bases of this base class. 2103*f4a2713aSLionel Sambuc if (BaseDecl->bases_begin() != BaseDecl->bases_end()) 2104*f4a2713aSLionel Sambuc Bases.push_back(BaseDecl); 2105*f4a2713aSLionel Sambuc } 2106*f4a2713aSLionel Sambuc } 2107*f4a2713aSLionel Sambuc } 2108*f4a2713aSLionel Sambuc } 2109*f4a2713aSLionel Sambuc 2110*f4a2713aSLionel Sambuc // \brief Add the associated classes and namespaces for 2111*f4a2713aSLionel Sambuc // argument-dependent lookup with an argument of type T 2112*f4a2713aSLionel Sambuc // (C++ [basic.lookup.koenig]p2). 2113*f4a2713aSLionel Sambuc static void 2114*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { 2115*f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2: 2116*f4a2713aSLionel Sambuc // 2117*f4a2713aSLionel Sambuc // For each argument type T in the function call, there is a set 2118*f4a2713aSLionel Sambuc // of zero or more associated namespaces and a set of zero or more 2119*f4a2713aSLionel Sambuc // associated classes to be considered. The sets of namespaces and 2120*f4a2713aSLionel Sambuc // classes is determined entirely by the types of the function 2121*f4a2713aSLionel Sambuc // arguments (and the namespace of any template template 2122*f4a2713aSLionel Sambuc // argument). Typedef names and using-declarations used to specify 2123*f4a2713aSLionel Sambuc // the types do not contribute to this set. The sets of namespaces 2124*f4a2713aSLionel Sambuc // and classes are determined in the following way: 2125*f4a2713aSLionel Sambuc 2126*f4a2713aSLionel Sambuc SmallVector<const Type *, 16> Queue; 2127*f4a2713aSLionel Sambuc const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); 2128*f4a2713aSLionel Sambuc 2129*f4a2713aSLionel Sambuc while (true) { 2130*f4a2713aSLionel Sambuc switch (T->getTypeClass()) { 2131*f4a2713aSLionel Sambuc 2132*f4a2713aSLionel Sambuc #define TYPE(Class, Base) 2133*f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(Class, Base) case Type::Class: 2134*f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2135*f4a2713aSLionel Sambuc #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2136*f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base) 2137*f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def" 2138*f4a2713aSLionel Sambuc // T is canonical. We can also ignore dependent types because 2139*f4a2713aSLionel Sambuc // we don't need to do ADL at the definition point, but if we 2140*f4a2713aSLionel Sambuc // wanted to implement template export (or if we find some other 2141*f4a2713aSLionel Sambuc // use for associated classes and namespaces...) this would be 2142*f4a2713aSLionel Sambuc // wrong. 2143*f4a2713aSLionel Sambuc break; 2144*f4a2713aSLionel Sambuc 2145*f4a2713aSLionel Sambuc // -- If T is a pointer to U or an array of U, its associated 2146*f4a2713aSLionel Sambuc // namespaces and classes are those associated with U. 2147*f4a2713aSLionel Sambuc case Type::Pointer: 2148*f4a2713aSLionel Sambuc T = cast<PointerType>(T)->getPointeeType().getTypePtr(); 2149*f4a2713aSLionel Sambuc continue; 2150*f4a2713aSLionel Sambuc case Type::ConstantArray: 2151*f4a2713aSLionel Sambuc case Type::IncompleteArray: 2152*f4a2713aSLionel Sambuc case Type::VariableArray: 2153*f4a2713aSLionel Sambuc T = cast<ArrayType>(T)->getElementType().getTypePtr(); 2154*f4a2713aSLionel Sambuc continue; 2155*f4a2713aSLionel Sambuc 2156*f4a2713aSLionel Sambuc // -- If T is a fundamental type, its associated sets of 2157*f4a2713aSLionel Sambuc // namespaces and classes are both empty. 2158*f4a2713aSLionel Sambuc case Type::Builtin: 2159*f4a2713aSLionel Sambuc break; 2160*f4a2713aSLionel Sambuc 2161*f4a2713aSLionel Sambuc // -- If T is a class type (including unions), its associated 2162*f4a2713aSLionel Sambuc // classes are: the class itself; the class of which it is a 2163*f4a2713aSLionel Sambuc // member, if any; and its direct and indirect base 2164*f4a2713aSLionel Sambuc // classes. Its associated namespaces are the namespaces in 2165*f4a2713aSLionel Sambuc // which its associated classes are defined. 2166*f4a2713aSLionel Sambuc case Type::Record: { 2167*f4a2713aSLionel Sambuc CXXRecordDecl *Class 2168*f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); 2169*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Class); 2170*f4a2713aSLionel Sambuc break; 2171*f4a2713aSLionel Sambuc } 2172*f4a2713aSLionel Sambuc 2173*f4a2713aSLionel Sambuc // -- If T is an enumeration type, its associated namespace is 2174*f4a2713aSLionel Sambuc // the namespace in which it is defined. If it is class 2175*f4a2713aSLionel Sambuc // member, its associated class is the member's class; else 2176*f4a2713aSLionel Sambuc // it has no associated class. 2177*f4a2713aSLionel Sambuc case Type::Enum: { 2178*f4a2713aSLionel Sambuc EnumDecl *Enum = cast<EnumType>(T)->getDecl(); 2179*f4a2713aSLionel Sambuc 2180*f4a2713aSLionel Sambuc DeclContext *Ctx = Enum->getDeclContext(); 2181*f4a2713aSLionel Sambuc if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2182*f4a2713aSLionel Sambuc Result.Classes.insert(EnclosingClass); 2183*f4a2713aSLionel Sambuc 2184*f4a2713aSLionel Sambuc // Add the associated namespace for this class. 2185*f4a2713aSLionel Sambuc CollectEnclosingNamespace(Result.Namespaces, Ctx); 2186*f4a2713aSLionel Sambuc 2187*f4a2713aSLionel Sambuc break; 2188*f4a2713aSLionel Sambuc } 2189*f4a2713aSLionel Sambuc 2190*f4a2713aSLionel Sambuc // -- If T is a function type, its associated namespaces and 2191*f4a2713aSLionel Sambuc // classes are those associated with the function parameter 2192*f4a2713aSLionel Sambuc // types and those associated with the return type. 2193*f4a2713aSLionel Sambuc case Type::FunctionProto: { 2194*f4a2713aSLionel Sambuc const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 2195*f4a2713aSLionel Sambuc for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), 2196*f4a2713aSLionel Sambuc ArgEnd = Proto->arg_type_end(); 2197*f4a2713aSLionel Sambuc Arg != ArgEnd; ++Arg) 2198*f4a2713aSLionel Sambuc Queue.push_back(Arg->getTypePtr()); 2199*f4a2713aSLionel Sambuc // fallthrough 2200*f4a2713aSLionel Sambuc } 2201*f4a2713aSLionel Sambuc case Type::FunctionNoProto: { 2202*f4a2713aSLionel Sambuc const FunctionType *FnType = cast<FunctionType>(T); 2203*f4a2713aSLionel Sambuc T = FnType->getResultType().getTypePtr(); 2204*f4a2713aSLionel Sambuc continue; 2205*f4a2713aSLionel Sambuc } 2206*f4a2713aSLionel Sambuc 2207*f4a2713aSLionel Sambuc // -- If T is a pointer to a member function of a class X, its 2208*f4a2713aSLionel Sambuc // associated namespaces and classes are those associated 2209*f4a2713aSLionel Sambuc // with the function parameter types and return type, 2210*f4a2713aSLionel Sambuc // together with those associated with X. 2211*f4a2713aSLionel Sambuc // 2212*f4a2713aSLionel Sambuc // -- If T is a pointer to a data member of class X, its 2213*f4a2713aSLionel Sambuc // associated namespaces and classes are those associated 2214*f4a2713aSLionel Sambuc // with the member type together with those associated with 2215*f4a2713aSLionel Sambuc // X. 2216*f4a2713aSLionel Sambuc case Type::MemberPointer: { 2217*f4a2713aSLionel Sambuc const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); 2218*f4a2713aSLionel Sambuc 2219*f4a2713aSLionel Sambuc // Queue up the class type into which this points. 2220*f4a2713aSLionel Sambuc Queue.push_back(MemberPtr->getClass()); 2221*f4a2713aSLionel Sambuc 2222*f4a2713aSLionel Sambuc // And directly continue with the pointee type. 2223*f4a2713aSLionel Sambuc T = MemberPtr->getPointeeType().getTypePtr(); 2224*f4a2713aSLionel Sambuc continue; 2225*f4a2713aSLionel Sambuc } 2226*f4a2713aSLionel Sambuc 2227*f4a2713aSLionel Sambuc // As an extension, treat this like a normal pointer. 2228*f4a2713aSLionel Sambuc case Type::BlockPointer: 2229*f4a2713aSLionel Sambuc T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); 2230*f4a2713aSLionel Sambuc continue; 2231*f4a2713aSLionel Sambuc 2232*f4a2713aSLionel Sambuc // References aren't covered by the standard, but that's such an 2233*f4a2713aSLionel Sambuc // obvious defect that we cover them anyway. 2234*f4a2713aSLionel Sambuc case Type::LValueReference: 2235*f4a2713aSLionel Sambuc case Type::RValueReference: 2236*f4a2713aSLionel Sambuc T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); 2237*f4a2713aSLionel Sambuc continue; 2238*f4a2713aSLionel Sambuc 2239*f4a2713aSLionel Sambuc // These are fundamental types. 2240*f4a2713aSLionel Sambuc case Type::Vector: 2241*f4a2713aSLionel Sambuc case Type::ExtVector: 2242*f4a2713aSLionel Sambuc case Type::Complex: 2243*f4a2713aSLionel Sambuc break; 2244*f4a2713aSLionel Sambuc 2245*f4a2713aSLionel Sambuc // Non-deduced auto types only get here for error cases. 2246*f4a2713aSLionel Sambuc case Type::Auto: 2247*f4a2713aSLionel Sambuc break; 2248*f4a2713aSLionel Sambuc 2249*f4a2713aSLionel Sambuc // If T is an Objective-C object or interface type, or a pointer to an 2250*f4a2713aSLionel Sambuc // object or interface type, the associated namespace is the global 2251*f4a2713aSLionel Sambuc // namespace. 2252*f4a2713aSLionel Sambuc case Type::ObjCObject: 2253*f4a2713aSLionel Sambuc case Type::ObjCInterface: 2254*f4a2713aSLionel Sambuc case Type::ObjCObjectPointer: 2255*f4a2713aSLionel Sambuc Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl()); 2256*f4a2713aSLionel Sambuc break; 2257*f4a2713aSLionel Sambuc 2258*f4a2713aSLionel Sambuc // Atomic types are just wrappers; use the associations of the 2259*f4a2713aSLionel Sambuc // contained type. 2260*f4a2713aSLionel Sambuc case Type::Atomic: 2261*f4a2713aSLionel Sambuc T = cast<AtomicType>(T)->getValueType().getTypePtr(); 2262*f4a2713aSLionel Sambuc continue; 2263*f4a2713aSLionel Sambuc } 2264*f4a2713aSLionel Sambuc 2265*f4a2713aSLionel Sambuc if (Queue.empty()) 2266*f4a2713aSLionel Sambuc break; 2267*f4a2713aSLionel Sambuc T = Queue.pop_back_val(); 2268*f4a2713aSLionel Sambuc } 2269*f4a2713aSLionel Sambuc } 2270*f4a2713aSLionel Sambuc 2271*f4a2713aSLionel Sambuc /// \brief Find the associated classes and namespaces for 2272*f4a2713aSLionel Sambuc /// argument-dependent lookup for a call with the given set of 2273*f4a2713aSLionel Sambuc /// arguments. 2274*f4a2713aSLionel Sambuc /// 2275*f4a2713aSLionel Sambuc /// This routine computes the sets of associated classes and associated 2276*f4a2713aSLionel Sambuc /// namespaces searched by argument-dependent lookup 2277*f4a2713aSLionel Sambuc /// (C++ [basic.lookup.argdep]) for a given set of arguments. 2278*f4a2713aSLionel Sambuc void Sema::FindAssociatedClassesAndNamespaces( 2279*f4a2713aSLionel Sambuc SourceLocation InstantiationLoc, ArrayRef<Expr *> Args, 2280*f4a2713aSLionel Sambuc AssociatedNamespaceSet &AssociatedNamespaces, 2281*f4a2713aSLionel Sambuc AssociatedClassSet &AssociatedClasses) { 2282*f4a2713aSLionel Sambuc AssociatedNamespaces.clear(); 2283*f4a2713aSLionel Sambuc AssociatedClasses.clear(); 2284*f4a2713aSLionel Sambuc 2285*f4a2713aSLionel Sambuc AssociatedLookup Result(*this, InstantiationLoc, 2286*f4a2713aSLionel Sambuc AssociatedNamespaces, AssociatedClasses); 2287*f4a2713aSLionel Sambuc 2288*f4a2713aSLionel Sambuc // C++ [basic.lookup.koenig]p2: 2289*f4a2713aSLionel Sambuc // For each argument type T in the function call, there is a set 2290*f4a2713aSLionel Sambuc // of zero or more associated namespaces and a set of zero or more 2291*f4a2713aSLionel Sambuc // associated classes to be considered. The sets of namespaces and 2292*f4a2713aSLionel Sambuc // classes is determined entirely by the types of the function 2293*f4a2713aSLionel Sambuc // arguments (and the namespace of any template template 2294*f4a2713aSLionel Sambuc // argument). 2295*f4a2713aSLionel Sambuc for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { 2296*f4a2713aSLionel Sambuc Expr *Arg = Args[ArgIdx]; 2297*f4a2713aSLionel Sambuc 2298*f4a2713aSLionel Sambuc if (Arg->getType() != Context.OverloadTy) { 2299*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, Arg->getType()); 2300*f4a2713aSLionel Sambuc continue; 2301*f4a2713aSLionel Sambuc } 2302*f4a2713aSLionel Sambuc 2303*f4a2713aSLionel Sambuc // [...] In addition, if the argument is the name or address of a 2304*f4a2713aSLionel Sambuc // set of overloaded functions and/or function templates, its 2305*f4a2713aSLionel Sambuc // associated classes and namespaces are the union of those 2306*f4a2713aSLionel Sambuc // associated with each of the members of the set: the namespace 2307*f4a2713aSLionel Sambuc // in which the function or function template is defined and the 2308*f4a2713aSLionel Sambuc // classes and namespaces associated with its (non-dependent) 2309*f4a2713aSLionel Sambuc // parameter types and return type. 2310*f4a2713aSLionel Sambuc Arg = Arg->IgnoreParens(); 2311*f4a2713aSLionel Sambuc if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) 2312*f4a2713aSLionel Sambuc if (unaryOp->getOpcode() == UO_AddrOf) 2313*f4a2713aSLionel Sambuc Arg = unaryOp->getSubExpr(); 2314*f4a2713aSLionel Sambuc 2315*f4a2713aSLionel Sambuc UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg); 2316*f4a2713aSLionel Sambuc if (!ULE) continue; 2317*f4a2713aSLionel Sambuc 2318*f4a2713aSLionel Sambuc for (UnresolvedSetIterator I = ULE->decls_begin(), E = ULE->decls_end(); 2319*f4a2713aSLionel Sambuc I != E; ++I) { 2320*f4a2713aSLionel Sambuc // Look through any using declarations to find the underlying function. 2321*f4a2713aSLionel Sambuc NamedDecl *Fn = (*I)->getUnderlyingDecl(); 2322*f4a2713aSLionel Sambuc 2323*f4a2713aSLionel Sambuc FunctionDecl *FDecl = dyn_cast<FunctionDecl>(Fn); 2324*f4a2713aSLionel Sambuc if (!FDecl) 2325*f4a2713aSLionel Sambuc FDecl = cast<FunctionTemplateDecl>(Fn)->getTemplatedDecl(); 2326*f4a2713aSLionel Sambuc 2327*f4a2713aSLionel Sambuc // Add the classes and namespaces associated with the parameter 2328*f4a2713aSLionel Sambuc // types and return type of this function. 2329*f4a2713aSLionel Sambuc addAssociatedClassesAndNamespaces(Result, FDecl->getType()); 2330*f4a2713aSLionel Sambuc } 2331*f4a2713aSLionel Sambuc } 2332*f4a2713aSLionel Sambuc } 2333*f4a2713aSLionel Sambuc 2334*f4a2713aSLionel Sambuc /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is 2335*f4a2713aSLionel Sambuc /// an acceptable non-member overloaded operator for a call whose 2336*f4a2713aSLionel Sambuc /// arguments have types T1 (and, if non-empty, T2). This routine 2337*f4a2713aSLionel Sambuc /// implements the check in C++ [over.match.oper]p3b2 concerning 2338*f4a2713aSLionel Sambuc /// enumeration types. 2339*f4a2713aSLionel Sambuc static bool 2340*f4a2713aSLionel Sambuc IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, 2341*f4a2713aSLionel Sambuc QualType T1, QualType T2, 2342*f4a2713aSLionel Sambuc ASTContext &Context) { 2343*f4a2713aSLionel Sambuc if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) 2344*f4a2713aSLionel Sambuc return true; 2345*f4a2713aSLionel Sambuc 2346*f4a2713aSLionel Sambuc if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) 2347*f4a2713aSLionel Sambuc return true; 2348*f4a2713aSLionel Sambuc 2349*f4a2713aSLionel Sambuc const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); 2350*f4a2713aSLionel Sambuc if (Proto->getNumArgs() < 1) 2351*f4a2713aSLionel Sambuc return false; 2352*f4a2713aSLionel Sambuc 2353*f4a2713aSLionel Sambuc if (T1->isEnumeralType()) { 2354*f4a2713aSLionel Sambuc QualType ArgType = Proto->getArgType(0).getNonReferenceType(); 2355*f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(T1, ArgType)) 2356*f4a2713aSLionel Sambuc return true; 2357*f4a2713aSLionel Sambuc } 2358*f4a2713aSLionel Sambuc 2359*f4a2713aSLionel Sambuc if (Proto->getNumArgs() < 2) 2360*f4a2713aSLionel Sambuc return false; 2361*f4a2713aSLionel Sambuc 2362*f4a2713aSLionel Sambuc if (!T2.isNull() && T2->isEnumeralType()) { 2363*f4a2713aSLionel Sambuc QualType ArgType = Proto->getArgType(1).getNonReferenceType(); 2364*f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(T2, ArgType)) 2365*f4a2713aSLionel Sambuc return true; 2366*f4a2713aSLionel Sambuc } 2367*f4a2713aSLionel Sambuc 2368*f4a2713aSLionel Sambuc return false; 2369*f4a2713aSLionel Sambuc } 2370*f4a2713aSLionel Sambuc 2371*f4a2713aSLionel Sambuc NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, 2372*f4a2713aSLionel Sambuc SourceLocation Loc, 2373*f4a2713aSLionel Sambuc LookupNameKind NameKind, 2374*f4a2713aSLionel Sambuc RedeclarationKind Redecl) { 2375*f4a2713aSLionel Sambuc LookupResult R(*this, Name, Loc, NameKind, Redecl); 2376*f4a2713aSLionel Sambuc LookupName(R, S); 2377*f4a2713aSLionel Sambuc return R.getAsSingle<NamedDecl>(); 2378*f4a2713aSLionel Sambuc } 2379*f4a2713aSLionel Sambuc 2380*f4a2713aSLionel Sambuc /// \brief Find the protocol with the given name, if any. 2381*f4a2713aSLionel Sambuc ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, 2382*f4a2713aSLionel Sambuc SourceLocation IdLoc, 2383*f4a2713aSLionel Sambuc RedeclarationKind Redecl) { 2384*f4a2713aSLionel Sambuc Decl *D = LookupSingleName(TUScope, II, IdLoc, 2385*f4a2713aSLionel Sambuc LookupObjCProtocolName, Redecl); 2386*f4a2713aSLionel Sambuc return cast_or_null<ObjCProtocolDecl>(D); 2387*f4a2713aSLionel Sambuc } 2388*f4a2713aSLionel Sambuc 2389*f4a2713aSLionel Sambuc void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, 2390*f4a2713aSLionel Sambuc QualType T1, QualType T2, 2391*f4a2713aSLionel Sambuc UnresolvedSetImpl &Functions) { 2392*f4a2713aSLionel Sambuc // C++ [over.match.oper]p3: 2393*f4a2713aSLionel Sambuc // -- The set of non-member candidates is the result of the 2394*f4a2713aSLionel Sambuc // unqualified lookup of operator@ in the context of the 2395*f4a2713aSLionel Sambuc // expression according to the usual rules for name lookup in 2396*f4a2713aSLionel Sambuc // unqualified function calls (3.4.2) except that all member 2397*f4a2713aSLionel Sambuc // functions are ignored. However, if no operand has a class 2398*f4a2713aSLionel Sambuc // type, only those non-member functions in the lookup set 2399*f4a2713aSLionel Sambuc // that have a first parameter of type T1 or "reference to 2400*f4a2713aSLionel Sambuc // (possibly cv-qualified) T1", when T1 is an enumeration 2401*f4a2713aSLionel Sambuc // type, or (if there is a right operand) a second parameter 2402*f4a2713aSLionel Sambuc // of type T2 or "reference to (possibly cv-qualified) T2", 2403*f4a2713aSLionel Sambuc // when T2 is an enumeration type, are candidate functions. 2404*f4a2713aSLionel Sambuc DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); 2405*f4a2713aSLionel Sambuc LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); 2406*f4a2713aSLionel Sambuc LookupName(Operators, S); 2407*f4a2713aSLionel Sambuc 2408*f4a2713aSLionel Sambuc assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); 2409*f4a2713aSLionel Sambuc 2410*f4a2713aSLionel Sambuc if (Operators.empty()) 2411*f4a2713aSLionel Sambuc return; 2412*f4a2713aSLionel Sambuc 2413*f4a2713aSLionel Sambuc for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); 2414*f4a2713aSLionel Sambuc Op != OpEnd; ++Op) { 2415*f4a2713aSLionel Sambuc NamedDecl *Found = (*Op)->getUnderlyingDecl(); 2416*f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Found)) { 2417*f4a2713aSLionel Sambuc if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) 2418*f4a2713aSLionel Sambuc Functions.addDecl(*Op, Op.getAccess()); // FIXME: canonical FD 2419*f4a2713aSLionel Sambuc } else if (FunctionTemplateDecl *FunTmpl 2420*f4a2713aSLionel Sambuc = dyn_cast<FunctionTemplateDecl>(Found)) { 2421*f4a2713aSLionel Sambuc // FIXME: friend operators? 2422*f4a2713aSLionel Sambuc // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate, 2423*f4a2713aSLionel Sambuc // later? 2424*f4a2713aSLionel Sambuc if (!FunTmpl->getDeclContext()->isRecord()) 2425*f4a2713aSLionel Sambuc Functions.addDecl(*Op, Op.getAccess()); 2426*f4a2713aSLionel Sambuc } 2427*f4a2713aSLionel Sambuc } 2428*f4a2713aSLionel Sambuc } 2429*f4a2713aSLionel Sambuc 2430*f4a2713aSLionel Sambuc Sema::SpecialMemberOverloadResult *Sema::LookupSpecialMember(CXXRecordDecl *RD, 2431*f4a2713aSLionel Sambuc CXXSpecialMember SM, 2432*f4a2713aSLionel Sambuc bool ConstArg, 2433*f4a2713aSLionel Sambuc bool VolatileArg, 2434*f4a2713aSLionel Sambuc bool RValueThis, 2435*f4a2713aSLionel Sambuc bool ConstThis, 2436*f4a2713aSLionel Sambuc bool VolatileThis) { 2437*f4a2713aSLionel Sambuc assert(CanDeclareSpecialMemberFunction(RD) && 2438*f4a2713aSLionel Sambuc "doing special member lookup into record that isn't fully complete"); 2439*f4a2713aSLionel Sambuc RD = RD->getDefinition(); 2440*f4a2713aSLionel Sambuc if (RValueThis || ConstThis || VolatileThis) 2441*f4a2713aSLionel Sambuc assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) && 2442*f4a2713aSLionel Sambuc "constructors and destructors always have unqualified lvalue this"); 2443*f4a2713aSLionel Sambuc if (ConstArg || VolatileArg) 2444*f4a2713aSLionel Sambuc assert((SM != CXXDefaultConstructor && SM != CXXDestructor) && 2445*f4a2713aSLionel Sambuc "parameter-less special members can't have qualified arguments"); 2446*f4a2713aSLionel Sambuc 2447*f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID; 2448*f4a2713aSLionel Sambuc ID.AddPointer(RD); 2449*f4a2713aSLionel Sambuc ID.AddInteger(SM); 2450*f4a2713aSLionel Sambuc ID.AddInteger(ConstArg); 2451*f4a2713aSLionel Sambuc ID.AddInteger(VolatileArg); 2452*f4a2713aSLionel Sambuc ID.AddInteger(RValueThis); 2453*f4a2713aSLionel Sambuc ID.AddInteger(ConstThis); 2454*f4a2713aSLionel Sambuc ID.AddInteger(VolatileThis); 2455*f4a2713aSLionel Sambuc 2456*f4a2713aSLionel Sambuc void *InsertPoint; 2457*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2458*f4a2713aSLionel Sambuc SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint); 2459*f4a2713aSLionel Sambuc 2460*f4a2713aSLionel Sambuc // This was already cached 2461*f4a2713aSLionel Sambuc if (Result) 2462*f4a2713aSLionel Sambuc return Result; 2463*f4a2713aSLionel Sambuc 2464*f4a2713aSLionel Sambuc Result = BumpAlloc.Allocate<SpecialMemberOverloadResult>(); 2465*f4a2713aSLionel Sambuc Result = new (Result) SpecialMemberOverloadResult(ID); 2466*f4a2713aSLionel Sambuc SpecialMemberCache.InsertNode(Result, InsertPoint); 2467*f4a2713aSLionel Sambuc 2468*f4a2713aSLionel Sambuc if (SM == CXXDestructor) { 2469*f4a2713aSLionel Sambuc if (RD->needsImplicitDestructor()) 2470*f4a2713aSLionel Sambuc DeclareImplicitDestructor(RD); 2471*f4a2713aSLionel Sambuc CXXDestructorDecl *DD = RD->getDestructor(); 2472*f4a2713aSLionel Sambuc assert(DD && "record without a destructor"); 2473*f4a2713aSLionel Sambuc Result->setMethod(DD); 2474*f4a2713aSLionel Sambuc Result->setKind(DD->isDeleted() ? 2475*f4a2713aSLionel Sambuc SpecialMemberOverloadResult::NoMemberOrDeleted : 2476*f4a2713aSLionel Sambuc SpecialMemberOverloadResult::Success); 2477*f4a2713aSLionel Sambuc return Result; 2478*f4a2713aSLionel Sambuc } 2479*f4a2713aSLionel Sambuc 2480*f4a2713aSLionel Sambuc // Prepare for overload resolution. Here we construct a synthetic argument 2481*f4a2713aSLionel Sambuc // if necessary and make sure that implicit functions are declared. 2482*f4a2713aSLionel Sambuc CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD)); 2483*f4a2713aSLionel Sambuc DeclarationName Name; 2484*f4a2713aSLionel Sambuc Expr *Arg = 0; 2485*f4a2713aSLionel Sambuc unsigned NumArgs; 2486*f4a2713aSLionel Sambuc 2487*f4a2713aSLionel Sambuc QualType ArgType = CanTy; 2488*f4a2713aSLionel Sambuc ExprValueKind VK = VK_LValue; 2489*f4a2713aSLionel Sambuc 2490*f4a2713aSLionel Sambuc if (SM == CXXDefaultConstructor) { 2491*f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXConstructorName(CanTy); 2492*f4a2713aSLionel Sambuc NumArgs = 0; 2493*f4a2713aSLionel Sambuc if (RD->needsImplicitDefaultConstructor()) 2494*f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(RD); 2495*f4a2713aSLionel Sambuc } else { 2496*f4a2713aSLionel Sambuc if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) { 2497*f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXConstructorName(CanTy); 2498*f4a2713aSLionel Sambuc if (RD->needsImplicitCopyConstructor()) 2499*f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(RD); 2500*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor()) 2501*f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(RD); 2502*f4a2713aSLionel Sambuc } else { 2503*f4a2713aSLionel Sambuc Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); 2504*f4a2713aSLionel Sambuc if (RD->needsImplicitCopyAssignment()) 2505*f4a2713aSLionel Sambuc DeclareImplicitCopyAssignment(RD); 2506*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment()) 2507*f4a2713aSLionel Sambuc DeclareImplicitMoveAssignment(RD); 2508*f4a2713aSLionel Sambuc } 2509*f4a2713aSLionel Sambuc 2510*f4a2713aSLionel Sambuc if (ConstArg) 2511*f4a2713aSLionel Sambuc ArgType.addConst(); 2512*f4a2713aSLionel Sambuc if (VolatileArg) 2513*f4a2713aSLionel Sambuc ArgType.addVolatile(); 2514*f4a2713aSLionel Sambuc 2515*f4a2713aSLionel Sambuc // This isn't /really/ specified by the standard, but it's implied 2516*f4a2713aSLionel Sambuc // we should be working from an RValue in the case of move to ensure 2517*f4a2713aSLionel Sambuc // that we prefer to bind to rvalue references, and an LValue in the 2518*f4a2713aSLionel Sambuc // case of copy to ensure we don't bind to rvalue references. 2519*f4a2713aSLionel Sambuc // Possibly an XValue is actually correct in the case of move, but 2520*f4a2713aSLionel Sambuc // there is no semantic difference for class types in this restricted 2521*f4a2713aSLionel Sambuc // case. 2522*f4a2713aSLionel Sambuc if (SM == CXXCopyConstructor || SM == CXXCopyAssignment) 2523*f4a2713aSLionel Sambuc VK = VK_LValue; 2524*f4a2713aSLionel Sambuc else 2525*f4a2713aSLionel Sambuc VK = VK_RValue; 2526*f4a2713aSLionel Sambuc } 2527*f4a2713aSLionel Sambuc 2528*f4a2713aSLionel Sambuc OpaqueValueExpr FakeArg(SourceLocation(), ArgType, VK); 2529*f4a2713aSLionel Sambuc 2530*f4a2713aSLionel Sambuc if (SM != CXXDefaultConstructor) { 2531*f4a2713aSLionel Sambuc NumArgs = 1; 2532*f4a2713aSLionel Sambuc Arg = &FakeArg; 2533*f4a2713aSLionel Sambuc } 2534*f4a2713aSLionel Sambuc 2535*f4a2713aSLionel Sambuc // Create the object argument 2536*f4a2713aSLionel Sambuc QualType ThisTy = CanTy; 2537*f4a2713aSLionel Sambuc if (ConstThis) 2538*f4a2713aSLionel Sambuc ThisTy.addConst(); 2539*f4a2713aSLionel Sambuc if (VolatileThis) 2540*f4a2713aSLionel Sambuc ThisTy.addVolatile(); 2541*f4a2713aSLionel Sambuc Expr::Classification Classification = 2542*f4a2713aSLionel Sambuc OpaqueValueExpr(SourceLocation(), ThisTy, 2543*f4a2713aSLionel Sambuc RValueThis ? VK_RValue : VK_LValue).Classify(Context); 2544*f4a2713aSLionel Sambuc 2545*f4a2713aSLionel Sambuc // Now we perform lookup on the name we computed earlier and do overload 2546*f4a2713aSLionel Sambuc // resolution. Lookup is only performed directly into the class since there 2547*f4a2713aSLionel Sambuc // will always be a (possibly implicit) declaration to shadow any others. 2548*f4a2713aSLionel Sambuc OverloadCandidateSet OCS((SourceLocation())); 2549*f4a2713aSLionel Sambuc DeclContext::lookup_result R = RD->lookup(Name); 2550*f4a2713aSLionel Sambuc assert(!R.empty() && 2551*f4a2713aSLionel Sambuc "lookup for a constructor or assignment operator was empty"); 2552*f4a2713aSLionel Sambuc 2553*f4a2713aSLionel Sambuc // Copy the candidates as our processing of them may load new declarations 2554*f4a2713aSLionel Sambuc // from an external source and invalidate lookup_result. 2555*f4a2713aSLionel Sambuc SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end()); 2556*f4a2713aSLionel Sambuc 2557*f4a2713aSLionel Sambuc for (SmallVectorImpl<NamedDecl *>::iterator I = Candidates.begin(), 2558*f4a2713aSLionel Sambuc E = Candidates.end(); 2559*f4a2713aSLionel Sambuc I != E; ++I) { 2560*f4a2713aSLionel Sambuc NamedDecl *Cand = *I; 2561*f4a2713aSLionel Sambuc 2562*f4a2713aSLionel Sambuc if (Cand->isInvalidDecl()) 2563*f4a2713aSLionel Sambuc continue; 2564*f4a2713aSLionel Sambuc 2565*f4a2713aSLionel Sambuc if (UsingShadowDecl *U = dyn_cast<UsingShadowDecl>(Cand)) { 2566*f4a2713aSLionel Sambuc // FIXME: [namespace.udecl]p15 says that we should only consider a 2567*f4a2713aSLionel Sambuc // using declaration here if it does not match a declaration in the 2568*f4a2713aSLionel Sambuc // derived class. We do not implement this correctly in other cases 2569*f4a2713aSLionel Sambuc // either. 2570*f4a2713aSLionel Sambuc Cand = U->getTargetDecl(); 2571*f4a2713aSLionel Sambuc 2572*f4a2713aSLionel Sambuc if (Cand->isInvalidDecl()) 2573*f4a2713aSLionel Sambuc continue; 2574*f4a2713aSLionel Sambuc } 2575*f4a2713aSLionel Sambuc 2576*f4a2713aSLionel Sambuc if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand)) { 2577*f4a2713aSLionel Sambuc if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) 2578*f4a2713aSLionel Sambuc AddMethodCandidate(M, DeclAccessPair::make(M, AS_public), RD, ThisTy, 2579*f4a2713aSLionel Sambuc Classification, llvm::makeArrayRef(&Arg, NumArgs), 2580*f4a2713aSLionel Sambuc OCS, true); 2581*f4a2713aSLionel Sambuc else 2582*f4a2713aSLionel Sambuc AddOverloadCandidate(M, DeclAccessPair::make(M, AS_public), 2583*f4a2713aSLionel Sambuc llvm::makeArrayRef(&Arg, NumArgs), OCS, true); 2584*f4a2713aSLionel Sambuc } else if (FunctionTemplateDecl *Tmpl = 2585*f4a2713aSLionel Sambuc dyn_cast<FunctionTemplateDecl>(Cand)) { 2586*f4a2713aSLionel Sambuc if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) 2587*f4a2713aSLionel Sambuc AddMethodTemplateCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), 2588*f4a2713aSLionel Sambuc RD, 0, ThisTy, Classification, 2589*f4a2713aSLionel Sambuc llvm::makeArrayRef(&Arg, NumArgs), 2590*f4a2713aSLionel Sambuc OCS, true); 2591*f4a2713aSLionel Sambuc else 2592*f4a2713aSLionel Sambuc AddTemplateOverloadCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), 2593*f4a2713aSLionel Sambuc 0, llvm::makeArrayRef(&Arg, NumArgs), 2594*f4a2713aSLionel Sambuc OCS, true); 2595*f4a2713aSLionel Sambuc } else { 2596*f4a2713aSLionel Sambuc assert(isa<UsingDecl>(Cand) && "illegal Kind of operator = Decl"); 2597*f4a2713aSLionel Sambuc } 2598*f4a2713aSLionel Sambuc } 2599*f4a2713aSLionel Sambuc 2600*f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best; 2601*f4a2713aSLionel Sambuc switch (OCS.BestViableFunction(*this, SourceLocation(), Best)) { 2602*f4a2713aSLionel Sambuc case OR_Success: 2603*f4a2713aSLionel Sambuc Result->setMethod(cast<CXXMethodDecl>(Best->Function)); 2604*f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::Success); 2605*f4a2713aSLionel Sambuc break; 2606*f4a2713aSLionel Sambuc 2607*f4a2713aSLionel Sambuc case OR_Deleted: 2608*f4a2713aSLionel Sambuc Result->setMethod(cast<CXXMethodDecl>(Best->Function)); 2609*f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); 2610*f4a2713aSLionel Sambuc break; 2611*f4a2713aSLionel Sambuc 2612*f4a2713aSLionel Sambuc case OR_Ambiguous: 2613*f4a2713aSLionel Sambuc Result->setMethod(0); 2614*f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::Ambiguous); 2615*f4a2713aSLionel Sambuc break; 2616*f4a2713aSLionel Sambuc 2617*f4a2713aSLionel Sambuc case OR_No_Viable_Function: 2618*f4a2713aSLionel Sambuc Result->setMethod(0); 2619*f4a2713aSLionel Sambuc Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); 2620*f4a2713aSLionel Sambuc break; 2621*f4a2713aSLionel Sambuc } 2622*f4a2713aSLionel Sambuc 2623*f4a2713aSLionel Sambuc return Result; 2624*f4a2713aSLionel Sambuc } 2625*f4a2713aSLionel Sambuc 2626*f4a2713aSLionel Sambuc /// \brief Look up the default constructor for the given class. 2627*f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) { 2628*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2629*f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false, 2630*f4a2713aSLionel Sambuc false, false); 2631*f4a2713aSLionel Sambuc 2632*f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 2633*f4a2713aSLionel Sambuc } 2634*f4a2713aSLionel Sambuc 2635*f4a2713aSLionel Sambuc /// \brief Look up the copying constructor for the given class. 2636*f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class, 2637*f4a2713aSLionel Sambuc unsigned Quals) { 2638*f4a2713aSLionel Sambuc assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2639*f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy ctor arg"); 2640*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2641*f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const, 2642*f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, false, false, false); 2643*f4a2713aSLionel Sambuc 2644*f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 2645*f4a2713aSLionel Sambuc } 2646*f4a2713aSLionel Sambuc 2647*f4a2713aSLionel Sambuc /// \brief Look up the moving constructor for the given class. 2648*f4a2713aSLionel Sambuc CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class, 2649*f4a2713aSLionel Sambuc unsigned Quals) { 2650*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2651*f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const, 2652*f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, false, false, false); 2653*f4a2713aSLionel Sambuc 2654*f4a2713aSLionel Sambuc return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 2655*f4a2713aSLionel Sambuc } 2656*f4a2713aSLionel Sambuc 2657*f4a2713aSLionel Sambuc /// \brief Look up the constructors for the given class. 2658*f4a2713aSLionel Sambuc DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { 2659*f4a2713aSLionel Sambuc // If the implicit constructors have not yet been declared, do so now. 2660*f4a2713aSLionel Sambuc if (CanDeclareSpecialMemberFunction(Class)) { 2661*f4a2713aSLionel Sambuc if (Class->needsImplicitDefaultConstructor()) 2662*f4a2713aSLionel Sambuc DeclareImplicitDefaultConstructor(Class); 2663*f4a2713aSLionel Sambuc if (Class->needsImplicitCopyConstructor()) 2664*f4a2713aSLionel Sambuc DeclareImplicitCopyConstructor(Class); 2665*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor()) 2666*f4a2713aSLionel Sambuc DeclareImplicitMoveConstructor(Class); 2667*f4a2713aSLionel Sambuc } 2668*f4a2713aSLionel Sambuc 2669*f4a2713aSLionel Sambuc CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); 2670*f4a2713aSLionel Sambuc DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); 2671*f4a2713aSLionel Sambuc return Class->lookup(Name); 2672*f4a2713aSLionel Sambuc } 2673*f4a2713aSLionel Sambuc 2674*f4a2713aSLionel Sambuc /// \brief Look up the copying assignment operator for the given class. 2675*f4a2713aSLionel Sambuc CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class, 2676*f4a2713aSLionel Sambuc unsigned Quals, bool RValueThis, 2677*f4a2713aSLionel Sambuc unsigned ThisQuals) { 2678*f4a2713aSLionel Sambuc assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2679*f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment arg"); 2680*f4a2713aSLionel Sambuc assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2681*f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment this"); 2682*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2683*f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const, 2684*f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, RValueThis, 2685*f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Const, 2686*f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Volatile); 2687*f4a2713aSLionel Sambuc 2688*f4a2713aSLionel Sambuc return Result->getMethod(); 2689*f4a2713aSLionel Sambuc } 2690*f4a2713aSLionel Sambuc 2691*f4a2713aSLionel Sambuc /// \brief Look up the moving assignment operator for the given class. 2692*f4a2713aSLionel Sambuc CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class, 2693*f4a2713aSLionel Sambuc unsigned Quals, 2694*f4a2713aSLionel Sambuc bool RValueThis, 2695*f4a2713aSLionel Sambuc unsigned ThisQuals) { 2696*f4a2713aSLionel Sambuc assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2697*f4a2713aSLionel Sambuc "non-const, non-volatile qualifiers for copy assignment this"); 2698*f4a2713aSLionel Sambuc SpecialMemberOverloadResult *Result = 2699*f4a2713aSLionel Sambuc LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const, 2700*f4a2713aSLionel Sambuc Quals & Qualifiers::Volatile, RValueThis, 2701*f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Const, 2702*f4a2713aSLionel Sambuc ThisQuals & Qualifiers::Volatile); 2703*f4a2713aSLionel Sambuc 2704*f4a2713aSLionel Sambuc return Result->getMethod(); 2705*f4a2713aSLionel Sambuc } 2706*f4a2713aSLionel Sambuc 2707*f4a2713aSLionel Sambuc /// \brief Look for the destructor of the given class. 2708*f4a2713aSLionel Sambuc /// 2709*f4a2713aSLionel Sambuc /// During semantic analysis, this routine should be used in lieu of 2710*f4a2713aSLionel Sambuc /// CXXRecordDecl::getDestructor(). 2711*f4a2713aSLionel Sambuc /// 2712*f4a2713aSLionel Sambuc /// \returns The destructor for this class. 2713*f4a2713aSLionel Sambuc CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { 2714*f4a2713aSLionel Sambuc return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor, 2715*f4a2713aSLionel Sambuc false, false, false, 2716*f4a2713aSLionel Sambuc false, false)->getMethod()); 2717*f4a2713aSLionel Sambuc } 2718*f4a2713aSLionel Sambuc 2719*f4a2713aSLionel Sambuc /// LookupLiteralOperator - Determine which literal operator should be used for 2720*f4a2713aSLionel Sambuc /// a user-defined literal, per C++11 [lex.ext]. 2721*f4a2713aSLionel Sambuc /// 2722*f4a2713aSLionel Sambuc /// Normal overload resolution is not used to select which literal operator to 2723*f4a2713aSLionel Sambuc /// call for a user-defined literal. Look up the provided literal operator name, 2724*f4a2713aSLionel Sambuc /// and filter the results to the appropriate set for the given argument types. 2725*f4a2713aSLionel Sambuc Sema::LiteralOperatorLookupResult 2726*f4a2713aSLionel Sambuc Sema::LookupLiteralOperator(Scope *S, LookupResult &R, 2727*f4a2713aSLionel Sambuc ArrayRef<QualType> ArgTys, 2728*f4a2713aSLionel Sambuc bool AllowRaw, bool AllowTemplate, 2729*f4a2713aSLionel Sambuc bool AllowStringTemplate) { 2730*f4a2713aSLionel Sambuc LookupName(R, S); 2731*f4a2713aSLionel Sambuc assert(R.getResultKind() != LookupResult::Ambiguous && 2732*f4a2713aSLionel Sambuc "literal operator lookup can't be ambiguous"); 2733*f4a2713aSLionel Sambuc 2734*f4a2713aSLionel Sambuc // Filter the lookup results appropriately. 2735*f4a2713aSLionel Sambuc LookupResult::Filter F = R.makeFilter(); 2736*f4a2713aSLionel Sambuc 2737*f4a2713aSLionel Sambuc bool FoundRaw = false; 2738*f4a2713aSLionel Sambuc bool FoundTemplate = false; 2739*f4a2713aSLionel Sambuc bool FoundStringTemplate = false; 2740*f4a2713aSLionel Sambuc bool FoundExactMatch = false; 2741*f4a2713aSLionel Sambuc 2742*f4a2713aSLionel Sambuc while (F.hasNext()) { 2743*f4a2713aSLionel Sambuc Decl *D = F.next(); 2744*f4a2713aSLionel Sambuc if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) 2745*f4a2713aSLionel Sambuc D = USD->getTargetDecl(); 2746*f4a2713aSLionel Sambuc 2747*f4a2713aSLionel Sambuc // If the declaration we found is invalid, skip it. 2748*f4a2713aSLionel Sambuc if (D->isInvalidDecl()) { 2749*f4a2713aSLionel Sambuc F.erase(); 2750*f4a2713aSLionel Sambuc continue; 2751*f4a2713aSLionel Sambuc } 2752*f4a2713aSLionel Sambuc 2753*f4a2713aSLionel Sambuc bool IsRaw = false; 2754*f4a2713aSLionel Sambuc bool IsTemplate = false; 2755*f4a2713aSLionel Sambuc bool IsStringTemplate = false; 2756*f4a2713aSLionel Sambuc bool IsExactMatch = false; 2757*f4a2713aSLionel Sambuc 2758*f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2759*f4a2713aSLionel Sambuc if (FD->getNumParams() == 1 && 2760*f4a2713aSLionel Sambuc FD->getParamDecl(0)->getType()->getAs<PointerType>()) 2761*f4a2713aSLionel Sambuc IsRaw = true; 2762*f4a2713aSLionel Sambuc else if (FD->getNumParams() == ArgTys.size()) { 2763*f4a2713aSLionel Sambuc IsExactMatch = true; 2764*f4a2713aSLionel Sambuc for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) { 2765*f4a2713aSLionel Sambuc QualType ParamTy = FD->getParamDecl(ArgIdx)->getType(); 2766*f4a2713aSLionel Sambuc if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) { 2767*f4a2713aSLionel Sambuc IsExactMatch = false; 2768*f4a2713aSLionel Sambuc break; 2769*f4a2713aSLionel Sambuc } 2770*f4a2713aSLionel Sambuc } 2771*f4a2713aSLionel Sambuc } 2772*f4a2713aSLionel Sambuc } 2773*f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) { 2774*f4a2713aSLionel Sambuc TemplateParameterList *Params = FD->getTemplateParameters(); 2775*f4a2713aSLionel Sambuc if (Params->size() == 1) 2776*f4a2713aSLionel Sambuc IsTemplate = true; 2777*f4a2713aSLionel Sambuc else 2778*f4a2713aSLionel Sambuc IsStringTemplate = true; 2779*f4a2713aSLionel Sambuc } 2780*f4a2713aSLionel Sambuc 2781*f4a2713aSLionel Sambuc if (IsExactMatch) { 2782*f4a2713aSLionel Sambuc FoundExactMatch = true; 2783*f4a2713aSLionel Sambuc AllowRaw = false; 2784*f4a2713aSLionel Sambuc AllowTemplate = false; 2785*f4a2713aSLionel Sambuc AllowStringTemplate = false; 2786*f4a2713aSLionel Sambuc if (FoundRaw || FoundTemplate || FoundStringTemplate) { 2787*f4a2713aSLionel Sambuc // Go through again and remove the raw and template decls we've 2788*f4a2713aSLionel Sambuc // already found. 2789*f4a2713aSLionel Sambuc F.restart(); 2790*f4a2713aSLionel Sambuc FoundRaw = FoundTemplate = FoundStringTemplate = false; 2791*f4a2713aSLionel Sambuc } 2792*f4a2713aSLionel Sambuc } else if (AllowRaw && IsRaw) { 2793*f4a2713aSLionel Sambuc FoundRaw = true; 2794*f4a2713aSLionel Sambuc } else if (AllowTemplate && IsTemplate) { 2795*f4a2713aSLionel Sambuc FoundTemplate = true; 2796*f4a2713aSLionel Sambuc } else if (AllowStringTemplate && IsStringTemplate) { 2797*f4a2713aSLionel Sambuc FoundStringTemplate = true; 2798*f4a2713aSLionel Sambuc } else { 2799*f4a2713aSLionel Sambuc F.erase(); 2800*f4a2713aSLionel Sambuc } 2801*f4a2713aSLionel Sambuc } 2802*f4a2713aSLionel Sambuc 2803*f4a2713aSLionel Sambuc F.done(); 2804*f4a2713aSLionel Sambuc 2805*f4a2713aSLionel Sambuc // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching 2806*f4a2713aSLionel Sambuc // parameter type, that is used in preference to a raw literal operator 2807*f4a2713aSLionel Sambuc // or literal operator template. 2808*f4a2713aSLionel Sambuc if (FoundExactMatch) 2809*f4a2713aSLionel Sambuc return LOLR_Cooked; 2810*f4a2713aSLionel Sambuc 2811*f4a2713aSLionel Sambuc // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal 2812*f4a2713aSLionel Sambuc // operator template, but not both. 2813*f4a2713aSLionel Sambuc if (FoundRaw && FoundTemplate) { 2814*f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); 2815*f4a2713aSLionel Sambuc for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 2816*f4a2713aSLionel Sambuc Decl *D = *I; 2817*f4a2713aSLionel Sambuc if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) 2818*f4a2713aSLionel Sambuc D = USD->getTargetDecl(); 2819*f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) 2820*f4a2713aSLionel Sambuc D = FunTmpl->getTemplatedDecl(); 2821*f4a2713aSLionel Sambuc NoteOverloadCandidate(cast<FunctionDecl>(D)); 2822*f4a2713aSLionel Sambuc } 2823*f4a2713aSLionel Sambuc return LOLR_Error; 2824*f4a2713aSLionel Sambuc } 2825*f4a2713aSLionel Sambuc 2826*f4a2713aSLionel Sambuc if (FoundRaw) 2827*f4a2713aSLionel Sambuc return LOLR_Raw; 2828*f4a2713aSLionel Sambuc 2829*f4a2713aSLionel Sambuc if (FoundTemplate) 2830*f4a2713aSLionel Sambuc return LOLR_Template; 2831*f4a2713aSLionel Sambuc 2832*f4a2713aSLionel Sambuc if (FoundStringTemplate) 2833*f4a2713aSLionel Sambuc return LOLR_StringTemplate; 2834*f4a2713aSLionel Sambuc 2835*f4a2713aSLionel Sambuc // Didn't find anything we could use. 2836*f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator) 2837*f4a2713aSLionel Sambuc << R.getLookupName() << (int)ArgTys.size() << ArgTys[0] 2838*f4a2713aSLionel Sambuc << (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw 2839*f4a2713aSLionel Sambuc << (AllowTemplate || AllowStringTemplate); 2840*f4a2713aSLionel Sambuc return LOLR_Error; 2841*f4a2713aSLionel Sambuc } 2842*f4a2713aSLionel Sambuc 2843*f4a2713aSLionel Sambuc void ADLResult::insert(NamedDecl *New) { 2844*f4a2713aSLionel Sambuc NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; 2845*f4a2713aSLionel Sambuc 2846*f4a2713aSLionel Sambuc // If we haven't yet seen a decl for this key, or the last decl 2847*f4a2713aSLionel Sambuc // was exactly this one, we're done. 2848*f4a2713aSLionel Sambuc if (Old == 0 || Old == New) { 2849*f4a2713aSLionel Sambuc Old = New; 2850*f4a2713aSLionel Sambuc return; 2851*f4a2713aSLionel Sambuc } 2852*f4a2713aSLionel Sambuc 2853*f4a2713aSLionel Sambuc // Otherwise, decide which is a more recent redeclaration. 2854*f4a2713aSLionel Sambuc FunctionDecl *OldFD, *NewFD; 2855*f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(New)) { 2856*f4a2713aSLionel Sambuc OldFD = cast<FunctionTemplateDecl>(Old)->getTemplatedDecl(); 2857*f4a2713aSLionel Sambuc NewFD = cast<FunctionTemplateDecl>(New)->getTemplatedDecl(); 2858*f4a2713aSLionel Sambuc } else { 2859*f4a2713aSLionel Sambuc OldFD = cast<FunctionDecl>(Old); 2860*f4a2713aSLionel Sambuc NewFD = cast<FunctionDecl>(New); 2861*f4a2713aSLionel Sambuc } 2862*f4a2713aSLionel Sambuc 2863*f4a2713aSLionel Sambuc FunctionDecl *Cursor = NewFD; 2864*f4a2713aSLionel Sambuc while (true) { 2865*f4a2713aSLionel Sambuc Cursor = Cursor->getPreviousDecl(); 2866*f4a2713aSLionel Sambuc 2867*f4a2713aSLionel Sambuc // If we got to the end without finding OldFD, OldFD is the newer 2868*f4a2713aSLionel Sambuc // declaration; leave things as they are. 2869*f4a2713aSLionel Sambuc if (!Cursor) return; 2870*f4a2713aSLionel Sambuc 2871*f4a2713aSLionel Sambuc // If we do find OldFD, then NewFD is newer. 2872*f4a2713aSLionel Sambuc if (Cursor == OldFD) break; 2873*f4a2713aSLionel Sambuc 2874*f4a2713aSLionel Sambuc // Otherwise, keep looking. 2875*f4a2713aSLionel Sambuc } 2876*f4a2713aSLionel Sambuc 2877*f4a2713aSLionel Sambuc Old = New; 2878*f4a2713aSLionel Sambuc } 2879*f4a2713aSLionel Sambuc 2880*f4a2713aSLionel Sambuc void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator, 2881*f4a2713aSLionel Sambuc SourceLocation Loc, ArrayRef<Expr *> Args, 2882*f4a2713aSLionel Sambuc ADLResult &Result) { 2883*f4a2713aSLionel Sambuc // Find all of the associated namespaces and classes based on the 2884*f4a2713aSLionel Sambuc // arguments we have. 2885*f4a2713aSLionel Sambuc AssociatedNamespaceSet AssociatedNamespaces; 2886*f4a2713aSLionel Sambuc AssociatedClassSet AssociatedClasses; 2887*f4a2713aSLionel Sambuc FindAssociatedClassesAndNamespaces(Loc, Args, 2888*f4a2713aSLionel Sambuc AssociatedNamespaces, 2889*f4a2713aSLionel Sambuc AssociatedClasses); 2890*f4a2713aSLionel Sambuc 2891*f4a2713aSLionel Sambuc QualType T1, T2; 2892*f4a2713aSLionel Sambuc if (Operator) { 2893*f4a2713aSLionel Sambuc T1 = Args[0]->getType(); 2894*f4a2713aSLionel Sambuc if (Args.size() >= 2) 2895*f4a2713aSLionel Sambuc T2 = Args[1]->getType(); 2896*f4a2713aSLionel Sambuc } 2897*f4a2713aSLionel Sambuc 2898*f4a2713aSLionel Sambuc // C++ [basic.lookup.argdep]p3: 2899*f4a2713aSLionel Sambuc // Let X be the lookup set produced by unqualified lookup (3.4.1) 2900*f4a2713aSLionel Sambuc // and let Y be the lookup set produced by argument dependent 2901*f4a2713aSLionel Sambuc // lookup (defined as follows). If X contains [...] then Y is 2902*f4a2713aSLionel Sambuc // empty. Otherwise Y is the set of declarations found in the 2903*f4a2713aSLionel Sambuc // namespaces associated with the argument types as described 2904*f4a2713aSLionel Sambuc // below. The set of declarations found by the lookup of the name 2905*f4a2713aSLionel Sambuc // is the union of X and Y. 2906*f4a2713aSLionel Sambuc // 2907*f4a2713aSLionel Sambuc // Here, we compute Y and add its members to the overloaded 2908*f4a2713aSLionel Sambuc // candidate set. 2909*f4a2713aSLionel Sambuc for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), 2910*f4a2713aSLionel Sambuc NSEnd = AssociatedNamespaces.end(); 2911*f4a2713aSLionel Sambuc NS != NSEnd; ++NS) { 2912*f4a2713aSLionel Sambuc // When considering an associated namespace, the lookup is the 2913*f4a2713aSLionel Sambuc // same as the lookup performed when the associated namespace is 2914*f4a2713aSLionel Sambuc // used as a qualifier (3.4.3.2) except that: 2915*f4a2713aSLionel Sambuc // 2916*f4a2713aSLionel Sambuc // -- Any using-directives in the associated namespace are 2917*f4a2713aSLionel Sambuc // ignored. 2918*f4a2713aSLionel Sambuc // 2919*f4a2713aSLionel Sambuc // -- Any namespace-scope friend functions declared in 2920*f4a2713aSLionel Sambuc // associated classes are visible within their respective 2921*f4a2713aSLionel Sambuc // namespaces even if they are not visible during an ordinary 2922*f4a2713aSLionel Sambuc // lookup (11.4). 2923*f4a2713aSLionel Sambuc DeclContext::lookup_result R = (*NS)->lookup(Name); 2924*f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; 2925*f4a2713aSLionel Sambuc ++I) { 2926*f4a2713aSLionel Sambuc NamedDecl *D = *I; 2927*f4a2713aSLionel Sambuc // If the only declaration here is an ordinary friend, consider 2928*f4a2713aSLionel Sambuc // it only if it was declared in an associated classes. 2929*f4a2713aSLionel Sambuc if ((D->getIdentifierNamespace() & Decl::IDNS_Ordinary) == 0) { 2930*f4a2713aSLionel Sambuc // If it's neither ordinarily visible nor a friend, we can't find it. 2931*f4a2713aSLionel Sambuc if ((D->getIdentifierNamespace() & Decl::IDNS_OrdinaryFriend) == 0) 2932*f4a2713aSLionel Sambuc continue; 2933*f4a2713aSLionel Sambuc 2934*f4a2713aSLionel Sambuc bool DeclaredInAssociatedClass = false; 2935*f4a2713aSLionel Sambuc for (Decl *DI = D; DI; DI = DI->getPreviousDecl()) { 2936*f4a2713aSLionel Sambuc DeclContext *LexDC = DI->getLexicalDeclContext(); 2937*f4a2713aSLionel Sambuc if (isa<CXXRecordDecl>(LexDC) && 2938*f4a2713aSLionel Sambuc AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) { 2939*f4a2713aSLionel Sambuc DeclaredInAssociatedClass = true; 2940*f4a2713aSLionel Sambuc break; 2941*f4a2713aSLionel Sambuc } 2942*f4a2713aSLionel Sambuc } 2943*f4a2713aSLionel Sambuc if (!DeclaredInAssociatedClass) 2944*f4a2713aSLionel Sambuc continue; 2945*f4a2713aSLionel Sambuc } 2946*f4a2713aSLionel Sambuc 2947*f4a2713aSLionel Sambuc if (isa<UsingShadowDecl>(D)) 2948*f4a2713aSLionel Sambuc D = cast<UsingShadowDecl>(D)->getTargetDecl(); 2949*f4a2713aSLionel Sambuc 2950*f4a2713aSLionel Sambuc if (isa<FunctionDecl>(D)) { 2951*f4a2713aSLionel Sambuc if (Operator && 2952*f4a2713aSLionel Sambuc !IsAcceptableNonMemberOperatorCandidate(cast<FunctionDecl>(D), 2953*f4a2713aSLionel Sambuc T1, T2, Context)) 2954*f4a2713aSLionel Sambuc continue; 2955*f4a2713aSLionel Sambuc } else if (!isa<FunctionTemplateDecl>(D)) 2956*f4a2713aSLionel Sambuc continue; 2957*f4a2713aSLionel Sambuc 2958*f4a2713aSLionel Sambuc Result.insert(D); 2959*f4a2713aSLionel Sambuc } 2960*f4a2713aSLionel Sambuc } 2961*f4a2713aSLionel Sambuc } 2962*f4a2713aSLionel Sambuc 2963*f4a2713aSLionel Sambuc //---------------------------------------------------------------------------- 2964*f4a2713aSLionel Sambuc // Search for all visible declarations. 2965*f4a2713aSLionel Sambuc //---------------------------------------------------------------------------- 2966*f4a2713aSLionel Sambuc VisibleDeclConsumer::~VisibleDeclConsumer() { } 2967*f4a2713aSLionel Sambuc 2968*f4a2713aSLionel Sambuc bool VisibleDeclConsumer::includeHiddenDecls() const { return false; } 2969*f4a2713aSLionel Sambuc 2970*f4a2713aSLionel Sambuc namespace { 2971*f4a2713aSLionel Sambuc 2972*f4a2713aSLionel Sambuc class ShadowContextRAII; 2973*f4a2713aSLionel Sambuc 2974*f4a2713aSLionel Sambuc class VisibleDeclsRecord { 2975*f4a2713aSLionel Sambuc public: 2976*f4a2713aSLionel Sambuc /// \brief An entry in the shadow map, which is optimized to store a 2977*f4a2713aSLionel Sambuc /// single declaration (the common case) but can also store a list 2978*f4a2713aSLionel Sambuc /// of declarations. 2979*f4a2713aSLionel Sambuc typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry; 2980*f4a2713aSLionel Sambuc 2981*f4a2713aSLionel Sambuc private: 2982*f4a2713aSLionel Sambuc /// \brief A mapping from declaration names to the declarations that have 2983*f4a2713aSLionel Sambuc /// this name within a particular scope. 2984*f4a2713aSLionel Sambuc typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; 2985*f4a2713aSLionel Sambuc 2986*f4a2713aSLionel Sambuc /// \brief A list of shadow maps, which is used to model name hiding. 2987*f4a2713aSLionel Sambuc std::list<ShadowMap> ShadowMaps; 2988*f4a2713aSLionel Sambuc 2989*f4a2713aSLionel Sambuc /// \brief The declaration contexts we have already visited. 2990*f4a2713aSLionel Sambuc llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; 2991*f4a2713aSLionel Sambuc 2992*f4a2713aSLionel Sambuc friend class ShadowContextRAII; 2993*f4a2713aSLionel Sambuc 2994*f4a2713aSLionel Sambuc public: 2995*f4a2713aSLionel Sambuc /// \brief Determine whether we have already visited this context 2996*f4a2713aSLionel Sambuc /// (and, if not, note that we are going to visit that context now). 2997*f4a2713aSLionel Sambuc bool visitedContext(DeclContext *Ctx) { 2998*f4a2713aSLionel Sambuc return !VisitedContexts.insert(Ctx); 2999*f4a2713aSLionel Sambuc } 3000*f4a2713aSLionel Sambuc 3001*f4a2713aSLionel Sambuc bool alreadyVisitedContext(DeclContext *Ctx) { 3002*f4a2713aSLionel Sambuc return VisitedContexts.count(Ctx); 3003*f4a2713aSLionel Sambuc } 3004*f4a2713aSLionel Sambuc 3005*f4a2713aSLionel Sambuc /// \brief Determine whether the given declaration is hidden in the 3006*f4a2713aSLionel Sambuc /// current scope. 3007*f4a2713aSLionel Sambuc /// 3008*f4a2713aSLionel Sambuc /// \returns the declaration that hides the given declaration, or 3009*f4a2713aSLionel Sambuc /// NULL if no such declaration exists. 3010*f4a2713aSLionel Sambuc NamedDecl *checkHidden(NamedDecl *ND); 3011*f4a2713aSLionel Sambuc 3012*f4a2713aSLionel Sambuc /// \brief Add a declaration to the current shadow map. 3013*f4a2713aSLionel Sambuc void add(NamedDecl *ND) { 3014*f4a2713aSLionel Sambuc ShadowMaps.back()[ND->getDeclName()].push_back(ND); 3015*f4a2713aSLionel Sambuc } 3016*f4a2713aSLionel Sambuc }; 3017*f4a2713aSLionel Sambuc 3018*f4a2713aSLionel Sambuc /// \brief RAII object that records when we've entered a shadow context. 3019*f4a2713aSLionel Sambuc class ShadowContextRAII { 3020*f4a2713aSLionel Sambuc VisibleDeclsRecord &Visible; 3021*f4a2713aSLionel Sambuc 3022*f4a2713aSLionel Sambuc typedef VisibleDeclsRecord::ShadowMap ShadowMap; 3023*f4a2713aSLionel Sambuc 3024*f4a2713aSLionel Sambuc public: 3025*f4a2713aSLionel Sambuc ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { 3026*f4a2713aSLionel Sambuc Visible.ShadowMaps.push_back(ShadowMap()); 3027*f4a2713aSLionel Sambuc } 3028*f4a2713aSLionel Sambuc 3029*f4a2713aSLionel Sambuc ~ShadowContextRAII() { 3030*f4a2713aSLionel Sambuc Visible.ShadowMaps.pop_back(); 3031*f4a2713aSLionel Sambuc } 3032*f4a2713aSLionel Sambuc }; 3033*f4a2713aSLionel Sambuc 3034*f4a2713aSLionel Sambuc } // end anonymous namespace 3035*f4a2713aSLionel Sambuc 3036*f4a2713aSLionel Sambuc NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { 3037*f4a2713aSLionel Sambuc // Look through using declarations. 3038*f4a2713aSLionel Sambuc ND = ND->getUnderlyingDecl(); 3039*f4a2713aSLionel Sambuc 3040*f4a2713aSLionel Sambuc unsigned IDNS = ND->getIdentifierNamespace(); 3041*f4a2713aSLionel Sambuc std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); 3042*f4a2713aSLionel Sambuc for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); 3043*f4a2713aSLionel Sambuc SM != SMEnd; ++SM) { 3044*f4a2713aSLionel Sambuc ShadowMap::iterator Pos = SM->find(ND->getDeclName()); 3045*f4a2713aSLionel Sambuc if (Pos == SM->end()) 3046*f4a2713aSLionel Sambuc continue; 3047*f4a2713aSLionel Sambuc 3048*f4a2713aSLionel Sambuc for (ShadowMapEntry::iterator I = Pos->second.begin(), 3049*f4a2713aSLionel Sambuc IEnd = Pos->second.end(); 3050*f4a2713aSLionel Sambuc I != IEnd; ++I) { 3051*f4a2713aSLionel Sambuc // A tag declaration does not hide a non-tag declaration. 3052*f4a2713aSLionel Sambuc if ((*I)->hasTagIdentifierNamespace() && 3053*f4a2713aSLionel Sambuc (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | 3054*f4a2713aSLionel Sambuc Decl::IDNS_ObjCProtocol))) 3055*f4a2713aSLionel Sambuc continue; 3056*f4a2713aSLionel Sambuc 3057*f4a2713aSLionel Sambuc // Protocols are in distinct namespaces from everything else. 3058*f4a2713aSLionel Sambuc if ((((*I)->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) 3059*f4a2713aSLionel Sambuc || (IDNS & Decl::IDNS_ObjCProtocol)) && 3060*f4a2713aSLionel Sambuc (*I)->getIdentifierNamespace() != IDNS) 3061*f4a2713aSLionel Sambuc continue; 3062*f4a2713aSLionel Sambuc 3063*f4a2713aSLionel Sambuc // Functions and function templates in the same scope overload 3064*f4a2713aSLionel Sambuc // rather than hide. FIXME: Look for hiding based on function 3065*f4a2713aSLionel Sambuc // signatures! 3066*f4a2713aSLionel Sambuc if ((*I)->isFunctionOrFunctionTemplate() && 3067*f4a2713aSLionel Sambuc ND->isFunctionOrFunctionTemplate() && 3068*f4a2713aSLionel Sambuc SM == ShadowMaps.rbegin()) 3069*f4a2713aSLionel Sambuc continue; 3070*f4a2713aSLionel Sambuc 3071*f4a2713aSLionel Sambuc // We've found a declaration that hides this one. 3072*f4a2713aSLionel Sambuc return *I; 3073*f4a2713aSLionel Sambuc } 3074*f4a2713aSLionel Sambuc } 3075*f4a2713aSLionel Sambuc 3076*f4a2713aSLionel Sambuc return 0; 3077*f4a2713aSLionel Sambuc } 3078*f4a2713aSLionel Sambuc 3079*f4a2713aSLionel Sambuc static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, 3080*f4a2713aSLionel Sambuc bool QualifiedNameLookup, 3081*f4a2713aSLionel Sambuc bool InBaseClass, 3082*f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer, 3083*f4a2713aSLionel Sambuc VisibleDeclsRecord &Visited) { 3084*f4a2713aSLionel Sambuc if (!Ctx) 3085*f4a2713aSLionel Sambuc return; 3086*f4a2713aSLionel Sambuc 3087*f4a2713aSLionel Sambuc // Make sure we don't visit the same context twice. 3088*f4a2713aSLionel Sambuc if (Visited.visitedContext(Ctx->getPrimaryContext())) 3089*f4a2713aSLionel Sambuc return; 3090*f4a2713aSLionel Sambuc 3091*f4a2713aSLionel Sambuc if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) 3092*f4a2713aSLionel Sambuc Result.getSema().ForceDeclarationOfImplicitMembers(Class); 3093*f4a2713aSLionel Sambuc 3094*f4a2713aSLionel Sambuc // Enumerate all of the results in this context. 3095*f4a2713aSLionel Sambuc for (DeclContext::all_lookups_iterator L = Ctx->lookups_begin(), 3096*f4a2713aSLionel Sambuc LEnd = Ctx->lookups_end(); 3097*f4a2713aSLionel Sambuc L != LEnd; ++L) { 3098*f4a2713aSLionel Sambuc DeclContext::lookup_result R = *L; 3099*f4a2713aSLionel Sambuc for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; 3100*f4a2713aSLionel Sambuc ++I) { 3101*f4a2713aSLionel Sambuc if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) { 3102*f4a2713aSLionel Sambuc if ((ND = Result.getAcceptableDecl(ND))) { 3103*f4a2713aSLionel Sambuc Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); 3104*f4a2713aSLionel Sambuc Visited.add(ND); 3105*f4a2713aSLionel Sambuc } 3106*f4a2713aSLionel Sambuc } 3107*f4a2713aSLionel Sambuc } 3108*f4a2713aSLionel Sambuc } 3109*f4a2713aSLionel Sambuc 3110*f4a2713aSLionel Sambuc // Traverse using directives for qualified name lookup. 3111*f4a2713aSLionel Sambuc if (QualifiedNameLookup) { 3112*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3113*f4a2713aSLionel Sambuc DeclContext::udir_iterator I, E; 3114*f4a2713aSLionel Sambuc for (llvm::tie(I, E) = Ctx->getUsingDirectives(); I != E; ++I) { 3115*f4a2713aSLionel Sambuc LookupVisibleDecls((*I)->getNominatedNamespace(), Result, 3116*f4a2713aSLionel Sambuc QualifiedNameLookup, InBaseClass, Consumer, Visited); 3117*f4a2713aSLionel Sambuc } 3118*f4a2713aSLionel Sambuc } 3119*f4a2713aSLionel Sambuc 3120*f4a2713aSLionel Sambuc // Traverse the contexts of inherited C++ classes. 3121*f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { 3122*f4a2713aSLionel Sambuc if (!Record->hasDefinition()) 3123*f4a2713aSLionel Sambuc return; 3124*f4a2713aSLionel Sambuc 3125*f4a2713aSLionel Sambuc for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), 3126*f4a2713aSLionel Sambuc BEnd = Record->bases_end(); 3127*f4a2713aSLionel Sambuc B != BEnd; ++B) { 3128*f4a2713aSLionel Sambuc QualType BaseType = B->getType(); 3129*f4a2713aSLionel Sambuc 3130*f4a2713aSLionel Sambuc // Don't look into dependent bases, because name lookup can't look 3131*f4a2713aSLionel Sambuc // there anyway. 3132*f4a2713aSLionel Sambuc if (BaseType->isDependentType()) 3133*f4a2713aSLionel Sambuc continue; 3134*f4a2713aSLionel Sambuc 3135*f4a2713aSLionel Sambuc const RecordType *Record = BaseType->getAs<RecordType>(); 3136*f4a2713aSLionel Sambuc if (!Record) 3137*f4a2713aSLionel Sambuc continue; 3138*f4a2713aSLionel Sambuc 3139*f4a2713aSLionel Sambuc // FIXME: It would be nice to be able to determine whether referencing 3140*f4a2713aSLionel Sambuc // a particular member would be ambiguous. For example, given 3141*f4a2713aSLionel Sambuc // 3142*f4a2713aSLionel Sambuc // struct A { int member; }; 3143*f4a2713aSLionel Sambuc // struct B { int member; }; 3144*f4a2713aSLionel Sambuc // struct C : A, B { }; 3145*f4a2713aSLionel Sambuc // 3146*f4a2713aSLionel Sambuc // void f(C *c) { c->### } 3147*f4a2713aSLionel Sambuc // 3148*f4a2713aSLionel Sambuc // accessing 'member' would result in an ambiguity. However, we 3149*f4a2713aSLionel Sambuc // could be smart enough to qualify the member with the base 3150*f4a2713aSLionel Sambuc // class, e.g., 3151*f4a2713aSLionel Sambuc // 3152*f4a2713aSLionel Sambuc // c->B::member 3153*f4a2713aSLionel Sambuc // 3154*f4a2713aSLionel Sambuc // or 3155*f4a2713aSLionel Sambuc // 3156*f4a2713aSLionel Sambuc // c->A::member 3157*f4a2713aSLionel Sambuc 3158*f4a2713aSLionel Sambuc // Find results in this base class (and its bases). 3159*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3160*f4a2713aSLionel Sambuc LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, 3161*f4a2713aSLionel Sambuc true, Consumer, Visited); 3162*f4a2713aSLionel Sambuc } 3163*f4a2713aSLionel Sambuc } 3164*f4a2713aSLionel Sambuc 3165*f4a2713aSLionel Sambuc // Traverse the contexts of Objective-C classes. 3166*f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { 3167*f4a2713aSLionel Sambuc // Traverse categories. 3168*f4a2713aSLionel Sambuc for (ObjCInterfaceDecl::visible_categories_iterator 3169*f4a2713aSLionel Sambuc Cat = IFace->visible_categories_begin(), 3170*f4a2713aSLionel Sambuc CatEnd = IFace->visible_categories_end(); 3171*f4a2713aSLionel Sambuc Cat != CatEnd; ++Cat) { 3172*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3173*f4a2713aSLionel Sambuc LookupVisibleDecls(*Cat, Result, QualifiedNameLookup, false, 3174*f4a2713aSLionel Sambuc Consumer, Visited); 3175*f4a2713aSLionel Sambuc } 3176*f4a2713aSLionel Sambuc 3177*f4a2713aSLionel Sambuc // Traverse protocols. 3178*f4a2713aSLionel Sambuc for (ObjCInterfaceDecl::all_protocol_iterator 3179*f4a2713aSLionel Sambuc I = IFace->all_referenced_protocol_begin(), 3180*f4a2713aSLionel Sambuc E = IFace->all_referenced_protocol_end(); I != E; ++I) { 3181*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3182*f4a2713aSLionel Sambuc LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 3183*f4a2713aSLionel Sambuc Visited); 3184*f4a2713aSLionel Sambuc } 3185*f4a2713aSLionel Sambuc 3186*f4a2713aSLionel Sambuc // Traverse the superclass. 3187*f4a2713aSLionel Sambuc if (IFace->getSuperClass()) { 3188*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3189*f4a2713aSLionel Sambuc LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, 3190*f4a2713aSLionel Sambuc true, Consumer, Visited); 3191*f4a2713aSLionel Sambuc } 3192*f4a2713aSLionel Sambuc 3193*f4a2713aSLionel Sambuc // If there is an implementation, traverse it. We do this to find 3194*f4a2713aSLionel Sambuc // synthesized ivars. 3195*f4a2713aSLionel Sambuc if (IFace->getImplementation()) { 3196*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3197*f4a2713aSLionel Sambuc LookupVisibleDecls(IFace->getImplementation(), Result, 3198*f4a2713aSLionel Sambuc QualifiedNameLookup, InBaseClass, Consumer, Visited); 3199*f4a2713aSLionel Sambuc } 3200*f4a2713aSLionel Sambuc } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { 3201*f4a2713aSLionel Sambuc for (ObjCProtocolDecl::protocol_iterator I = Protocol->protocol_begin(), 3202*f4a2713aSLionel Sambuc E = Protocol->protocol_end(); I != E; ++I) { 3203*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3204*f4a2713aSLionel Sambuc LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 3205*f4a2713aSLionel Sambuc Visited); 3206*f4a2713aSLionel Sambuc } 3207*f4a2713aSLionel Sambuc } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { 3208*f4a2713aSLionel Sambuc for (ObjCCategoryDecl::protocol_iterator I = Category->protocol_begin(), 3209*f4a2713aSLionel Sambuc E = Category->protocol_end(); I != E; ++I) { 3210*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3211*f4a2713aSLionel Sambuc LookupVisibleDecls(*I, Result, QualifiedNameLookup, false, Consumer, 3212*f4a2713aSLionel Sambuc Visited); 3213*f4a2713aSLionel Sambuc } 3214*f4a2713aSLionel Sambuc 3215*f4a2713aSLionel Sambuc // If there is an implementation, traverse it. 3216*f4a2713aSLionel Sambuc if (Category->getImplementation()) { 3217*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3218*f4a2713aSLionel Sambuc LookupVisibleDecls(Category->getImplementation(), Result, 3219*f4a2713aSLionel Sambuc QualifiedNameLookup, true, Consumer, Visited); 3220*f4a2713aSLionel Sambuc } 3221*f4a2713aSLionel Sambuc } 3222*f4a2713aSLionel Sambuc } 3223*f4a2713aSLionel Sambuc 3224*f4a2713aSLionel Sambuc static void LookupVisibleDecls(Scope *S, LookupResult &Result, 3225*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet &UDirs, 3226*f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer, 3227*f4a2713aSLionel Sambuc VisibleDeclsRecord &Visited) { 3228*f4a2713aSLionel Sambuc if (!S) 3229*f4a2713aSLionel Sambuc return; 3230*f4a2713aSLionel Sambuc 3231*f4a2713aSLionel Sambuc if (!S->getEntity() || 3232*f4a2713aSLionel Sambuc (!S->getParent() && 3233*f4a2713aSLionel Sambuc !Visited.alreadyVisitedContext(S->getEntity())) || 3234*f4a2713aSLionel Sambuc (S->getEntity())->isFunctionOrMethod()) { 3235*f4a2713aSLionel Sambuc FindLocalExternScope FindLocals(Result); 3236*f4a2713aSLionel Sambuc // Walk through the declarations in this Scope. 3237*f4a2713aSLionel Sambuc for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 3238*f4a2713aSLionel Sambuc D != DEnd; ++D) { 3239*f4a2713aSLionel Sambuc if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) 3240*f4a2713aSLionel Sambuc if ((ND = Result.getAcceptableDecl(ND))) { 3241*f4a2713aSLionel Sambuc Consumer.FoundDecl(ND, Visited.checkHidden(ND), 0, false); 3242*f4a2713aSLionel Sambuc Visited.add(ND); 3243*f4a2713aSLionel Sambuc } 3244*f4a2713aSLionel Sambuc } 3245*f4a2713aSLionel Sambuc } 3246*f4a2713aSLionel Sambuc 3247*f4a2713aSLionel Sambuc // FIXME: C++ [temp.local]p8 3248*f4a2713aSLionel Sambuc DeclContext *Entity = 0; 3249*f4a2713aSLionel Sambuc if (S->getEntity()) { 3250*f4a2713aSLionel Sambuc // Look into this scope's declaration context, along with any of its 3251*f4a2713aSLionel Sambuc // parent lookup contexts (e.g., enclosing classes), up to the point 3252*f4a2713aSLionel Sambuc // where we hit the context stored in the next outer scope. 3253*f4a2713aSLionel Sambuc Entity = S->getEntity(); 3254*f4a2713aSLionel Sambuc DeclContext *OuterCtx = findOuterContext(S).first; // FIXME 3255*f4a2713aSLionel Sambuc 3256*f4a2713aSLionel Sambuc for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); 3257*f4a2713aSLionel Sambuc Ctx = Ctx->getLookupParent()) { 3258*f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 3259*f4a2713aSLionel Sambuc if (Method->isInstanceMethod()) { 3260*f4a2713aSLionel Sambuc // For instance methods, look for ivars in the method's interface. 3261*f4a2713aSLionel Sambuc LookupResult IvarResult(Result.getSema(), Result.getLookupName(), 3262*f4a2713aSLionel Sambuc Result.getNameLoc(), Sema::LookupMemberName); 3263*f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { 3264*f4a2713aSLionel Sambuc LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, 3265*f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited); 3266*f4a2713aSLionel Sambuc } 3267*f4a2713aSLionel Sambuc } 3268*f4a2713aSLionel Sambuc 3269*f4a2713aSLionel Sambuc // We've already performed all of the name lookup that we need 3270*f4a2713aSLionel Sambuc // to for Objective-C methods; the next context will be the 3271*f4a2713aSLionel Sambuc // outer scope. 3272*f4a2713aSLionel Sambuc break; 3273*f4a2713aSLionel Sambuc } 3274*f4a2713aSLionel Sambuc 3275*f4a2713aSLionel Sambuc if (Ctx->isFunctionOrMethod()) 3276*f4a2713aSLionel Sambuc continue; 3277*f4a2713aSLionel Sambuc 3278*f4a2713aSLionel Sambuc LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, 3279*f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited); 3280*f4a2713aSLionel Sambuc } 3281*f4a2713aSLionel Sambuc } else if (!S->getParent()) { 3282*f4a2713aSLionel Sambuc // Look into the translation unit scope. We walk through the translation 3283*f4a2713aSLionel Sambuc // unit's declaration context, because the Scope itself won't have all of 3284*f4a2713aSLionel Sambuc // the declarations if we loaded a precompiled header. 3285*f4a2713aSLionel Sambuc // FIXME: We would like the translation unit's Scope object to point to the 3286*f4a2713aSLionel Sambuc // translation unit, so we don't need this special "if" branch. However, 3287*f4a2713aSLionel Sambuc // doing so would force the normal C++ name-lookup code to look into the 3288*f4a2713aSLionel Sambuc // translation unit decl when the IdentifierInfo chains would suffice. 3289*f4a2713aSLionel Sambuc // Once we fix that problem (which is part of a more general "don't look 3290*f4a2713aSLionel Sambuc // in DeclContexts unless we have to" optimization), we can eliminate this. 3291*f4a2713aSLionel Sambuc Entity = Result.getSema().Context.getTranslationUnitDecl(); 3292*f4a2713aSLionel Sambuc LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, 3293*f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited); 3294*f4a2713aSLionel Sambuc } 3295*f4a2713aSLionel Sambuc 3296*f4a2713aSLionel Sambuc if (Entity) { 3297*f4a2713aSLionel Sambuc // Lookup visible declarations in any namespaces found by using 3298*f4a2713aSLionel Sambuc // directives. 3299*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet::const_iterator UI, UEnd; 3300*f4a2713aSLionel Sambuc llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity); 3301*f4a2713aSLionel Sambuc for (; UI != UEnd; ++UI) 3302*f4a2713aSLionel Sambuc LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()), 3303*f4a2713aSLionel Sambuc Result, /*QualifiedNameLookup=*/false, 3304*f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited); 3305*f4a2713aSLionel Sambuc } 3306*f4a2713aSLionel Sambuc 3307*f4a2713aSLionel Sambuc // Lookup names in the parent scope. 3308*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3309*f4a2713aSLionel Sambuc LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); 3310*f4a2713aSLionel Sambuc } 3311*f4a2713aSLionel Sambuc 3312*f4a2713aSLionel Sambuc void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, 3313*f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer, 3314*f4a2713aSLionel Sambuc bool IncludeGlobalScope) { 3315*f4a2713aSLionel Sambuc // Determine the set of using directives available during 3316*f4a2713aSLionel Sambuc // unqualified name lookup. 3317*f4a2713aSLionel Sambuc Scope *Initial = S; 3318*f4a2713aSLionel Sambuc UnqualUsingDirectiveSet UDirs; 3319*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) { 3320*f4a2713aSLionel Sambuc // Find the first namespace or translation-unit scope. 3321*f4a2713aSLionel Sambuc while (S && !isNamespaceOrTranslationUnitScope(S)) 3322*f4a2713aSLionel Sambuc S = S->getParent(); 3323*f4a2713aSLionel Sambuc 3324*f4a2713aSLionel Sambuc UDirs.visitScopeChain(Initial, S); 3325*f4a2713aSLionel Sambuc } 3326*f4a2713aSLionel Sambuc UDirs.done(); 3327*f4a2713aSLionel Sambuc 3328*f4a2713aSLionel Sambuc // Look for visible declarations. 3329*f4a2713aSLionel Sambuc LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 3330*f4a2713aSLionel Sambuc Result.setAllowHidden(Consumer.includeHiddenDecls()); 3331*f4a2713aSLionel Sambuc VisibleDeclsRecord Visited; 3332*f4a2713aSLionel Sambuc if (!IncludeGlobalScope) 3333*f4a2713aSLionel Sambuc Visited.visitedContext(Context.getTranslationUnitDecl()); 3334*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3335*f4a2713aSLionel Sambuc ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); 3336*f4a2713aSLionel Sambuc } 3337*f4a2713aSLionel Sambuc 3338*f4a2713aSLionel Sambuc void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, 3339*f4a2713aSLionel Sambuc VisibleDeclConsumer &Consumer, 3340*f4a2713aSLionel Sambuc bool IncludeGlobalScope) { 3341*f4a2713aSLionel Sambuc LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 3342*f4a2713aSLionel Sambuc Result.setAllowHidden(Consumer.includeHiddenDecls()); 3343*f4a2713aSLionel Sambuc VisibleDeclsRecord Visited; 3344*f4a2713aSLionel Sambuc if (!IncludeGlobalScope) 3345*f4a2713aSLionel Sambuc Visited.visitedContext(Context.getTranslationUnitDecl()); 3346*f4a2713aSLionel Sambuc ShadowContextRAII Shadow(Visited); 3347*f4a2713aSLionel Sambuc ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, 3348*f4a2713aSLionel Sambuc /*InBaseClass=*/false, Consumer, Visited); 3349*f4a2713aSLionel Sambuc } 3350*f4a2713aSLionel Sambuc 3351*f4a2713aSLionel Sambuc /// LookupOrCreateLabel - Do a name lookup of a label with the specified name. 3352*f4a2713aSLionel Sambuc /// If GnuLabelLoc is a valid source location, then this is a definition 3353*f4a2713aSLionel Sambuc /// of an __label__ label name, otherwise it is a normal label definition 3354*f4a2713aSLionel Sambuc /// or use. 3355*f4a2713aSLionel Sambuc LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc, 3356*f4a2713aSLionel Sambuc SourceLocation GnuLabelLoc) { 3357*f4a2713aSLionel Sambuc // Do a lookup to see if we have a label with this name already. 3358*f4a2713aSLionel Sambuc NamedDecl *Res = 0; 3359*f4a2713aSLionel Sambuc 3360*f4a2713aSLionel Sambuc if (GnuLabelLoc.isValid()) { 3361*f4a2713aSLionel Sambuc // Local label definitions always shadow existing labels. 3362*f4a2713aSLionel Sambuc Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc); 3363*f4a2713aSLionel Sambuc Scope *S = CurScope; 3364*f4a2713aSLionel Sambuc PushOnScopeChains(Res, S, true); 3365*f4a2713aSLionel Sambuc return cast<LabelDecl>(Res); 3366*f4a2713aSLionel Sambuc } 3367*f4a2713aSLionel Sambuc 3368*f4a2713aSLionel Sambuc // Not a GNU local label. 3369*f4a2713aSLionel Sambuc Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration); 3370*f4a2713aSLionel Sambuc // If we found a label, check to see if it is in the same context as us. 3371*f4a2713aSLionel Sambuc // When in a Block, we don't want to reuse a label in an enclosing function. 3372*f4a2713aSLionel Sambuc if (Res && Res->getDeclContext() != CurContext) 3373*f4a2713aSLionel Sambuc Res = 0; 3374*f4a2713aSLionel Sambuc if (Res == 0) { 3375*f4a2713aSLionel Sambuc // If not forward referenced or defined already, create the backing decl. 3376*f4a2713aSLionel Sambuc Res = LabelDecl::Create(Context, CurContext, Loc, II); 3377*f4a2713aSLionel Sambuc Scope *S = CurScope->getFnParent(); 3378*f4a2713aSLionel Sambuc assert(S && "Not in a function?"); 3379*f4a2713aSLionel Sambuc PushOnScopeChains(Res, S, true); 3380*f4a2713aSLionel Sambuc } 3381*f4a2713aSLionel Sambuc return cast<LabelDecl>(Res); 3382*f4a2713aSLionel Sambuc } 3383*f4a2713aSLionel Sambuc 3384*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 3385*f4a2713aSLionel Sambuc // Typo correction 3386*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 3387*f4a2713aSLionel Sambuc 3388*f4a2713aSLionel Sambuc namespace { 3389*f4a2713aSLionel Sambuc 3390*f4a2713aSLionel Sambuc typedef SmallVector<TypoCorrection, 1> TypoResultList; 3391*f4a2713aSLionel Sambuc typedef llvm::StringMap<TypoResultList, llvm::BumpPtrAllocator> TypoResultsMap; 3392*f4a2713aSLionel Sambuc typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap; 3393*f4a2713aSLionel Sambuc 3394*f4a2713aSLionel Sambuc static const unsigned MaxTypoDistanceResultSets = 5; 3395*f4a2713aSLionel Sambuc 3396*f4a2713aSLionel Sambuc class TypoCorrectionConsumer : public VisibleDeclConsumer { 3397*f4a2713aSLionel Sambuc /// \brief The name written that is a typo in the source. 3398*f4a2713aSLionel Sambuc StringRef Typo; 3399*f4a2713aSLionel Sambuc 3400*f4a2713aSLionel Sambuc /// \brief The results found that have the smallest edit distance 3401*f4a2713aSLionel Sambuc /// found (so far) with the typo name. 3402*f4a2713aSLionel Sambuc /// 3403*f4a2713aSLionel Sambuc /// The pointer value being set to the current DeclContext indicates 3404*f4a2713aSLionel Sambuc /// whether there is a keyword with this name. 3405*f4a2713aSLionel Sambuc TypoEditDistanceMap CorrectionResults; 3406*f4a2713aSLionel Sambuc 3407*f4a2713aSLionel Sambuc Sema &SemaRef; 3408*f4a2713aSLionel Sambuc 3409*f4a2713aSLionel Sambuc public: 3410*f4a2713aSLionel Sambuc explicit TypoCorrectionConsumer(Sema &SemaRef, IdentifierInfo *Typo) 3411*f4a2713aSLionel Sambuc : Typo(Typo->getName()), 3412*f4a2713aSLionel Sambuc SemaRef(SemaRef) {} 3413*f4a2713aSLionel Sambuc 3414*f4a2713aSLionel Sambuc bool includeHiddenDecls() const { return true; } 3415*f4a2713aSLionel Sambuc 3416*f4a2713aSLionel Sambuc virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, 3417*f4a2713aSLionel Sambuc bool InBaseClass); 3418*f4a2713aSLionel Sambuc void FoundName(StringRef Name); 3419*f4a2713aSLionel Sambuc void addKeywordResult(StringRef Keyword); 3420*f4a2713aSLionel Sambuc void addName(StringRef Name, NamedDecl *ND, NestedNameSpecifier *NNS = NULL, 3421*f4a2713aSLionel Sambuc bool isKeyword = false); 3422*f4a2713aSLionel Sambuc void addCorrection(TypoCorrection Correction); 3423*f4a2713aSLionel Sambuc 3424*f4a2713aSLionel Sambuc typedef TypoResultsMap::iterator result_iterator; 3425*f4a2713aSLionel Sambuc typedef TypoEditDistanceMap::iterator distance_iterator; 3426*f4a2713aSLionel Sambuc distance_iterator begin() { return CorrectionResults.begin(); } 3427*f4a2713aSLionel Sambuc distance_iterator end() { return CorrectionResults.end(); } 3428*f4a2713aSLionel Sambuc void erase(distance_iterator I) { CorrectionResults.erase(I); } 3429*f4a2713aSLionel Sambuc unsigned size() const { return CorrectionResults.size(); } 3430*f4a2713aSLionel Sambuc bool empty() const { return CorrectionResults.empty(); } 3431*f4a2713aSLionel Sambuc 3432*f4a2713aSLionel Sambuc TypoResultList &operator[](StringRef Name) { 3433*f4a2713aSLionel Sambuc return CorrectionResults.begin()->second[Name]; 3434*f4a2713aSLionel Sambuc } 3435*f4a2713aSLionel Sambuc 3436*f4a2713aSLionel Sambuc unsigned getBestEditDistance(bool Normalized) { 3437*f4a2713aSLionel Sambuc if (CorrectionResults.empty()) 3438*f4a2713aSLionel Sambuc return (std::numeric_limits<unsigned>::max)(); 3439*f4a2713aSLionel Sambuc 3440*f4a2713aSLionel Sambuc unsigned BestED = CorrectionResults.begin()->first; 3441*f4a2713aSLionel Sambuc return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : BestED; 3442*f4a2713aSLionel Sambuc } 3443*f4a2713aSLionel Sambuc 3444*f4a2713aSLionel Sambuc TypoResultsMap &getBestResults() { 3445*f4a2713aSLionel Sambuc return CorrectionResults.begin()->second; 3446*f4a2713aSLionel Sambuc } 3447*f4a2713aSLionel Sambuc 3448*f4a2713aSLionel Sambuc }; 3449*f4a2713aSLionel Sambuc 3450*f4a2713aSLionel Sambuc } 3451*f4a2713aSLionel Sambuc 3452*f4a2713aSLionel Sambuc void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, 3453*f4a2713aSLionel Sambuc DeclContext *Ctx, bool InBaseClass) { 3454*f4a2713aSLionel Sambuc // Don't consider hidden names for typo correction. 3455*f4a2713aSLionel Sambuc if (Hiding) 3456*f4a2713aSLionel Sambuc return; 3457*f4a2713aSLionel Sambuc 3458*f4a2713aSLionel Sambuc // Only consider entities with identifiers for names, ignoring 3459*f4a2713aSLionel Sambuc // special names (constructors, overloaded operators, selectors, 3460*f4a2713aSLionel Sambuc // etc.). 3461*f4a2713aSLionel Sambuc IdentifierInfo *Name = ND->getIdentifier(); 3462*f4a2713aSLionel Sambuc if (!Name) 3463*f4a2713aSLionel Sambuc return; 3464*f4a2713aSLionel Sambuc 3465*f4a2713aSLionel Sambuc // Only consider visible declarations and declarations from modules with 3466*f4a2713aSLionel Sambuc // names that exactly match. 3467*f4a2713aSLionel Sambuc if (!LookupResult::isVisible(SemaRef, ND) && Name->getName() != Typo && 3468*f4a2713aSLionel Sambuc !findAcceptableDecl(SemaRef, ND)) 3469*f4a2713aSLionel Sambuc return; 3470*f4a2713aSLionel Sambuc 3471*f4a2713aSLionel Sambuc FoundName(Name->getName()); 3472*f4a2713aSLionel Sambuc } 3473*f4a2713aSLionel Sambuc 3474*f4a2713aSLionel Sambuc void TypoCorrectionConsumer::FoundName(StringRef Name) { 3475*f4a2713aSLionel Sambuc // Compute the edit distance between the typo and the name of this 3476*f4a2713aSLionel Sambuc // entity, and add the identifier to the list of results. 3477*f4a2713aSLionel Sambuc addName(Name, NULL); 3478*f4a2713aSLionel Sambuc } 3479*f4a2713aSLionel Sambuc 3480*f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) { 3481*f4a2713aSLionel Sambuc // Compute the edit distance between the typo and this keyword, 3482*f4a2713aSLionel Sambuc // and add the keyword to the list of results. 3483*f4a2713aSLionel Sambuc addName(Keyword, NULL, NULL, true); 3484*f4a2713aSLionel Sambuc } 3485*f4a2713aSLionel Sambuc 3486*f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND, 3487*f4a2713aSLionel Sambuc NestedNameSpecifier *NNS, bool isKeyword) { 3488*f4a2713aSLionel Sambuc // Use a simple length-based heuristic to determine the minimum possible 3489*f4a2713aSLionel Sambuc // edit distance. If the minimum isn't good enough, bail out early. 3490*f4a2713aSLionel Sambuc unsigned MinED = abs((int)Name.size() - (int)Typo.size()); 3491*f4a2713aSLionel Sambuc if (MinED && Typo.size() / MinED < 3) 3492*f4a2713aSLionel Sambuc return; 3493*f4a2713aSLionel Sambuc 3494*f4a2713aSLionel Sambuc // Compute an upper bound on the allowable edit distance, so that the 3495*f4a2713aSLionel Sambuc // edit-distance algorithm can short-circuit. 3496*f4a2713aSLionel Sambuc unsigned UpperBound = (Typo.size() + 2) / 3 + 1; 3497*f4a2713aSLionel Sambuc unsigned ED = Typo.edit_distance(Name, true, UpperBound); 3498*f4a2713aSLionel Sambuc if (ED >= UpperBound) return; 3499*f4a2713aSLionel Sambuc 3500*f4a2713aSLionel Sambuc TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED); 3501*f4a2713aSLionel Sambuc if (isKeyword) TC.makeKeyword(); 3502*f4a2713aSLionel Sambuc addCorrection(TC); 3503*f4a2713aSLionel Sambuc } 3504*f4a2713aSLionel Sambuc 3505*f4a2713aSLionel Sambuc void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) { 3506*f4a2713aSLionel Sambuc StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName(); 3507*f4a2713aSLionel Sambuc TypoResultList &CList = 3508*f4a2713aSLionel Sambuc CorrectionResults[Correction.getEditDistance(false)][Name]; 3509*f4a2713aSLionel Sambuc 3510*f4a2713aSLionel Sambuc if (!CList.empty() && !CList.back().isResolved()) 3511*f4a2713aSLionel Sambuc CList.pop_back(); 3512*f4a2713aSLionel Sambuc if (NamedDecl *NewND = Correction.getCorrectionDecl()) { 3513*f4a2713aSLionel Sambuc std::string CorrectionStr = Correction.getAsString(SemaRef.getLangOpts()); 3514*f4a2713aSLionel Sambuc for (TypoResultList::iterator RI = CList.begin(), RIEnd = CList.end(); 3515*f4a2713aSLionel Sambuc RI != RIEnd; ++RI) { 3516*f4a2713aSLionel Sambuc // If the Correction refers to a decl already in the result list, 3517*f4a2713aSLionel Sambuc // replace the existing result if the string representation of Correction 3518*f4a2713aSLionel Sambuc // comes before the current result alphabetically, then stop as there is 3519*f4a2713aSLionel Sambuc // nothing more to be done to add Correction to the candidate set. 3520*f4a2713aSLionel Sambuc if (RI->getCorrectionDecl() == NewND) { 3521*f4a2713aSLionel Sambuc if (CorrectionStr < RI->getAsString(SemaRef.getLangOpts())) 3522*f4a2713aSLionel Sambuc *RI = Correction; 3523*f4a2713aSLionel Sambuc return; 3524*f4a2713aSLionel Sambuc } 3525*f4a2713aSLionel Sambuc } 3526*f4a2713aSLionel Sambuc } 3527*f4a2713aSLionel Sambuc if (CList.empty() || Correction.isResolved()) 3528*f4a2713aSLionel Sambuc CList.push_back(Correction); 3529*f4a2713aSLionel Sambuc 3530*f4a2713aSLionel Sambuc while (CorrectionResults.size() > MaxTypoDistanceResultSets) 3531*f4a2713aSLionel Sambuc erase(llvm::prior(CorrectionResults.end())); 3532*f4a2713aSLionel Sambuc } 3533*f4a2713aSLionel Sambuc 3534*f4a2713aSLionel Sambuc // Fill the supplied vector with the IdentifierInfo pointers for each piece of 3535*f4a2713aSLionel Sambuc // the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::", 3536*f4a2713aSLionel Sambuc // fill the vector with the IdentifierInfo pointers for "foo" and "bar"). 3537*f4a2713aSLionel Sambuc static void getNestedNameSpecifierIdentifiers( 3538*f4a2713aSLionel Sambuc NestedNameSpecifier *NNS, 3539*f4a2713aSLionel Sambuc SmallVectorImpl<const IdentifierInfo*> &Identifiers) { 3540*f4a2713aSLionel Sambuc if (NestedNameSpecifier *Prefix = NNS->getPrefix()) 3541*f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(Prefix, Identifiers); 3542*f4a2713aSLionel Sambuc else 3543*f4a2713aSLionel Sambuc Identifiers.clear(); 3544*f4a2713aSLionel Sambuc 3545*f4a2713aSLionel Sambuc const IdentifierInfo *II = NULL; 3546*f4a2713aSLionel Sambuc 3547*f4a2713aSLionel Sambuc switch (NNS->getKind()) { 3548*f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier: 3549*f4a2713aSLionel Sambuc II = NNS->getAsIdentifier(); 3550*f4a2713aSLionel Sambuc break; 3551*f4a2713aSLionel Sambuc 3552*f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace: 3553*f4a2713aSLionel Sambuc if (NNS->getAsNamespace()->isAnonymousNamespace()) 3554*f4a2713aSLionel Sambuc return; 3555*f4a2713aSLionel Sambuc II = NNS->getAsNamespace()->getIdentifier(); 3556*f4a2713aSLionel Sambuc break; 3557*f4a2713aSLionel Sambuc 3558*f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias: 3559*f4a2713aSLionel Sambuc II = NNS->getAsNamespaceAlias()->getIdentifier(); 3560*f4a2713aSLionel Sambuc break; 3561*f4a2713aSLionel Sambuc 3562*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate: 3563*f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec: 3564*f4a2713aSLionel Sambuc II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier(); 3565*f4a2713aSLionel Sambuc break; 3566*f4a2713aSLionel Sambuc 3567*f4a2713aSLionel Sambuc case NestedNameSpecifier::Global: 3568*f4a2713aSLionel Sambuc return; 3569*f4a2713aSLionel Sambuc } 3570*f4a2713aSLionel Sambuc 3571*f4a2713aSLionel Sambuc if (II) 3572*f4a2713aSLionel Sambuc Identifiers.push_back(II); 3573*f4a2713aSLionel Sambuc } 3574*f4a2713aSLionel Sambuc 3575*f4a2713aSLionel Sambuc namespace { 3576*f4a2713aSLionel Sambuc 3577*f4a2713aSLionel Sambuc class SpecifierInfo { 3578*f4a2713aSLionel Sambuc public: 3579*f4a2713aSLionel Sambuc DeclContext* DeclCtx; 3580*f4a2713aSLionel Sambuc NestedNameSpecifier* NameSpecifier; 3581*f4a2713aSLionel Sambuc unsigned EditDistance; 3582*f4a2713aSLionel Sambuc 3583*f4a2713aSLionel Sambuc SpecifierInfo(DeclContext *Ctx, NestedNameSpecifier *NNS, unsigned ED) 3584*f4a2713aSLionel Sambuc : DeclCtx(Ctx), NameSpecifier(NNS), EditDistance(ED) {} 3585*f4a2713aSLionel Sambuc }; 3586*f4a2713aSLionel Sambuc 3587*f4a2713aSLionel Sambuc typedef SmallVector<DeclContext*, 4> DeclContextList; 3588*f4a2713aSLionel Sambuc typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList; 3589*f4a2713aSLionel Sambuc 3590*f4a2713aSLionel Sambuc class NamespaceSpecifierSet { 3591*f4a2713aSLionel Sambuc ASTContext &Context; 3592*f4a2713aSLionel Sambuc DeclContextList CurContextChain; 3593*f4a2713aSLionel Sambuc std::string CurNameSpecifier; 3594*f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers; 3595*f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers; 3596*f4a2713aSLionel Sambuc bool isSorted; 3597*f4a2713aSLionel Sambuc 3598*f4a2713aSLionel Sambuc SpecifierInfoList Specifiers; 3599*f4a2713aSLionel Sambuc llvm::SmallSetVector<unsigned, 4> Distances; 3600*f4a2713aSLionel Sambuc llvm::DenseMap<unsigned, SpecifierInfoList> DistanceMap; 3601*f4a2713aSLionel Sambuc 3602*f4a2713aSLionel Sambuc /// \brief Helper for building the list of DeclContexts between the current 3603*f4a2713aSLionel Sambuc /// context and the top of the translation unit 3604*f4a2713aSLionel Sambuc static DeclContextList BuildContextChain(DeclContext *Start); 3605*f4a2713aSLionel Sambuc 3606*f4a2713aSLionel Sambuc void SortNamespaces(); 3607*f4a2713aSLionel Sambuc 3608*f4a2713aSLionel Sambuc public: 3609*f4a2713aSLionel Sambuc NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext, 3610*f4a2713aSLionel Sambuc CXXScopeSpec *CurScopeSpec) 3611*f4a2713aSLionel Sambuc : Context(Context), CurContextChain(BuildContextChain(CurContext)), 3612*f4a2713aSLionel Sambuc isSorted(false) { 3613*f4a2713aSLionel Sambuc if (NestedNameSpecifier *NNS = 3614*f4a2713aSLionel Sambuc CurScopeSpec ? CurScopeSpec->getScopeRep() : 0) { 3615*f4a2713aSLionel Sambuc llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier); 3616*f4a2713aSLionel Sambuc NNS->print(SpecifierOStream, Context.getPrintingPolicy()); 3617*f4a2713aSLionel Sambuc 3618*f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers); 3619*f4a2713aSLionel Sambuc } 3620*f4a2713aSLionel Sambuc // Build the list of identifiers that would be used for an absolute 3621*f4a2713aSLionel Sambuc // (from the global context) NestedNameSpecifier referring to the current 3622*f4a2713aSLionel Sambuc // context. 3623*f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), 3624*f4a2713aSLionel Sambuc CEnd = CurContextChain.rend(); 3625*f4a2713aSLionel Sambuc C != CEnd; ++C) { 3626*f4a2713aSLionel Sambuc if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) 3627*f4a2713aSLionel Sambuc CurContextIdentifiers.push_back(ND->getIdentifier()); 3628*f4a2713aSLionel Sambuc } 3629*f4a2713aSLionel Sambuc 3630*f4a2713aSLionel Sambuc // Add the global context as a NestedNameSpecifier 3631*f4a2713aSLionel Sambuc Distances.insert(1); 3632*f4a2713aSLionel Sambuc DistanceMap[1].push_back( 3633*f4a2713aSLionel Sambuc SpecifierInfo(cast<DeclContext>(Context.getTranslationUnitDecl()), 3634*f4a2713aSLionel Sambuc NestedNameSpecifier::GlobalSpecifier(Context), 1)); 3635*f4a2713aSLionel Sambuc } 3636*f4a2713aSLionel Sambuc 3637*f4a2713aSLionel Sambuc /// \brief Add the DeclContext (a namespace or record) to the set, computing 3638*f4a2713aSLionel Sambuc /// the corresponding NestedNameSpecifier and its distance in the process. 3639*f4a2713aSLionel Sambuc void AddNameSpecifier(DeclContext *Ctx); 3640*f4a2713aSLionel Sambuc 3641*f4a2713aSLionel Sambuc typedef SpecifierInfoList::iterator iterator; 3642*f4a2713aSLionel Sambuc iterator begin() { 3643*f4a2713aSLionel Sambuc if (!isSorted) SortNamespaces(); 3644*f4a2713aSLionel Sambuc return Specifiers.begin(); 3645*f4a2713aSLionel Sambuc } 3646*f4a2713aSLionel Sambuc iterator end() { return Specifiers.end(); } 3647*f4a2713aSLionel Sambuc }; 3648*f4a2713aSLionel Sambuc 3649*f4a2713aSLionel Sambuc } 3650*f4a2713aSLionel Sambuc 3651*f4a2713aSLionel Sambuc DeclContextList NamespaceSpecifierSet::BuildContextChain(DeclContext *Start) { 3652*f4a2713aSLionel Sambuc assert(Start && "Building a context chain from a null context"); 3653*f4a2713aSLionel Sambuc DeclContextList Chain; 3654*f4a2713aSLionel Sambuc for (DeclContext *DC = Start->getPrimaryContext(); DC != NULL; 3655*f4a2713aSLionel Sambuc DC = DC->getLookupParent()) { 3656*f4a2713aSLionel Sambuc NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC); 3657*f4a2713aSLionel Sambuc if (!DC->isInlineNamespace() && !DC->isTransparentContext() && 3658*f4a2713aSLionel Sambuc !(ND && ND->isAnonymousNamespace())) 3659*f4a2713aSLionel Sambuc Chain.push_back(DC->getPrimaryContext()); 3660*f4a2713aSLionel Sambuc } 3661*f4a2713aSLionel Sambuc return Chain; 3662*f4a2713aSLionel Sambuc } 3663*f4a2713aSLionel Sambuc 3664*f4a2713aSLionel Sambuc void NamespaceSpecifierSet::SortNamespaces() { 3665*f4a2713aSLionel Sambuc SmallVector<unsigned, 4> sortedDistances; 3666*f4a2713aSLionel Sambuc sortedDistances.append(Distances.begin(), Distances.end()); 3667*f4a2713aSLionel Sambuc 3668*f4a2713aSLionel Sambuc if (sortedDistances.size() > 1) 3669*f4a2713aSLionel Sambuc std::sort(sortedDistances.begin(), sortedDistances.end()); 3670*f4a2713aSLionel Sambuc 3671*f4a2713aSLionel Sambuc Specifiers.clear(); 3672*f4a2713aSLionel Sambuc for (SmallVectorImpl<unsigned>::iterator DI = sortedDistances.begin(), 3673*f4a2713aSLionel Sambuc DIEnd = sortedDistances.end(); 3674*f4a2713aSLionel Sambuc DI != DIEnd; ++DI) { 3675*f4a2713aSLionel Sambuc SpecifierInfoList &SpecList = DistanceMap[*DI]; 3676*f4a2713aSLionel Sambuc Specifiers.append(SpecList.begin(), SpecList.end()); 3677*f4a2713aSLionel Sambuc } 3678*f4a2713aSLionel Sambuc 3679*f4a2713aSLionel Sambuc isSorted = true; 3680*f4a2713aSLionel Sambuc } 3681*f4a2713aSLionel Sambuc 3682*f4a2713aSLionel Sambuc static unsigned BuildNestedNameSpecifier(ASTContext &Context, 3683*f4a2713aSLionel Sambuc DeclContextList &DeclChain, 3684*f4a2713aSLionel Sambuc NestedNameSpecifier *&NNS) { 3685*f4a2713aSLionel Sambuc unsigned NumSpecifiers = 0; 3686*f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = DeclChain.rbegin(), 3687*f4a2713aSLionel Sambuc CEnd = DeclChain.rend(); 3688*f4a2713aSLionel Sambuc C != CEnd; ++C) { 3689*f4a2713aSLionel Sambuc if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) { 3690*f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::Create(Context, NNS, ND); 3691*f4a2713aSLionel Sambuc ++NumSpecifiers; 3692*f4a2713aSLionel Sambuc } else if (RecordDecl *RD = dyn_cast_or_null<RecordDecl>(*C)) { 3693*f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(), 3694*f4a2713aSLionel Sambuc RD->getTypeForDecl()); 3695*f4a2713aSLionel Sambuc ++NumSpecifiers; 3696*f4a2713aSLionel Sambuc } 3697*f4a2713aSLionel Sambuc } 3698*f4a2713aSLionel Sambuc return NumSpecifiers; 3699*f4a2713aSLionel Sambuc } 3700*f4a2713aSLionel Sambuc 3701*f4a2713aSLionel Sambuc void NamespaceSpecifierSet::AddNameSpecifier(DeclContext *Ctx) { 3702*f4a2713aSLionel Sambuc NestedNameSpecifier *NNS = NULL; 3703*f4a2713aSLionel Sambuc unsigned NumSpecifiers = 0; 3704*f4a2713aSLionel Sambuc DeclContextList NamespaceDeclChain(BuildContextChain(Ctx)); 3705*f4a2713aSLionel Sambuc DeclContextList FullNamespaceDeclChain(NamespaceDeclChain); 3706*f4a2713aSLionel Sambuc 3707*f4a2713aSLionel Sambuc // Eliminate common elements from the two DeclContext chains. 3708*f4a2713aSLionel Sambuc for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), 3709*f4a2713aSLionel Sambuc CEnd = CurContextChain.rend(); 3710*f4a2713aSLionel Sambuc C != CEnd && !NamespaceDeclChain.empty() && 3711*f4a2713aSLionel Sambuc NamespaceDeclChain.back() == *C; ++C) { 3712*f4a2713aSLionel Sambuc NamespaceDeclChain.pop_back(); 3713*f4a2713aSLionel Sambuc } 3714*f4a2713aSLionel Sambuc 3715*f4a2713aSLionel Sambuc // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain 3716*f4a2713aSLionel Sambuc NumSpecifiers = BuildNestedNameSpecifier(Context, NamespaceDeclChain, NNS); 3717*f4a2713aSLionel Sambuc 3718*f4a2713aSLionel Sambuc // Add an explicit leading '::' specifier if needed. 3719*f4a2713aSLionel Sambuc if (NamespaceDeclChain.empty()) { 3720*f4a2713aSLionel Sambuc // Rebuild the NestedNameSpecifier as a globally-qualified specifier. 3721*f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::GlobalSpecifier(Context); 3722*f4a2713aSLionel Sambuc NumSpecifiers = 3723*f4a2713aSLionel Sambuc BuildNestedNameSpecifier(Context, FullNamespaceDeclChain, NNS); 3724*f4a2713aSLionel Sambuc } else if (NamedDecl *ND = 3725*f4a2713aSLionel Sambuc dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) { 3726*f4a2713aSLionel Sambuc IdentifierInfo *Name = ND->getIdentifier(); 3727*f4a2713aSLionel Sambuc bool SameNameSpecifier = false; 3728*f4a2713aSLionel Sambuc if (std::find(CurNameSpecifierIdentifiers.begin(), 3729*f4a2713aSLionel Sambuc CurNameSpecifierIdentifiers.end(), 3730*f4a2713aSLionel Sambuc Name) != CurNameSpecifierIdentifiers.end()) { 3731*f4a2713aSLionel Sambuc std::string NewNameSpecifier; 3732*f4a2713aSLionel Sambuc llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier); 3733*f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers; 3734*f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); 3735*f4a2713aSLionel Sambuc NNS->print(SpecifierOStream, Context.getPrintingPolicy()); 3736*f4a2713aSLionel Sambuc SpecifierOStream.flush(); 3737*f4a2713aSLionel Sambuc SameNameSpecifier = NewNameSpecifier == CurNameSpecifier; 3738*f4a2713aSLionel Sambuc } 3739*f4a2713aSLionel Sambuc if (SameNameSpecifier || 3740*f4a2713aSLionel Sambuc std::find(CurContextIdentifiers.begin(), CurContextIdentifiers.end(), 3741*f4a2713aSLionel Sambuc Name) != CurContextIdentifiers.end()) { 3742*f4a2713aSLionel Sambuc // Rebuild the NestedNameSpecifier as a globally-qualified specifier. 3743*f4a2713aSLionel Sambuc NNS = NestedNameSpecifier::GlobalSpecifier(Context); 3744*f4a2713aSLionel Sambuc NumSpecifiers = 3745*f4a2713aSLionel Sambuc BuildNestedNameSpecifier(Context, FullNamespaceDeclChain, NNS); 3746*f4a2713aSLionel Sambuc } 3747*f4a2713aSLionel Sambuc } 3748*f4a2713aSLionel Sambuc 3749*f4a2713aSLionel Sambuc // If the built NestedNameSpecifier would be replacing an existing 3750*f4a2713aSLionel Sambuc // NestedNameSpecifier, use the number of component identifiers that 3751*f4a2713aSLionel Sambuc // would need to be changed as the edit distance instead of the number 3752*f4a2713aSLionel Sambuc // of components in the built NestedNameSpecifier. 3753*f4a2713aSLionel Sambuc if (NNS && !CurNameSpecifierIdentifiers.empty()) { 3754*f4a2713aSLionel Sambuc SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers; 3755*f4a2713aSLionel Sambuc getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); 3756*f4a2713aSLionel Sambuc NumSpecifiers = llvm::ComputeEditDistance( 3757*f4a2713aSLionel Sambuc ArrayRef<const IdentifierInfo *>(CurNameSpecifierIdentifiers), 3758*f4a2713aSLionel Sambuc ArrayRef<const IdentifierInfo *>(NewNameSpecifierIdentifiers)); 3759*f4a2713aSLionel Sambuc } 3760*f4a2713aSLionel Sambuc 3761*f4a2713aSLionel Sambuc isSorted = false; 3762*f4a2713aSLionel Sambuc Distances.insert(NumSpecifiers); 3763*f4a2713aSLionel Sambuc DistanceMap[NumSpecifiers].push_back(SpecifierInfo(Ctx, NNS, NumSpecifiers)); 3764*f4a2713aSLionel Sambuc } 3765*f4a2713aSLionel Sambuc 3766*f4a2713aSLionel Sambuc /// \brief Perform name lookup for a possible result for typo correction. 3767*f4a2713aSLionel Sambuc static void LookupPotentialTypoResult(Sema &SemaRef, 3768*f4a2713aSLionel Sambuc LookupResult &Res, 3769*f4a2713aSLionel Sambuc IdentifierInfo *Name, 3770*f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec *SS, 3771*f4a2713aSLionel Sambuc DeclContext *MemberContext, 3772*f4a2713aSLionel Sambuc bool EnteringContext, 3773*f4a2713aSLionel Sambuc bool isObjCIvarLookup, 3774*f4a2713aSLionel Sambuc bool FindHidden) { 3775*f4a2713aSLionel Sambuc Res.suppressDiagnostics(); 3776*f4a2713aSLionel Sambuc Res.clear(); 3777*f4a2713aSLionel Sambuc Res.setLookupName(Name); 3778*f4a2713aSLionel Sambuc Res.setAllowHidden(FindHidden); 3779*f4a2713aSLionel Sambuc if (MemberContext) { 3780*f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { 3781*f4a2713aSLionel Sambuc if (isObjCIvarLookup) { 3782*f4a2713aSLionel Sambuc if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { 3783*f4a2713aSLionel Sambuc Res.addDecl(Ivar); 3784*f4a2713aSLionel Sambuc Res.resolveKind(); 3785*f4a2713aSLionel Sambuc return; 3786*f4a2713aSLionel Sambuc } 3787*f4a2713aSLionel Sambuc } 3788*f4a2713aSLionel Sambuc 3789*f4a2713aSLionel Sambuc if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(Name)) { 3790*f4a2713aSLionel Sambuc Res.addDecl(Prop); 3791*f4a2713aSLionel Sambuc Res.resolveKind(); 3792*f4a2713aSLionel Sambuc return; 3793*f4a2713aSLionel Sambuc } 3794*f4a2713aSLionel Sambuc } 3795*f4a2713aSLionel Sambuc 3796*f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Res, MemberContext); 3797*f4a2713aSLionel Sambuc return; 3798*f4a2713aSLionel Sambuc } 3799*f4a2713aSLionel Sambuc 3800*f4a2713aSLionel Sambuc SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, 3801*f4a2713aSLionel Sambuc EnteringContext); 3802*f4a2713aSLionel Sambuc 3803*f4a2713aSLionel Sambuc // Fake ivar lookup; this should really be part of 3804*f4a2713aSLionel Sambuc // LookupParsedName. 3805*f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { 3806*f4a2713aSLionel Sambuc if (Method->isInstanceMethod() && Method->getClassInterface() && 3807*f4a2713aSLionel Sambuc (Res.empty() || 3808*f4a2713aSLionel Sambuc (Res.isSingleResult() && 3809*f4a2713aSLionel Sambuc Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) { 3810*f4a2713aSLionel Sambuc if (ObjCIvarDecl *IV 3811*f4a2713aSLionel Sambuc = Method->getClassInterface()->lookupInstanceVariable(Name)) { 3812*f4a2713aSLionel Sambuc Res.addDecl(IV); 3813*f4a2713aSLionel Sambuc Res.resolveKind(); 3814*f4a2713aSLionel Sambuc } 3815*f4a2713aSLionel Sambuc } 3816*f4a2713aSLionel Sambuc } 3817*f4a2713aSLionel Sambuc } 3818*f4a2713aSLionel Sambuc 3819*f4a2713aSLionel Sambuc /// \brief Add keywords to the consumer as possible typo corrections. 3820*f4a2713aSLionel Sambuc static void AddKeywordsToConsumer(Sema &SemaRef, 3821*f4a2713aSLionel Sambuc TypoCorrectionConsumer &Consumer, 3822*f4a2713aSLionel Sambuc Scope *S, CorrectionCandidateCallback &CCC, 3823*f4a2713aSLionel Sambuc bool AfterNestedNameSpecifier) { 3824*f4a2713aSLionel Sambuc if (AfterNestedNameSpecifier) { 3825*f4a2713aSLionel Sambuc // For 'X::', we know exactly which keywords can appear next. 3826*f4a2713aSLionel Sambuc Consumer.addKeywordResult("template"); 3827*f4a2713aSLionel Sambuc if (CCC.WantExpressionKeywords) 3828*f4a2713aSLionel Sambuc Consumer.addKeywordResult("operator"); 3829*f4a2713aSLionel Sambuc return; 3830*f4a2713aSLionel Sambuc } 3831*f4a2713aSLionel Sambuc 3832*f4a2713aSLionel Sambuc if (CCC.WantObjCSuper) 3833*f4a2713aSLionel Sambuc Consumer.addKeywordResult("super"); 3834*f4a2713aSLionel Sambuc 3835*f4a2713aSLionel Sambuc if (CCC.WantTypeSpecifiers) { 3836*f4a2713aSLionel Sambuc // Add type-specifier keywords to the set of results. 3837*f4a2713aSLionel Sambuc static const char *const CTypeSpecs[] = { 3838*f4a2713aSLionel Sambuc "char", "const", "double", "enum", "float", "int", "long", "short", 3839*f4a2713aSLionel Sambuc "signed", "struct", "union", "unsigned", "void", "volatile", 3840*f4a2713aSLionel Sambuc "_Complex", "_Imaginary", 3841*f4a2713aSLionel Sambuc // storage-specifiers as well 3842*f4a2713aSLionel Sambuc "extern", "inline", "static", "typedef" 3843*f4a2713aSLionel Sambuc }; 3844*f4a2713aSLionel Sambuc 3845*f4a2713aSLionel Sambuc const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs); 3846*f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCTypeSpecs; ++I) 3847*f4a2713aSLionel Sambuc Consumer.addKeywordResult(CTypeSpecs[I]); 3848*f4a2713aSLionel Sambuc 3849*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().C99) 3850*f4a2713aSLionel Sambuc Consumer.addKeywordResult("restrict"); 3851*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) 3852*f4a2713aSLionel Sambuc Consumer.addKeywordResult("bool"); 3853*f4a2713aSLionel Sambuc else if (SemaRef.getLangOpts().C99) 3854*f4a2713aSLionel Sambuc Consumer.addKeywordResult("_Bool"); 3855*f4a2713aSLionel Sambuc 3856*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) { 3857*f4a2713aSLionel Sambuc Consumer.addKeywordResult("class"); 3858*f4a2713aSLionel Sambuc Consumer.addKeywordResult("typename"); 3859*f4a2713aSLionel Sambuc Consumer.addKeywordResult("wchar_t"); 3860*f4a2713aSLionel Sambuc 3861*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11) { 3862*f4a2713aSLionel Sambuc Consumer.addKeywordResult("char16_t"); 3863*f4a2713aSLionel Sambuc Consumer.addKeywordResult("char32_t"); 3864*f4a2713aSLionel Sambuc Consumer.addKeywordResult("constexpr"); 3865*f4a2713aSLionel Sambuc Consumer.addKeywordResult("decltype"); 3866*f4a2713aSLionel Sambuc Consumer.addKeywordResult("thread_local"); 3867*f4a2713aSLionel Sambuc } 3868*f4a2713aSLionel Sambuc } 3869*f4a2713aSLionel Sambuc 3870*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().GNUMode) 3871*f4a2713aSLionel Sambuc Consumer.addKeywordResult("typeof"); 3872*f4a2713aSLionel Sambuc } 3873*f4a2713aSLionel Sambuc 3874*f4a2713aSLionel Sambuc if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) { 3875*f4a2713aSLionel Sambuc Consumer.addKeywordResult("const_cast"); 3876*f4a2713aSLionel Sambuc Consumer.addKeywordResult("dynamic_cast"); 3877*f4a2713aSLionel Sambuc Consumer.addKeywordResult("reinterpret_cast"); 3878*f4a2713aSLionel Sambuc Consumer.addKeywordResult("static_cast"); 3879*f4a2713aSLionel Sambuc } 3880*f4a2713aSLionel Sambuc 3881*f4a2713aSLionel Sambuc if (CCC.WantExpressionKeywords) { 3882*f4a2713aSLionel Sambuc Consumer.addKeywordResult("sizeof"); 3883*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) { 3884*f4a2713aSLionel Sambuc Consumer.addKeywordResult("false"); 3885*f4a2713aSLionel Sambuc Consumer.addKeywordResult("true"); 3886*f4a2713aSLionel Sambuc } 3887*f4a2713aSLionel Sambuc 3888*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) { 3889*f4a2713aSLionel Sambuc static const char *const CXXExprs[] = { 3890*f4a2713aSLionel Sambuc "delete", "new", "operator", "throw", "typeid" 3891*f4a2713aSLionel Sambuc }; 3892*f4a2713aSLionel Sambuc const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs); 3893*f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCXXExprs; ++I) 3894*f4a2713aSLionel Sambuc Consumer.addKeywordResult(CXXExprs[I]); 3895*f4a2713aSLionel Sambuc 3896*f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(SemaRef.CurContext) && 3897*f4a2713aSLionel Sambuc cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance()) 3898*f4a2713aSLionel Sambuc Consumer.addKeywordResult("this"); 3899*f4a2713aSLionel Sambuc 3900*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11) { 3901*f4a2713aSLionel Sambuc Consumer.addKeywordResult("alignof"); 3902*f4a2713aSLionel Sambuc Consumer.addKeywordResult("nullptr"); 3903*f4a2713aSLionel Sambuc } 3904*f4a2713aSLionel Sambuc } 3905*f4a2713aSLionel Sambuc 3906*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().C11) { 3907*f4a2713aSLionel Sambuc // FIXME: We should not suggest _Alignof if the alignof macro 3908*f4a2713aSLionel Sambuc // is present. 3909*f4a2713aSLionel Sambuc Consumer.addKeywordResult("_Alignof"); 3910*f4a2713aSLionel Sambuc } 3911*f4a2713aSLionel Sambuc } 3912*f4a2713aSLionel Sambuc 3913*f4a2713aSLionel Sambuc if (CCC.WantRemainingKeywords) { 3914*f4a2713aSLionel Sambuc if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) { 3915*f4a2713aSLionel Sambuc // Statements. 3916*f4a2713aSLionel Sambuc static const char *const CStmts[] = { 3917*f4a2713aSLionel Sambuc "do", "else", "for", "goto", "if", "return", "switch", "while" }; 3918*f4a2713aSLionel Sambuc const unsigned NumCStmts = llvm::array_lengthof(CStmts); 3919*f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumCStmts; ++I) 3920*f4a2713aSLionel Sambuc Consumer.addKeywordResult(CStmts[I]); 3921*f4a2713aSLionel Sambuc 3922*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) { 3923*f4a2713aSLionel Sambuc Consumer.addKeywordResult("catch"); 3924*f4a2713aSLionel Sambuc Consumer.addKeywordResult("try"); 3925*f4a2713aSLionel Sambuc } 3926*f4a2713aSLionel Sambuc 3927*f4a2713aSLionel Sambuc if (S && S->getBreakParent()) 3928*f4a2713aSLionel Sambuc Consumer.addKeywordResult("break"); 3929*f4a2713aSLionel Sambuc 3930*f4a2713aSLionel Sambuc if (S && S->getContinueParent()) 3931*f4a2713aSLionel Sambuc Consumer.addKeywordResult("continue"); 3932*f4a2713aSLionel Sambuc 3933*f4a2713aSLionel Sambuc if (!SemaRef.getCurFunction()->SwitchStack.empty()) { 3934*f4a2713aSLionel Sambuc Consumer.addKeywordResult("case"); 3935*f4a2713aSLionel Sambuc Consumer.addKeywordResult("default"); 3936*f4a2713aSLionel Sambuc } 3937*f4a2713aSLionel Sambuc } else { 3938*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) { 3939*f4a2713aSLionel Sambuc Consumer.addKeywordResult("namespace"); 3940*f4a2713aSLionel Sambuc Consumer.addKeywordResult("template"); 3941*f4a2713aSLionel Sambuc } 3942*f4a2713aSLionel Sambuc 3943*f4a2713aSLionel Sambuc if (S && S->isClassScope()) { 3944*f4a2713aSLionel Sambuc Consumer.addKeywordResult("explicit"); 3945*f4a2713aSLionel Sambuc Consumer.addKeywordResult("friend"); 3946*f4a2713aSLionel Sambuc Consumer.addKeywordResult("mutable"); 3947*f4a2713aSLionel Sambuc Consumer.addKeywordResult("private"); 3948*f4a2713aSLionel Sambuc Consumer.addKeywordResult("protected"); 3949*f4a2713aSLionel Sambuc Consumer.addKeywordResult("public"); 3950*f4a2713aSLionel Sambuc Consumer.addKeywordResult("virtual"); 3951*f4a2713aSLionel Sambuc } 3952*f4a2713aSLionel Sambuc } 3953*f4a2713aSLionel Sambuc 3954*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) { 3955*f4a2713aSLionel Sambuc Consumer.addKeywordResult("using"); 3956*f4a2713aSLionel Sambuc 3957*f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11) 3958*f4a2713aSLionel Sambuc Consumer.addKeywordResult("static_assert"); 3959*f4a2713aSLionel Sambuc } 3960*f4a2713aSLionel Sambuc } 3961*f4a2713aSLionel Sambuc } 3962*f4a2713aSLionel Sambuc 3963*f4a2713aSLionel Sambuc static bool isCandidateViable(CorrectionCandidateCallback &CCC, 3964*f4a2713aSLionel Sambuc TypoCorrection &Candidate) { 3965*f4a2713aSLionel Sambuc Candidate.setCallbackDistance(CCC.RankCandidate(Candidate)); 3966*f4a2713aSLionel Sambuc return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance; 3967*f4a2713aSLionel Sambuc } 3968*f4a2713aSLionel Sambuc 3969*f4a2713aSLionel Sambuc /// \brief Check whether the declarations found for a typo correction are 3970*f4a2713aSLionel Sambuc /// visible, and if none of them are, convert the correction to an 'import 3971*f4a2713aSLionel Sambuc /// a module' correction. 3972*f4a2713aSLionel Sambuc static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC, 3973*f4a2713aSLionel Sambuc DeclarationName TypoName) { 3974*f4a2713aSLionel Sambuc if (TC.begin() == TC.end()) 3975*f4a2713aSLionel Sambuc return; 3976*f4a2713aSLionel Sambuc 3977*f4a2713aSLionel Sambuc TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end(); 3978*f4a2713aSLionel Sambuc 3979*f4a2713aSLionel Sambuc for (/**/; DI != DE; ++DI) 3980*f4a2713aSLionel Sambuc if (!LookupResult::isVisible(SemaRef, *DI)) 3981*f4a2713aSLionel Sambuc break; 3982*f4a2713aSLionel Sambuc // Nothing to do if all decls are visible. 3983*f4a2713aSLionel Sambuc if (DI == DE) 3984*f4a2713aSLionel Sambuc return; 3985*f4a2713aSLionel Sambuc 3986*f4a2713aSLionel Sambuc llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI); 3987*f4a2713aSLionel Sambuc bool AnyVisibleDecls = !NewDecls.empty(); 3988*f4a2713aSLionel Sambuc 3989*f4a2713aSLionel Sambuc for (/**/; DI != DE; ++DI) { 3990*f4a2713aSLionel Sambuc NamedDecl *VisibleDecl = *DI; 3991*f4a2713aSLionel Sambuc if (!LookupResult::isVisible(SemaRef, *DI)) 3992*f4a2713aSLionel Sambuc VisibleDecl = findAcceptableDecl(SemaRef, *DI); 3993*f4a2713aSLionel Sambuc 3994*f4a2713aSLionel Sambuc if (VisibleDecl) { 3995*f4a2713aSLionel Sambuc if (!AnyVisibleDecls) { 3996*f4a2713aSLionel Sambuc // Found a visible decl, discard all hidden ones. 3997*f4a2713aSLionel Sambuc AnyVisibleDecls = true; 3998*f4a2713aSLionel Sambuc NewDecls.clear(); 3999*f4a2713aSLionel Sambuc } 4000*f4a2713aSLionel Sambuc NewDecls.push_back(VisibleDecl); 4001*f4a2713aSLionel Sambuc } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate()) 4002*f4a2713aSLionel Sambuc NewDecls.push_back(*DI); 4003*f4a2713aSLionel Sambuc } 4004*f4a2713aSLionel Sambuc 4005*f4a2713aSLionel Sambuc if (NewDecls.empty()) 4006*f4a2713aSLionel Sambuc TC = TypoCorrection(); 4007*f4a2713aSLionel Sambuc else { 4008*f4a2713aSLionel Sambuc TC.setCorrectionDecls(NewDecls); 4009*f4a2713aSLionel Sambuc TC.setRequiresImport(!AnyVisibleDecls); 4010*f4a2713aSLionel Sambuc } 4011*f4a2713aSLionel Sambuc } 4012*f4a2713aSLionel Sambuc 4013*f4a2713aSLionel Sambuc /// \brief Try to "correct" a typo in the source code by finding 4014*f4a2713aSLionel Sambuc /// visible declarations whose names are similar to the name that was 4015*f4a2713aSLionel Sambuc /// present in the source code. 4016*f4a2713aSLionel Sambuc /// 4017*f4a2713aSLionel Sambuc /// \param TypoName the \c DeclarationNameInfo structure that contains 4018*f4a2713aSLionel Sambuc /// the name that was present in the source code along with its location. 4019*f4a2713aSLionel Sambuc /// 4020*f4a2713aSLionel Sambuc /// \param LookupKind the name-lookup criteria used to search for the name. 4021*f4a2713aSLionel Sambuc /// 4022*f4a2713aSLionel Sambuc /// \param S the scope in which name lookup occurs. 4023*f4a2713aSLionel Sambuc /// 4024*f4a2713aSLionel Sambuc /// \param SS the nested-name-specifier that precedes the name we're 4025*f4a2713aSLionel Sambuc /// looking for, if present. 4026*f4a2713aSLionel Sambuc /// 4027*f4a2713aSLionel Sambuc /// \param CCC A CorrectionCandidateCallback object that provides further 4028*f4a2713aSLionel Sambuc /// validation of typo correction candidates. It also provides flags for 4029*f4a2713aSLionel Sambuc /// determining the set of keywords permitted. 4030*f4a2713aSLionel Sambuc /// 4031*f4a2713aSLionel Sambuc /// \param MemberContext if non-NULL, the context in which to look for 4032*f4a2713aSLionel Sambuc /// a member access expression. 4033*f4a2713aSLionel Sambuc /// 4034*f4a2713aSLionel Sambuc /// \param EnteringContext whether we're entering the context described by 4035*f4a2713aSLionel Sambuc /// the nested-name-specifier SS. 4036*f4a2713aSLionel Sambuc /// 4037*f4a2713aSLionel Sambuc /// \param OPT when non-NULL, the search for visible declarations will 4038*f4a2713aSLionel Sambuc /// also walk the protocols in the qualified interfaces of \p OPT. 4039*f4a2713aSLionel Sambuc /// 4040*f4a2713aSLionel Sambuc /// \returns a \c TypoCorrection containing the corrected name if the typo 4041*f4a2713aSLionel Sambuc /// along with information such as the \c NamedDecl where the corrected name 4042*f4a2713aSLionel Sambuc /// was declared, and any additional \c NestedNameSpecifier needed to access 4043*f4a2713aSLionel Sambuc /// it (C++ only). The \c TypoCorrection is empty if there is no correction. 4044*f4a2713aSLionel Sambuc TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName, 4045*f4a2713aSLionel Sambuc Sema::LookupNameKind LookupKind, 4046*f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec *SS, 4047*f4a2713aSLionel Sambuc CorrectionCandidateCallback &CCC, 4048*f4a2713aSLionel Sambuc DeclContext *MemberContext, 4049*f4a2713aSLionel Sambuc bool EnteringContext, 4050*f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT, 4051*f4a2713aSLionel Sambuc bool RecordFailure) { 4052*f4a2713aSLionel Sambuc // Always let the ExternalSource have the first chance at correction, even 4053*f4a2713aSLionel Sambuc // if we would otherwise have given up. 4054*f4a2713aSLionel Sambuc if (ExternalSource) { 4055*f4a2713aSLionel Sambuc if (TypoCorrection Correction = ExternalSource->CorrectTypo( 4056*f4a2713aSLionel Sambuc TypoName, LookupKind, S, SS, CCC, MemberContext, EnteringContext, OPT)) 4057*f4a2713aSLionel Sambuc return Correction; 4058*f4a2713aSLionel Sambuc } 4059*f4a2713aSLionel Sambuc 4060*f4a2713aSLionel Sambuc if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking || 4061*f4a2713aSLionel Sambuc DisableTypoCorrection) 4062*f4a2713aSLionel Sambuc return TypoCorrection(); 4063*f4a2713aSLionel Sambuc 4064*f4a2713aSLionel Sambuc // In Microsoft mode, don't perform typo correction in a template member 4065*f4a2713aSLionel Sambuc // function dependent context because it interferes with the "lookup into 4066*f4a2713aSLionel Sambuc // dependent bases of class templates" feature. 4067*f4a2713aSLionel Sambuc if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && 4068*f4a2713aSLionel Sambuc isa<CXXMethodDecl>(CurContext)) 4069*f4a2713aSLionel Sambuc return TypoCorrection(); 4070*f4a2713aSLionel Sambuc 4071*f4a2713aSLionel Sambuc // We only attempt to correct typos for identifiers. 4072*f4a2713aSLionel Sambuc IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); 4073*f4a2713aSLionel Sambuc if (!Typo) 4074*f4a2713aSLionel Sambuc return TypoCorrection(); 4075*f4a2713aSLionel Sambuc 4076*f4a2713aSLionel Sambuc // If the scope specifier itself was invalid, don't try to correct 4077*f4a2713aSLionel Sambuc // typos. 4078*f4a2713aSLionel Sambuc if (SS && SS->isInvalid()) 4079*f4a2713aSLionel Sambuc return TypoCorrection(); 4080*f4a2713aSLionel Sambuc 4081*f4a2713aSLionel Sambuc // Never try to correct typos during template deduction or 4082*f4a2713aSLionel Sambuc // instantiation. 4083*f4a2713aSLionel Sambuc if (!ActiveTemplateInstantiations.empty()) 4084*f4a2713aSLionel Sambuc return TypoCorrection(); 4085*f4a2713aSLionel Sambuc 4086*f4a2713aSLionel Sambuc // Don't try to correct 'super'. 4087*f4a2713aSLionel Sambuc if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier()) 4088*f4a2713aSLionel Sambuc return TypoCorrection(); 4089*f4a2713aSLionel Sambuc 4090*f4a2713aSLionel Sambuc // Abort if typo correction already failed for this specific typo. 4091*f4a2713aSLionel Sambuc IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo); 4092*f4a2713aSLionel Sambuc if (locs != TypoCorrectionFailures.end() && 4093*f4a2713aSLionel Sambuc locs->second.count(TypoName.getLoc())) 4094*f4a2713aSLionel Sambuc return TypoCorrection(); 4095*f4a2713aSLionel Sambuc 4096*f4a2713aSLionel Sambuc // Don't try to correct the identifier "vector" when in AltiVec mode. 4097*f4a2713aSLionel Sambuc // TODO: Figure out why typo correction misbehaves in this case, fix it, and 4098*f4a2713aSLionel Sambuc // remove this workaround. 4099*f4a2713aSLionel Sambuc if (getLangOpts().AltiVec && Typo->isStr("vector")) 4100*f4a2713aSLionel Sambuc return TypoCorrection(); 4101*f4a2713aSLionel Sambuc 4102*f4a2713aSLionel Sambuc NamespaceSpecifierSet Namespaces(Context, CurContext, SS); 4103*f4a2713aSLionel Sambuc 4104*f4a2713aSLionel Sambuc TypoCorrectionConsumer Consumer(*this, Typo); 4105*f4a2713aSLionel Sambuc 4106*f4a2713aSLionel Sambuc // If a callback object considers an empty typo correction candidate to be 4107*f4a2713aSLionel Sambuc // viable, assume it does not do any actual validation of the candidates. 4108*f4a2713aSLionel Sambuc TypoCorrection EmptyCorrection; 4109*f4a2713aSLionel Sambuc bool ValidatingCallback = !isCandidateViable(CCC, EmptyCorrection); 4110*f4a2713aSLionel Sambuc 4111*f4a2713aSLionel Sambuc // Perform name lookup to find visible, similarly-named entities. 4112*f4a2713aSLionel Sambuc bool IsUnqualifiedLookup = false; 4113*f4a2713aSLionel Sambuc DeclContext *QualifiedDC = MemberContext; 4114*f4a2713aSLionel Sambuc if (MemberContext) { 4115*f4a2713aSLionel Sambuc LookupVisibleDecls(MemberContext, LookupKind, Consumer); 4116*f4a2713aSLionel Sambuc 4117*f4a2713aSLionel Sambuc // Look in qualified interfaces. 4118*f4a2713aSLionel Sambuc if (OPT) { 4119*f4a2713aSLionel Sambuc for (ObjCObjectPointerType::qual_iterator 4120*f4a2713aSLionel Sambuc I = OPT->qual_begin(), E = OPT->qual_end(); 4121*f4a2713aSLionel Sambuc I != E; ++I) 4122*f4a2713aSLionel Sambuc LookupVisibleDecls(*I, LookupKind, Consumer); 4123*f4a2713aSLionel Sambuc } 4124*f4a2713aSLionel Sambuc } else if (SS && SS->isSet()) { 4125*f4a2713aSLionel Sambuc QualifiedDC = computeDeclContext(*SS, EnteringContext); 4126*f4a2713aSLionel Sambuc if (!QualifiedDC) 4127*f4a2713aSLionel Sambuc return TypoCorrection(); 4128*f4a2713aSLionel Sambuc 4129*f4a2713aSLionel Sambuc // Provide a stop gap for files that are just seriously broken. Trying 4130*f4a2713aSLionel Sambuc // to correct all typos can turn into a HUGE performance penalty, causing 4131*f4a2713aSLionel Sambuc // some files to take minutes to get rejected by the parser. 4132*f4a2713aSLionel Sambuc if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) 4133*f4a2713aSLionel Sambuc return TypoCorrection(); 4134*f4a2713aSLionel Sambuc ++TyposCorrected; 4135*f4a2713aSLionel Sambuc 4136*f4a2713aSLionel Sambuc LookupVisibleDecls(QualifiedDC, LookupKind, Consumer); 4137*f4a2713aSLionel Sambuc } else { 4138*f4a2713aSLionel Sambuc IsUnqualifiedLookup = true; 4139*f4a2713aSLionel Sambuc UnqualifiedTyposCorrectedMap::iterator Cached 4140*f4a2713aSLionel Sambuc = UnqualifiedTyposCorrected.find(Typo); 4141*f4a2713aSLionel Sambuc if (Cached != UnqualifiedTyposCorrected.end()) { 4142*f4a2713aSLionel Sambuc // Add the cached value, unless it's a keyword or fails validation. In the 4143*f4a2713aSLionel Sambuc // keyword case, we'll end up adding the keyword below. 4144*f4a2713aSLionel Sambuc if (Cached->second) { 4145*f4a2713aSLionel Sambuc if (!Cached->second.isKeyword() && 4146*f4a2713aSLionel Sambuc isCandidateViable(CCC, Cached->second)) { 4147*f4a2713aSLionel Sambuc // Do not use correction that is unaccessible in the given scope. 4148*f4a2713aSLionel Sambuc NamedDecl *CorrectionDecl = Cached->second.getCorrectionDecl(); 4149*f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo(CorrectionDecl->getDeclName(), 4150*f4a2713aSLionel Sambuc CorrectionDecl->getLocation()); 4151*f4a2713aSLionel Sambuc LookupResult R(*this, NameInfo, LookupOrdinaryName); 4152*f4a2713aSLionel Sambuc if (LookupName(R, S)) 4153*f4a2713aSLionel Sambuc Consumer.addCorrection(Cached->second); 4154*f4a2713aSLionel Sambuc } 4155*f4a2713aSLionel Sambuc } else { 4156*f4a2713aSLionel Sambuc // Only honor no-correction cache hits when a callback that will validate 4157*f4a2713aSLionel Sambuc // correction candidates is not being used. 4158*f4a2713aSLionel Sambuc if (!ValidatingCallback) 4159*f4a2713aSLionel Sambuc return TypoCorrection(); 4160*f4a2713aSLionel Sambuc } 4161*f4a2713aSLionel Sambuc } 4162*f4a2713aSLionel Sambuc if (Cached == UnqualifiedTyposCorrected.end()) { 4163*f4a2713aSLionel Sambuc // Provide a stop gap for files that are just seriously broken. Trying 4164*f4a2713aSLionel Sambuc // to correct all typos can turn into a HUGE performance penalty, causing 4165*f4a2713aSLionel Sambuc // some files to take minutes to get rejected by the parser. 4166*f4a2713aSLionel Sambuc if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) 4167*f4a2713aSLionel Sambuc return TypoCorrection(); 4168*f4a2713aSLionel Sambuc } 4169*f4a2713aSLionel Sambuc } 4170*f4a2713aSLionel Sambuc 4171*f4a2713aSLionel Sambuc // Determine whether we are going to search in the various namespaces for 4172*f4a2713aSLionel Sambuc // corrections. 4173*f4a2713aSLionel Sambuc bool SearchNamespaces 4174*f4a2713aSLionel Sambuc = getLangOpts().CPlusPlus && 4175*f4a2713aSLionel Sambuc (IsUnqualifiedLookup || (SS && SS->isSet())); 4176*f4a2713aSLionel Sambuc // In a few cases we *only* want to search for corrections based on just 4177*f4a2713aSLionel Sambuc // adding or changing the nested name specifier. 4178*f4a2713aSLionel Sambuc unsigned TypoLen = Typo->getName().size(); 4179*f4a2713aSLionel Sambuc bool AllowOnlyNNSChanges = TypoLen < 3; 4180*f4a2713aSLionel Sambuc 4181*f4a2713aSLionel Sambuc if (IsUnqualifiedLookup || SearchNamespaces) { 4182*f4a2713aSLionel Sambuc // For unqualified lookup, look through all of the names that we have 4183*f4a2713aSLionel Sambuc // seen in this translation unit. 4184*f4a2713aSLionel Sambuc // FIXME: Re-add the ability to skip very unlikely potential corrections. 4185*f4a2713aSLionel Sambuc for (IdentifierTable::iterator I = Context.Idents.begin(), 4186*f4a2713aSLionel Sambuc IEnd = Context.Idents.end(); 4187*f4a2713aSLionel Sambuc I != IEnd; ++I) 4188*f4a2713aSLionel Sambuc Consumer.FoundName(I->getKey()); 4189*f4a2713aSLionel Sambuc 4190*f4a2713aSLionel Sambuc // Walk through identifiers in external identifier sources. 4191*f4a2713aSLionel Sambuc // FIXME: Re-add the ability to skip very unlikely potential corrections. 4192*f4a2713aSLionel Sambuc if (IdentifierInfoLookup *External 4193*f4a2713aSLionel Sambuc = Context.Idents.getExternalIdentifierLookup()) { 4194*f4a2713aSLionel Sambuc OwningPtr<IdentifierIterator> Iter(External->getIdentifiers()); 4195*f4a2713aSLionel Sambuc do { 4196*f4a2713aSLionel Sambuc StringRef Name = Iter->Next(); 4197*f4a2713aSLionel Sambuc if (Name.empty()) 4198*f4a2713aSLionel Sambuc break; 4199*f4a2713aSLionel Sambuc 4200*f4a2713aSLionel Sambuc Consumer.FoundName(Name); 4201*f4a2713aSLionel Sambuc } while (true); 4202*f4a2713aSLionel Sambuc } 4203*f4a2713aSLionel Sambuc } 4204*f4a2713aSLionel Sambuc 4205*f4a2713aSLionel Sambuc AddKeywordsToConsumer(*this, Consumer, S, CCC, SS && SS->isNotEmpty()); 4206*f4a2713aSLionel Sambuc 4207*f4a2713aSLionel Sambuc // If we haven't found anything, we're done. 4208*f4a2713aSLionel Sambuc if (Consumer.empty()) 4209*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 4210*f4a2713aSLionel Sambuc IsUnqualifiedLookup); 4211*f4a2713aSLionel Sambuc 4212*f4a2713aSLionel Sambuc // Make sure the best edit distance (prior to adding any namespace qualifiers) 4213*f4a2713aSLionel Sambuc // is not more that about a third of the length of the typo's identifier. 4214*f4a2713aSLionel Sambuc unsigned ED = Consumer.getBestEditDistance(true); 4215*f4a2713aSLionel Sambuc if (ED > 0 && TypoLen / ED < 3) 4216*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 4217*f4a2713aSLionel Sambuc IsUnqualifiedLookup); 4218*f4a2713aSLionel Sambuc 4219*f4a2713aSLionel Sambuc // Build the NestedNameSpecifiers for the KnownNamespaces, if we're going 4220*f4a2713aSLionel Sambuc // to search those namespaces. 4221*f4a2713aSLionel Sambuc if (SearchNamespaces) { 4222*f4a2713aSLionel Sambuc // Load any externally-known namespaces. 4223*f4a2713aSLionel Sambuc if (ExternalSource && !LoadedExternalKnownNamespaces) { 4224*f4a2713aSLionel Sambuc SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces; 4225*f4a2713aSLionel Sambuc LoadedExternalKnownNamespaces = true; 4226*f4a2713aSLionel Sambuc ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces); 4227*f4a2713aSLionel Sambuc for (unsigned I = 0, N = ExternalKnownNamespaces.size(); I != N; ++I) 4228*f4a2713aSLionel Sambuc KnownNamespaces[ExternalKnownNamespaces[I]] = true; 4229*f4a2713aSLionel Sambuc } 4230*f4a2713aSLionel Sambuc 4231*f4a2713aSLionel Sambuc for (llvm::MapVector<NamespaceDecl*, bool>::iterator 4232*f4a2713aSLionel Sambuc KNI = KnownNamespaces.begin(), 4233*f4a2713aSLionel Sambuc KNIEnd = KnownNamespaces.end(); 4234*f4a2713aSLionel Sambuc KNI != KNIEnd; ++KNI) 4235*f4a2713aSLionel Sambuc Namespaces.AddNameSpecifier(KNI->first); 4236*f4a2713aSLionel Sambuc 4237*f4a2713aSLionel Sambuc for (ASTContext::type_iterator TI = Context.types_begin(), 4238*f4a2713aSLionel Sambuc TIEnd = Context.types_end(); 4239*f4a2713aSLionel Sambuc TI != TIEnd; ++TI) { 4240*f4a2713aSLionel Sambuc if (CXXRecordDecl *CD = (*TI)->getAsCXXRecordDecl()) { 4241*f4a2713aSLionel Sambuc CD = CD->getCanonicalDecl(); 4242*f4a2713aSLionel Sambuc if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() && 4243*f4a2713aSLionel Sambuc !CD->isUnion() && 4244*f4a2713aSLionel Sambuc (CD->isBeingDefined() || CD->isCompleteDefinition())) 4245*f4a2713aSLionel Sambuc Namespaces.AddNameSpecifier(CD); 4246*f4a2713aSLionel Sambuc } 4247*f4a2713aSLionel Sambuc } 4248*f4a2713aSLionel Sambuc } 4249*f4a2713aSLionel Sambuc 4250*f4a2713aSLionel Sambuc // Weed out any names that could not be found by name lookup or, if a 4251*f4a2713aSLionel Sambuc // CorrectionCandidateCallback object was provided, failed validation. 4252*f4a2713aSLionel Sambuc SmallVector<TypoCorrection, 16> QualifiedResults; 4253*f4a2713aSLionel Sambuc LookupResult TmpRes(*this, TypoName, LookupKind); 4254*f4a2713aSLionel Sambuc TmpRes.suppressDiagnostics(); 4255*f4a2713aSLionel Sambuc while (!Consumer.empty()) { 4256*f4a2713aSLionel Sambuc TypoCorrectionConsumer::distance_iterator DI = Consumer.begin(); 4257*f4a2713aSLionel Sambuc for (TypoCorrectionConsumer::result_iterator I = DI->second.begin(), 4258*f4a2713aSLionel Sambuc IEnd = DI->second.end(); 4259*f4a2713aSLionel Sambuc I != IEnd; /* Increment in loop. */) { 4260*f4a2713aSLionel Sambuc // If we only want nested name specifier corrections, ignore potential 4261*f4a2713aSLionel Sambuc // corrections that have a different base identifier from the typo. 4262*f4a2713aSLionel Sambuc if (AllowOnlyNNSChanges && 4263*f4a2713aSLionel Sambuc I->second.front().getCorrectionAsIdentifierInfo() != Typo) { 4264*f4a2713aSLionel Sambuc TypoCorrectionConsumer::result_iterator Prev = I; 4265*f4a2713aSLionel Sambuc ++I; 4266*f4a2713aSLionel Sambuc DI->second.erase(Prev); 4267*f4a2713aSLionel Sambuc continue; 4268*f4a2713aSLionel Sambuc } 4269*f4a2713aSLionel Sambuc 4270*f4a2713aSLionel Sambuc // If the item already has been looked up or is a keyword, keep it. 4271*f4a2713aSLionel Sambuc // If a validator callback object was given, drop the correction 4272*f4a2713aSLionel Sambuc // unless it passes validation. 4273*f4a2713aSLionel Sambuc bool Viable = false; 4274*f4a2713aSLionel Sambuc for (TypoResultList::iterator RI = I->second.begin(); 4275*f4a2713aSLionel Sambuc RI != I->second.end(); /* Increment in loop. */) { 4276*f4a2713aSLionel Sambuc TypoResultList::iterator Prev = RI; 4277*f4a2713aSLionel Sambuc ++RI; 4278*f4a2713aSLionel Sambuc if (Prev->isResolved()) { 4279*f4a2713aSLionel Sambuc if (!isCandidateViable(CCC, *Prev)) 4280*f4a2713aSLionel Sambuc RI = I->second.erase(Prev); 4281*f4a2713aSLionel Sambuc else 4282*f4a2713aSLionel Sambuc Viable = true; 4283*f4a2713aSLionel Sambuc } 4284*f4a2713aSLionel Sambuc } 4285*f4a2713aSLionel Sambuc if (Viable || I->second.empty()) { 4286*f4a2713aSLionel Sambuc TypoCorrectionConsumer::result_iterator Prev = I; 4287*f4a2713aSLionel Sambuc ++I; 4288*f4a2713aSLionel Sambuc if (!Viable) 4289*f4a2713aSLionel Sambuc DI->second.erase(Prev); 4290*f4a2713aSLionel Sambuc continue; 4291*f4a2713aSLionel Sambuc } 4292*f4a2713aSLionel Sambuc assert(I->second.size() == 1 && "Expected a single unresolved candidate"); 4293*f4a2713aSLionel Sambuc 4294*f4a2713aSLionel Sambuc // Perform name lookup on this name. 4295*f4a2713aSLionel Sambuc TypoCorrection &Candidate = I->second.front(); 4296*f4a2713aSLionel Sambuc IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo(); 4297*f4a2713aSLionel Sambuc DeclContext *TempMemberContext = MemberContext; 4298*f4a2713aSLionel Sambuc CXXScopeSpec *TempSS = SS; 4299*f4a2713aSLionel Sambuc retry_lookup: 4300*f4a2713aSLionel Sambuc LookupPotentialTypoResult(*this, TmpRes, Name, S, TempSS, 4301*f4a2713aSLionel Sambuc TempMemberContext, EnteringContext, 4302*f4a2713aSLionel Sambuc CCC.IsObjCIvarLookup, 4303*f4a2713aSLionel Sambuc Name == TypoName.getName() && 4304*f4a2713aSLionel Sambuc !Candidate.WillReplaceSpecifier()); 4305*f4a2713aSLionel Sambuc 4306*f4a2713aSLionel Sambuc switch (TmpRes.getResultKind()) { 4307*f4a2713aSLionel Sambuc case LookupResult::NotFound: 4308*f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation: 4309*f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue: 4310*f4a2713aSLionel Sambuc if (TempSS) { 4311*f4a2713aSLionel Sambuc // Immediately retry the lookup without the given CXXScopeSpec 4312*f4a2713aSLionel Sambuc TempSS = NULL; 4313*f4a2713aSLionel Sambuc Candidate.WillReplaceSpecifier(true); 4314*f4a2713aSLionel Sambuc goto retry_lookup; 4315*f4a2713aSLionel Sambuc } 4316*f4a2713aSLionel Sambuc if (TempMemberContext) { 4317*f4a2713aSLionel Sambuc if (SS && !TempSS) 4318*f4a2713aSLionel Sambuc TempSS = SS; 4319*f4a2713aSLionel Sambuc TempMemberContext = NULL; 4320*f4a2713aSLionel Sambuc goto retry_lookup; 4321*f4a2713aSLionel Sambuc } 4322*f4a2713aSLionel Sambuc QualifiedResults.push_back(Candidate); 4323*f4a2713aSLionel Sambuc // We didn't find this name in our scope, or didn't like what we found; 4324*f4a2713aSLionel Sambuc // ignore it. 4325*f4a2713aSLionel Sambuc { 4326*f4a2713aSLionel Sambuc TypoCorrectionConsumer::result_iterator Next = I; 4327*f4a2713aSLionel Sambuc ++Next; 4328*f4a2713aSLionel Sambuc DI->second.erase(I); 4329*f4a2713aSLionel Sambuc I = Next; 4330*f4a2713aSLionel Sambuc } 4331*f4a2713aSLionel Sambuc break; 4332*f4a2713aSLionel Sambuc 4333*f4a2713aSLionel Sambuc case LookupResult::Ambiguous: 4334*f4a2713aSLionel Sambuc // We don't deal with ambiguities. 4335*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4336*f4a2713aSLionel Sambuc 4337*f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded: { 4338*f4a2713aSLionel Sambuc TypoCorrectionConsumer::result_iterator Prev = I; 4339*f4a2713aSLionel Sambuc // Store all of the Decls for overloaded symbols 4340*f4a2713aSLionel Sambuc for (LookupResult::iterator TRD = TmpRes.begin(), 4341*f4a2713aSLionel Sambuc TRDEnd = TmpRes.end(); 4342*f4a2713aSLionel Sambuc TRD != TRDEnd; ++TRD) 4343*f4a2713aSLionel Sambuc Candidate.addCorrectionDecl(*TRD); 4344*f4a2713aSLionel Sambuc ++I; 4345*f4a2713aSLionel Sambuc if (!isCandidateViable(CCC, Candidate)) { 4346*f4a2713aSLionel Sambuc QualifiedResults.push_back(Candidate); 4347*f4a2713aSLionel Sambuc DI->second.erase(Prev); 4348*f4a2713aSLionel Sambuc } 4349*f4a2713aSLionel Sambuc break; 4350*f4a2713aSLionel Sambuc } 4351*f4a2713aSLionel Sambuc 4352*f4a2713aSLionel Sambuc case LookupResult::Found: { 4353*f4a2713aSLionel Sambuc TypoCorrectionConsumer::result_iterator Prev = I; 4354*f4a2713aSLionel Sambuc Candidate.setCorrectionDecl(TmpRes.getAsSingle<NamedDecl>()); 4355*f4a2713aSLionel Sambuc ++I; 4356*f4a2713aSLionel Sambuc if (!isCandidateViable(CCC, Candidate)) { 4357*f4a2713aSLionel Sambuc QualifiedResults.push_back(Candidate); 4358*f4a2713aSLionel Sambuc DI->second.erase(Prev); 4359*f4a2713aSLionel Sambuc } 4360*f4a2713aSLionel Sambuc break; 4361*f4a2713aSLionel Sambuc } 4362*f4a2713aSLionel Sambuc 4363*f4a2713aSLionel Sambuc } 4364*f4a2713aSLionel Sambuc } 4365*f4a2713aSLionel Sambuc 4366*f4a2713aSLionel Sambuc if (DI->second.empty()) 4367*f4a2713aSLionel Sambuc Consumer.erase(DI); 4368*f4a2713aSLionel Sambuc else if (!getLangOpts().CPlusPlus || QualifiedResults.empty() || !DI->first) 4369*f4a2713aSLionel Sambuc // If there are results in the closest possible bucket, stop 4370*f4a2713aSLionel Sambuc break; 4371*f4a2713aSLionel Sambuc 4372*f4a2713aSLionel Sambuc // Only perform the qualified lookups for C++ 4373*f4a2713aSLionel Sambuc if (SearchNamespaces) { 4374*f4a2713aSLionel Sambuc TmpRes.suppressDiagnostics(); 4375*f4a2713aSLionel Sambuc for (SmallVector<TypoCorrection, 4376*f4a2713aSLionel Sambuc 16>::iterator QRI = QualifiedResults.begin(), 4377*f4a2713aSLionel Sambuc QRIEnd = QualifiedResults.end(); 4378*f4a2713aSLionel Sambuc QRI != QRIEnd; ++QRI) { 4379*f4a2713aSLionel Sambuc for (NamespaceSpecifierSet::iterator NI = Namespaces.begin(), 4380*f4a2713aSLionel Sambuc NIEnd = Namespaces.end(); 4381*f4a2713aSLionel Sambuc NI != NIEnd; ++NI) { 4382*f4a2713aSLionel Sambuc DeclContext *Ctx = NI->DeclCtx; 4383*f4a2713aSLionel Sambuc const Type *NSType = NI->NameSpecifier->getAsType(); 4384*f4a2713aSLionel Sambuc 4385*f4a2713aSLionel Sambuc // If the current NestedNameSpecifier refers to a class and the 4386*f4a2713aSLionel Sambuc // current correction candidate is the name of that class, then skip 4387*f4a2713aSLionel Sambuc // it as it is unlikely a qualified version of the class' constructor 4388*f4a2713aSLionel Sambuc // is an appropriate correction. 4389*f4a2713aSLionel Sambuc if (CXXRecordDecl *NSDecl = 4390*f4a2713aSLionel Sambuc NSType ? NSType->getAsCXXRecordDecl() : 0) { 4391*f4a2713aSLionel Sambuc if (NSDecl->getIdentifier() == QRI->getCorrectionAsIdentifierInfo()) 4392*f4a2713aSLionel Sambuc continue; 4393*f4a2713aSLionel Sambuc } 4394*f4a2713aSLionel Sambuc 4395*f4a2713aSLionel Sambuc TypoCorrection TC(*QRI); 4396*f4a2713aSLionel Sambuc TC.ClearCorrectionDecls(); 4397*f4a2713aSLionel Sambuc TC.setCorrectionSpecifier(NI->NameSpecifier); 4398*f4a2713aSLionel Sambuc TC.setQualifierDistance(NI->EditDistance); 4399*f4a2713aSLionel Sambuc TC.setCallbackDistance(0); // Reset the callback distance 4400*f4a2713aSLionel Sambuc 4401*f4a2713aSLionel Sambuc // If the current correction candidate and namespace combination are 4402*f4a2713aSLionel Sambuc // too far away from the original typo based on the normalized edit 4403*f4a2713aSLionel Sambuc // distance, then skip performing a qualified name lookup. 4404*f4a2713aSLionel Sambuc unsigned TmpED = TC.getEditDistance(true); 4405*f4a2713aSLionel Sambuc if (QRI->getCorrectionAsIdentifierInfo() != Typo && 4406*f4a2713aSLionel Sambuc TmpED && TypoLen / TmpED < 3) 4407*f4a2713aSLionel Sambuc continue; 4408*f4a2713aSLionel Sambuc 4409*f4a2713aSLionel Sambuc TmpRes.clear(); 4410*f4a2713aSLionel Sambuc TmpRes.setLookupName(QRI->getCorrectionAsIdentifierInfo()); 4411*f4a2713aSLionel Sambuc if (!LookupQualifiedName(TmpRes, Ctx)) continue; 4412*f4a2713aSLionel Sambuc 4413*f4a2713aSLionel Sambuc // Any corrections added below will be validated in subsequent 4414*f4a2713aSLionel Sambuc // iterations of the main while() loop over the Consumer's contents. 4415*f4a2713aSLionel Sambuc switch (TmpRes.getResultKind()) { 4416*f4a2713aSLionel Sambuc case LookupResult::Found: 4417*f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded: { 4418*f4a2713aSLionel Sambuc if (SS && SS->isValid()) { 4419*f4a2713aSLionel Sambuc std::string NewQualified = TC.getAsString(getLangOpts()); 4420*f4a2713aSLionel Sambuc std::string OldQualified; 4421*f4a2713aSLionel Sambuc llvm::raw_string_ostream OldOStream(OldQualified); 4422*f4a2713aSLionel Sambuc SS->getScopeRep()->print(OldOStream, getPrintingPolicy()); 4423*f4a2713aSLionel Sambuc OldOStream << TypoName; 4424*f4a2713aSLionel Sambuc // If correction candidate would be an identical written qualified 4425*f4a2713aSLionel Sambuc // identifer, then the existing CXXScopeSpec probably included a 4426*f4a2713aSLionel Sambuc // typedef that didn't get accounted for properly. 4427*f4a2713aSLionel Sambuc if (OldOStream.str() == NewQualified) 4428*f4a2713aSLionel Sambuc break; 4429*f4a2713aSLionel Sambuc } 4430*f4a2713aSLionel Sambuc for (LookupResult::iterator TRD = TmpRes.begin(), 4431*f4a2713aSLionel Sambuc TRDEnd = TmpRes.end(); 4432*f4a2713aSLionel Sambuc TRD != TRDEnd; ++TRD) { 4433*f4a2713aSLionel Sambuc if (CheckMemberAccess(TC.getCorrectionRange().getBegin(), 4434*f4a2713aSLionel Sambuc NSType ? NSType->getAsCXXRecordDecl() : 0, 4435*f4a2713aSLionel Sambuc TRD.getPair()) == AR_accessible) 4436*f4a2713aSLionel Sambuc TC.addCorrectionDecl(*TRD); 4437*f4a2713aSLionel Sambuc } 4438*f4a2713aSLionel Sambuc if (TC.isResolved()) 4439*f4a2713aSLionel Sambuc Consumer.addCorrection(TC); 4440*f4a2713aSLionel Sambuc break; 4441*f4a2713aSLionel Sambuc } 4442*f4a2713aSLionel Sambuc case LookupResult::NotFound: 4443*f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation: 4444*f4a2713aSLionel Sambuc case LookupResult::Ambiguous: 4445*f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue: 4446*f4a2713aSLionel Sambuc break; 4447*f4a2713aSLionel Sambuc } 4448*f4a2713aSLionel Sambuc } 4449*f4a2713aSLionel Sambuc } 4450*f4a2713aSLionel Sambuc } 4451*f4a2713aSLionel Sambuc 4452*f4a2713aSLionel Sambuc QualifiedResults.clear(); 4453*f4a2713aSLionel Sambuc } 4454*f4a2713aSLionel Sambuc 4455*f4a2713aSLionel Sambuc // No corrections remain... 4456*f4a2713aSLionel Sambuc if (Consumer.empty()) 4457*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4458*f4a2713aSLionel Sambuc 4459*f4a2713aSLionel Sambuc TypoResultsMap &BestResults = Consumer.getBestResults(); 4460*f4a2713aSLionel Sambuc ED = Consumer.getBestEditDistance(true); 4461*f4a2713aSLionel Sambuc 4462*f4a2713aSLionel Sambuc if (!AllowOnlyNNSChanges && ED > 0 && TypoLen / ED < 3) { 4463*f4a2713aSLionel Sambuc // If this was an unqualified lookup and we believe the callback 4464*f4a2713aSLionel Sambuc // object wouldn't have filtered out possible corrections, note 4465*f4a2713aSLionel Sambuc // that no correction was found. 4466*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 4467*f4a2713aSLionel Sambuc IsUnqualifiedLookup && !ValidatingCallback); 4468*f4a2713aSLionel Sambuc } 4469*f4a2713aSLionel Sambuc 4470*f4a2713aSLionel Sambuc // If only a single name remains, return that result. 4471*f4a2713aSLionel Sambuc if (BestResults.size() == 1) { 4472*f4a2713aSLionel Sambuc const TypoResultList &CorrectionList = BestResults.begin()->second; 4473*f4a2713aSLionel Sambuc const TypoCorrection &Result = CorrectionList.front(); 4474*f4a2713aSLionel Sambuc if (CorrectionList.size() != 1) 4475*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4476*f4a2713aSLionel Sambuc 4477*f4a2713aSLionel Sambuc // Don't correct to a keyword that's the same as the typo; the keyword 4478*f4a2713aSLionel Sambuc // wasn't actually in scope. 4479*f4a2713aSLionel Sambuc if (ED == 0 && Result.isKeyword()) 4480*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4481*f4a2713aSLionel Sambuc 4482*f4a2713aSLionel Sambuc // Record the correction for unqualified lookup. 4483*f4a2713aSLionel Sambuc if (IsUnqualifiedLookup) 4484*f4a2713aSLionel Sambuc UnqualifiedTyposCorrected[Typo] = Result; 4485*f4a2713aSLionel Sambuc 4486*f4a2713aSLionel Sambuc TypoCorrection TC = Result; 4487*f4a2713aSLionel Sambuc TC.setCorrectionRange(SS, TypoName); 4488*f4a2713aSLionel Sambuc checkCorrectionVisibility(*this, TC, TypoName.getName()); 4489*f4a2713aSLionel Sambuc return TC; 4490*f4a2713aSLionel Sambuc } 4491*f4a2713aSLionel Sambuc else if (BestResults.size() > 1 4492*f4a2713aSLionel Sambuc // Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver; 4493*f4a2713aSLionel Sambuc // WantObjCSuper is only true for CTC_ObjCMessageReceiver and for 4494*f4a2713aSLionel Sambuc // some instances of CTC_Unknown, while WantRemainingKeywords is true 4495*f4a2713aSLionel Sambuc // for CTC_Unknown but not for CTC_ObjCMessageReceiver. 4496*f4a2713aSLionel Sambuc && CCC.WantObjCSuper && !CCC.WantRemainingKeywords 4497*f4a2713aSLionel Sambuc && BestResults["super"].front().isKeyword()) { 4498*f4a2713aSLionel Sambuc // Prefer 'super' when we're completing in a message-receiver 4499*f4a2713aSLionel Sambuc // context. 4500*f4a2713aSLionel Sambuc 4501*f4a2713aSLionel Sambuc // Don't correct to a keyword that's the same as the typo; the keyword 4502*f4a2713aSLionel Sambuc // wasn't actually in scope. 4503*f4a2713aSLionel Sambuc if (ED == 0) 4504*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4505*f4a2713aSLionel Sambuc 4506*f4a2713aSLionel Sambuc // Record the correction for unqualified lookup. 4507*f4a2713aSLionel Sambuc if (IsUnqualifiedLookup) 4508*f4a2713aSLionel Sambuc UnqualifiedTyposCorrected[Typo] = BestResults["super"].front(); 4509*f4a2713aSLionel Sambuc 4510*f4a2713aSLionel Sambuc TypoCorrection TC = BestResults["super"].front(); 4511*f4a2713aSLionel Sambuc TC.setCorrectionRange(SS, TypoName); 4512*f4a2713aSLionel Sambuc return TC; 4513*f4a2713aSLionel Sambuc } 4514*f4a2713aSLionel Sambuc 4515*f4a2713aSLionel Sambuc // If this was an unqualified lookup and we believe the callback object did 4516*f4a2713aSLionel Sambuc // not filter out possible corrections, note that no correction was found. 4517*f4a2713aSLionel Sambuc if (IsUnqualifiedLookup && !ValidatingCallback) 4518*f4a2713aSLionel Sambuc (void)UnqualifiedTyposCorrected[Typo]; 4519*f4a2713aSLionel Sambuc 4520*f4a2713aSLionel Sambuc return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4521*f4a2713aSLionel Sambuc } 4522*f4a2713aSLionel Sambuc 4523*f4a2713aSLionel Sambuc void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) { 4524*f4a2713aSLionel Sambuc if (!CDecl) return; 4525*f4a2713aSLionel Sambuc 4526*f4a2713aSLionel Sambuc if (isKeyword()) 4527*f4a2713aSLionel Sambuc CorrectionDecls.clear(); 4528*f4a2713aSLionel Sambuc 4529*f4a2713aSLionel Sambuc CorrectionDecls.push_back(CDecl->getUnderlyingDecl()); 4530*f4a2713aSLionel Sambuc 4531*f4a2713aSLionel Sambuc if (!CorrectionName) 4532*f4a2713aSLionel Sambuc CorrectionName = CDecl->getDeclName(); 4533*f4a2713aSLionel Sambuc } 4534*f4a2713aSLionel Sambuc 4535*f4a2713aSLionel Sambuc std::string TypoCorrection::getAsString(const LangOptions &LO) const { 4536*f4a2713aSLionel Sambuc if (CorrectionNameSpec) { 4537*f4a2713aSLionel Sambuc std::string tmpBuffer; 4538*f4a2713aSLionel Sambuc llvm::raw_string_ostream PrefixOStream(tmpBuffer); 4539*f4a2713aSLionel Sambuc CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO)); 4540*f4a2713aSLionel Sambuc PrefixOStream << CorrectionName; 4541*f4a2713aSLionel Sambuc return PrefixOStream.str(); 4542*f4a2713aSLionel Sambuc } 4543*f4a2713aSLionel Sambuc 4544*f4a2713aSLionel Sambuc return CorrectionName.getAsString(); 4545*f4a2713aSLionel Sambuc } 4546*f4a2713aSLionel Sambuc 4547*f4a2713aSLionel Sambuc bool CorrectionCandidateCallback::ValidateCandidate(const TypoCorrection &candidate) { 4548*f4a2713aSLionel Sambuc if (!candidate.isResolved()) 4549*f4a2713aSLionel Sambuc return true; 4550*f4a2713aSLionel Sambuc 4551*f4a2713aSLionel Sambuc if (candidate.isKeyword()) 4552*f4a2713aSLionel Sambuc return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts || 4553*f4a2713aSLionel Sambuc WantRemainingKeywords || WantObjCSuper; 4554*f4a2713aSLionel Sambuc 4555*f4a2713aSLionel Sambuc for (TypoCorrection::const_decl_iterator CDecl = candidate.begin(), 4556*f4a2713aSLionel Sambuc CDeclEnd = candidate.end(); 4557*f4a2713aSLionel Sambuc CDecl != CDeclEnd; ++CDecl) { 4558*f4a2713aSLionel Sambuc if (!isa<TypeDecl>(*CDecl)) 4559*f4a2713aSLionel Sambuc return true; 4560*f4a2713aSLionel Sambuc } 4561*f4a2713aSLionel Sambuc 4562*f4a2713aSLionel Sambuc return WantTypeSpecifiers; 4563*f4a2713aSLionel Sambuc } 4564*f4a2713aSLionel Sambuc 4565*f4a2713aSLionel Sambuc FunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs, 4566*f4a2713aSLionel Sambuc bool HasExplicitTemplateArgs) 4567*f4a2713aSLionel Sambuc : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { 4568*f4a2713aSLionel Sambuc WantTypeSpecifiers = SemaRef.getLangOpts().CPlusPlus; 4569*f4a2713aSLionel Sambuc WantRemainingKeywords = false; 4570*f4a2713aSLionel Sambuc } 4571*f4a2713aSLionel Sambuc 4572*f4a2713aSLionel Sambuc bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) { 4573*f4a2713aSLionel Sambuc if (!candidate.getCorrectionDecl()) 4574*f4a2713aSLionel Sambuc return candidate.isKeyword(); 4575*f4a2713aSLionel Sambuc 4576*f4a2713aSLionel Sambuc for (TypoCorrection::const_decl_iterator DI = candidate.begin(), 4577*f4a2713aSLionel Sambuc DIEnd = candidate.end(); 4578*f4a2713aSLionel Sambuc DI != DIEnd; ++DI) { 4579*f4a2713aSLionel Sambuc FunctionDecl *FD = 0; 4580*f4a2713aSLionel Sambuc NamedDecl *ND = (*DI)->getUnderlyingDecl(); 4581*f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 4582*f4a2713aSLionel Sambuc FD = FTD->getTemplatedDecl(); 4583*f4a2713aSLionel Sambuc if (!HasExplicitTemplateArgs && !FD) { 4584*f4a2713aSLionel Sambuc if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { 4585*f4a2713aSLionel Sambuc // If the Decl is neither a function nor a template function, 4586*f4a2713aSLionel Sambuc // determine if it is a pointer or reference to a function. If so, 4587*f4a2713aSLionel Sambuc // check against the number of arguments expected for the pointee. 4588*f4a2713aSLionel Sambuc QualType ValType = cast<ValueDecl>(ND)->getType(); 4589*f4a2713aSLionel Sambuc if (ValType->isAnyPointerType() || ValType->isReferenceType()) 4590*f4a2713aSLionel Sambuc ValType = ValType->getPointeeType(); 4591*f4a2713aSLionel Sambuc if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) 4592*f4a2713aSLionel Sambuc if (FPT->getNumArgs() == NumArgs) 4593*f4a2713aSLionel Sambuc return true; 4594*f4a2713aSLionel Sambuc } 4595*f4a2713aSLionel Sambuc } 4596*f4a2713aSLionel Sambuc if (FD && FD->getNumParams() >= NumArgs && 4597*f4a2713aSLionel Sambuc FD->getMinRequiredArguments() <= NumArgs) 4598*f4a2713aSLionel Sambuc return true; 4599*f4a2713aSLionel Sambuc } 4600*f4a2713aSLionel Sambuc return false; 4601*f4a2713aSLionel Sambuc } 4602*f4a2713aSLionel Sambuc 4603*f4a2713aSLionel Sambuc void Sema::diagnoseTypo(const TypoCorrection &Correction, 4604*f4a2713aSLionel Sambuc const PartialDiagnostic &TypoDiag, 4605*f4a2713aSLionel Sambuc bool ErrorRecovery) { 4606*f4a2713aSLionel Sambuc diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl), 4607*f4a2713aSLionel Sambuc ErrorRecovery); 4608*f4a2713aSLionel Sambuc } 4609*f4a2713aSLionel Sambuc 4610*f4a2713aSLionel Sambuc /// Find which declaration we should import to provide the definition of 4611*f4a2713aSLionel Sambuc /// the given declaration. 4612*f4a2713aSLionel Sambuc static const NamedDecl *getDefinitionToImport(const NamedDecl *D) { 4613*f4a2713aSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 4614*f4a2713aSLionel Sambuc return VD->getDefinition(); 4615*f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 4616*f4a2713aSLionel Sambuc return FD->isDefined(FD) ? FD : 0; 4617*f4a2713aSLionel Sambuc if (const TagDecl *TD = dyn_cast<TagDecl>(D)) 4618*f4a2713aSLionel Sambuc return TD->getDefinition(); 4619*f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 4620*f4a2713aSLionel Sambuc return ID->getDefinition(); 4621*f4a2713aSLionel Sambuc if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 4622*f4a2713aSLionel Sambuc return PD->getDefinition(); 4623*f4a2713aSLionel Sambuc if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) 4624*f4a2713aSLionel Sambuc return getDefinitionToImport(TD->getTemplatedDecl()); 4625*f4a2713aSLionel Sambuc return 0; 4626*f4a2713aSLionel Sambuc } 4627*f4a2713aSLionel Sambuc 4628*f4a2713aSLionel Sambuc /// \brief Diagnose a successfully-corrected typo. Separated from the correction 4629*f4a2713aSLionel Sambuc /// itself to allow external validation of the result, etc. 4630*f4a2713aSLionel Sambuc /// 4631*f4a2713aSLionel Sambuc /// \param Correction The result of performing typo correction. 4632*f4a2713aSLionel Sambuc /// \param TypoDiag The diagnostic to produce. This will have the corrected 4633*f4a2713aSLionel Sambuc /// string added to it (and usually also a fixit). 4634*f4a2713aSLionel Sambuc /// \param PrevNote A note to use when indicating the location of the entity to 4635*f4a2713aSLionel Sambuc /// which we are correcting. Will have the correction string added to it. 4636*f4a2713aSLionel Sambuc /// \param ErrorRecovery If \c true (the default), the caller is going to 4637*f4a2713aSLionel Sambuc /// recover from the typo as if the corrected string had been typed. 4638*f4a2713aSLionel Sambuc /// In this case, \c PDiag must be an error, and we will attach a fixit 4639*f4a2713aSLionel Sambuc /// to it. 4640*f4a2713aSLionel Sambuc void Sema::diagnoseTypo(const TypoCorrection &Correction, 4641*f4a2713aSLionel Sambuc const PartialDiagnostic &TypoDiag, 4642*f4a2713aSLionel Sambuc const PartialDiagnostic &PrevNote, 4643*f4a2713aSLionel Sambuc bool ErrorRecovery) { 4644*f4a2713aSLionel Sambuc std::string CorrectedStr = Correction.getAsString(getLangOpts()); 4645*f4a2713aSLionel Sambuc std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts()); 4646*f4a2713aSLionel Sambuc FixItHint FixTypo = FixItHint::CreateReplacement( 4647*f4a2713aSLionel Sambuc Correction.getCorrectionRange(), CorrectedStr); 4648*f4a2713aSLionel Sambuc 4649*f4a2713aSLionel Sambuc // Maybe we're just missing a module import. 4650*f4a2713aSLionel Sambuc if (Correction.requiresImport()) { 4651*f4a2713aSLionel Sambuc NamedDecl *Decl = Correction.getCorrectionDecl(); 4652*f4a2713aSLionel Sambuc assert(Decl && "import required but no declaration to import"); 4653*f4a2713aSLionel Sambuc 4654*f4a2713aSLionel Sambuc // Suggest importing a module providing the definition of this entity, if 4655*f4a2713aSLionel Sambuc // possible. 4656*f4a2713aSLionel Sambuc const NamedDecl *Def = getDefinitionToImport(Decl); 4657*f4a2713aSLionel Sambuc if (!Def) 4658*f4a2713aSLionel Sambuc Def = Decl; 4659*f4a2713aSLionel Sambuc Module *Owner = Def->getOwningModule(); 4660*f4a2713aSLionel Sambuc assert(Owner && "definition of hidden declaration is not in a module"); 4661*f4a2713aSLionel Sambuc 4662*f4a2713aSLionel Sambuc Diag(Correction.getCorrectionRange().getBegin(), 4663*f4a2713aSLionel Sambuc diag::err_module_private_declaration) 4664*f4a2713aSLionel Sambuc << Def << Owner->getFullModuleName(); 4665*f4a2713aSLionel Sambuc Diag(Def->getLocation(), diag::note_previous_declaration); 4666*f4a2713aSLionel Sambuc 4667*f4a2713aSLionel Sambuc // Recover by implicitly importing this module. 4668*f4a2713aSLionel Sambuc if (!isSFINAEContext() && ErrorRecovery) 4669*f4a2713aSLionel Sambuc createImplicitModuleImport(Correction.getCorrectionRange().getBegin(), 4670*f4a2713aSLionel Sambuc Owner); 4671*f4a2713aSLionel Sambuc return; 4672*f4a2713aSLionel Sambuc } 4673*f4a2713aSLionel Sambuc 4674*f4a2713aSLionel Sambuc Diag(Correction.getCorrectionRange().getBegin(), TypoDiag) 4675*f4a2713aSLionel Sambuc << CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint()); 4676*f4a2713aSLionel Sambuc 4677*f4a2713aSLionel Sambuc NamedDecl *ChosenDecl = 4678*f4a2713aSLionel Sambuc Correction.isKeyword() ? 0 : Correction.getCorrectionDecl(); 4679*f4a2713aSLionel Sambuc if (PrevNote.getDiagID() && ChosenDecl) 4680*f4a2713aSLionel Sambuc Diag(ChosenDecl->getLocation(), PrevNote) 4681*f4a2713aSLionel Sambuc << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo); 4682*f4a2713aSLionel Sambuc } 4683