1eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor//===--------------------- SemaLookup.cpp - Name Lookup ------------------===// 2eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// 3eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// The LLVM Compiler Infrastructure 4eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// 5eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// This file is distributed under the University of Illinois Open Source 6eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// License. See LICENSE.TXT for details. 7eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// 8eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor//===----------------------------------------------------------------------===// 9eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// 10eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// This file implements name lookup for C, C++, Objective-C, and 11eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// Objective-C++. 12eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor// 13eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor//===----------------------------------------------------------------------===// 14e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h" 157176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor#include "clang/AST/ASTContext.h" 16a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h" 17eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor#include "clang/AST/Decl.h" 18eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor#include "clang/AST/DeclCXX.h" 19173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#include "clang/AST/DeclLookups.h" 20eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor#include "clang/AST/DeclObjC.h" 2142af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor#include "clang/AST/DeclTemplate.h" 22fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor#include "clang/AST/Expr.h" 23daa439a6c47d3299157b94a496bf22389bbc77a3Douglas Gregor#include "clang/AST/ExprCXX.h" 241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 25eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor#include "clang/Basic/LangOptions.h" 266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "clang/Lex/ModuleLoader.h" 2755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/DeclSpec.h" 2855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ExternalSemaSource.h" 2955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Overload.h" 3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h" 3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h" 3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Sema.h" 3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/SemaInternal.h" 3455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/TemplateDeduction.h" 3555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/TypoCorrection.h" 36eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor#include "llvm/ADT/STLExtras.h" 3755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/SetVector.h" 38fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor#include "llvm/ADT/SmallPtrSet.h" 39e24b575f06985336973bb721e198bd184bf7bdd3Douglas Gregor#include "llvm/ADT/StringMap.h" 40b5f65475d25b67f87e368daa1583b762af7e2e45Chris Lattner#include "llvm/ADT/TinyPtrVector.h" 412d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain#include "llvm/ADT/edit_distance.h" 426e24726524c2b51b31bb4b622aa678a46b024f42John McCall#include "llvm/Support/ErrorHandling.h" 43893a6eaf52930701950b7fc01a2478bb86899b77Nick Lewycky#include <algorithm> 44893a6eaf52930701950b7fc01a2478bb86899b77Nick Lewycky#include <iterator> 45e24b575f06985336973bb721e198bd184bf7bdd3Douglas Gregor#include <limits> 46546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor#include <list> 47893a6eaf52930701950b7fc01a2478bb86899b77Nick Lewycky#include <map> 484dc6b1c81c5efbaf68b868df10b18466b5e14b34Douglas Gregor#include <set> 492a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor#include <utility> 50893a6eaf52930701950b7fc01a2478bb86899b77Nick Lewycky#include <vector> 51eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 52eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregorusing namespace clang; 532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCallusing namespace sema; 54eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 55d7be78a0524251c30329359c9d92485c6d7ac424John McCallnamespace { 56d7be78a0524251c30329359c9d92485c6d7ac424John McCall class UnqualUsingEntry { 57d7be78a0524251c30329359c9d92485c6d7ac424John McCall const DeclContext *Nominated; 58d7be78a0524251c30329359c9d92485c6d7ac424John McCall const DeclContext *CommonAncestor; 59d7be78a0524251c30329359c9d92485c6d7ac424John McCall 60d7be78a0524251c30329359c9d92485c6d7ac424John McCall public: 61d7be78a0524251c30329359c9d92485c6d7ac424John McCall UnqualUsingEntry(const DeclContext *Nominated, 62d7be78a0524251c30329359c9d92485c6d7ac424John McCall const DeclContext *CommonAncestor) 63d7be78a0524251c30329359c9d92485c6d7ac424John McCall : Nominated(Nominated), CommonAncestor(CommonAncestor) { 64d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 652a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 66d7be78a0524251c30329359c9d92485c6d7ac424John McCall const DeclContext *getCommonAncestor() const { 67d7be78a0524251c30329359c9d92485c6d7ac424John McCall return CommonAncestor; 68d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 692a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 70d7be78a0524251c30329359c9d92485c6d7ac424John McCall const DeclContext *getNominatedNamespace() const { 71d7be78a0524251c30329359c9d92485c6d7ac424John McCall return Nominated; 72d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 732a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 74d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Sort by the pointer value of the common ancestor. 75d7be78a0524251c30329359c9d92485c6d7ac424John McCall struct Comparator { 76d7be78a0524251c30329359c9d92485c6d7ac424John McCall bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { 77d7be78a0524251c30329359c9d92485c6d7ac424John McCall return L.getCommonAncestor() < R.getCommonAncestor(); 78d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 792a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 80d7be78a0524251c30329359c9d92485c6d7ac424John McCall bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { 81d7be78a0524251c30329359c9d92485c6d7ac424John McCall return E.getCommonAncestor() < DC; 82d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 832a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 84d7be78a0524251c30329359c9d92485c6d7ac424John McCall bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { 85d7be78a0524251c30329359c9d92485c6d7ac424John McCall return DC < E.getCommonAncestor(); 86d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 87d7be78a0524251c30329359c9d92485c6d7ac424John McCall }; 88d7be78a0524251c30329359c9d92485c6d7ac424John McCall }; 89d7be78a0524251c30329359c9d92485c6d7ac424John McCall 90d7be78a0524251c30329359c9d92485c6d7ac424John McCall /// A collection of using directives, as used by C++ unqualified 91d7be78a0524251c30329359c9d92485c6d7ac424John McCall /// lookup. 92d7be78a0524251c30329359c9d92485c6d7ac424John McCall class UnqualUsingDirectiveSet { 935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<UnqualUsingEntry, 8> ListTy; 94d7be78a0524251c30329359c9d92485c6d7ac424John McCall 95d7be78a0524251c30329359c9d92485c6d7ac424John McCall ListTy list; 96d7be78a0524251c30329359c9d92485c6d7ac424John McCall llvm::SmallPtrSet<DeclContext*, 8> visited; 97d7be78a0524251c30329359c9d92485c6d7ac424John McCall 98d7be78a0524251c30329359c9d92485c6d7ac424John McCall public: 99d7be78a0524251c30329359c9d92485c6d7ac424John McCall UnqualUsingDirectiveSet() {} 100d7be78a0524251c30329359c9d92485c6d7ac424John McCall 101d7be78a0524251c30329359c9d92485c6d7ac424John McCall void visitScopeChain(Scope *S, Scope *InnermostFileScope) { 102dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // C++ [namespace.udir]p1: 103d7be78a0524251c30329359c9d92485c6d7ac424John McCall // During unqualified name lookup, the names appear as if they 104d7be78a0524251c30329359c9d92485c6d7ac424John McCall // were declared in the nearest enclosing namespace which contains 105d7be78a0524251c30329359c9d92485c6d7ac424John McCall // both the using-directive and the nominated namespace. 106f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek DeclContext *InnermostFileDC = InnermostFileScope->getEntity(); 107d7be78a0524251c30329359c9d92485c6d7ac424John McCall assert(InnermostFileDC && InnermostFileDC->isFileContext()); 108d7be78a0524251c30329359c9d92485c6d7ac424John McCall 109d7be78a0524251c30329359c9d92485c6d7ac424John McCall for (; S; S = S->getParent()) { 11065daef179790a02eab1b5a989f53984375a06483Nick Lewycky // C++ [namespace.udir]p1: 11165daef179790a02eab1b5a989f53984375a06483Nick Lewycky // A using-directive shall not appear in class scope, but may 11265daef179790a02eab1b5a989f53984375a06483Nick Lewycky // appear in namespace scope or in block scope. 113f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek DeclContext *Ctx = S->getEntity(); 11465daef179790a02eab1b5a989f53984375a06483Nick Lewycky if (Ctx && Ctx->isFileContext()) { 11565daef179790a02eab1b5a989f53984375a06483Nick Lewycky visit(Ctx, Ctx); 11665daef179790a02eab1b5a989f53984375a06483Nick Lewycky } else if (!Ctx || Ctx->isFunctionOrMethod()) { 117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : S->using_directives()) 118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines visit(I, InnermostFileDC); 119d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 120d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 121d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 1222a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 123d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Visits a context and collect all of its using directives 124d7be78a0524251c30329359c9d92485c6d7ac424John McCall // recursively. Treats all using directives as if they were 125d7be78a0524251c30329359c9d92485c6d7ac424John McCall // declared in the context. 126d7be78a0524251c30329359c9d92485c6d7ac424John McCall // 127d7be78a0524251c30329359c9d92485c6d7ac424John McCall // A given context is only every visited once, so it is important 128d7be78a0524251c30329359c9d92485c6d7ac424John McCall // that contexts be visited from the inside out in order to get 129d7be78a0524251c30329359c9d92485c6d7ac424John McCall // the effective DCs right. 130d7be78a0524251c30329359c9d92485c6d7ac424John McCall void visit(DeclContext *DC, DeclContext *EffectiveDC) { 131d7be78a0524251c30329359c9d92485c6d7ac424John McCall if (!visited.insert(DC)) 132d7be78a0524251c30329359c9d92485c6d7ac424John McCall return; 133d7be78a0524251c30329359c9d92485c6d7ac424John McCall 134d7be78a0524251c30329359c9d92485c6d7ac424John McCall addUsingDirectives(DC, EffectiveDC); 135d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 1362a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 137d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Visits a using directive and collects all of its using 138d7be78a0524251c30329359c9d92485c6d7ac424John McCall // directives recursively. Treats all using directives as if they 139d7be78a0524251c30329359c9d92485c6d7ac424John McCall // were declared in the effective DC. 140d7be78a0524251c30329359c9d92485c6d7ac424John McCall void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 141d7be78a0524251c30329359c9d92485c6d7ac424John McCall DeclContext *NS = UD->getNominatedNamespace(); 142d7be78a0524251c30329359c9d92485c6d7ac424John McCall if (!visited.insert(NS)) 143d7be78a0524251c30329359c9d92485c6d7ac424John McCall return; 1442a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 145d7be78a0524251c30329359c9d92485c6d7ac424John McCall addUsingDirective(UD, EffectiveDC); 146d7be78a0524251c30329359c9d92485c6d7ac424John McCall addUsingDirectives(NS, EffectiveDC); 147d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 1482a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 149d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Adds all the using directives in a context (and those nominated 150d7be78a0524251c30329359c9d92485c6d7ac424John McCall // by its using directives, transitively) as if they appeared in 151d7be78a0524251c30329359c9d92485c6d7ac424John McCall // the given effective context. 152d7be78a0524251c30329359c9d92485c6d7ac424John McCall void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { 1535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<DeclContext*,4> queue; 154d7be78a0524251c30329359c9d92485c6d7ac424John McCall while (true) { 155651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto UD : DC->using_directives()) { 156d7be78a0524251c30329359c9d92485c6d7ac424John McCall DeclContext *NS = UD->getNominatedNamespace(); 157d7be78a0524251c30329359c9d92485c6d7ac424John McCall if (visited.insert(NS)) { 158d7be78a0524251c30329359c9d92485c6d7ac424John McCall addUsingDirective(UD, EffectiveDC); 159d7be78a0524251c30329359c9d92485c6d7ac424John McCall queue.push_back(NS); 160d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 161d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 162d7be78a0524251c30329359c9d92485c6d7ac424John McCall 163d7be78a0524251c30329359c9d92485c6d7ac424John McCall if (queue.empty()) 164d7be78a0524251c30329359c9d92485c6d7ac424John McCall return; 165d7be78a0524251c30329359c9d92485c6d7ac424John McCall 166344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm DC = queue.pop_back_val(); 1672a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 1682a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 169d7be78a0524251c30329359c9d92485c6d7ac424John McCall 170d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Add a using directive as if it had been declared in the given 171d7be78a0524251c30329359c9d92485c6d7ac424John McCall // context. This helps implement C++ [namespace.udir]p3: 172d7be78a0524251c30329359c9d92485c6d7ac424John McCall // The using-directive is transitive: if a scope contains a 173d7be78a0524251c30329359c9d92485c6d7ac424John McCall // using-directive that nominates a second namespace that itself 174d7be78a0524251c30329359c9d92485c6d7ac424John McCall // contains using-directives, the effect is as if the 175d7be78a0524251c30329359c9d92485c6d7ac424John McCall // using-directives from the second namespace also appeared in 176d7be78a0524251c30329359c9d92485c6d7ac424John McCall // the first. 177d7be78a0524251c30329359c9d92485c6d7ac424John McCall void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { 178d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Find the common ancestor between the effective context and 179d7be78a0524251c30329359c9d92485c6d7ac424John McCall // the nominated namespace. 180d7be78a0524251c30329359c9d92485c6d7ac424John McCall DeclContext *Common = UD->getNominatedNamespace(); 181d7be78a0524251c30329359c9d92485c6d7ac424John McCall while (!Common->Encloses(EffectiveDC)) 182d7be78a0524251c30329359c9d92485c6d7ac424John McCall Common = Common->getParent(); 18312ea5780e7729de3fee009525e49468fd8d0b713John McCall Common = Common->getPrimaryContext(); 184dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 185d7be78a0524251c30329359c9d92485c6d7ac424John McCall list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); 186d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 187d7be78a0524251c30329359c9d92485c6d7ac424John McCall 188d7be78a0524251c30329359c9d92485c6d7ac424John McCall void done() { 189d7be78a0524251c30329359c9d92485c6d7ac424John McCall std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator()); 190d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 191d7be78a0524251c30329359c9d92485c6d7ac424John McCall 192d7be78a0524251c30329359c9d92485c6d7ac424John McCall typedef ListTy::const_iterator const_iterator; 193dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 194d7be78a0524251c30329359c9d92485c6d7ac424John McCall const_iterator begin() const { return list.begin(); } 195d7be78a0524251c30329359c9d92485c6d7ac424John McCall const_iterator end() const { return list.end(); } 196d7be78a0524251c30329359c9d92485c6d7ac424John McCall 197d7be78a0524251c30329359c9d92485c6d7ac424John McCall std::pair<const_iterator,const_iterator> 198d7be78a0524251c30329359c9d92485c6d7ac424John McCall getNamespacesFor(DeclContext *DC) const { 19912ea5780e7729de3fee009525e49468fd8d0b713John McCall return std::equal_range(begin(), end(), DC->getPrimaryContext(), 200d7be78a0524251c30329359c9d92485c6d7ac424John McCall UnqualUsingEntry::Comparator()); 201d7be78a0524251c30329359c9d92485c6d7ac424John McCall } 202d7be78a0524251c30329359c9d92485c6d7ac424John McCall }; 2032a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor} 2042a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 2052a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor// Retrieve the set of identifier namespaces that correspond to a 2062a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor// specific kind of name lookup. 2071d7c52803e49d651a66cee782e264f62078c1da5John McCallstatic inline unsigned getIDNS(Sema::LookupNameKind NameKind, 2081d7c52803e49d651a66cee782e264f62078c1da5John McCall bool CPlusPlus, 2091d7c52803e49d651a66cee782e264f62078c1da5John McCall bool Redeclaration) { 2102a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor unsigned IDNS = 0; 2112a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor switch (NameKind) { 21298a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian case Sema::LookupObjCImplicitSelfParam: 2132a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor case Sema::LookupOrdinaryName: 214d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor case Sema::LookupRedeclarationWithLinkage: 2154e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith case Sema::LookupLocalFriendName: 2162a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor IDNS = Decl::IDNS_Ordinary; 2171d7c52803e49d651a66cee782e264f62078c1da5John McCall if (CPlusPlus) { 2180d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; 219337e550218128e7d922c09bb354fbc71de90c568Chris Lattner if (Redeclaration) 220337e550218128e7d922c09bb354fbc71de90c568Chris Lattner IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; 2211d7c52803e49d651a66cee782e264f62078c1da5John McCall } 222a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith if (Redeclaration) 223a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith IDNS |= Decl::IDNS_LocalExtern; 2242a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor break; 2252a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 22676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall case Sema::LookupOperatorName: 22776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall // Operator lookup is its own crazy thing; it is not the same 22876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall // as (e.g.) looking up an operator name for redeclaration. 22976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall assert(!Redeclaration && "cannot do redeclaration operator lookup"); 23076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall IDNS = Decl::IDNS_NonMemberOperator; 23176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall break; 23276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall 2332a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor case Sema::LookupTagName: 2340d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall if (CPlusPlus) { 2350d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS = Decl::IDNS_Type; 2360d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall 2370d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall // When looking for a redeclaration of a tag name, we add: 2380d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall // 1) TagFriend to find undeclared friend decls 2390d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall // 2) Namespace because they can't "overload" with tag decls. 2400d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall // 3) Tag because it includes class templates, which can't 2410d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall // "overload" with tag decls. 2420d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall if (Redeclaration) 2430d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; 2440d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall } else { 2450d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS = Decl::IDNS_Tag; 2460d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall } 2472a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor break; 2486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 249337e550218128e7d922c09bb354fbc71de90c568Chris Lattner case Sema::LookupLabel: 250337e550218128e7d922c09bb354fbc71de90c568Chris Lattner IDNS = Decl::IDNS_Label; 251337e550218128e7d922c09bb354fbc71de90c568Chris Lattner break; 2526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 2532a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor case Sema::LookupMemberName: 2542a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor IDNS = Decl::IDNS_Member; 2552a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor if (CPlusPlus) 2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; 2572a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor break; 2582a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 2592a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor case Sema::LookupNestedNameSpecifierName: 2600d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; 2610d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall break; 2620d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall 2632a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor case Sema::LookupNamespaceName: 2640d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall IDNS = Decl::IDNS_Namespace; 2652a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor break; 2666e378de1aebdfeb44f2a7677ed207b32b3a41fbfDouglas Gregor 2679f54ad4381370c6b771424b53d219e661d6d6706John McCall case Sema::LookupUsingDeclName: 2686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines assert(Redeclaration && "should only be used for redecl lookup"); 2696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member | 2706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend | 2716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Decl::IDNS_LocalExtern; 2729f54ad4381370c6b771424b53d219e661d6d6706John McCall break; 2739f54ad4381370c6b771424b53d219e661d6d6706John McCall 2748fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor case Sema::LookupObjCProtocolName: 2758fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor IDNS = Decl::IDNS_ObjCProtocol; 2768fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor break; 277dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2788071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor case Sema::LookupAnyName: 279dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member 2808071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol 2818071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor | Decl::IDNS_Type; 2828071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor break; 2832a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 2842a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor return IDNS; 2852a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor} 2862a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 2871d7c52803e49d651a66cee782e264f62078c1da5John McCallvoid LookupResult::configure() { 2884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie IDNS = getIDNS(LookupKind, SemaRef.getLangOpts().CPlusPlus, 2891d7c52803e49d651a66cee782e264f62078c1da5John McCall isForRedeclaration()); 290b5b2ccbbb69f49146d937e1dc6d7dc11d631908cDouglas Gregor 291651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // If we're looking for one of the allocation or deallocation 292651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // operators, make sure that the implicitly-declared new and delete 293651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // operators can be found. 294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines switch (NameInfo.getName().getCXXOverloadedOperator()) { 295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines case OO_New: 296651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines case OO_Delete: 297651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines case OO_Array_New: 298651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines case OO_Array_Delete: 299651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines SemaRef.DeclareGlobalNewDelete(); 300651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines break; 301b5b2ccbbb69f49146d937e1dc6d7dc11d631908cDouglas Gregor 302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines default: 303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines break; 304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 30596df3562752e13237df051469271f8999ad60fe6Douglas Gregor 306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Compiler builtins are always visible, regardless of where they end 307651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // up being declared. 308651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) { 309651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (unsigned BuiltinID = Id->getBuiltinID()) { 310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!SemaRef.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 311651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines AllowHidden = true; 31296df3562752e13237df051469271f8999ad60fe6Douglas Gregor } 313b5b2ccbbb69f49146d937e1dc6d7dc11d631908cDouglas Gregor } 3141d7c52803e49d651a66cee782e264f62078c1da5John McCall} 3151d7c52803e49d651a66cee782e264f62078c1da5John McCall 316651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool LookupResult::sanity() const { 317651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // This function is never called by NDEBUG builds. 3182a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall assert(ResultKind != NotFound || Decls.size() == 0); 3192a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall assert(ResultKind != Found || Decls.size() == 1); 3202a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall assert(ResultKind != FoundOverloaded || Decls.size() > 1 || 3212a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall (Decls.size() == 1 && 3222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))); 3232a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved()); 3242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall assert(ResultKind != Ambiguous || Decls.size() > 1 || 325f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || 326f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor Ambiguity == AmbiguousBaseSubobjectTypes))); 3276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines assert((Paths != nullptr) == (ResultKind == Ambiguous && 3286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines (Ambiguity == AmbiguousBaseSubobjectTypes || 3296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ambiguity == AmbiguousBaseSubobjects))); 330651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return true; 3312a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall} 3322a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall 333f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall// Necessary because CXXBasePaths is not complete in Sema.h 3347d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallvoid LookupResult::deletePaths(CXXBasePaths *Paths) { 335f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall delete Paths; 3362a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor} 3372a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 338190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith/// Get a representative context for a declaration such that two declarations 339190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith/// will have the same context if they were found within the same scope. 340e28bdc329ebe990844795018fc8271fbd9d28364Benjamin Kramerstatic DeclContext *getContextForScopeMatching(Decl *D) { 341190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith // For function-local declarations, use that function as the context. This 342190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith // doesn't account for scopes within the function; the caller must deal with 343190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith // those. 344190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith DeclContext *DC = D->getLexicalDeclContext(); 345190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith if (DC->isFunctionOrMethod()) 346190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith return DC; 347190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith 348190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith // Otherwise, look at the semantic context of the declaration. The 349190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith // declaration must have been found there. 350190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith return D->getDeclContext()->getRedeclContext(); 351190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith} 352190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith 3537453ed4cb2cab113de3378df371b1c6f1243d832John McCall/// Resolves the result kind of this lookup. 3547d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallvoid LookupResult::resolveKind() { 355f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall unsigned N = Decls.size(); 356dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 357f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // Fast case: no possible ambiguity. 3586826314938f8510cd1a6b03b5d032592456ae27bJohn McCall if (N == 0) { 359dc5c78639f4e64d790c08e7b6494ae86ceea4ff1John McCall assert(ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation); 3606826314938f8510cd1a6b03b5d032592456ae27bJohn McCall return; 3616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall } 3626826314938f8510cd1a6b03b5d032592456ae27bJohn McCall 3637453ed4cb2cab113de3378df371b1c6f1243d832John McCall // If there's a single decl, we need to examine it to decide what 3647453ed4cb2cab113de3378df371b1c6f1243d832John McCall // kind of lookup this is. 3657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall if (N == 1) { 3662b147f072b118607aafafd2f05843243abfe4488Douglas Gregor NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); 3672b147f072b118607aafafd2f05843243abfe4488Douglas Gregor if (isa<FunctionTemplateDecl>(D)) 3687453ed4cb2cab113de3378df371b1c6f1243d832John McCall ResultKind = FoundOverloaded; 3692b147f072b118607aafafd2f05843243abfe4488Douglas Gregor else if (isa<UnresolvedUsingValueDecl>(D)) 3707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall ResultKind = FoundUnresolvedValue; 3717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall return; 3727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall } 3738b50d01949c649fbe89ea83f76e5f445170c7d26Anders Carlsson 3746e24726524c2b51b31bb4b622aa678a46b024f42John McCall // Don't do any extra resolution if we've already resolved as ambiguous. 375a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall if (ResultKind == Ambiguous) return; 3766e24726524c2b51b31bb4b622aa678a46b024f42John McCall 377f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall llvm::SmallPtrSet<NamedDecl*, 16> Unique; 3787f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor llvm::SmallPtrSet<QualType, 16> UniqueTypes; 379dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 380f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall bool Ambiguous = false; 381f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall bool HasTag = false, HasFunction = false, HasNonFunction = false; 3827453ed4cb2cab113de3378df371b1c6f1243d832John McCall bool HasFunctionTemplate = false, HasUnresolved = false; 3837176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 384f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall unsigned UniqueTagIndex = 0; 385dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 386f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall unsigned I = 0; 387f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall while (I < N) { 388314be4e7d8ef86202b0ec8e9ff0dcef853db3320John McCall NamedDecl *D = Decls[I]->getUnderlyingDecl(); 389314be4e7d8ef86202b0ec8e9ff0dcef853db3320John McCall D = cast<NamedDecl>(D->getCanonicalDecl()); 390f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 391745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis // Ignore an invalid declaration unless it's the only one left. 392745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis if (D->isInvalidDecl() && I < N-1) { 393745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis Decls[I] = Decls[--N]; 394745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis continue; 395745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis } 396745af1c348191bf31cfcd3ae61443a0321ec2d75Argyrios Kyrtzidis 3977f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // Redeclarations of types via typedef can occur both within a scope 3987f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // and, through using declarations and directives, across scopes. There is 3997f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // no ambiguity if they all refer to the same type, so unique based on the 4007f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // canonical type. 4017f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { 4027f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (!TD->getDeclContext()->isRecord()) { 4037f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor QualType T = SemaRef.Context.getTypeDeclType(TD); 4047f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (!UniqueTypes.insert(SemaRef.Context.getCanonicalType(T))) { 4057f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // The type is not unique; pull something off the back and continue 4067f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // at this index. 4077f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor Decls[I] = Decls[--N]; 4087f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor continue; 4097f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } 4107f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } 4117f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } 412dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 413314be4e7d8ef86202b0ec8e9ff0dcef853db3320John McCall if (!Unique.insert(D)) { 414f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // If it's not unique, pull something off the back (and 415f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // continue at this index). 416f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Decls[I] = Decls[--N]; 4177f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor continue; 418dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 419dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4207f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor // Otherwise, do some decl type analysis and then continue. 4217f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor 4227f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (isa<UnresolvedUsingValueDecl>(D)) { 4237f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasUnresolved = true; 4247f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } else if (isa<TagDecl>(D)) { 4257f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (HasTag) 4267f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor Ambiguous = true; 4277f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor UniqueTagIndex = I; 4287f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasTag = true; 4297f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } else if (isa<FunctionTemplateDecl>(D)) { 4307f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasFunction = true; 4317f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasFunctionTemplate = true; 4327f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor } else if (isa<FunctionDecl>(D)) { 4337f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasFunction = true; 434f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall } else { 4357f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor if (HasNonFunction) 4367f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor Ambiguous = true; 4377f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor HasNonFunction = true; 4387176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor } 4397f1c547ee6cd413845f7df9713c701f99866f91dDouglas Gregor I++; 4407176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor } 441516ff43cc4e20b637335d3dfa5b197ca8faa09cbDouglas Gregor 442f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // C++ [basic.scope.hiding]p2: 443f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // A class name or enumeration name can be hidden by the name of 444f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // an object, function, or enumerator declared in the same 445f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // scope. If a class or enumeration name and an object, function, 446f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // or enumerator are declared in the same scope (in any order) 447f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // with the same name, the class or enumeration name is hidden 448f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // wherever the object, function, or enumerator name is visible. 449f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // But it's still an error if there are distinct tag types found, 450f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // even if they're not visible. (ref?) 451fda8e12774921e3cac3ebcba1148bcf4479ddd7aJohn McCall if (HideTags && HasTag && !Ambiguous && 45277a1a8868e03bb74412f001f32d66ce0c26afae6Douglas Gregor (HasFunction || HasNonFunction || HasUnresolved)) { 453190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith if (getContextForScopeMatching(Decls[UniqueTagIndex])->Equals( 454190d1af17608f05bc9792af21fd0b4e20e438588Richard Smith getContextForScopeMatching(Decls[UniqueTagIndex ? 0 : N - 1]))) 45577a1a8868e03bb74412f001f32d66ce0c26afae6Douglas Gregor Decls[UniqueTagIndex] = Decls[--N]; 45677a1a8868e03bb74412f001f32d66ce0c26afae6Douglas Gregor else 45777a1a8868e03bb74412f001f32d66ce0c26afae6Douglas Gregor Ambiguous = true; 45877a1a8868e03bb74412f001f32d66ce0c26afae6Douglas Gregor } 459f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 460f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Decls.set_size(N); 461f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 462fda8e12774921e3cac3ebcba1148bcf4479ddd7aJohn McCall if (HasNonFunction && (HasFunction || HasUnresolved)) 463f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Ambiguous = true; 464eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 465f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (Ambiguous) 4666e24726524c2b51b31bb4b622aa678a46b024f42John McCall setAmbiguous(LookupResult::AmbiguousReference); 4677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall else if (HasUnresolved) 4687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall ResultKind = LookupResult::FoundUnresolvedValue; 4697453ed4cb2cab113de3378df371b1c6f1243d832John McCall else if (N > 1 || HasFunctionTemplate) 470a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall ResultKind = LookupResult::FoundOverloaded; 471f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall else 472a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall ResultKind = LookupResult::Found; 473eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor} 474eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 4757d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallvoid LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { 4766b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall CXXBasePaths::const_paths_iterator I, E; 477f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall for (I = P.begin(), E = P.end(); I != E; ++I) 4783bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie for (DeclContext::lookup_iterator DI = I->Decls.begin(), 4793bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DE = I->Decls.end(); DI != DE; ++DI) 480f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall addDecl(*DI); 4817176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor} 4827176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 4837d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallvoid LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { 484f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Paths = new CXXBasePaths; 485f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Paths->swap(P); 486f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall addDeclsFromBasePaths(*Paths); 487f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall resolveKind(); 4886e24726524c2b51b31bb4b622aa678a46b024f42John McCall setAmbiguous(AmbiguousBaseSubobjects); 489d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor} 490d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 4917d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallvoid LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { 492f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Paths = new CXXBasePaths; 493f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Paths->swap(P); 494f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall addDeclsFromBasePaths(*Paths); 495f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall resolveKind(); 4966e24726524c2b51b31bb4b622aa678a46b024f42John McCall setAmbiguous(AmbiguousBaseSubobjectTypes); 497d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor} 498d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 4995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid LookupResult::print(raw_ostream &Out) { 500f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Out << Decls.size() << " result(s)"; 501f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (isAmbiguous()) Out << ", ambiguous"; 502f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (Paths) Out << ", base paths present"; 503dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 504f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall for (iterator I = begin(), E = end(); I != E; ++I) { 505f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Out << "\n"; 506f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall (*I)->print(Out, 2); 50731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor } 508d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor} 509d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 51085910986c4ab01927c192aea86b2234fec568259Douglas Gregor/// \brief Lookup a builtin function, when name lookup would otherwise 51185910986c4ab01927c192aea86b2234fec568259Douglas Gregor/// fail. 51285910986c4ab01927c192aea86b2234fec568259Douglas Gregorstatic bool LookupBuiltin(Sema &S, LookupResult &R) { 51385910986c4ab01927c192aea86b2234fec568259Douglas Gregor Sema::LookupNameKind NameKind = R.getLookupKind(); 51485910986c4ab01927c192aea86b2234fec568259Douglas Gregor 51585910986c4ab01927c192aea86b2234fec568259Douglas Gregor // If we didn't find a use of this identifier, and if the identifier 51685910986c4ab01927c192aea86b2234fec568259Douglas Gregor // corresponds to a compiler builtin, create the decl object for the builtin 51785910986c4ab01927c192aea86b2234fec568259Douglas Gregor // now, injecting it into translation unit scope, and return it. 51885910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (NameKind == Sema::LookupOrdinaryName || 51985910986c4ab01927c192aea86b2234fec568259Douglas Gregor NameKind == Sema::LookupRedeclarationWithLinkage) { 52085910986c4ab01927c192aea86b2234fec568259Douglas Gregor IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); 52185910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (II) { 522cac18add73d095eaab600aefe27ea7174aec4922Nico Weber if (S.getLangOpts().CPlusPlus11 && S.getLangOpts().GNUMode && 523cac18add73d095eaab600aefe27ea7174aec4922Nico Weber II == S.getFloat128Identifier()) { 524cac18add73d095eaab600aefe27ea7174aec4922Nico Weber // libstdc++4.7's type_traits expects type __float128 to exist, so 525cac18add73d095eaab600aefe27ea7174aec4922Nico Weber // insert a dummy type to make that header build in gnu++11 mode. 526cac18add73d095eaab600aefe27ea7174aec4922Nico Weber R.addDecl(S.getASTContext().getFloat128StubType()); 527cac18add73d095eaab600aefe27ea7174aec4922Nico Weber return true; 528cac18add73d095eaab600aefe27ea7174aec4922Nico Weber } 529cac18add73d095eaab600aefe27ea7174aec4922Nico Weber 53085910986c4ab01927c192aea86b2234fec568259Douglas Gregor // If this is a builtin on this (or all) targets, create the decl. 53185910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (unsigned BuiltinID = II->getBuiltinID()) { 53285910986c4ab01927c192aea86b2234fec568259Douglas Gregor // In C++, we don't have any predefined library functions like 53385910986c4ab01927c192aea86b2234fec568259Douglas Gregor // 'malloc'. Instead, we'll just error. 5344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (S.getLangOpts().CPlusPlus && 53585910986c4ab01927c192aea86b2234fec568259Douglas Gregor S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 53685910986c4ab01927c192aea86b2234fec568259Douglas Gregor return false; 537dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, 539dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi BuiltinID, S.TUScope, 5406b9109e9d5446f213b12d2a6b76495bffb06b794Douglas Gregor R.isForRedeclaration(), 5416b9109e9d5446f213b12d2a6b76495bffb06b794Douglas Gregor R.getNameLoc())) { 54285910986c4ab01927c192aea86b2234fec568259Douglas Gregor R.addDecl(D); 5436b9109e9d5446f213b12d2a6b76495bffb06b794Douglas Gregor return true; 5446b9109e9d5446f213b12d2a6b76495bffb06b794Douglas Gregor } 54585910986c4ab01927c192aea86b2234fec568259Douglas Gregor } 54685910986c4ab01927c192aea86b2234fec568259Douglas Gregor } 54785910986c4ab01927c192aea86b2234fec568259Douglas Gregor } 54885910986c4ab01927c192aea86b2234fec568259Douglas Gregor 54985910986c4ab01927c192aea86b2234fec568259Douglas Gregor return false; 55085910986c4ab01927c192aea86b2234fec568259Douglas Gregor} 55185910986c4ab01927c192aea86b2234fec568259Douglas Gregor 5524923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor/// \brief Determine whether we can declare a special member function within 5534923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor/// the class at this point. 554d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smithstatic bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) { 5554923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor // We need to have a definition for the class. 5564923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor if (!Class->getDefinition() || Class->isDependentContext()) 5574923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor return false; 558dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5594923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor // We can't be in the middle of defining the class. 560d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith return !Class->isBeingDefined(); 5614923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor} 5624923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor 5634923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorvoid Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { 564d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith if (!CanDeclareSpecialMemberFunction(Class)) 565225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor return; 5661827403a7138946305c0058f262e02b595cf882fDouglas Gregor 5671827403a7138946305c0058f262e02b595cf882fDouglas Gregor // If the default constructor has not yet been declared, do so now. 568cdee3fee8ca4df7fb9179f29cc3ba96ac4fd0f95Sean Hunt if (Class->needsImplicitDefaultConstructor()) 5691827403a7138946305c0058f262e02b595cf882fDouglas Gregor DeclareImplicitDefaultConstructor(Class); 570dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 571225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor // If the copy constructor has not yet been declared, do so now. 572e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Class->needsImplicitCopyConstructor()) 573225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor DeclareImplicitCopyConstructor(Class); 574dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 575a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor // If the copy assignment operator has not yet been declared, do so now. 576e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Class->needsImplicitCopyAssignment()) 577a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor DeclareImplicitCopyAssignment(Class); 578a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor 57980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (getLangOpts().CPlusPlus11) { 58085ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl // If the move constructor has not yet been declared, do so now. 58185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl if (Class->needsImplicitMoveConstructor()) 58285ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl DeclareImplicitMoveConstructor(Class); // might not actually do it 58385ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 58485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl // If the move assignment operator has not yet been declared, do so now. 58585ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl if (Class->needsImplicitMoveAssignment()) 58685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl DeclareImplicitMoveAssignment(Class); // might not actually do it 58785ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl } 58885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 5894923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor // If the destructor has not yet been declared, do so now. 590e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Class->needsImplicitDestructor()) 591dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi DeclareImplicitDestructor(Class); 5924923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor} 5934923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor 594dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether this is the name of an implicitly-declared 595a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor/// special member function. 596a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorstatic bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { 597a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor switch (Name.getNameKind()) { 598225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor case DeclarationName::CXXConstructorName: 599a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor case DeclarationName::CXXDestructorName: 600a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor return true; 601dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 602a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor case DeclarationName::CXXOperatorName: 603a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor return Name.getCXXOverloadedOperator() == OO_Equal; 604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 605a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor default: 606dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi break; 607a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor } 608dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 609a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor return false; 610a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor} 611a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor 612a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor/// \brief If there are any implicit member functions with the given name 613a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor/// that need to be declared in the given declaration context, do so. 614dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void DeclareImplicitMemberFunctionsWithName(Sema &S, 615a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor DeclarationName Name, 616a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor const DeclContext *DC) { 617a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor if (!DC) 618a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor return; 619dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 620a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor switch (Name.getNameKind()) { 621225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor case DeclarationName::CXXConstructorName: 622225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) 623d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { 62485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); 625cdee3fee8ca4df7fb9179f29cc3ba96ac4fd0f95Sean Hunt if (Record->needsImplicitDefaultConstructor()) 62685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl S.DeclareImplicitDefaultConstructor(Class); 627e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Record->needsImplicitCopyConstructor()) 62885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl S.DeclareImplicitCopyConstructor(Class); 62980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (S.getLangOpts().CPlusPlus11 && 63085ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl Record->needsImplicitMoveConstructor()) 63185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl S.DeclareImplicitMoveConstructor(Class); 6321827403a7138946305c0058f262e02b595cf882fDouglas Gregor } 633225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor break; 634dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 635a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor case DeclarationName::CXXDestructorName: 636a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) 637e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Record->getDefinition() && Record->needsImplicitDestructor() && 638d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith CanDeclareSpecialMemberFunction(Record)) 639a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); 640a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor break; 641dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 642a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor case DeclarationName::CXXOperatorName: 643a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor if (Name.getCXXOverloadedOperator() != OO_Equal) 644a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor break; 645dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 64685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) { 647d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { 64885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); 649e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Record->needsImplicitCopyAssignment()) 65085ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl S.DeclareImplicitCopyAssignment(Class); 65180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (S.getLangOpts().CPlusPlus11 && 65285ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl Record->needsImplicitMoveAssignment()) 65385ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl S.DeclareImplicitMoveAssignment(Class); 65485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl } 65585ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl } 656a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor break; 657dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 658a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor default: 659dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi break; 660a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor } 661a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor} 6624923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor 663f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall// Adds all qualifying matches for a name within a decl context to the 664f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall// given lookup result. Returns true if any matches were found. 66585910986c4ab01927c192aea86b2234fec568259Douglas Gregorstatic bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { 666f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall bool Found = false; 66731a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor 6684923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor // Lazily declare C++ special member functions. 6694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (S.getLangOpts().CPlusPlus) 670a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC); 671dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 6724923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor // Perform lookup into this declaration context. 6733bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_const_result DR = DC->lookup(R.getLookupName()); 6743bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie for (DeclContext::lookup_const_iterator I = DR.begin(), E = DR.end(); I != E; 6753bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie ++I) { 67646460a68f6508775e98c19b4bb8454bb471aac24John McCall NamedDecl *D = *I; 677553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if ((D = R.getAcceptableDecl(D))) { 67846460a68f6508775e98c19b4bb8454bb471aac24John McCall R.addDecl(D); 67948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor Found = true; 68048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor } 68148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor } 68231a19b6989bbf326d2de5ae12e712e2a65ca9c34Douglas Gregor 68385910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R)) 68485910986c4ab01927c192aea86b2234fec568259Douglas Gregor return true; 68585910986c4ab01927c192aea86b2234fec568259Douglas Gregor 68648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor if (R.getLookupName().getNameKind() 687aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth != DeclarationName::CXXConversionFunctionName || 688aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth R.getLookupName().getCXXNameType()->isDependentType() || 689aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth !isa<CXXRecordDecl>(DC)) 690aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth return Found; 691aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth 692aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // C++ [temp.mem]p6: 693dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // A specialization of a conversion function template is not found by 694aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // name lookup. Instead, any conversion function templates visible in the 695aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // context of the use are considered. [...] 696aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); 6975e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (!Record->isCompleteDefinition()) 698aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth return Found; 699aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth 7009d29543284e75648ac89c6e9586fc7cf786cf66fArgyrios Kyrtzidis for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(), 7019d29543284e75648ac89c6e9586fc7cf786cf66fArgyrios Kyrtzidis UEnd = Record->conversion_end(); U != UEnd; ++U) { 702aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); 703aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth if (!ConvTemplate) 704aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth continue; 705dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 706aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // When we're performing lookup for the purposes of redeclaration, just 707dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // add the conversion function template. When we deduce template 708dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // arguments for specializations, we'll end up unifying the return 709aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // type of the new declaration with the type of the function template. 710aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth if (R.isForRedeclaration()) { 711aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth R.addDecl(ConvTemplate); 712aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth Found = true; 713aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth continue; 714aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth } 715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 71648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor // C++ [temp.mem]p6: 717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // [...] For each such operator, if argument deduction succeeds 718dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // (14.9.2.3), the resulting specialization is used as if found by 719aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // name lookup. 720aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // 721aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // When referencing a conversion function for any purpose other than 722aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // a redeclaration (such that we'll be building an expression with the 723dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // result), perform template argument deduction and place the 724aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // specialization into the result set. We do this to avoid forcing all 725aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // callers to perform special deduction for conversion functions. 72693e4599196654907dad3bd476013ad05d36ce604Craig Topper TemplateDeductionInfo Info(R.getNameLoc()); 7276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines FunctionDecl *Specialization = nullptr; 728dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 729dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi const FunctionProtoType *ConvProto 730aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); 731aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth assert(ConvProto && "Nonsensical conversion function template type"); 732aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth 733aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // Compute the type of the function that we would expect the conversion 734aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // function to have, if it were to match the name given. 735aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // FIXME: Calling convention! 736e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo(); 737ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C); 7388b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl EPI.ExceptionSpecType = EST_None; 739e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall EPI.NumExceptions = 0; 740aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth QualType ExpectedType 741aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth = R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(), 7425543169296beeb183b9c9392debc774fcf493eebDmitri Gribenko None, EPI); 743dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 744aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // Perform template argument deduction against the type that we would 745aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth // expect the function to have. 7466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType, 747aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth Specialization, Info) 748aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth == Sema::TDK_Success) { 749aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth R.addDecl(Specialization); 750aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth Found = true; 75148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor } 75248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor } 753aaa1a895725f3977c005304be9ca36747d9e3c97Chandler Carruth 754f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return Found; 755d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor} 756d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 757d7be78a0524251c30329359c9d92485c6d7ac424John McCall// Performs C++ unqualified lookup into the given file context. 758f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCallstatic bool 759dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiCppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, 76085910986c4ab01927c192aea86b2234fec568259Douglas Gregor DeclContext *NS, UnqualUsingDirectiveSet &UDirs) { 7617dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor 7627dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); 7637dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor 764d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Perform direct name lookup into the LookupCtx. 76585910986c4ab01927c192aea86b2234fec568259Douglas Gregor bool Found = LookupDirect(S, R, NS); 7667dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor 767d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Perform direct name lookup into the namespaces nominated by the 768d7be78a0524251c30329359c9d92485c6d7ac424John McCall // using directives whose common ancestor is this namespace. 769d7be78a0524251c30329359c9d92485c6d7ac424John McCall UnqualUsingDirectiveSet::const_iterator UI, UEnd; 770651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(UI, UEnd) = UDirs.getNamespacesFor(NS); 7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 772d7be78a0524251c30329359c9d92485c6d7ac424John McCall for (; UI != UEnd; ++UI) 77385910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (LookupDirect(S, R, UI->getNominatedNamespace())) 774d7be78a0524251c30329359c9d92485c6d7ac424John McCall Found = true; 775f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 776f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.resolveKind(); 777f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 778f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return Found; 7797dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor} 7807dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor 7817dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregorstatic bool isNamespaceOrTranslationUnitScope(Scope *S) { 782f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek if (DeclContext *Ctx = S->getEntity()) 7837dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor return Ctx->isFileContext(); 7847dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor return false; 7852a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor} 7864c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor 787711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// Find the next outer declaration context from this scope. This 788711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// routine actually returns the semantic outer context, which may 789711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// differ from the lexical context (encoded directly in the Scope 790711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// stack) when we are parsing a member of a class template. In this 791711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// case, the second element of the pair will be true, to indicate that 792711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// name lookup should continue searching in this semantic context when 793711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor// it leaves the current template parameter scope. 794711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregorstatic std::pair<DeclContext *, bool> findOuterContext(Scope *S) { 795f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek DeclContext *DC = S->getEntity(); 7966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DeclContext *Lexical = nullptr; 797dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi for (Scope *OuterS = S->getParent(); OuterS; 798711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor OuterS = OuterS->getParent()) { 799711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor if (OuterS->getEntity()) { 800f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek Lexical = OuterS->getEntity(); 801711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor break; 802711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor } 803711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor } 804711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor 805711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // C++ [temp.local]p8: 806711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // In the definition of a member of a class template that appears 807711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // outside of the namespace containing the class template 808711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // definition, the name of a template-parameter hides the name of 809711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // a member of this namespace. 810711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // 811711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // Example: 812711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // 813dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // namespace N { 814dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // class C { }; 815711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // 816711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // template<class T> class B { 817711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // void f(T); 818dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // }; 819711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // } 820711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // 821711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // template<class C> void N::B<C>::f(C) { 822711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // C b; // C is the template parameter, not N::C 823711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // } 824711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // 825711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // In this example, the lexical context we return is the 826711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // TranslationUnit, while the semantic context is the namespace N. 827dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!Lexical || !DC || !S->getParent() || 828711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor !S->getParent()->isTemplateParamScope()) 829711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor return std::make_pair(Lexical, false); 830711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor 831dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // Find the outermost template parameter scope. 832711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // For the example, this is the scope for the template parameters of 833711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // template<class C>. 834711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor Scope *OutermostTemplateScope = S->getParent(); 835711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor while (OutermostTemplateScope->getParent() && 836711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor OutermostTemplateScope->getParent()->isTemplateParamScope()) 837711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor OutermostTemplateScope = OutermostTemplateScope->getParent(); 838dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 839711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // Find the namespace context in which the original scope occurs. In 840711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // the example, this is namespace N. 841711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor DeclContext *Semantic = DC; 842711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor while (!Semantic->isFileContext()) 843711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor Semantic = Semantic->getParent(); 844dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 845711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // Find the declaration context just outside of the template 846711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // parameter scope. This is the context in which the template is 847711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // being lexically declaration (a namespace context). In the 848711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // example, this is the global scope. 849711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor if (Lexical->isFileContext() && !Lexical->Equals(Semantic) && 850711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor Lexical->Encloses(Semantic)) 851711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor return std::make_pair(Semantic, true); 852711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor 853711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor return std::make_pair(Lexical, false); 854e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor} 855e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor 856a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smithnamespace { 857a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith/// An RAII object to specify that we want to find block scope extern 858a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith/// declarations. 859a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smithstruct FindLocalExternScope { 860a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith FindLocalExternScope(LookupResult &R) 861a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith : R(R), OldFindLocalExtern(R.getIdentifierNamespace() & 862a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith Decl::IDNS_LocalExtern) { 863a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith R.setFindLocalExtern(R.getIdentifierNamespace() & Decl::IDNS_Ordinary); 864a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith } 865a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith void restore() { 866a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith R.setFindLocalExtern(OldFindLocalExtern); 867a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith } 868a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith ~FindLocalExternScope() { 869a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith restore(); 870a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith } 871a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith LookupResult &R; 872a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith bool OldFindLocalExtern; 873a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith}; 874a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith} 875a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 876a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCallbool Sema::CppLookupName(LookupResult &R, Scope *S) { 8774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup"); 878a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall 879a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall DeclarationName Name = R.getLookupName(); 880dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith Sema::LookupNameKind NameKind = R.getLookupKind(); 881a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall 882a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor // If this is the name of an implicitly-declared special member function, 883a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor // go through the scope stack to implicitly declare 884a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor if (isImplicitlyDeclaredMemberFunctionName(Name)) { 885a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor for (Scope *PreS = S; PreS; PreS = PreS->getParent()) 886f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek if (DeclContext *DC = PreS->getEntity()) 887a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor DeclareImplicitMemberFunctionsWithName(*this, Name, DC); 888a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor } 889dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 890a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor // Implicitly declare member functions with the name we're looking for, if in 891a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor // fact we are in a scope where it matters. 892a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor 8932a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor Scope *Initial = S; 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump IdentifierResolver::iterator 8952a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor I = IdResolver.begin(Name), 8962a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor IEnd = IdResolver.end(); 8972a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 8982a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // First we lookup local scope. 899aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] 9002a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // ...During unqualified name lookup (3.4.1), the names appear as if 9012a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // they were declared in the nearest enclosing namespace which contains 9022a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // both the using-directive and the nominated namespace. 90333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman // [Note: in this context, "contains" means "contains directly or 9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // indirectly". 9052a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // 9062a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // For example: 9072a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // namespace A { int i; } 9082a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // void foo() { 9092a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // int i; 9102a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // { 9112a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // using namespace A; 9122a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // ++i; // finds local 'i', A::i appears at global scope 9132a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // } 9142a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // } 91547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // 91644b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UnqualUsingDirectiveSet UDirs; 91744b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor bool VisitedUsingDirectives = false; 918dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith bool LeftStartingScope = false; 9196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DeclContext *OutsideOfTemplateParamDC = nullptr; 920a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 921a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith // When performing a scope lookup, we want to find local extern decls. 922a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith FindLocalExternScope FindLocals(R); 923a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 9247dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { 925f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek DeclContext *Ctx = S->getEntity(); 926d2235f60e7bbd9e690c05fced371df9da76adc2bDouglas Gregor 9272a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // Check whether the IdResolver has anything in this scope. 928f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall bool Found = false; 929d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall for (; I != IEnd && S->isDeclScope(*I); ++I) { 930553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if (NamedDecl *ND = R.getAcceptableDecl(*I)) { 931dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith if (NameKind == LookupRedeclarationWithLinkage) { 932dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith // Determine whether this (or a previous) declaration is 933dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith // out-of-scope. 934dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith if (!LeftStartingScope && !Initial->isDeclScope(*I)) 935dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith LeftStartingScope = true; 936dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith 937dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith // If we found something outside of our starting scope that 93849ef481bc1b0b0f757ba6ac1ad6d0425390e7654Richard Smith // does not have linkage, skip it. If it's a template parameter, 93949ef481bc1b0b0f757ba6ac1ad6d0425390e7654Richard Smith // we still find it, so we can diagnose the invalid redeclaration. 94049ef481bc1b0b0f757ba6ac1ad6d0425390e7654Richard Smith if (LeftStartingScope && !((*I)->hasLinkage()) && 94149ef481bc1b0b0f757ba6ac1ad6d0425390e7654Richard Smith !(*I)->isTemplateParameter()) { 942dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith R.setShadowed(); 943dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith continue; 944dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith } 945dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith } 946dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith 947f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Found = true; 948553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor R.addDecl(ND); 9492a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 9502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 951f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (Found) { 952f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.resolveKind(); 953d2235f60e7bbd9e690c05fced371df9da76adc2bDouglas Gregor if (S->isClassScope()) 954d2235f60e7bbd9e690c05fced371df9da76adc2bDouglas Gregor if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx)) 955d2235f60e7bbd9e690c05fced371df9da76adc2bDouglas Gregor R.setNamingClass(Record); 956f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 957f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall } 958f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 959dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith if (NameKind == LookupLocalFriendName && !S->isClassScope()) { 9604e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith // C++11 [class.friend]p11: 9614e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith // If a friend declaration appears in a local class and the name 9624e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith // specified is an unqualified name, a prior declaration is 9634e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith // looked up without considering scopes that are outside the 9644e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith // innermost enclosing non-class scope. 9654e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith return false; 9664e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith } 9674e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith 968711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 969711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor S->getParent() && !S->getParent()->isTemplateParamScope()) { 970711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // We've just searched the last template parameter scope and 971bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru // found nothing, so look into the contexts between the 972711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // lexical and semantic declaration contexts returned by 973711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // findOuterContext(). This implements the name lookup behavior 974711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // of C++ [temp.local]p8. 975711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor Ctx = OutsideOfTemplateParamDC; 9766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines OutsideOfTemplateParamDC = nullptr; 977711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor } 978711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor 979711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor if (Ctx) { 980711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor DeclContext *OuterCtx; 981711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor bool SearchAfterTemplateScope; 982651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 983711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor if (SearchAfterTemplateScope) 984711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor OutsideOfTemplateParamDC = OuterCtx; 985711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor 986dbdf5e7d0b6f1f8d8c496c1a0ada6f706cddf100Douglas Gregor for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 98736262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // We do not directly look into transparent contexts, since 98836262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // those entities will be found in the nearest enclosing 98936262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // non-transparent context. 99036262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (Ctx->isTransparentContext()) 991e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor continue; 99236262b81559cfce4f67256d052e4fed343a02861Douglas Gregor 99336262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // We do not look directly into function or method contexts, 99436262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // since all of the local variables and parameters of the 99536262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // function/method are present within the Scope. 99636262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (Ctx->isFunctionOrMethod()) { 99736262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // If we have an Objective-C instance method, look for ivars 99836262b81559cfce4f67256d052e4fed343a02861Douglas Gregor // in the corresponding interface. 99936262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 100036262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) 100136262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { 100236262b81559cfce4f67256d052e4fed343a02861Douglas Gregor ObjCInterfaceDecl *ClassDeclared; 100336262b81559cfce4f67256d052e4fed343a02861Douglas Gregor if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( 1004dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Name.getAsIdentifierInfo(), 100536262b81559cfce4f67256d052e4fed343a02861Douglas Gregor ClassDeclared)) { 1006553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) { 1007553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor R.addDecl(ND); 100836262b81559cfce4f67256d052e4fed343a02861Douglas Gregor R.resolveKind(); 100936262b81559cfce4f67256d052e4fed343a02861Douglas Gregor return true; 101036262b81559cfce4f67256d052e4fed343a02861Douglas Gregor } 101136262b81559cfce4f67256d052e4fed343a02861Douglas Gregor } 101236262b81559cfce4f67256d052e4fed343a02861Douglas Gregor } 101336262b81559cfce4f67256d052e4fed343a02861Douglas Gregor } 101436262b81559cfce4f67256d052e4fed343a02861Douglas Gregor 101536262b81559cfce4f67256d052e4fed343a02861Douglas Gregor continue; 101636262b81559cfce4f67256d052e4fed343a02861Douglas Gregor } 101736262b81559cfce4f67256d052e4fed343a02861Douglas Gregor 10186bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor // If this is a file context, we need to perform unqualified name 10196bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor // lookup considering using directives. 10206bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor if (Ctx->isFileContext()) { 102144b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor // If we haven't handled using directives yet, do so now. 102244b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor if (!VisitedUsingDirectives) { 102344b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor // Add using directives from this context up to the top level. 102434366208e3ec6876ef501e85978466d2ddecb3d2Douglas Gregor for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) { 102534366208e3ec6876ef501e85978466d2ddecb3d2Douglas Gregor if (UCtx->isTransparentContext()) 102634366208e3ec6876ef501e85978466d2ddecb3d2Douglas Gregor continue; 102734366208e3ec6876ef501e85978466d2ddecb3d2Douglas Gregor 102844b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UDirs.visit(UCtx, UCtx); 102934366208e3ec6876ef501e85978466d2ddecb3d2Douglas Gregor } 103044b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor 103144b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor // Find the innermost file scope, so we can add using directives 103244b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor // from local scopes. 103344b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor Scope *InnermostFileScope = S; 103444b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor while (InnermostFileScope && 103544b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor !isNamespaceOrTranslationUnitScope(InnermostFileScope)) 103644b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor InnermostFileScope = InnermostFileScope->getParent(); 103744b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UDirs.visitScopeChain(Initial, InnermostFileScope); 103844b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor 103944b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UDirs.done(); 104044b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor 104144b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor VisitedUsingDirectives = true; 104244b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor } 10436bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor 10446bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) { 10456bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor R.resolveKind(); 10466bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor return true; 10476bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor } 10486bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor 10496bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor continue; 10506bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor } 10516bed88e9d25fd7e16edf3d95447ba414d9d73d72Douglas Gregor 1052e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // Perform qualified name lookup into this context. 1053e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // FIXME: In some cases, we know that every name that could be found by 1054e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // this qualified name lookup will also be on the identifier chain. For 1055e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // example, inside a class without any base classes, we never need to 1056e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // perform qualified lookup because all of the members are on top of the 1057e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor // identifier chain. 10587d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) 1059f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1060551f48c2d96c8a519feb195d34a691c8b97b3994Douglas Gregor } 10617dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor } 10622a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 10634c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor 1064d7be78a0524251c30329359c9d92485c6d7ac424John McCall // Stop if we ran out of scopes. 1065d7be78a0524251c30329359c9d92485c6d7ac424John McCall // FIXME: This really, really shouldn't be happening. 1066d7be78a0524251c30329359c9d92485c6d7ac424John McCall if (!S) return false; 1067d7be78a0524251c30329359c9d92485c6d7ac424John McCall 106878f5911a4ef8742c51a7f7643957e1aaa0098324Argyrios Kyrtzidis // If we are looking for members, no need to look into global/namespace scope. 1069dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith if (NameKind == LookupMemberName) 107078f5911a4ef8742c51a7f7643957e1aaa0098324Argyrios Kyrtzidis return false; 107178f5911a4ef8742c51a7f7643957e1aaa0098324Argyrios Kyrtzidis 10727dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor // Collect UsingDirectiveDecls in all scopes, and recursively all 10732a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // nominated namespaces by those using-directives. 1074d7be78a0524251c30329359c9d92485c6d7ac424John McCall // 1075390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we 1076390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // don't build it for each lookup! 107744b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor if (!VisitedUsingDirectives) { 107844b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UDirs.visitScopeChain(Initial, S); 107944b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor UDirs.done(); 108044b2ea97957b553e76e757c1926c3ad1fadbe1b2Douglas Gregor } 1081a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 1082a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith // If we're not performing redeclaration lookup, do not look for local 1083a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith // extern declarations outside of a function scope. 1084a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith if (!R.isForRedeclaration()) 1085a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith FindLocals.restore(); 1086a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 10877dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor // Lookup namespace scope, and global scope. 10882a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // Unqualified name lookup in C++ requires looking into scopes 10892a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // that aren't strictly lexical, and therefore we walk through the 10902a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // context as well as walking through the scopes. 10912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor for (; S; S = S->getParent()) { 10922a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // Check whether the IdResolver has anything in this scope. 1093f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall bool Found = false; 1094d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall for (; I != IEnd && S->isDeclScope(*I); ++I) { 1095553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if (NamedDecl *ND = R.getAcceptableDecl(*I)) { 10962a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // We found something. Look for anything else in our scope 10972a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // with this same name and in an acceptable identifier 10982a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // namespace, so that we can construct an overload set if we 10992a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // need to. 1100f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall Found = true; 1101553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor R.addDecl(ND); 11022a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 11032a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 11044c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor 110500b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (Found && S->isTemplateParamScope()) { 110600b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor R.resolveKind(); 110700b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor return true; 110800b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor } 11091df0ee91a9d55b5c2eb0d96e7590f1c8b8fe8734Douglas Gregor 1110f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek DeclContext *Ctx = S->getEntity(); 111100b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC && 111200b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor S->getParent() && !S->getParent()->isTemplateParamScope()) { 111300b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // We've just searched the last template parameter scope and 1114bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru // found nothing, so look into the contexts between the 111500b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // lexical and semantic declaration contexts returned by 111600b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // findOuterContext(). This implements the name lookup behavior 111700b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // of C++ [temp.local]p8. 111800b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor Ctx = OutsideOfTemplateParamDC; 11196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines OutsideOfTemplateParamDC = nullptr; 11201df0ee91a9d55b5c2eb0d96e7590f1c8b8fe8734Douglas Gregor } 1121dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 112200b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (Ctx) { 112300b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor DeclContext *OuterCtx; 112400b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor bool SearchAfterTemplateScope; 1125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S); 112600b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (SearchAfterTemplateScope) 112700b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor OutsideOfTemplateParamDC = OuterCtx; 11282a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 112900b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { 113000b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // We do not directly look into transparent contexts, since 113100b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // those entities will be found in the nearest enclosing 113200b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // non-transparent context. 113300b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (Ctx->isTransparentContext()) 113400b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor continue; 1135dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 113600b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // If we have a context, and it's not a context stashed in the 113700b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // template parameter scope for an out-of-line definition, also 113800b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // look into that context. 113900b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (!(Found && S && S->isTemplateParamScope())) { 114000b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor assert(Ctx->isFileContext() && 114100b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor "We should have been looking only at file context here already."); 1142dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 114300b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor // Look into context considering using-directives. 114400b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) 114500b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor Found = true; 114600b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor } 1147dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 114800b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (Found) { 114900b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor R.resolveKind(); 115000b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor return true; 115100b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor } 1152dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 115300b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor if (R.isForRedeclaration() && !Ctx->isTransparentContext()) 115400b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor return false; 115500b4b039f02d338ae4774797053235a7e65abbdeDouglas Gregor } 1156f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall } 11572a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 11581df0ee91a9d55b5c2eb0d96e7590f1c8b8fe8734Douglas Gregor if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) 1159f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 11604c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor } 1161f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 1162f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return !R.empty(); 11634c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor} 11644c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor 1165b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// \brief Find the declaration that a class temploid member specialization was 1166b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// instantiated from, or the member itself if it is an explicit specialization. 1167b775100fea6d8955149897dae1adca50ca471d17Richard Smithstatic Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) { 1168b775100fea6d8955149897dae1adca50ca471d17Richard Smith return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom(); 1169b775100fea6d8955149897dae1adca50ca471d17Richard Smith} 1170b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1171b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// \brief Find the module in which the given declaration was defined. 1172b775100fea6d8955149897dae1adca50ca471d17Richard Smithstatic Module *getDefiningModule(Decl *Entity) { 1173b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) { 1174b775100fea6d8955149897dae1adca50ca471d17Richard Smith // If this function was instantiated from a template, the defining module is 1175b775100fea6d8955149897dae1adca50ca471d17Richard Smith // the module containing the pattern. 1176b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) 1177b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = Pattern; 1178b775100fea6d8955149897dae1adca50ca471d17Richard Smith } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) { 1179b775100fea6d8955149897dae1adca50ca471d17Richard Smith // If it's a class template specialization, find the template or partial 1180b775100fea6d8955149897dae1adca50ca471d17Richard Smith // specialization from which it was instantiated. 1181b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (ClassTemplateSpecializationDecl *SpecRD = 1182b775100fea6d8955149897dae1adca50ca471d17Richard Smith dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 1183b775100fea6d8955149897dae1adca50ca471d17Richard Smith llvm::PointerUnion<ClassTemplateDecl*, 1184b775100fea6d8955149897dae1adca50ca471d17Richard Smith ClassTemplatePartialSpecializationDecl*> From = 1185b775100fea6d8955149897dae1adca50ca471d17Richard Smith SpecRD->getInstantiatedFrom(); 1186b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (ClassTemplateDecl *FromTemplate = From.dyn_cast<ClassTemplateDecl*>()) 1187b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = FromTemplate->getTemplatedDecl(); 1188b775100fea6d8955149897dae1adca50ca471d17Richard Smith else if (From) 1189b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = From.get<ClassTemplatePartialSpecializationDecl*>(); 1190b775100fea6d8955149897dae1adca50ca471d17Richard Smith // Otherwise, it's an explicit specialization. 1191b775100fea6d8955149897dae1adca50ca471d17Richard Smith } else if (MemberSpecializationInfo *MSInfo = 1192b775100fea6d8955149897dae1adca50ca471d17Richard Smith RD->getMemberSpecializationInfo()) 1193b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = getInstantiatedFrom(RD, MSInfo); 1194b775100fea6d8955149897dae1adca50ca471d17Richard Smith } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) { 1195b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo()) 1196b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = getInstantiatedFrom(ED, MSInfo); 1197b775100fea6d8955149897dae1adca50ca471d17Richard Smith } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) { 1198b775100fea6d8955149897dae1adca50ca471d17Richard Smith // FIXME: Map from variable template specializations back to the template. 1199b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) 1200b775100fea6d8955149897dae1adca50ca471d17Richard Smith Entity = getInstantiatedFrom(VD, MSInfo); 1201b775100fea6d8955149897dae1adca50ca471d17Richard Smith } 1202b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1203b775100fea6d8955149897dae1adca50ca471d17Richard Smith // Walk up to the containing context. That might also have been instantiated 1204b775100fea6d8955149897dae1adca50ca471d17Richard Smith // from a template. 1205b775100fea6d8955149897dae1adca50ca471d17Richard Smith DeclContext *Context = Entity->getDeclContext(); 1206b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (Context->isFileContext()) 1207b775100fea6d8955149897dae1adca50ca471d17Richard Smith return Entity->getOwningModule(); 1208b775100fea6d8955149897dae1adca50ca471d17Richard Smith return getDefiningModule(cast<Decl>(Context)); 1209b775100fea6d8955149897dae1adca50ca471d17Richard Smith} 1210b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1211b775100fea6d8955149897dae1adca50ca471d17Richard Smithllvm::DenseSet<Module*> &Sema::getLookupModules() { 1212b775100fea6d8955149897dae1adca50ca471d17Richard Smith unsigned N = ActiveTemplateInstantiations.size(); 1213b775100fea6d8955149897dae1adca50ca471d17Richard Smith for (unsigned I = ActiveTemplateInstantiationLookupModules.size(); 1214b775100fea6d8955149897dae1adca50ca471d17Richard Smith I != N; ++I) { 1215b775100fea6d8955149897dae1adca50ca471d17Richard Smith Module *M = getDefiningModule(ActiveTemplateInstantiations[I].Entity); 1216b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (M && !LookupModulesCache.insert(M).second) 12176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines M = nullptr; 1218b775100fea6d8955149897dae1adca50ca471d17Richard Smith ActiveTemplateInstantiationLookupModules.push_back(M); 1219b775100fea6d8955149897dae1adca50ca471d17Richard Smith } 1220b775100fea6d8955149897dae1adca50ca471d17Richard Smith return LookupModulesCache; 1221b775100fea6d8955149897dae1adca50ca471d17Richard Smith} 1222b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1223b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// \brief Determine whether a declaration is visible to name lookup. 1224b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// 1225b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// This routine determines whether the declaration D is visible in the current 1226b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// lookup context, taking into account the current template instantiation 1227b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// stack. During template instantiation, a declaration is visible if it is 1228b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// visible from a module containing any entity on the template instantiation 1229b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// path (by instantiating a template, you allow it to see the declarations that 1230b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// your module can see, including those later on in your module). 1231b775100fea6d8955149897dae1adca50ca471d17Richard Smithbool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) { 1232b775100fea6d8955149897dae1adca50ca471d17Richard Smith assert(D->isHidden() && !SemaRef.ActiveTemplateInstantiations.empty() && 1233b775100fea6d8955149897dae1adca50ca471d17Richard Smith "should not call this: not in slow case"); 1234b775100fea6d8955149897dae1adca50ca471d17Richard Smith Module *DeclModule = D->getOwningModule(); 1235b775100fea6d8955149897dae1adca50ca471d17Richard Smith assert(DeclModule && "hidden decl not from a module"); 1236b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1237b775100fea6d8955149897dae1adca50ca471d17Richard Smith // Find the extra places where we need to look. 1238b775100fea6d8955149897dae1adca50ca471d17Richard Smith llvm::DenseSet<Module*> &LookupModules = SemaRef.getLookupModules(); 1239b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (LookupModules.empty()) 1240b775100fea6d8955149897dae1adca50ca471d17Richard Smith return false; 1241b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1242b775100fea6d8955149897dae1adca50ca471d17Richard Smith // If our lookup set contains the decl's module, it's visible. 1243b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (LookupModules.count(DeclModule)) 1244b775100fea6d8955149897dae1adca50ca471d17Richard Smith return true; 1245b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1246b775100fea6d8955149897dae1adca50ca471d17Richard Smith // If the declaration isn't exported, it's not visible in any other module. 1247b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (D->isModulePrivate()) 1248b775100fea6d8955149897dae1adca50ca471d17Richard Smith return false; 1249b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1250b775100fea6d8955149897dae1adca50ca471d17Richard Smith // Check whether DeclModule is transitively exported to an import of 1251b775100fea6d8955149897dae1adca50ca471d17Richard Smith // the lookup set. 1252b775100fea6d8955149897dae1adca50ca471d17Richard Smith for (llvm::DenseSet<Module *>::iterator I = LookupModules.begin(), 1253b775100fea6d8955149897dae1adca50ca471d17Richard Smith E = LookupModules.end(); 1254b775100fea6d8955149897dae1adca50ca471d17Richard Smith I != E; ++I) 1255b775100fea6d8955149897dae1adca50ca471d17Richard Smith if ((*I)->isModuleVisible(DeclModule)) 1256b775100fea6d8955149897dae1adca50ca471d17Richard Smith return true; 1257b775100fea6d8955149897dae1adca50ca471d17Richard Smith return false; 1258b775100fea6d8955149897dae1adca50ca471d17Richard Smith} 1259b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1260553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// \brief Retrieve the visible declaration corresponding to D, if any. 1261553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// 1262553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// This routine determines whether the declaration D is visible in the current 1263553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// module, with the current imports. If not, it checks whether any 1264553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// redeclaration of D is visible, and if so, returns that declaration. 1265b775100fea6d8955149897dae1adca50ca471d17Richard Smith/// 1266553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// \returns D, or a visible previous declaration of D, whichever is more recent 1267553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor/// and visible. If no declaration of D is visible, returns null. 1268d67679d7439bd17b06574781b908630f4640c662Richard Smithstatic NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 1269d67679d7439bd17b06574781b908630f4640c662Richard Smith assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 1270b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1271651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto RD : D->redecls()) { 1272651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (auto ND = dyn_cast<NamedDecl>(RD)) { 1273d67679d7439bd17b06574781b908630f4640c662Richard Smith if (LookupResult::isVisible(SemaRef, ND)) 12740782ef2bd0ef5025ac6512cfa445a80a464c3b7fDouglas Gregor return ND; 12750782ef2bd0ef5025ac6512cfa445a80a464c3b7fDouglas Gregor } 1276553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor } 1277b775100fea6d8955149897dae1adca50ca471d17Richard Smith 12786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 1279553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor} 1280553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor 1281d67679d7439bd17b06574781b908630f4640c662Richard SmithNamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const { 1282d67679d7439bd17b06574781b908630f4640c662Richard Smith return findAcceptableDecl(SemaRef, D); 1283d67679d7439bd17b06574781b908630f4640c662Richard Smith} 1284d67679d7439bd17b06574781b908630f4640c662Richard Smith 1285eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @brief Perform unqualified name lookup starting from a given 1286eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// scope. 1287eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1288eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is 1289eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// used to find names within the current scope. For example, 'x' in 1290eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @code 1291eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// int x; 1292eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// int f() { 1293eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// return x; // unqualified name look finds 'x' in the global scope 1294eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// } 1295eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @endcode 1296eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1297eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// Different lookup criteria can find different names. For example, a 1298eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// particular scope can have both a struct and a function of the same 1299eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// name, and each can be found by certain lookup criteria. For more 1300eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// information about lookup criteria, see the documentation for the 1301eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// class LookupCriteria. 1302eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1303eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @param S The scope from which unqualified name lookup will 1304eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// begin. If the lookup criteria permits, name lookup may also search 1305eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// in the parent scopes. 1306eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 13078da16874f48d2c5c8f1275c7fd77caf8a8af3f81James Dennett/// @param [in,out] R Specifies the lookup to perform (e.g., the name to 13088da16874f48d2c5c8f1275c7fd77caf8a8af3f81James Dennett/// look up and the lookup kind), and is updated with the results of lookup 13098da16874f48d2c5c8f1275c7fd77caf8a8af3f81James Dennett/// including zero or more declarations and possibly additional information 13108da16874f48d2c5c8f1275c7fd77caf8a8af3f81James Dennett/// used to diagnose ambiguities. 1311eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 13128da16874f48d2c5c8f1275c7fd77caf8a8af3f81James Dennett/// @returns \c true if lookup succeeded and false otherwise. 1313a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCallbool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) { 1314a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall DeclarationName Name = R.getLookupName(); 1315f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall if (!Name) return false; 1316eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 1317a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall LookupNameKind NameKind = R.getLookupKind(); 1318a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall 13194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus) { 1320eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // Unqualified name lookup in C/Objective-C is purely lexical, so 1321eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // search in the declarations attached to the name. 13221d7c52803e49d651a66cee782e264f62078c1da5John McCall if (NameKind == Sema::LookupRedeclarationWithLinkage) { 1323d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // Find the nearest non-transparent declaration scope. 1324d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor while (!(S->getFlags() & Scope::DeclScope) || 1325f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek (S->getEntity() && S->getEntity()->isTransparentContext())) 1326d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor S = S->getParent(); 13274c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor } 1328eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 1329a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith // When performing a scope lookup, we want to find local extern decls. 1330a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith FindLocalExternScope FindLocals(R); 1331a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 1332eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // Scan up the scope chain looking for a decl that matches this 1333eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // identifier that is in the appropriate namespace. This search 1334eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // should not take long, as shadowing of names is uncommon, and 1335eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // deep shadowing is extremely uncommon. 1336d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor bool LeftStartingScope = false; 1337d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor 13384c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor for (IdentifierResolver::iterator I = IdResolver.begin(Name), 13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump IEnd = IdResolver.end(); 13404c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor I != IEnd; ++I) 1341b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (NamedDecl *D = R.getAcceptableDecl(*I)) { 1342d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor if (NameKind == LookupRedeclarationWithLinkage) { 1343d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // Determine whether this (or a previous) declaration is 1344d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // out-of-scope. 1345d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (!LeftStartingScope && !S->isDeclScope(*I)) 1346d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor LeftStartingScope = true; 1347d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor 1348d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // If we found something outside of our starting scope that 1349d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // does not have linkage, skip it. 1350dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith if (LeftStartingScope && !((*I)->hasLinkage())) { 1351dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith R.setShadowed(); 1352d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor continue; 1353dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith } 1354d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor } 135598a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian else if (NameKind == LookupObjCImplicitSelfParam && 135698a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian !isa<ImplicitParamDecl>(*I)) 135798a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian continue; 1358b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1359553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor R.addDecl(D); 1360f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 13617a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor // Check whether there are any other declarations with the same name 13627a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor // and in the same scope. 1363da795b45f47df61a1b5a491e8d4ea078cf2a217bDouglas Gregor if (I != IEnd) { 1364117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // Find the scope in which this declaration was declared (if it 1365117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // actually exists in a Scope). 1366117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor while (S && !S->isDeclScope(D)) 1367117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor S = S->getParent(); 1368117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor 1369117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // If the scope containing the declaration is the translation unit, 1370117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // then we'll need to perform our checks based on the matching 1371117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // DeclContexts rather than matching scopes. 1372117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor if (S && isNamespaceOrTranslationUnitScope(S)) 13736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines S = nullptr; 1374117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor 1375117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // Compute the DeclContext, if we need it. 13766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DeclContext *DC = nullptr; 1377117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor if (!S) 1378117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor DC = (*I)->getDeclContext()->getRedeclContext(); 1379117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor 1380da795b45f47df61a1b5a491e8d4ea078cf2a217bDouglas Gregor IdentifierResolver::iterator LastI = I; 1381da795b45f47df61a1b5a491e8d4ea078cf2a217bDouglas Gregor for (++LastI; LastI != IEnd; ++LastI) { 1382117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor if (S) { 1383117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // Match based on scope. 1384117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor if (!S->isDeclScope(*LastI)) 1385117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor break; 1386117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor } else { 1387117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor // Match based on DeclContext. 1388117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor DeclContext *LastDC 1389117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor = (*LastI)->getDeclContext()->getRedeclContext(); 1390117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor if (!LastDC->Equals(DC)) 1391117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor break; 1392117c45666c99f1797c708767d7c0ccaa7902ec42Douglas Gregor } 1393b775100fea6d8955149897dae1adca50ca471d17Richard Smith 1394b775100fea6d8955149897dae1adca50ca471d17Richard Smith // If the declaration is in the right namespace and visible, add it. 1395b775100fea6d8955149897dae1adca50ca471d17Richard Smith if (NamedDecl *LastD = R.getAcceptableDecl(*LastI)) 1396b775100fea6d8955149897dae1adca50ca471d17Richard Smith R.addDecl(LastD); 1397da795b45f47df61a1b5a491e8d4ea078cf2a217bDouglas Gregor } 13987a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor 1399da795b45f47df61a1b5a491e8d4ea078cf2a217bDouglas Gregor R.resolveKind(); 1400f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor } 1401a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 1402f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1403f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor } 1404eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor } else { 14052a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // Perform C++ unqualified name lookup. 1406a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall if (CppLookupName(R, S)) 1407f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1408eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor } 1409eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 1410eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // If we didn't find a use of this identifier, and if the identifier 1411eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // corresponds to a compiler builtin, create the decl object for the builtin 1412eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // now, injecting it into translation unit scope, and return it. 141342151d5ba957841a15ffda0fac71cd7cb653d45eAxel Naumann if (AllowBuiltinCreation && LookupBuiltin(*this, R)) 141442151d5ba957841a15ffda0fac71cd7cb653d45eAxel Naumann return true; 14153e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor 1416f8291a190cc6765630312911f441d9e23564eda2Axel Naumann // If we didn't find a use of this identifier, the ExternalSource 1417f8291a190cc6765630312911f441d9e23564eda2Axel Naumann // may be able to handle the situation. 1418f8291a190cc6765630312911f441d9e23564eda2Axel Naumann // Note: some lookup failures are expected! 1419f8291a190cc6765630312911f441d9e23564eda2Axel Naumann // See e.g. R.isForRedeclaration(). 1420f8291a190cc6765630312911f441d9e23564eda2Axel Naumann return (ExternalSource && ExternalSource->LookupUnqualified(R, S)); 1421eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor} 1422eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 14236e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// @brief Perform qualified name lookup in the namespaces nominated by 14246e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// using directives by the given context. 14256e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// 14266e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// C++98 [namespace.qual]p2: 14277ba759237afee52f4d53ed1fe07dbfdcfdbabdc6James Dennett/// Given X::m (where X is a user-declared namespace), or given \::m 14286e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// (where X is the global namespace), let S be the set of all 14296e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// declarations of m in X and in the transitive closure of all 14306e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// namespaces nominated by using-directives in X and its used 14316e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// namespaces, except that using-directives are ignored in any 14326e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// namespace, including X, directly containing one or more 14336e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// declarations of m. No namespace is searched more than once in 14346e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// the lookup of a name. If S is the empty set, the program is 14356e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// ill-formed. Otherwise, if S has exactly one member, or if the 14366e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// context of the reference is a using-declaration 14376e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// (namespace.udecl), S is the required set of declarations of 14386e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// m. Otherwise if the use of m is not one that allows a unique 14396e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// declaration to be chosen from S, the program is ill-formed. 14407ba759237afee52f4d53ed1fe07dbfdcfdbabdc6James Dennett/// 14416e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// C++98 [namespace.qual]p5: 14426e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// During the lookup of a qualified namespace member name, if the 14436e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// lookup finds more than one declaration of the member, and if one 14446e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// declaration introduces a class name or enumeration name and the 14456e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// other declarations either introduce the same object, the same 14466e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// enumerator or a set of functions, the non-type name hides the 14476e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// class or enumeration name if and only if the declarations are 14486e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// from the same namespace; otherwise (the declarations are from 14496e24726524c2b51b31bb4b622aa678a46b024f42John McCall/// different namespaces), the program is ill-formed. 145085910986c4ab01927c192aea86b2234fec568259Douglas Gregorstatic bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, 1451a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall DeclContext *StartDC) { 14526e24726524c2b51b31bb4b622aa678a46b024f42John McCall assert(StartDC->isFileContext() && "start context is not a file context"); 14536e24726524c2b51b31bb4b622aa678a46b024f42John McCall 1454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DeclContext::udir_range UsingDirectives = StartDC->using_directives(); 1455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (UsingDirectives.begin() == UsingDirectives.end()) return false; 14566e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14576e24726524c2b51b31bb4b622aa678a46b024f42John McCall // We have at least added all these contexts to the queue. 14588c43dccdae3083e73061cb1b2f517b77d35876a0Benjamin Kramer llvm::SmallPtrSet<DeclContext*, 8> Visited; 14596e24726524c2b51b31bb4b622aa678a46b024f42John McCall Visited.insert(StartDC); 14606e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14616e24726524c2b51b31bb4b622aa678a46b024f42John McCall // We have not yet looked into these namespaces, much less added 14626e24726524c2b51b31bb4b622aa678a46b024f42John McCall // their "using-children" to the queue. 14635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<NamespaceDecl*, 8> Queue; 14646e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14656e24726524c2b51b31bb4b622aa678a46b024f42John McCall // We have already looked into the initial namespace; seed the queue 14666e24726524c2b51b31bb4b622aa678a46b024f42John McCall // with its using-children. 1467651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : UsingDirectives) { 1468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace(); 14698c43dccdae3083e73061cb1b2f517b77d35876a0Benjamin Kramer if (Visited.insert(ND)) 14706e24726524c2b51b31bb4b622aa678a46b024f42John McCall Queue.push_back(ND); 14716e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 14726e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14736e24726524c2b51b31bb4b622aa678a46b024f42John McCall // The easiest way to implement the restriction in [namespace.qual]p5 14746e24726524c2b51b31bb4b622aa678a46b024f42John McCall // is to check whether any of the individual results found a tag 14756e24726524c2b51b31bb4b622aa678a46b024f42John McCall // and, if so, to declare an ambiguity if the final result is not 14766e24726524c2b51b31bb4b622aa678a46b024f42John McCall // a tag. 14776e24726524c2b51b31bb4b622aa678a46b024f42John McCall bool FoundTag = false; 14786e24726524c2b51b31bb4b622aa678a46b024f42John McCall bool FoundNonTag = false; 14796e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14807d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall LookupResult LocalR(LookupResult::Temporary, R); 14816e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14826e24726524c2b51b31bb4b622aa678a46b024f42John McCall bool Found = false; 14836e24726524c2b51b31bb4b622aa678a46b024f42John McCall while (!Queue.empty()) { 1484344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm NamespaceDecl *ND = Queue.pop_back_val(); 14856e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14866e24726524c2b51b31bb4b622aa678a46b024f42John McCall // We go through some convolutions here to avoid copying results 14876e24726524c2b51b31bb4b622aa678a46b024f42John McCall // between LookupResults. 14886e24726524c2b51b31bb4b622aa678a46b024f42John McCall bool UseLocal = !R.empty(); 14897d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall LookupResult &DirectR = UseLocal ? LocalR : R; 149085910986c4ab01927c192aea86b2234fec568259Douglas Gregor bool FoundDirect = LookupDirect(S, DirectR, ND); 14916e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14926e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (FoundDirect) { 14936e24726524c2b51b31bb4b622aa678a46b024f42John McCall // First do any local hiding. 14946e24726524c2b51b31bb4b622aa678a46b024f42John McCall DirectR.resolveKind(); 14956e24726524c2b51b31bb4b622aa678a46b024f42John McCall 14966e24726524c2b51b31bb4b622aa678a46b024f42John McCall // If the local result is a tag, remember that. 14976e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (DirectR.isSingleTagDecl()) 14986e24726524c2b51b31bb4b622aa678a46b024f42John McCall FoundTag = true; 14996e24726524c2b51b31bb4b622aa678a46b024f42John McCall else 15006e24726524c2b51b31bb4b622aa678a46b024f42John McCall FoundNonTag = true; 15016e24726524c2b51b31bb4b622aa678a46b024f42John McCall 15026e24726524c2b51b31bb4b622aa678a46b024f42John McCall // Append the local results to the total results if necessary. 15036e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (UseLocal) { 15046e24726524c2b51b31bb4b622aa678a46b024f42John McCall R.addAllDecls(LocalR); 15056e24726524c2b51b31bb4b622aa678a46b024f42John McCall LocalR.clear(); 15066e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15076e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15086e24726524c2b51b31bb4b622aa678a46b024f42John McCall 15096e24726524c2b51b31bb4b622aa678a46b024f42John McCall // If we find names in this namespace, ignore its using directives. 15106e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (FoundDirect) { 15116e24726524c2b51b31bb4b622aa678a46b024f42John McCall Found = true; 15126e24726524c2b51b31bb4b622aa678a46b024f42John McCall continue; 15136e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15146e24726524c2b51b31bb4b622aa678a46b024f42John McCall 1515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto I : ND->using_directives()) { 1516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines NamespaceDecl *Nom = I->getNominatedNamespace(); 15178c43dccdae3083e73061cb1b2f517b77d35876a0Benjamin Kramer if (Visited.insert(Nom)) 15186e24726524c2b51b31bb4b622aa678a46b024f42John McCall Queue.push_back(Nom); 15196e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15206e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15216e24726524c2b51b31bb4b622aa678a46b024f42John McCall 15226e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (Found) { 15236e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (FoundTag && FoundNonTag) 15246e24726524c2b51b31bb4b622aa678a46b024f42John McCall R.setAmbiguousQualifiedTagHiding(); 15256e24726524c2b51b31bb4b622aa678a46b024f42John McCall else 15266e24726524c2b51b31bb4b622aa678a46b024f42John McCall R.resolveKind(); 15276e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 15286e24726524c2b51b31bb4b622aa678a46b024f42John McCall 15296e24726524c2b51b31bb4b622aa678a46b024f42John McCall return Found; 15306e24726524c2b51b31bb4b622aa678a46b024f42John McCall} 15316e24726524c2b51b31bb4b622aa678a46b024f42John McCall 15328071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor/// \brief Callback that looks for any member of a class with the given name. 1533dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool LookupAnyMember(const CXXBaseSpecifier *Specifier, 15348071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor CXXBasePath &Path, 15358071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor void *Name) { 15368071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); 1537dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 15388071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor DeclarationName N = DeclarationName::getFromOpaquePtr(Name); 15398071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor Path.Decls = BaseRecord->lookup(N); 15403bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie return !Path.Decls.empty(); 15418071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor} 15428071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor 1543dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given set of member declarations contains only 1544f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor/// static members, nested types, and enumerators. 1545f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregortemplate<typename InputIterator> 1546f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregorstatic bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) { 1547f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor Decl *D = (*First)->getUnderlyingDecl(); 1548f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D)) 1549f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor return true; 1550dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1551f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (isa<CXXMethodDecl>(D)) { 1552f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor // Determine whether all of the methods are static. 1553f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor bool AllMethodsAreStatic = true; 1554f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor for(; First != Last; ++First) { 1555f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor D = (*First)->getUnderlyingDecl(); 1556dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1557f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (!isa<CXXMethodDecl>(D)) { 1558f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor assert(isa<TagDecl>(D) && "Non-function must be a tag decl"); 1559f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor break; 1560f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1561dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1562f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (!cast<CXXMethodDecl>(D)->isStatic()) { 1563f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor AllMethodsAreStatic = false; 1564f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor break; 1565f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1566f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1568f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (AllMethodsAreStatic) 1569f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor return true; 1570f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1571f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor 1572f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor return false; 1573f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor} 1574f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor 15757d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// \brief Perform qualified name lookup into a given context. 1576eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1577eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find 1578eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// names when the context of those names is explicit specified, e.g., 15797d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// "std::vector" or "x->member", or as part of unqualified name lookup. 1580eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1581eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// Different lookup criteria can find different names. For example, a 1582eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// particular scope can have both a struct and a function of the same 1583eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// name, and each can be found by certain lookup criteria. For more 1584eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// information about lookup criteria, see the documentation for the 1585eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// class LookupCriteria. 1586eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 15877d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// \param R captures both the lookup criteria and any lookup results found. 15887d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// 15897d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// \param LookupCtx The context in which qualified name lookup will 1590eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// search. If the lookup criteria permits, name lookup may also search 1591eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// in the parent contexts or (for C++ classes) base classes. 1592eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1593dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param InUnqualifiedLookup true if this is qualified name lookup that 15947d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// occurs as part of unqualified name lookup. 1595eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 15967d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor/// \returns true if lookup succeeded, false if it failed. 15977d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregorbool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, 15987d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor bool InUnqualifiedLookup) { 1599eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); 16001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1601a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall if (!R.getLookupName()) 1602f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16042dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // Make sure that the declaration context is complete. 16052dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor assert((!isa<TagDecl>(LookupCtx) || 16062dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor LookupCtx->isDependentContext() || 16075e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall cast<TagDecl>(LookupCtx)->isCompleteDefinition() || 16081af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith cast<TagDecl>(LookupCtx)->isBeingDefined()) && 16092dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor "Declaration context must already be complete!"); 16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1611eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor // Perform qualified name lookup into the LookupCtx. 161285910986c4ab01927c192aea86b2234fec568259Douglas Gregor if (LookupDirect(*this, R, LookupCtx)) { 1613f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.resolveKind(); 161492f883177b162928a8e632e4e3b93fafd2b26072John McCall if (isa<CXXRecordDecl>(LookupCtx)) 161592f883177b162928a8e632e4e3b93fafd2b26072John McCall R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); 1616f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1617f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall } 1618eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 16196e24726524c2b51b31bb4b622aa678a46b024f42John McCall // Don't descend into implied contexts for redeclarations. 16206e24726524c2b51b31bb4b622aa678a46b024f42John McCall // C++98 [namespace.qual]p6: 16216e24726524c2b51b31bb4b622aa678a46b024f42John McCall // In a declaration for a namespace member in which the 16226e24726524c2b51b31bb4b622aa678a46b024f42John McCall // declarator-id is a qualified-id, given that the qualified-id 16236e24726524c2b51b31bb4b622aa678a46b024f42John McCall // for the namespace member has the form 16246e24726524c2b51b31bb4b622aa678a46b024f42John McCall // nested-name-specifier unqualified-id 16256e24726524c2b51b31bb4b622aa678a46b024f42John McCall // the unqualified-id shall name a member of the namespace 16266e24726524c2b51b31bb4b622aa678a46b024f42John McCall // designated by the nested-name-specifier. 16276e24726524c2b51b31bb4b622aa678a46b024f42John McCall // See also [class.mfct]p5 and [class.static.data]p2. 1628a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall if (R.isForRedeclaration()) 16296e24726524c2b51b31bb4b622aa678a46b024f42John McCall return false; 16306e24726524c2b51b31bb4b622aa678a46b024f42John McCall 1631a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall // If this is a namespace, look it up in the implied namespaces. 16326e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (LookupCtx->isFileContext()) 163385910986c4ab01927c192aea86b2234fec568259Douglas Gregor return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); 16346e24726524c2b51b31bb4b622aa678a46b024f42John McCall 16352dd078ae50ff7be1fb25ebeedde45e9ab691a4f0Douglas Gregor // If this isn't a C++ class, we aren't allowed to look into base 16364719f4e86a84dec6f5a45771ae51d4ec72e4617aDouglas Gregor // classes, we're done. 16377d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); 1638025291b591a528d8a3f303991f65e19fa1e90a9dDouglas Gregor if (!LookupRec || !LookupRec->getDefinition()) 1639f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 16407176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 16417d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor // If we're performing qualified name lookup into a dependent class, 16427d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor // then we are actually looking into a current instantiation. If we have any 1643dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // dependent base classes, then we either have to delay lookup until 16447d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor // template instantiation time (at which point all bases will be available) 16457d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor // or we have to fail. 16467d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor if (!InUnqualifiedLookup && LookupRec->isDependentContext() && 16477d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor LookupRec->hasAnyDependentBases()) { 16487d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor R.setNotFoundInCurrentInstantiation(); 16497d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor return false; 16507d3f576dc9ea6e866757abcd1736eb7e7433c325Douglas Gregor } 1651dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16527176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // Perform lookup into our base classes. 1653a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor CXXBasePaths Paths; 1654a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor Paths.setOrigin(LookupRec); 16557176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 16567176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // Look for this member in our base classes 16576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CXXRecordDecl::BaseMatchesCallback *BaseCallback = nullptr; 1658a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall switch (R.getLookupKind()) { 165998a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian case LookupObjCImplicitSelfParam: 1660a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupOrdinaryName: 1661a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupMemberName: 1662a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupRedeclarationWithLinkage: 16634e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith case LookupLocalFriendName: 1664a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor BaseCallback = &CXXRecordDecl::FindOrdinaryMember; 1665a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor break; 1666dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1667a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupTagName: 1668a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor BaseCallback = &CXXRecordDecl::FindTagMember; 1669a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor break; 16709f54ad4381370c6b771424b53d219e661d6d6706John McCall 16718071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor case LookupAnyName: 16728071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor BaseCallback = &LookupAnyMember; 16738071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor break; 1674dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16759f54ad4381370c6b771424b53d219e661d6d6706John McCall case LookupUsingDeclName: 16769f54ad4381370c6b771424b53d219e661d6d6706John McCall // This lookup is for redeclarations only. 1677dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1678a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupOperatorName: 1679a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupNamespaceName: 1680a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupObjCProtocolName: 1681337e550218128e7d922c09bb354fbc71de90c568Chris Lattner case LookupLabel: 1682a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor // These lookups will never find a member in a C++ class (or base class). 1683f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 1684dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1685a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor case LookupNestedNameSpecifierName: 1686a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember; 1687a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor break; 1688a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor } 1689dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1690a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall if (!LookupRec->lookupInBases(BaseCallback, 1691a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall R.getLookupName().getAsOpaquePtr(), Paths)) 1692f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 16937176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 169492f883177b162928a8e632e4e3b93fafd2b26072John McCall R.setNamingClass(LookupRec); 169592f883177b162928a8e632e4e3b93fafd2b26072John McCall 16967176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // C++ [class.member.lookup]p2: 16977176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // [...] If the resulting set of declarations are not all from 16987176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // sub-objects of the same type, or the set has a nonstatic member 16997176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // and includes members from distinct sub-objects, there is an 17007176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // ambiguity and the program is ill-formed. Otherwise that set is 17017176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // the result of the lookup. 17027176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor QualType SubobjectType; 1703f185319f25efd6094870f287030270fad26085baDaniel Dunbar int SubobjectNumber = 0; 17047aceaf8cee77c98478e8934dc283910292711a7eJohn McCall AccessSpecifier SubobjectAccess = AS_none; 1705dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1706a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); 17077176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor Path != PathEnd; ++Path) { 1708a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor const CXXBasePathElement &PathElement = Path->back(); 17097176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 171046460a68f6508775e98c19b4bb8454bb471aac24John McCall // Pick the best (i.e. most permissive i.e. numerically lowest) access 171146460a68f6508775e98c19b4bb8454bb471aac24John McCall // across all paths. 171246460a68f6508775e98c19b4bb8454bb471aac24John McCall SubobjectAccess = std::min(SubobjectAccess, Path->Access); 1713dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17147176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // Determine whether we're looking at a distinct sub-object or not. 17157176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor if (SubobjectType.isNull()) { 1716f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall // This is the first subobject we've looked at. Record its type. 17177176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); 17187176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor SubobjectNumber = PathElement.SubobjectNumber; 1719f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor continue; 1720dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 1721dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1722f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (SubobjectType 17237176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor != Context.getCanonicalType(PathElement.Base->getType())) { 17247176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // We found members of the given name in two subobjects of 1725f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor // different types. If the declaration sets aren't the same, this 1726ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // lookup is ambiguous. 17273bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) { 1728f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor CXXBasePaths::paths_iterator FirstPath = Paths.begin(); 17293bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_iterator FirstD = FirstPath->Decls.begin(); 17303bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_iterator CurrentD = Path->Decls.begin(); 1731dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17323bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie while (FirstD != FirstPath->Decls.end() && 17333bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie CurrentD != Path->Decls.end()) { 1734f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() != 1735f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor (*CurrentD)->getUnderlyingDecl()->getCanonicalDecl()) 1736f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor break; 1737dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1738f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor ++FirstD; 1739f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor ++CurrentD; 1740f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1741dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17423bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie if (FirstD == FirstPath->Decls.end() && 17433bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie CurrentD == Path->Decls.end()) 1744f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor continue; 1745f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor } 1746dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1747f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.setAmbiguousBaseSubobjectTypes(Paths); 1748f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1749dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 1750dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1751f17b58c98b57537e9abfaaa8b5f19ea7e6de01eeDouglas Gregor if (SubobjectNumber != PathElement.SubobjectNumber) { 17527176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // We have a different subobject of the same type. 17537176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 17547176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // C++ [class.member.lookup]p5: 17557176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // A static member, a nested type or an enumerator defined in 17567176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // a base class T can unambiguously be found even if an object 17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // has more than one base class subobject of type T. 17583bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) 17597176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor continue; 1760dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17617176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // We have found a nonstatic member name in multiple, distinct 17627176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // subobjects. Name lookup is ambiguous. 1763f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.setAmbiguousBaseSubobjects(Paths); 1764f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 17657176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor } 17667176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor } 17677176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 17687176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor // Lookup in a base class succeeded; return these results. 17697176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 17703bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_result DR = Paths.front().Decls; 17713bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie for (DeclContext::lookup_iterator I = DR.begin(), E = DR.end(); I != E; ++I) { 177292f883177b162928a8e632e4e3b93fafd2b26072John McCall NamedDecl *D = *I; 177392f883177b162928a8e632e4e3b93fafd2b26072John McCall AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, 177492f883177b162928a8e632e4e3b93fafd2b26072John McCall D->getAccess()); 177592f883177b162928a8e632e4e3b93fafd2b26072John McCall R.addDecl(D, AS); 177692f883177b162928a8e632e4e3b93fafd2b26072John McCall } 1777f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall R.resolveKind(); 1778f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return true; 1779eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor} 1780eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 1781eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @brief Performs name lookup for a name that was parsed in the 1782eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// source code, and may contain a C++ scope specifier. 1783eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1784eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// This routine is a convenience routine meant to be called from 1785eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// contexts that receive a name and an optional C++ scope specifier 1786eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// (e.g., "N::M::x"). It will then perform either qualified or 1787eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// unqualified name lookup (with LookupQualifiedName or LookupName, 1788eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// respectively) on the given name and return those results. 1789eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1790eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// @param S The scope from which unqualified name lookup will 1791eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// begin. 17921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 1793495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor/// @param SS An optional C++ scope-specifier, e.g., "::N::M". 1794eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor/// 1795495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor/// @param EnteringContext Indicates whether we are going to enter the 1796495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor/// context of the scope-specifier SS (if present). 1797495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor/// 1798f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall/// @returns True if any decls were found (but possibly ambiguous) 17999ab14541716928894821cf5d53d6b4c95ffdf3a3Jeffrey Yasskinbool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, 1800a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall bool AllowBuiltinCreation, bool EnteringContext) { 1801495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor if (SS && SS->isInvalid()) { 1802495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor // When the scope specifier is invalid, don't even look for 180342af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor // anything. 1804f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 1805495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor } 18061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1807495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor if (SS && SS->isSet()) { 1808495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { 18091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We have resolved the scope specifier to a particular declaration 1810495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor // contex, and will perform name lookup in that context. 181177bb1aa78bcd26e42c0382043e65a2b03242be4dJohn McCall if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) 1812f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1814a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall R.setContextRange(SS->getRange()); 1815a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall return LookupQualifiedName(R, DC); 1816e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor } 181742af25f865a82022a04bedeb483ac251c4412e29Douglas Gregor 1818495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor // We could not resolve the scope specified to a specific declaration 18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // context, which means that SS refers to an unknown specialization. 1820495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor // Name lookup can't find anything in this case. 18213eafbb85d1d9d108f089fd2451ddb6932eaafef2Douglas Gregor R.setNotFoundInCurrentInstantiation(); 18223eafbb85d1d9d108f089fd2451ddb6932eaafef2Douglas Gregor R.setContextRange(SS->getRange()); 1823f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall return false; 18244c921ae760cbdd9270c16d48417d7d527eb0ceb8Douglas Gregor } 1825eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 18261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Perform unqualified name lookup starting in the given scope. 1827a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall return LookupName(R, S, AllowBuiltinCreation); 1828eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor} 1829eb11cd078ba8682bbb9b082f8f6ead8be5c98581Douglas Gregor 18302a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 183116ae9de07730832945204877d752db7f1c070962James Dennett/// \brief Produce a diagnostic describing the ambiguity that resulted 18327176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor/// from name lookup. 18337176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor/// 183416ae9de07730832945204877d752db7f1c070962James Dennett/// \param Result The result of the ambiguous lookup to be diagnosed. 18358ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlovvoid Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { 18367176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); 18377176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor 1838a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall DeclarationName Name = Result.getLookupName(); 1839a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall SourceLocation NameLoc = Result.getNameLoc(); 1840a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall SourceRange LookupRange = Result.getContextRange(); 1841a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall 18426e24726524c2b51b31bb4b622aa678a46b024f42John McCall switch (Result.getAmbiguityKind()) { 18436e24726524c2b51b31bb4b622aa678a46b024f42John McCall case LookupResult::AmbiguousBaseSubobjects: { 18446e24726524c2b51b31bb4b622aa678a46b024f42John McCall CXXBasePaths *Paths = Result.getBasePaths(); 18456e24726524c2b51b31bb4b622aa678a46b024f42John McCall QualType SubobjectType = Paths->front().back().Base->getType(); 18466e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) 18476e24726524c2b51b31bb4b622aa678a46b024f42John McCall << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) 18486e24726524c2b51b31bb4b622aa678a46b024f42John McCall << LookupRange; 1849dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 18503bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_iterator Found = Paths->front().Decls.begin(); 18516e24726524c2b51b31bb4b622aa678a46b024f42John McCall while (isa<CXXMethodDecl>(*Found) && 18526e24726524c2b51b31bb4b622aa678a46b024f42John McCall cast<CXXMethodDecl>(*Found)->isStatic()) 18536e24726524c2b51b31bb4b622aa678a46b024f42John McCall ++Found; 1854dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 18556e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); 18568ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlov break; 18576e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 18582a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor 18596e24726524c2b51b31bb4b622aa678a46b024f42John McCall case LookupResult::AmbiguousBaseSubobjectTypes: { 18602a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) 18612a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor << Name << LookupRange; 1862dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 18636e24726524c2b51b31bb4b622aa678a46b024f42John McCall CXXBasePaths *Paths = Result.getBasePaths(); 18642a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor std::set<Decl *> DeclsPrinted; 18656e24726524c2b51b31bb4b622aa678a46b024f42John McCall for (CXXBasePaths::paths_iterator Path = Paths->begin(), 18666e24726524c2b51b31bb4b622aa678a46b024f42John McCall PathEnd = Paths->end(); 18672a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor Path != PathEnd; ++Path) { 18683bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie Decl *D = Path->Decls.front(); 18692a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor if (DeclsPrinted.insert(D).second) 18702a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor Diag(D->getLocation(), diag::note_ambiguous_member_found); 18712a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor } 18728ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlov break; 18734dc6b1c81c5efbaf68b868df10b18466b5e14b34Douglas Gregor } 18744dc6b1c81c5efbaf68b868df10b18466b5e14b34Douglas Gregor 18756e24726524c2b51b31bb4b622aa678a46b024f42John McCall case LookupResult::AmbiguousTagHiding: { 18766e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; 1877f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall 18786e24726524c2b51b31bb4b622aa678a46b024f42John McCall llvm::SmallPtrSet<NamedDecl*,8> TagDecls; 18796e24726524c2b51b31bb4b622aa678a46b024f42John McCall 18806e24726524c2b51b31bb4b622aa678a46b024f42John McCall LookupResult::iterator DI, DE = Result.end(); 18816e24726524c2b51b31bb4b622aa678a46b024f42John McCall for (DI = Result.begin(); DI != DE; ++DI) 18826e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) { 18836e24726524c2b51b31bb4b622aa678a46b024f42John McCall TagDecls.insert(TD); 18846e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag(TD->getLocation(), diag::note_hidden_tag); 18856e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 18866e24726524c2b51b31bb4b622aa678a46b024f42John McCall 18876e24726524c2b51b31bb4b622aa678a46b024f42John McCall for (DI = Result.begin(); DI != DE; ++DI) 18886e24726524c2b51b31bb4b622aa678a46b024f42John McCall if (!isa<TagDecl>(*DI)) 18896e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag((*DI)->getLocation(), diag::note_hiding_object); 18906e24726524c2b51b31bb4b622aa678a46b024f42John McCall 18916e24726524c2b51b31bb4b622aa678a46b024f42John McCall // For recovery purposes, go ahead and implement the hiding. 1892eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall LookupResult::Filter F = Result.makeFilter(); 1893eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall while (F.hasNext()) { 1894eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall if (TagDecls.count(F.next())) 1895eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall F.erase(); 1896eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall } 1897eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall F.done(); 18988ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlov break; 18996e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 19006e24726524c2b51b31bb4b622aa678a46b024f42John McCall 19016e24726524c2b51b31bb4b622aa678a46b024f42John McCall case LookupResult::AmbiguousReference: { 19026e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; 1903dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 19046e24726524c2b51b31bb4b622aa678a46b024f42John McCall LookupResult::iterator DI = Result.begin(), DE = Result.end(); 19056e24726524c2b51b31bb4b622aa678a46b024f42John McCall for (; DI != DE; ++DI) 19066e24726524c2b51b31bb4b622aa678a46b024f42John McCall Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; 19078ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlov break; 19086e24726524c2b51b31bb4b622aa678a46b024f42John McCall } 19098ed2f3ad9505962d8dec4630caeaad607edbbb7dSerge Pavlov } 19107176fff961e04c4dff61efb967b1d344d41335a7Douglas Gregor} 1911fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 1912c7e04dad588a30c94648b9bd70cdbe25688d7629John McCallnamespace { 1913c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall struct AssociatedLookup { 191442f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall AssociatedLookup(Sema &S, SourceLocation InstantiationLoc, 1915c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Sema::AssociatedNamespaceSet &Namespaces, 1916c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Sema::AssociatedClassSet &Classes) 191742f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall : S(S), Namespaces(Namespaces), Classes(Classes), 191842f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall InstantiationLoc(InstantiationLoc) { 1919c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall } 1920c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall 1921c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Sema &S; 1922c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Sema::AssociatedNamespaceSet &Namespaces; 1923c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Sema::AssociatedClassSet &Classes; 192442f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall SourceLocation InstantiationLoc; 1925c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall }; 1926c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall} 1927c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall 19281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 1929c7e04dad588a30c94648b9bd70cdbe25688d7629John McCalladdAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); 19306ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall 193154022952450beff428a30ef5adfb82874063603dDouglas Gregorstatic void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, 193254022952450beff428a30ef5adfb82874063603dDouglas Gregor DeclContext *Ctx) { 193354022952450beff428a30ef5adfb82874063603dDouglas Gregor // Add the associated namespace for this class. 193454022952450beff428a30ef5adfb82874063603dDouglas Gregor 193554022952450beff428a30ef5adfb82874063603dDouglas Gregor // We don't use DeclContext::getEnclosingNamespaceContext() as this may 193654022952450beff428a30ef5adfb82874063603dDouglas Gregor // be a locally scoped record. 193754022952450beff428a30ef5adfb82874063603dDouglas Gregor 1938410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl // We skip out of inline namespaces. The innermost non-inline namespace 1939410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl // contains all names of all its nested inline namespaces anyway, so we can 1940410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl // replace the entire inline namespace tree with its root. 1941410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl while (Ctx->isRecord() || Ctx->isTransparentContext() || 1942410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl Ctx->isInlineNamespace()) 194354022952450beff428a30ef5adfb82874063603dDouglas Gregor Ctx = Ctx->getParent(); 194454022952450beff428a30ef5adfb82874063603dDouglas Gregor 19456ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall if (Ctx->isFileContext()) 194654022952450beff428a30ef5adfb82874063603dDouglas Gregor Namespaces.insert(Ctx->getPrimaryContext()); 19476ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall} 194869be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor 19491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// \brief Add the associated classes and namespaces for argument-dependent 195069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor// lookup that involves a template argument (C++ [basic.lookup.koenig]p2). 19511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 1952c7e04dad588a30c94648b9bd70cdbe25688d7629John McCalladdAssociatedClassesAndNamespaces(AssociatedLookup &Result, 1953c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall const TemplateArgument &Arg) { 195469be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // C++ [basic.lookup.koenig]p2, last bullet: 19551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // -- [...] ; 195669be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor switch (Arg.getKind()) { 195769be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor case TemplateArgument::Null: 195869be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor break; 19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 196069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor case TemplateArgument::Type: 196169be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // [...] the namespaces and classes associated with the types of the 196269be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // template arguments provided for template type parameters (excluding 196369be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // template template parameters) 1964c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); 196569be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor break; 19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1967dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi case TemplateArgument::Template: 1968a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: { 19691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] the namespaces in which any template template arguments are 19701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // defined; and the classes in which any member templates used as 197169be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // template template arguments are defined. 1972a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); 19731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (ClassTemplateDecl *ClassTemplate 1974788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { 197569be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor DeclContext *Ctx = ClassTemplate->getDeclContext(); 197669be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 1977c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Result.Classes.insert(EnclosingClass); 197869be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // Add the associated namespace for this class. 1979c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CollectEnclosingNamespace(Result.Namespaces, Ctx); 198069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor } 198169be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor break; 1982788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor } 1983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1984788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Declaration: 198569be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor case TemplateArgument::Integral: 198669be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor case TemplateArgument::Expression: 1987d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman case TemplateArgument::NullPtr: 19881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [Note: non-type template arguments do not contribute to the set of 198969be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // associated namespaces. ] 199069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor break; 19911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 199269be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor case TemplateArgument::Pack: 199369be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor for (TemplateArgument::pack_iterator P = Arg.pack_begin(), 199469be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor PEnd = Arg.pack_end(); 199569be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor P != PEnd; ++P) 1996c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, *P); 199769be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor break; 199869be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor } 199969be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor} 200069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor 2001fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor// \brief Add the associated classes and namespaces for 20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// argument-dependent lookup with an argument of class type 20031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// (C++ [basic.lookup.koenig]p2). 20041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2005c7e04dad588a30c94648b9bd70cdbe25688d7629John McCalladdAssociatedClassesAndNamespaces(AssociatedLookup &Result, 2006c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CXXRecordDecl *Class) { 2007c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall 2008c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall // Just silently ignore anything whose name is __va_list_tag. 2009c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall if (Class->getDeclName() == Result.S.VAListTagName) 2010c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall return; 2011c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall 2012fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // C++ [basic.lookup.koenig]p2: 2013fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // [...] 2014fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // -- If T is a class type (including unions), its associated 2015fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // classes are: the class itself; the class of which it is a 2016fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // member, if any; and its direct and indirect base 2017fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // classes. Its associated namespaces are the namespaces in 20181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // which its associated classes are defined. 2019fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2020fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Add the class of which it is a member, if any. 2021fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor DeclContext *Ctx = Class->getDeclContext(); 2022fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2023c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Result.Classes.insert(EnclosingClass); 2024fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Add the associated namespace for this class. 2025c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CollectEnclosingNamespace(Result.Namespaces, Ctx); 20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2027fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Add the class itself. If we've already seen this class, we don't 2028fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // need to visit base classes. 2029651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // 2030651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // FIXME: That's not correct, we may have added this class only because it 2031651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // was the enclosing class of another class, and in that case we won't have 2032651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // added its base classes yet. 2033c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall if (!Result.Classes.insert(Class)) 2034fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor return; 2035fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // -- If T is a template-id, its associated namespaces and classes are 20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // the namespace in which the template is defined; for member 20380099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // templates, the member template's class; the namespaces and classes 20391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // associated with the types of the template arguments provided for 204069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // template type parameters (excluding template template parameters); the 20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // namespaces in which any template template arguments are defined; and 20421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // the classes in which any member templates used as template template 20431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // arguments are defined. [Note: non-type template arguments do not 204469be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // contribute to the set of associated namespaces. ] 20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (ClassTemplateSpecializationDecl *Spec 204669be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { 204769be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); 204869be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2049c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Result.Classes.insert(EnclosingClass); 205069be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor // Add the associated namespace for this class. 2051c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CollectEnclosingNamespace(Result.Namespaces, Ctx); 20521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 205369be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 205469be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 2055c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); 205669be8d60ded8bbfbd30facf0590c97920a074f46Douglas Gregor } 20571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 205886ff308724171494395a840fd2efbe25e62f352eJohn McCall // Only recurse into base classes for complete types. 2059651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!Class->hasDefinition()) 2060651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return; 206186ff308724171494395a840fd2efbe25e62f352eJohn McCall 2062fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Add direct and indirect base classes along with their associated 2063fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // namespaces. 20645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<CXXRecordDecl *, 32> Bases; 2065fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor Bases.push_back(Class); 2066fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor while (!Bases.empty()) { 2067fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Pop this class off the stack. 2068344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm Class = Bases.pop_back_val(); 2069fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2070fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Visit the base classes. 2071651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &Base : Class->bases()) { 2072651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const RecordType *BaseType = Base.getType()->getAs<RecordType>(); 2073bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // In dependent contexts, we do ADL twice, and the first time around, 2074bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // the base type might be a dependent TemplateSpecializationType, or a 2075bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // TemplateTypeParmType. If that happens, simply ignore it. 2076bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // FIXME: If we want to support export, we probably need to add the 2077bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // namespace of the template in a TemplateSpecializationType, or even 2078bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl // the classes and namespaces of known non-dependent arguments. 2079bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl if (!BaseType) 2080bbc1cc5757d0ef068bef583ee4eda108dece806cSebastian Redl continue; 2081fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); 2082c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall if (Result.Classes.insert(BaseDecl)) { 2083fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Find the associated namespace for this base class. 2084fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor DeclContext *BaseCtx = BaseDecl->getDeclContext(); 2085c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CollectEnclosingNamespace(Result.Namespaces, BaseCtx); 2086fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2087fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Make sure we visit the bases of this base class. 2088fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor if (BaseDecl->bases_begin() != BaseDecl->bases_end()) 2089fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor Bases.push_back(BaseDecl); 2090fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2091fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2092fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2093fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 2094fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2095fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor// \brief Add the associated classes and namespaces for 2096fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor// argument-dependent lookup with an argument of type T 20971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// (C++ [basic.lookup.koenig]p2). 20981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2099c7e04dad588a30c94648b9bd70cdbe25688d7629John McCalladdAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { 2100fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // C++ [basic.lookup.koenig]p2: 2101fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // 2102fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // For each argument type T in the function call, there is a set 2103fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // of zero or more associated namespaces and a set of zero or more 2104fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // associated classes to be considered. The sets of namespaces and 2105fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // classes is determined entirely by the types of the function 2106fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // arguments (and the namespace of any template template 2107fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // argument). Typedef names and using-declarations used to specify 2108fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // the types do not contribute to this set. The sets of namespaces 2109fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // and classes are determined in the following way: 2110fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 21115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<const Type *, 16> Queue; 2112fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); 2113fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2114fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor while (true) { 2115fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall switch (T->getTypeClass()) { 2116fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2117fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#define TYPE(Class, Base) 2118fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2119fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2120fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2121fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#define ABSTRACT_TYPE(Class, Base) 2122fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall#include "clang/AST/TypeNodes.def" 2123fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // T is canonical. We can also ignore dependent types because 2124fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // we don't need to do ADL at the definition point, but if we 2125fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // wanted to implement template export (or if we find some other 2126fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // use for associated classes and namespaces...) this would be 2127fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // wrong. 2128fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor break; 2129fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2130fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a pointer to U or an array of U, its associated 2131fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // namespaces and classes are those associated with U. 2132fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Pointer: 2133fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall T = cast<PointerType>(T)->getPointeeType().getTypePtr(); 2134fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2135fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::ConstantArray: 2136fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::IncompleteArray: 2137fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::VariableArray: 2138fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall T = cast<ArrayType>(T)->getElementType().getTypePtr(); 2139fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2140fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2141fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a fundamental type, its associated sets of 2142fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // namespaces and classes are both empty. 2143fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Builtin: 2144fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall break; 2145fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2146fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a class type (including unions), its associated 2147fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // classes are: the class itself; the class of which it is a 2148fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // member, if any; and its direct and indirect base 2149fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // classes. Its associated namespaces are the namespaces in 2150fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // which its associated classes are defined. 2151fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Record: { 2152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Result.S.RequireCompleteType(Result.InstantiationLoc, QualType(T, 0), 2153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /*no diagnostic*/ 0); 2154fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall CXXRecordDecl *Class 2155fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); 2156c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, Class); 2157fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall break; 2158c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor } 21594e58c25aa579d30cd1e4b0bfb36321c14652be12Douglas Gregor 2160fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is an enumeration type, its associated namespace is 2161fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // the namespace in which it is defined. If it is class 21620099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // member, its associated class is the member's class; else 2163fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // it has no associated class. 2164fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Enum: { 2165fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall EnumDecl *Enum = cast<EnumType>(T)->getDecl(); 2166fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2167fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall DeclContext *Ctx = Enum->getDeclContext(); 2168fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) 2169c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall Result.Classes.insert(EnclosingClass); 2170fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2171fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // Add the associated namespace for this class. 2172c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall CollectEnclosingNamespace(Result.Namespaces, Ctx); 2173fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2174fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall break; 2175fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 2176fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2177fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a function type, its associated namespaces and 2178fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // classes are those associated with the function parameter 2179fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // types and those associated with the return type. 2180fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::FunctionProto: { 2181fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 2182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &Arg : Proto->param_types()) 2183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Queue.push_back(Arg.getTypePtr()); 2184fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // fallthrough 2185fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 2186fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::FunctionNoProto: { 2187fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall const FunctionType *FnType = cast<FunctionType>(T); 2188651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines T = FnType->getReturnType().getTypePtr(); 2189fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2190fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 21911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2192fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a pointer to a member function of a class X, its 2193fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // associated namespaces and classes are those associated 2194fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // with the function parameter types and return type, 2195fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // together with those associated with X. 2196fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // 2197fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // -- If T is a pointer to a data member of class X, its 2198fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // associated namespaces and classes are those associated 2199fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // with the member type together with those associated with 2200fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // X. 2201fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::MemberPointer: { 2202fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); 2203fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2204fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // Queue up the class type into which this points. 2205fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall Queue.push_back(MemberPtr->getClass()); 2206fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2207fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // And directly continue with the pointee type. 2208fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall T = MemberPtr->getPointeeType().getTypePtr(); 2209fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2210fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 2211fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2212fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // As an extension, treat this like a normal pointer. 2213fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::BlockPointer: 2214fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); 2215fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2216fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2217fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // References aren't covered by the standard, but that's such an 2218fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // obvious defect that we cover them anyway. 2219fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::LValueReference: 2220fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::RValueReference: 2221fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); 2222fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall continue; 2223fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2224fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall // These are fundamental types. 2225fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Vector: 2226fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::ExtVector: 2227fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::Complex: 2228fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall break; 2229fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2230dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith // Non-deduced auto types only get here for error cases. 2231dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith case Type::Auto: 2232dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith break; 2233dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith 2234f25760e17af1167c6fcf24e1ea8e3fcbd0386702Douglas Gregor // If T is an Objective-C object or interface type, or a pointer to an 2235f25760e17af1167c6fcf24e1ea8e3fcbd0386702Douglas Gregor // object or interface type, the associated namespace is the global 2236f25760e17af1167c6fcf24e1ea8e3fcbd0386702Douglas Gregor // namespace. 2237fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::ObjCObject: 2238fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::ObjCInterface: 2239fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall case Type::ObjCObjectPointer: 2240f25760e17af1167c6fcf24e1ea8e3fcbd0386702Douglas Gregor Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl()); 2241fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall break; 2242b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman 2243b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman // Atomic types are just wrappers; use the associations of the 2244b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman // contained type. 2245b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman case Type::Atomic: 2246b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman T = cast<AtomicType>(T)->getValueType().getTypePtr(); 2247b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman continue; 2248fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 2249fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall 2250344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm if (Queue.empty()) 2251344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm break; 2252344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm T = Queue.pop_back_val(); 2253fa4edcf2639415bd3430a61e6d5bfc71d5b8981eJohn McCall } 2254fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 2255fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2256fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// \brief Find the associated classes and namespaces for 2257fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// argument-dependent lookup for a call with the given set of 2258fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// arguments. 2259fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// 2260fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// This routine computes the sets of associated classes and associated 22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// namespaces searched by argument-dependent lookup 2262fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor/// (C++ [basic.lookup.argdep]) for a given set of arguments. 2263834c058cb6b57465e60a4590afdab86c4ea6921dRobert Wilhelmvoid Sema::FindAssociatedClassesAndNamespaces( 2264834c058cb6b57465e60a4590afdab86c4ea6921dRobert Wilhelm SourceLocation InstantiationLoc, ArrayRef<Expr *> Args, 2265834c058cb6b57465e60a4590afdab86c4ea6921dRobert Wilhelm AssociatedNamespaceSet &AssociatedNamespaces, 2266834c058cb6b57465e60a4590afdab86c4ea6921dRobert Wilhelm AssociatedClassSet &AssociatedClasses) { 2267fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor AssociatedNamespaces.clear(); 2268fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor AssociatedClasses.clear(); 2269fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 227042f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall AssociatedLookup Result(*this, InstantiationLoc, 227142f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall AssociatedNamespaces, AssociatedClasses); 2272c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall 2273fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // C++ [basic.lookup.koenig]p2: 2274fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // For each argument type T in the function call, there is a set 2275fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // of zero or more associated namespaces and a set of zero or more 2276fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // associated classes to be considered. The sets of namespaces and 2277fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // classes is determined entirely by the types of the function 2278fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // arguments (and the namespace of any template template 22791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // argument). 228013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { 2281fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor Expr *Arg = Args[ArgIdx]; 2282fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2283fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor if (Arg->getType() != Context.OverloadTy) { 2284c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, Arg->getType()); 2285fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor continue; 2286fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2287fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2288fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // [...] In addition, if the argument is the name or address of a 2289fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // set of overloaded functions and/or function templates, its 2290fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // associated classes and namespaces are the union of those 2291fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // associated with each of the members of the set: the namespace 2292fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // in which the function or function template is defined and the 2293fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // classes and namespaces associated with its (non-dependent) 2294fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // parameter types and return type. 2295daa439a6c47d3299157b94a496bf22389bbc77a3Douglas Gregor Arg = Arg->IgnoreParens(); 2296ba13543329afac4a0d01304ec2ec4924d99306a6John McCall if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) 22972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (unaryOp->getOpcode() == UO_AddrOf) 2298ba13543329afac4a0d01304ec2ec4924d99306a6John McCall Arg = unaryOp->getSubExpr(); 2299ba13543329afac4a0d01304ec2ec4924d99306a6John McCall 2300c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg); 2301c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall if (!ULE) continue; 2302fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2303c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall for (UnresolvedSetIterator I = ULE->decls_begin(), E = ULE->decls_end(); 2304c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall I != E; ++I) { 2305bd64729ac6de8fed320e7a722597cc5444709c63Chandler Carruth // Look through any using declarations to find the underlying function. 2306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines FunctionDecl *FDecl = (*I)->getUnderlyingDecl()->getAsFunction(); 2307fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 2308fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // Add the classes and namespaces associated with the parameter 2309fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor // types and return type of this function. 2310c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall addAssociatedClassesAndNamespaces(Result, FDecl->getType()); 2311fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2312fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 2313fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 23143fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor 23157d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCallNamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, 2316c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor SourceLocation Loc, 23177d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall LookupNameKind NameKind, 23187d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall RedeclarationKind Redecl) { 2319c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor LookupResult R(*this, Name, Loc, NameKind, Redecl); 23207d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall LookupName(R, S); 23211bcee0a5a29981f8c78a8620d1c78841dbc5c348John McCall return R.getAsSingle<NamedDecl>(); 23227d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall} 23237d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall 23246e378de1aebdfeb44f2a7677ed207b32b3a41fbfDouglas Gregor/// \brief Find the protocol with the given name, if any. 2325dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, 232627c6da284f90e32cda0ec8f52a2b6ba5a2613252Douglas Gregor SourceLocation IdLoc, 232727c6da284f90e32cda0ec8f52a2b6ba5a2613252Douglas Gregor RedeclarationKind Redecl) { 2328c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor Decl *D = LookupSingleName(TUScope, II, IdLoc, 232927c6da284f90e32cda0ec8f52a2b6ba5a2613252Douglas Gregor LookupObjCProtocolName, Redecl); 23306e378de1aebdfeb44f2a7677ed207b32b3a41fbfDouglas Gregor return cast_or_null<ObjCProtocolDecl>(D); 23316e378de1aebdfeb44f2a7677ed207b32b3a41fbfDouglas Gregor} 23326e378de1aebdfeb44f2a7677ed207b32b3a41fbfDouglas Gregor 23333fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregorvoid Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, 23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType T1, QualType T2, 23356e26689f5d513e24ad7783a4493201930fdeccc0John McCall UnresolvedSetImpl &Functions) { 23363fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // C++ [over.match.oper]p3: 23373fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // -- The set of non-member candidates is the result of the 23383fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // unqualified lookup of operator@ in the context of the 23393fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // expression according to the usual rules for name lookup in 23403fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // unqualified function calls (3.4.2) except that all member 23416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // functions are ignored. 23423fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); 2343a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); 2344a24dc2e38c7fb0f7f138b3d14b5f0f241fd0eccfJohn McCall LookupName(Operators, S); 23451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23463fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); 23476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Functions.append(Operators.begin(), Operators.end()); 23483fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor} 23493fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor 2350c39b6bc958982171b45c658389840a6241739a5eSean HuntSema::SpecialMemberOverloadResult *Sema::LookupSpecialMember(CXXRecordDecl *RD, 2351308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt CXXSpecialMember SM, 2352308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt bool ConstArg, 2353308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt bool VolatileArg, 2354308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt bool RValueThis, 2355308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt bool ConstThis, 2356308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt bool VolatileThis) { 2357d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith assert(CanDeclareSpecialMemberFunction(RD) && 2358308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt "doing special member lookup into record that isn't fully complete"); 2359d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith RD = RD->getDefinition(); 2360308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt if (RValueThis || ConstThis || VolatileThis) 2361308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) && 2362308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt "constructors and destructors always have unqualified lvalue this"); 2363308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt if (ConstArg || VolatileArg) 2364308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt assert((SM != CXXDefaultConstructor && SM != CXXDestructor) && 2365308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt "parameter-less special members can't have qualified arguments"); 2366308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2367308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt llvm::FoldingSetNodeID ID; 2368c39b6bc958982171b45c658389840a6241739a5eSean Hunt ID.AddPointer(RD); 2369308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(SM); 2370308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(ConstArg); 2371308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(VolatileArg); 2372308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(RValueThis); 2373308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(ConstThis); 2374308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt ID.AddInteger(VolatileThis); 2375308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2376308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt void *InsertPoint; 2377308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt SpecialMemberOverloadResult *Result = 2378308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint); 2379308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2380308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt // This was already cached 2381308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt if (Result) 2382308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt return Result; 2383308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2384305435867ccb14db1c3937a7f91945c42739b980Sean Hunt Result = BumpAlloc.Allocate<SpecialMemberOverloadResult>(); 2385305435867ccb14db1c3937a7f91945c42739b980Sean Hunt Result = new (Result) SpecialMemberOverloadResult(ID); 2386308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt SpecialMemberCache.InsertNode(Result, InsertPoint); 2387308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2388308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt if (SM == CXXDestructor) { 2389e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (RD->needsImplicitDestructor()) 2390c39b6bc958982171b45c658389840a6241739a5eSean Hunt DeclareImplicitDestructor(RD); 2391c39b6bc958982171b45c658389840a6241739a5eSean Hunt CXXDestructorDecl *DD = RD->getDestructor(); 2392308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt assert(DD && "record without a destructor"); 2393308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt Result->setMethod(DD); 23946c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith Result->setKind(DD->isDeleted() ? 23956c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith SpecialMemberOverloadResult::NoMemberOrDeleted : 2396704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith SpecialMemberOverloadResult::Success); 2397308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt return Result; 2398308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt } 2399308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 2400b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // Prepare for overload resolution. Here we construct a synthetic argument 2401b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // if necessary and make sure that implicit functions are declared. 2402c39b6bc958982171b45c658389840a6241739a5eSean Hunt CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD)); 2403b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt DeclarationName Name; 24046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Expr *Arg = nullptr; 2405b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt unsigned NumArgs; 2406b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2407704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith QualType ArgType = CanTy; 2408704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith ExprValueKind VK = VK_LValue; 2409704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith 2410b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (SM == CXXDefaultConstructor) { 2411b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt Name = Context.DeclarationNames.getCXXConstructorName(CanTy); 2412b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt NumArgs = 0; 2413c39b6bc958982171b45c658389840a6241739a5eSean Hunt if (RD->needsImplicitDefaultConstructor()) 2414c39b6bc958982171b45c658389840a6241739a5eSean Hunt DeclareImplicitDefaultConstructor(RD); 2415b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } else { 2416b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) { 2417b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt Name = Context.DeclarationNames.getCXXConstructorName(CanTy); 2418e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (RD->needsImplicitCopyConstructor()) 2419c39b6bc958982171b45c658389840a6241739a5eSean Hunt DeclareImplicitCopyConstructor(RD); 242080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor()) 242185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl DeclareImplicitMoveConstructor(RD); 2422b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } else { 2423b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); 2424e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (RD->needsImplicitCopyAssignment()) 2425c39b6bc958982171b45c658389840a6241739a5eSean Hunt DeclareImplicitCopyAssignment(RD); 242680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment()) 242785ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl DeclareImplicitMoveAssignment(RD); 2428b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } 2429b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2430b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (ConstArg) 2431b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt ArgType.addConst(); 2432b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (VolatileArg) 2433b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt ArgType.addVolatile(); 2434b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2435b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // This isn't /really/ specified by the standard, but it's implied 2436b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // we should be working from an RValue in the case of move to ensure 2437b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // that we prefer to bind to rvalue references, and an LValue in the 2438b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // case of copy to ensure we don't bind to rvalue references. 2439b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // Possibly an XValue is actually correct in the case of move, but 2440b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // there is no semantic difference for class types in this restricted 2441b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // case. 2442ab183dff7ed681f7140a8e32510c4f619b791d2eSean Hunt if (SM == CXXCopyConstructor || SM == CXXCopyAssignment) 2443b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt VK = VK_LValue; 2444b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt else 2445b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt VK = VK_RValue; 2446704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith } 2447b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2448704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith OpaqueValueExpr FakeArg(SourceLocation(), ArgType, VK); 2449704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith 2450704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith if (SM != CXXDefaultConstructor) { 2451b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt NumArgs = 1; 2452704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith Arg = &FakeArg; 2453b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } 2454b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2455b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // Create the object argument 2456b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt QualType ThisTy = CanTy; 2457b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (ConstThis) 2458b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt ThisTy.addConst(); 2459b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt if (VolatileThis) 2460b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt ThisTy.addVolatile(); 24614cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt Expr::Classification Classification = 2462704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith OpaqueValueExpr(SourceLocation(), ThisTy, 2463704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith RValueThis ? VK_RValue : VK_LValue).Classify(Context); 2464b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2465b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // Now we perform lookup on the name we computed earlier and do overload 2466b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // resolution. Lookup is only performed directly into the class since there 2467b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // will always be a (possibly implicit) declaration to shadow any others. 24686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines OverloadCandidateSet OCS(RD->getLocation(), OverloadCandidateSet::CSK_Normal); 24693bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_result R = RD->lookup(Name); 24703bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie assert(!R.empty() && 2471b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt "lookup for a constructor or assignment operator was empty"); 2472ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth 2473ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth // Copy the candidates as our processing of them may load new declarations 2474ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth // from an external source and invalidate lookup_result. 2475ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end()); 2476ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth 2477ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth for (SmallVectorImpl<NamedDecl *>::iterator I = Candidates.begin(), 2478b60fae50d38a0291e1c5731b2fb22849d26ca342Richard Smith E = Candidates.end(); 2479ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth I != E; ++I) { 2480ab4e0c0ad9cbb744e159f6ff6022a9b7f889147cChandler Carruth NamedDecl *Cand = *I; 24814cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt 2482c39b6bc958982171b45c658389840a6241739a5eSean Hunt if (Cand->isInvalidDecl()) 2483b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt continue; 2484b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2485c39b6bc958982171b45c658389840a6241739a5eSean Hunt if (UsingShadowDecl *U = dyn_cast<UsingShadowDecl>(Cand)) { 2486c39b6bc958982171b45c658389840a6241739a5eSean Hunt // FIXME: [namespace.udecl]p15 says that we should only consider a 2487c39b6bc958982171b45c658389840a6241739a5eSean Hunt // using declaration here if it does not match a declaration in the 2488c39b6bc958982171b45c658389840a6241739a5eSean Hunt // derived class. We do not implement this correctly in other cases 2489c39b6bc958982171b45c658389840a6241739a5eSean Hunt // either. 2490c39b6bc958982171b45c658389840a6241739a5eSean Hunt Cand = U->getTargetDecl(); 2491c39b6bc958982171b45c658389840a6241739a5eSean Hunt 2492c39b6bc958982171b45c658389840a6241739a5eSean Hunt if (Cand->isInvalidDecl()) 2493c39b6bc958982171b45c658389840a6241739a5eSean Hunt continue; 2494c39b6bc958982171b45c658389840a6241739a5eSean Hunt } 2495c39b6bc958982171b45c658389840a6241739a5eSean Hunt 2496c39b6bc958982171b45c658389840a6241739a5eSean Hunt if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand)) { 24974cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) 2498c39b6bc958982171b45c658389840a6241739a5eSean Hunt AddMethodCandidate(M, DeclAccessPair::make(M, AS_public), RD, ThisTy, 249913a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles Classification, llvm::makeArrayRef(&Arg, NumArgs), 250013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles OCS, true); 25014cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt else 250213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles AddOverloadCandidate(M, DeclAccessPair::make(M, AS_public), 250313a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles llvm::makeArrayRef(&Arg, NumArgs), OCS, true); 2504431a1cbd3dc5267247b0d66a4e65d51ef307fdcfSean Hunt } else if (FunctionTemplateDecl *Tmpl = 2505c39b6bc958982171b45c658389840a6241739a5eSean Hunt dyn_cast<FunctionTemplateDecl>(Cand)) { 25064cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) 25074cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt AddMethodTemplateCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), 25086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines RD, nullptr, ThisTy, Classification, 250913a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles llvm::makeArrayRef(&Arg, NumArgs), 25104cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt OCS, true); 25114cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt else 25124cc12c6e47a200cf166ac21efc09dd033f34c9b2Sean Hunt AddTemplateOverloadCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public), 25136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines nullptr, llvm::makeArrayRef(&Arg, NumArgs), 251413a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles OCS, true); 2515c39b6bc958982171b45c658389840a6241739a5eSean Hunt } else { 2516c39b6bc958982171b45c658389840a6241739a5eSean Hunt assert(isa<UsingDecl>(Cand) && "illegal Kind of operator = Decl"); 2517b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } 2518b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } 2519b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2520b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt OverloadCandidateSet::iterator Best; 2521b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt switch (OCS.BestViableFunction(*this, SourceLocation(), Best)) { 2522b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt case OR_Success: 2523b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt Result->setMethod(cast<CXXMethodDecl>(Best->Function)); 2524704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith Result->setKind(SpecialMemberOverloadResult::Success); 2525b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt break; 2526b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2527b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt case OR_Deleted: 2528b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt Result->setMethod(cast<CXXMethodDecl>(Best->Function)); 25296c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); 2530b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt break; 2531b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2532b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt case OR_Ambiguous: 25336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Result->setMethod(nullptr); 25346c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith Result->setKind(SpecialMemberOverloadResult::Ambiguous); 25356c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith break; 25366c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith 2537b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt case OR_No_Viable_Function: 25386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Result->setMethod(nullptr); 25396c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); 2540b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt break; 2541b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt } 2542b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2543b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt return Result; 2544b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt} 2545b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2546b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt/// \brief Look up the default constructor for the given class. 2547b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean HuntCXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) { 2548c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt SpecialMemberOverloadResult *Result = 2549b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false, 2550b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt false, false); 2551b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt 2552b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 2553c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt} 2554c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt 2555661c67a9227708056850403847a10395308705e5Sean Hunt/// \brief Look up the copying constructor for the given class. 2556661c67a9227708056850403847a10395308705e5Sean HuntCXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class, 2557704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith unsigned Quals) { 2558c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2559c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt "non-const, non-volatile qualifiers for copy ctor arg"); 2560c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt SpecialMemberOverloadResult *Result = 2561c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const, 2562c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt Quals & Qualifiers::Volatile, false, false, false); 2563c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt 2564c530d1758cef1acc596f2ec905665da534e66d29Sean Hunt return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 2565308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt} 2566308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt 256785ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl/// \brief Look up the moving constructor for the given class. 25686a06e5ff3b680dcf7234d200309fd2400c478095Richard SmithCXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class, 25696a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith unsigned Quals) { 257085ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl SpecialMemberOverloadResult *Result = 25716a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const, 25726a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith Quals & Qualifiers::Volatile, false, false, false); 257385ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 257485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl return cast_or_null<CXXConstructorDecl>(Result->getMethod()); 257585ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl} 257685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 2577e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor/// \brief Look up the constructors for the given class. 2578e5eee5a52d98021de862b7965c88577598d52ccbDouglas GregorDeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { 2579b320e0c322fb71d6235ebca9ec22940a97bdcdc7Sean Hunt // If the implicit constructors have not yet been declared, do so now. 2580d0adeb65c743e01ca3436db1d47a97cdcc78df89Richard Smith if (CanDeclareSpecialMemberFunction(Class)) { 2581cdee3fee8ca4df7fb9179f29cc3ba96ac4fd0f95Sean Hunt if (Class->needsImplicitDefaultConstructor()) 25821827403a7138946305c0058f262e02b595cf882fDouglas Gregor DeclareImplicitDefaultConstructor(Class); 2583e5411b7c9af683dab6b035825b71c284bfec364dRichard Smith if (Class->needsImplicitCopyConstructor()) 25841827403a7138946305c0058f262e02b595cf882fDouglas Gregor DeclareImplicitCopyConstructor(Class); 258580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor()) 258685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl DeclareImplicitMoveConstructor(Class); 25871827403a7138946305c0058f262e02b595cf882fDouglas Gregor } 2588dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2589e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); 2590e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); 2591e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor return Class->lookup(Name); 2592e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor} 2593e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor 2594661c67a9227708056850403847a10395308705e5Sean Hunt/// \brief Look up the copying assignment operator for the given class. 2595661c67a9227708056850403847a10395308705e5Sean HuntCXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class, 2596661c67a9227708056850403847a10395308705e5Sean Hunt unsigned Quals, bool RValueThis, 2597704c8f76bbe2de68375f7f146e75bd74de6dd518Richard Smith unsigned ThisQuals) { 2598661c67a9227708056850403847a10395308705e5Sean Hunt assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2599661c67a9227708056850403847a10395308705e5Sean Hunt "non-const, non-volatile qualifiers for copy assignment arg"); 2600661c67a9227708056850403847a10395308705e5Sean Hunt assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 2601661c67a9227708056850403847a10395308705e5Sean Hunt "non-const, non-volatile qualifiers for copy assignment this"); 2602661c67a9227708056850403847a10395308705e5Sean Hunt SpecialMemberOverloadResult *Result = 2603661c67a9227708056850403847a10395308705e5Sean Hunt LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const, 2604661c67a9227708056850403847a10395308705e5Sean Hunt Quals & Qualifiers::Volatile, RValueThis, 2605661c67a9227708056850403847a10395308705e5Sean Hunt ThisQuals & Qualifiers::Const, 2606661c67a9227708056850403847a10395308705e5Sean Hunt ThisQuals & Qualifiers::Volatile); 2607661c67a9227708056850403847a10395308705e5Sean Hunt 2608661c67a9227708056850403847a10395308705e5Sean Hunt return Result->getMethod(); 2609661c67a9227708056850403847a10395308705e5Sean Hunt} 2610661c67a9227708056850403847a10395308705e5Sean Hunt 261185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl/// \brief Look up the moving assignment operator for the given class. 261285ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian RedlCXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class, 26136a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith unsigned Quals, 261485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl bool RValueThis, 261585ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl unsigned ThisQuals) { 261685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && 261785ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl "non-const, non-volatile qualifiers for copy assignment this"); 261885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl SpecialMemberOverloadResult *Result = 26196a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const, 26206a06e5ff3b680dcf7234d200309fd2400c478095Richard Smith Quals & Qualifiers::Volatile, RValueThis, 262185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl ThisQuals & Qualifiers::Const, 262285ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl ThisQuals & Qualifiers::Volatile); 262385ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 262485ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl return Result->getMethod(); 262585ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl} 262685ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl 2627db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor/// \brief Look for the destructor of the given class. 2628db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor/// 2629c5c9b538de70124a57bee0adbfdd7853a582176eSean Hunt/// During semantic analysis, this routine should be used in lieu of 2630c5c9b538de70124a57bee0adbfdd7853a582176eSean Hunt/// CXXRecordDecl::getDestructor(). 2631db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor/// 2632db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor/// \returns The destructor for this class. 2633db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas GregorCXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { 2634308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor, 2635308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt false, false, false, 2636308742c87e2311c3f349092e5b823c3821e61b10Sean Hunt false, false)->getMethod()); 2637db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor} 2638db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor 263936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// LookupLiteralOperator - Determine which literal operator should be used for 264036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// a user-defined literal, per C++11 [lex.ext]. 264136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// 264236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// Normal overload resolution is not used to select which literal operator to 264336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// call for a user-defined literal. Look up the provided literal operator name, 264436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith/// and filter the results to the appropriate set for the given argument types. 264536f5cfe4df32af6c5fe01228102512996f566f9dRichard SmithSema::LiteralOperatorLookupResult 264636f5cfe4df32af6c5fe01228102512996f566f9dRichard SmithSema::LookupLiteralOperator(Scope *S, LookupResult &R, 264736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith ArrayRef<QualType> ArgTys, 2648b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool AllowRaw, bool AllowTemplate, 2649b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool AllowStringTemplate) { 265036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith LookupName(R, S); 265136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith assert(R.getResultKind() != LookupResult::Ambiguous && 265236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith "literal operator lookup can't be ambiguous"); 265336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 265436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // Filter the lookup results appropriately. 265536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith LookupResult::Filter F = R.makeFilter(); 265636f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 265736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith bool FoundRaw = false; 2658b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool FoundTemplate = false; 2659b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool FoundStringTemplate = false; 266036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith bool FoundExactMatch = false; 266136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 266236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith while (F.hasNext()) { 266336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith Decl *D = F.next(); 266436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) 266536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith D = USD->getTargetDecl(); 266636f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 26677e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor // If the declaration we found is invalid, skip it. 26687e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor if (D->isInvalidDecl()) { 26697e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor F.erase(); 26707e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor continue; 26717e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor } 26727e0c22714e4750a2f730035957f9bb678ae681c0Douglas Gregor 2673b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool IsRaw = false; 2674b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool IsTemplate = false; 2675b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool IsStringTemplate = false; 2676b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith bool IsExactMatch = false; 2677b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith 267836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 267936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FD->getNumParams() == 1 && 268036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith FD->getParamDecl(0)->getType()->getAs<PointerType>()) 268136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith IsRaw = true; 2682a121eb3af061a9402aa88630a0eb535d92962e2cRichard Smith else if (FD->getNumParams() == ArgTys.size()) { 268336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith IsExactMatch = true; 268436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) { 268536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith QualType ParamTy = FD->getParamDecl(ArgIdx)->getType(); 268636f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) { 268736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith IsExactMatch = false; 268836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith break; 268936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 269036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 269136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 269236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 2693b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) { 2694b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith TemplateParameterList *Params = FD->getTemplateParameters(); 2695b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith if (Params->size() == 1) 2696b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith IsTemplate = true; 2697b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith else 2698b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith IsStringTemplate = true; 2699b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith } 270036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 270136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (IsExactMatch) { 270236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith FoundExactMatch = true; 2703b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith AllowRaw = false; 2704b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith AllowTemplate = false; 2705b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith AllowStringTemplate = false; 2706b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith if (FoundRaw || FoundTemplate || FoundStringTemplate) { 270736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // Go through again and remove the raw and template decls we've 270836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // already found. 270936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith F.restart(); 2710b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith FoundRaw = FoundTemplate = FoundStringTemplate = false; 271136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 2712b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith } else if (AllowRaw && IsRaw) { 2713b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith FoundRaw = true; 2714b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith } else if (AllowTemplate && IsTemplate) { 2715b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith FoundTemplate = true; 2716b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith } else if (AllowStringTemplate && IsStringTemplate) { 2717b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith FoundStringTemplate = true; 271836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } else { 271936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith F.erase(); 272036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 272136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 272236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 272336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith F.done(); 272436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 272536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching 272636f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // parameter type, that is used in preference to a raw literal operator 272736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // or literal operator template. 272836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FoundExactMatch) 272936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith return LOLR_Cooked; 273036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 273136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal 273236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // operator template, but not both. 273336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FoundRaw && FoundTemplate) { 273436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); 2735651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) 2736651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines NoteOverloadCandidate((*I)->getUnderlyingDecl()->getAsFunction()); 273736f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith return LOLR_Error; 273836f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith } 273936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 274036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FoundRaw) 274136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith return LOLR_Raw; 274236f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 274336f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith if (FoundTemplate) 274436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith return LOLR_Template; 274536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 2746b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith if (FoundStringTemplate) 2747b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith return LOLR_StringTemplate; 2748b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith 274936f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith // Didn't find anything we could use. 275036f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator) 275136f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith << R.getLookupName() << (int)ArgTys.size() << ArgTys[0] 2752b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith << (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw 2753b328e29173d15baf3f44ccdc9c310ee741caaaf7Richard Smith << (AllowTemplate || AllowStringTemplate); 275436f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith return LOLR_Error; 275536f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith} 275636f5cfe4df32af6c5fe01228102512996f566f9dRichard Smith 27577edb5fdf9703e1abd780417db691b77d5fcbc610John McCallvoid ADLResult::insert(NamedDecl *New) { 27587edb5fdf9703e1abd780417db691b77d5fcbc610John McCall NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; 27597edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27607edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // If we haven't yet seen a decl for this key, or the last decl 27617edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // was exactly this one, we're done. 27626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Old == nullptr || Old == New) { 27637edb5fdf9703e1abd780417db691b77d5fcbc610John McCall Old = New; 27647edb5fdf9703e1abd780417db691b77d5fcbc610John McCall return; 27657edb5fdf9703e1abd780417db691b77d5fcbc610John McCall } 27667edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27677edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // Otherwise, decide which is a more recent redeclaration. 2768651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines FunctionDecl *OldFD = Old->getAsFunction(); 2769651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines FunctionDecl *NewFD = New->getAsFunction(); 27707edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27717edb5fdf9703e1abd780417db691b77d5fcbc610John McCall FunctionDecl *Cursor = NewFD; 27727edb5fdf9703e1abd780417db691b77d5fcbc610John McCall while (true) { 2773ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor Cursor = Cursor->getPreviousDecl(); 27747edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27757edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // If we got to the end without finding OldFD, OldFD is the newer 27767edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // declaration; leave things as they are. 27777edb5fdf9703e1abd780417db691b77d5fcbc610John McCall if (!Cursor) return; 27787edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27797edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // If we do find OldFD, then NewFD is newer. 27807edb5fdf9703e1abd780417db691b77d5fcbc610John McCall if (Cursor == OldFD) break; 27817edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27827edb5fdf9703e1abd780417db691b77d5fcbc610John McCall // Otherwise, keep looking. 27837edb5fdf9703e1abd780417db691b77d5fcbc610John McCall } 27847edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27857edb5fdf9703e1abd780417db691b77d5fcbc610John McCall Old = New; 27867edb5fdf9703e1abd780417db691b77d5fcbc610John McCall} 27877edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 27886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc, 27896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ArrayRef<Expr *> Args, ADLResult &Result) { 27903fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // Find all of the associated namespaces and classes based on the 27913fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // arguments we have. 27923fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor AssociatedNamespaceSet AssociatedNamespaces; 27933fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor AssociatedClassSet AssociatedClasses; 279442f48fbdf31a7e8c516ba5eed486ff53966459fcJohn McCall FindAssociatedClassesAndNamespaces(Loc, Args, 27956ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall AssociatedNamespaces, 27966ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall AssociatedClasses); 27973fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor 27983fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // C++ [basic.lookup.argdep]p3: 27993fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // Let X be the lookup set produced by unqualified lookup (3.4.1) 28003fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // and let Y be the lookup set produced by argument dependent 28013fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // lookup (defined as follows). If X contains [...] then Y is 28023fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // empty. Otherwise Y is the set of declarations found in the 28033fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // namespaces associated with the argument types as described 28043fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // below. The set of declarations found by the lookup of the name 28053fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // is the union of X and Y. 28063fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // 28073fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // Here, we compute Y and add its members to the overloaded 28083fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // candidate set. 28093fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), 28101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NSEnd = AssociatedNamespaces.end(); 28111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NS != NSEnd; ++NS) { 28123fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // When considering an associated namespace, the lookup is the 28133fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // same as the lookup performed when the associated namespace is 28143fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // used as a qualifier (3.4.3.2) except that: 28153fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // 28163fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // -- Any using-directives in the associated namespace are 28173fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // ignored. 28183fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // 28196ff0785f1e58fb2cfb702d2995d076c6648a9a13John McCall // -- Any namespace-scope friend functions declared in 28203fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // associated classes are visible within their respective 28213fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // namespaces even if they are not visible during an ordinary 28223fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor // lookup (11.4). 28233bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie DeclContext::lookup_result R = (*NS)->lookup(Name); 28243bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; 28253bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie ++I) { 28266e26689f5d513e24ad7783a4493201930fdeccc0John McCall NamedDecl *D = *I; 282702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // If the only declaration here is an ordinary friend, consider 282802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // it only if it was declared in an associated classes. 2829a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith if ((D->getIdentifierNamespace() & Decl::IDNS_Ordinary) == 0) { 2830a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith // If it's neither ordinarily visible nor a friend, we can't find it. 2831a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith if ((D->getIdentifierNamespace() & Decl::IDNS_OrdinaryFriend) == 0) 2832a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith continue; 2833a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith 283422050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith bool DeclaredInAssociatedClass = false; 283522050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith for (Decl *DI = D; DI; DI = DI->getPreviousDecl()) { 283622050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith DeclContext *LexDC = DI->getLexicalDeclContext(); 283722050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith if (isa<CXXRecordDecl>(LexDC) && 283822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith AssociatedClasses.count(cast<CXXRecordDecl>(LexDC))) { 283922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith DeclaredInAssociatedClass = true; 284022050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith break; 284122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith } 284222050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith } 284322050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith if (!DeclaredInAssociatedClass) 28443f9a8a60614b763785d54ad08821745d03a4af70John McCall continue; 28453f9a8a60614b763785d54ad08821745d03a4af70John McCall } 28461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2847a113e7263c5337731c65fada9de7ff72af25423bJohn McCall if (isa<UsingShadowDecl>(D)) 2848a113e7263c5337731c65fada9de7ff72af25423bJohn McCall D = cast<UsingShadowDecl>(D)->getTargetDecl(); 2849a113e7263c5337731c65fada9de7ff72af25423bJohn McCall 28506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) 28517edb5fdf9703e1abd780417db691b77d5fcbc610John McCall continue; 28527edb5fdf9703e1abd780417db691b77d5fcbc610John McCall 28537edb5fdf9703e1abd780417db691b77d5fcbc610John McCall Result.insert(D); 285444bc2d53ebf1b7ad7439a0f73aba3d00a462d63aDouglas Gregor } 285544bc2d53ebf1b7ad7439a0f73aba3d00a462d63aDouglas Gregor } 28563fd95ce225393fe4a3623e429766a8c3f487ff9dDouglas Gregor} 2857546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2858546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor//---------------------------------------------------------------------------- 2859546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor// Search for all visible declarations. 2860546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor//---------------------------------------------------------------------------- 2861546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas GregorVisibleDeclConsumer::~VisibleDeclConsumer() { } 2862546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2863d67679d7439bd17b06574781b908630f4640c662Richard Smithbool VisibleDeclConsumer::includeHiddenDecls() const { return false; } 2864d67679d7439bd17b06574781b908630f4640c662Richard Smith 2865546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregornamespace { 2866546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2867546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorclass ShadowContextRAII; 2868546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2869546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorclass VisibleDeclsRecord { 2870546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorpublic: 2871546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief An entry in the shadow map, which is optimized to store a 2872546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// single declaration (the common case) but can also store a list 2873546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// of declarations. 2874b5f65475d25b67f87e368daa1583b762af7e2e45Chris Lattner typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry; 2875546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2876546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorprivate: 2877546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief A mapping from declaration names to the declarations that have 2878546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// this name within a particular scope. 2879546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; 2880546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2881546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief A list of shadow maps, which is used to model name hiding. 2882546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor std::list<ShadowMap> ShadowMaps; 2883546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2884546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief The declaration contexts we have already visited. 2885546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; 2886546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2887546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor friend class ShadowContextRAII; 2888546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2889546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorpublic: 2890546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief Determine whether we have already visited this context 2891546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// (and, if not, note that we are going to visit that context now). 2892546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor bool visitedContext(DeclContext *Ctx) { 2893546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return !VisitedContexts.insert(Ctx); 2894546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2895546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 28968071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor bool alreadyVisitedContext(DeclContext *Ctx) { 28978071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor return VisitedContexts.count(Ctx); 28988071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor } 28998071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor 2900546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief Determine whether the given declaration is hidden in the 2901546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// current scope. 2902546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// 2903546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \returns the declaration that hides the given declaration, or 2904546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// NULL if no such declaration exists. 2905546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor NamedDecl *checkHidden(NamedDecl *ND); 2906546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2907546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor /// \brief Add a declaration to the current shadow map. 2908b5f65475d25b67f87e368daa1583b762af7e2e45Chris Lattner void add(NamedDecl *ND) { 2909b5f65475d25b67f87e368daa1583b762af7e2e45Chris Lattner ShadowMaps.back()[ND->getDeclName()].push_back(ND); 2910b5f65475d25b67f87e368daa1583b762af7e2e45Chris Lattner } 2911546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor}; 2912546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2913546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// \brief RAII object that records when we've entered a shadow context. 2914546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorclass ShadowContextRAII { 2915546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclsRecord &Visible; 2916546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2917546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor typedef VisibleDeclsRecord::ShadowMap ShadowMap; 2918546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2919546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorpublic: 2920546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { 2921546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Visible.ShadowMaps.push_back(ShadowMap()); 2922546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2923546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2924546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ~ShadowContextRAII() { 2925546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Visible.ShadowMaps.pop_back(); 2926546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2927546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor}; 2928546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2929546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} // end anonymous namespace 2930546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2931546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas GregorNamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { 2932efcf16d87d7964015e05e917041beef9947c32cdDouglas Gregor // Look through using declarations. 2933efcf16d87d7964015e05e917041beef9947c32cdDouglas Gregor ND = ND->getUnderlyingDecl(); 2934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2935546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor unsigned IDNS = ND->getIdentifierNamespace(); 2936546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); 2937546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); 2938546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor SM != SMEnd; ++SM) { 2939546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowMap::iterator Pos = SM->find(ND->getDeclName()); 2940546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (Pos == SM->end()) 2941546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 2942546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2943dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi for (ShadowMapEntry::iterator I = Pos->second.begin(), 2944546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor IEnd = Pos->second.end(); 2945546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor I != IEnd; ++I) { 2946546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // A tag declaration does not hide a non-tag declaration. 29470d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall if ((*I)->hasTagIdentifierNamespace() && 2948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | 2949546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Decl::IDNS_ObjCProtocol))) 2950546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 2951546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2952546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Protocols are in distinct namespaces from everything else. 2953546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if ((((*I)->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) 2954546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor || (IDNS & Decl::IDNS_ObjCProtocol)) && 2955546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor (*I)->getIdentifierNamespace() != IDNS) 2956546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 2957546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 29580cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor // Functions and function templates in the same scope overload 29590cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor // rather than hide. FIXME: Look for hiding based on function 29600cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor // signatures! 2961651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if ((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && 2962651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && 29630cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor SM == ShadowMaps.rbegin()) 2964def9107059d460ada5f0eb3d58189c59122e3c3fDouglas Gregor continue; 2965dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2966546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // We've found a declaration that hides this one. 2967546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return *I; 2968546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2969546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2970546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 29716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 2972546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 2973546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 2974546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorstatic void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result, 2975546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor bool QualifiedNameLookup, 29760cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor bool InBaseClass, 2977546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclConsumer &Consumer, 2978546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclsRecord &Visited) { 29796202119003aba91e5ff4e579cb9c26a8d1b9514fDouglas Gregor if (!Ctx) 29806202119003aba91e5ff4e579cb9c26a8d1b9514fDouglas Gregor return; 29816202119003aba91e5ff4e579cb9c26a8d1b9514fDouglas Gregor 2982546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Make sure we don't visit the same context twice. 2983546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (Visited.visitedContext(Ctx->getPrimaryContext())) 2984546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return; 2985dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 29864923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) 29874923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor Result.getSema().ForceDeclarationOfImplicitMembers(Class); 29884923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor 2989546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Enumerate all of the results in this context. 2990651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &R : Ctx->lookups()) { 2991651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : R) { 2992651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (NamedDecl *ND = dyn_cast<NamedDecl>(I)) { 2993553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if ((ND = Result.getAcceptableDecl(ND))) { 2994d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); 2995546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Visited.add(ND); 2996546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 299770c233591ad2f77a01c8a318283ae03010c64c8fDouglas Gregor } 2998546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 2999546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3000546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3001546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Traverse using directives for qualified name lookup. 3002546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (QualifiedNameLookup) { 3003546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII Shadow(Visited); 3004651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto I : Ctx->using_directives()) { 3005651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(I->getNominatedNamespace(), Result, 30060cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor QualifiedNameLookup, InBaseClass, Consumer, Visited); 3007546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3008546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3009546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3010f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Traverse the contexts of inherited C++ classes. 3011546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { 301286ff308724171494395a840fd2efbe25e62f352eJohn McCall if (!Record->hasDefinition()) 301386ff308724171494395a840fd2efbe25e62f352eJohn McCall return; 301486ff308724171494395a840fd2efbe25e62f352eJohn McCall 3015651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &B : Record->bases()) { 3016651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines QualType BaseType = B.getType(); 3017dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3018546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Don't look into dependent bases, because name lookup can't look 3019546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // there anyway. 3020546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (BaseType->isDependentType()) 3021546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 3022dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3023546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor const RecordType *Record = BaseType->getAs<RecordType>(); 3024546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (!Record) 3025546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 3026dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3027546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // FIXME: It would be nice to be able to determine whether referencing 3028546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // a particular member would be ambiguous. For example, given 3029546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3030546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // struct A { int member; }; 3031546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // struct B { int member; }; 3032546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // struct C : A, B { }; 3033546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3034546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // void f(C *c) { c->### } 3035546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3036546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // accessing 'member' would result in an ambiguity. However, we 3037546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // could be smart enough to qualify the member with the base 3038546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // class, e.g., 3039546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3040546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // c->B::member 3041546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3042546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // or 3043546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // 3044546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // c->A::member 3045dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3046546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Find results in this base class (and its bases). 3047546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII Shadow(Visited); 3048546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup, 30490cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor true, Consumer, Visited); 3050546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3051546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3052dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3053f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Traverse the contexts of Objective-C classes. 3054f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { 3055f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Traverse categories. 3056651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *Cat : IFace->visible_categories()) { 3057f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor ShadowContextRAII Shadow(Visited); 3058651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(Cat, Result, QualifiedNameLookup, false, 30590cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor Consumer, Visited); 3060f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3061f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor 3062f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Traverse protocols. 3063651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : IFace->all_referenced_protocols()) { 3064f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor ShadowContextRAII Shadow(Visited); 3065651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, 30660cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor Visited); 3067f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3068f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor 3069f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Traverse the superclass. 3070f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (IFace->getSuperClass()) { 3071f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor ShadowContextRAII Shadow(Visited); 3072f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup, 30730cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor true, Consumer, Visited); 3074f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3075dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3076c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor // If there is an implementation, traverse it. We do this to find 3077c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor // synthesized ivars. 3078c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor if (IFace->getImplementation()) { 3079c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor ShadowContextRAII Shadow(Visited); 3080dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi LookupVisibleDecls(IFace->getImplementation(), Result, 3081893a6eaf52930701950b7fc01a2478bb86899b77Nick Lewycky QualifiedNameLookup, InBaseClass, Consumer, Visited); 3082c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor } 3083f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { 3084651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : Protocol->protocols()) { 3085f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor ShadowContextRAII Shadow(Visited); 3086651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, 30870cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor Visited); 3088f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3089f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { 3090651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : Category->protocols()) { 3091f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor ShadowContextRAII Shadow(Visited); 3092651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer, 30930cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor Visited); 3094f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3095dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3096c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor // If there is an implementation, traverse it. 3097c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor if (Category->getImplementation()) { 3098c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor ShadowContextRAII Shadow(Visited); 3099dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi LookupVisibleDecls(Category->getImplementation(), Result, 3100c220a18ecdfa42ebb3ce267974cf24e343e2aa6dDouglas Gregor QualifiedNameLookup, true, Consumer, Visited); 3101dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 3102f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3103546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3104546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3105546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorstatic void LookupVisibleDecls(Scope *S, LookupResult &Result, 3106546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor UnqualUsingDirectiveSet &UDirs, 3107546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclConsumer &Consumer, 3108546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclsRecord &Visited) { 3109546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (!S) 3110546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return; 3111546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3112dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!S->getEntity() || 3113dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi (!S->getParent() && 3114f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek !Visited.alreadyVisitedContext(S->getEntity())) || 3115f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek (S->getEntity())->isFunctionOrMethod()) { 3116a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith FindLocalExternScope FindLocals(Result); 3117539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor // Walk through the declarations in this Scope. 3118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *D : S->decls()) { 3119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 3120553689148f546783e127749438bf6d7806e6bb1dDouglas Gregor if ((ND = Result.getAcceptableDecl(ND))) { 31216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false); 3122539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor Visited.add(ND); 3123539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor } 3124539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor } 3125539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor } 3126dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3127711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor // FIXME: C++ [temp.local]p8 31286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DeclContext *Entity = nullptr; 3129e3582011a63a78cdfa0917077ed87ccb9b3a87b5Douglas Gregor if (S->getEntity()) { 3130546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Look into this scope's declaration context, along with any of its 3131546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // parent lookup contexts (e.g., enclosing classes), up to the point 3132546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // where we hit the context stored in the next outer scope. 3133f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek Entity = S->getEntity(); 3134711be1e89a56cdf679143ad18afaa58ed59f0584Douglas Gregor DeclContext *OuterCtx = findOuterContext(S).first; // FIXME 3135dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3136dbdf5e7d0b6f1f8d8c496c1a0ada6f706cddf100Douglas Gregor for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); 3137546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Ctx = Ctx->getLookupParent()) { 3138f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { 3139f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (Method->isInstanceMethod()) { 3140f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // For instance methods, look for ivars in the method's interface. 3141f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor LookupResult IvarResult(Result.getSema(), Result.getLookupName(), 3142f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor Result.getNameLoc(), Sema::LookupMemberName); 3143ca45da0c6da20d9c0c903370f99af5e9a186e0daDouglas Gregor if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { 3144dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false, 3145d67679d7439bd17b06574781b908630f4640c662Richard Smith /*InBaseClass=*/false, Consumer, Visited); 3146ca45da0c6da20d9c0c903370f99af5e9a186e0daDouglas Gregor } 3147f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3148f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor 3149f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // We've already performed all of the name lookup that we need 3150f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // to for Objective-C methods; the next context will be the 3151f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // outer scope. 3152f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor break; 3153f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 3154f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor 3155546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (Ctx->isFunctionOrMethod()) 3156546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor continue; 3157dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3158dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false, 31590cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor /*InBaseClass=*/false, Consumer, Visited); 3160546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3161546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } else if (!S->getParent()) { 3162546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Look into the translation unit scope. We walk through the translation 3163546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // unit's declaration context, because the Scope itself won't have all of 3164546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // the declarations if we loaded a precompiled header. 3165546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // FIXME: We would like the translation unit's Scope object to point to the 3166546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // translation unit, so we don't need this special "if" branch. However, 3167546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // doing so would force the normal C++ name-lookup code to look into the 3168dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // translation unit decl when the IdentifierInfo chains would suffice. 3169546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Once we fix that problem (which is part of a more general "don't look 3170539c5c34063066753f27bb0f39fdcde1acc06328Douglas Gregor // in DeclContexts unless we have to" optimization), we can eliminate this. 3171546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Entity = Result.getSema().Context.getTranslationUnitDecl(); 3172dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false, 31730cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor /*InBaseClass=*/false, Consumer, Visited); 3174dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 3175dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3176546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (Entity) { 3177546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Lookup visible declarations in any namespaces found by using 3178546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // directives. 3179546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor UnqualUsingDirectiveSet::const_iterator UI, UEnd; 3180651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(UI, UEnd) = UDirs.getNamespacesFor(Entity); 3181546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor for (; UI != UEnd; ++UI) 3182546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor LookupVisibleDecls(const_cast<DeclContext *>(UI->getNominatedNamespace()), 3183dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Result, /*QualifiedNameLookup=*/false, 31840cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor /*InBaseClass=*/false, Consumer, Visited); 3185546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3186546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3187546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Lookup names in the parent scope. 3188546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII Shadow(Visited); 3189546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited); 3190546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3191546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3192546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorvoid Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, 31938071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor VisibleDeclConsumer &Consumer, 31948071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor bool IncludeGlobalScope) { 3195546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Determine the set of using directives available during 3196546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // unqualified name lookup. 3197546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor Scope *Initial = S; 3198546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor UnqualUsingDirectiveSet UDirs; 31994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) { 3200546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Find the first namespace or translation-unit scope. 3201546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor while (S && !isNamespaceOrTranslationUnitScope(S)) 3202546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor S = S->getParent(); 3203546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3204546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor UDirs.visitScopeChain(Initial, S); 3205546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 3206546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor UDirs.done(); 3207546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3208546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Look for visible declarations. 3209546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 3210d67679d7439bd17b06574781b908630f4640c662Richard Smith Result.setAllowHidden(Consumer.includeHiddenDecls()); 3211546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclsRecord Visited; 32128071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor if (!IncludeGlobalScope) 32138071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor Visited.visitedContext(Context.getTranslationUnitDecl()); 3214546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII Shadow(Visited); 3215546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited); 3216546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3217546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3218546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorvoid Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, 32198071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor VisibleDeclConsumer &Consumer, 32208071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor bool IncludeGlobalScope) { 3221546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind); 3222d67679d7439bd17b06574781b908630f4640c662Richard Smith Result.setAllowHidden(Consumer.includeHiddenDecls()); 3223546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor VisibleDeclsRecord Visited; 32248071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor if (!IncludeGlobalScope) 32258071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor Visited.visitedContext(Context.getTranslationUnitDecl()); 3226546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor ShadowContextRAII Shadow(Visited); 3227dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true, 32280cc8404f8cf320da140e560899e1560bce80fe8cDouglas Gregor /*InBaseClass=*/false, Consumer, Visited); 3229546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3230546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 32314ae493cccbfbf122ec6ebac0e330232c22fa8489Chris Lattner/// LookupOrCreateLabel - Do a name lookup of a label with the specified name. 32326784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara/// If GnuLabelLoc is a valid source location, then this is a definition 32336784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara/// of an __label__ label name, otherwise it is a normal label definition 32346784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara/// or use. 32354ae493cccbfbf122ec6ebac0e330232c22fa8489Chris LattnerLabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc, 32366784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara SourceLocation GnuLabelLoc) { 3237337e550218128e7d922c09bb354fbc71de90c568Chris Lattner // Do a lookup to see if we have a label with this name already. 32386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines NamedDecl *Res = nullptr; 32396784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara 32406784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara if (GnuLabelLoc.isValid()) { 32416784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara // Local label definitions always shadow existing labels. 32426784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc); 32436784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara Scope *S = CurScope; 32446784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara PushOnScopeChains(Res, S, true); 32456784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara return cast<LabelDecl>(Res); 32466784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara } 32476784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara 32486784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara // Not a GNU local label. 32496784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration); 32506784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara // If we found a label, check to see if it is in the same context as us. 32516784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara // When in a Block, we don't want to reuse a label in an enclosing function. 3252337e550218128e7d922c09bb354fbc71de90c568Chris Lattner if (Res && Res->getDeclContext() != CurContext) 32536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Res = nullptr; 32546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!Res) { 3255337e550218128e7d922c09bb354fbc71de90c568Chris Lattner // If not forward referenced or defined already, create the backing decl. 32566784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara Res = LabelDecl::Create(Context, CurContext, Loc, II); 32576784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara Scope *S = CurScope->getFnParent(); 3258febb5b884b1bb946b83800cb77cbea0119570d84Chris Lattner assert(S && "Not in a function?"); 3259febb5b884b1bb946b83800cb77cbea0119570d84Chris Lattner PushOnScopeChains(Res, S, true); 3260337e550218128e7d922c09bb354fbc71de90c568Chris Lattner } 3261337e550218128e7d922c09bb354fbc71de90c568Chris Lattner return cast<LabelDecl>(Res); 3262337e550218128e7d922c09bb354fbc71de90c568Chris Lattner} 3263337e550218128e7d922c09bb354fbc71de90c568Chris Lattner 3264337e550218128e7d922c09bb354fbc71de90c568Chris Lattner//===----------------------------------------------------------------------===// 3265546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor// Typo correction 3266337e550218128e7d922c09bb354fbc71de90c568Chris Lattner//===----------------------------------------------------------------------===// 3267546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3268ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic bool isCandidateViable(CorrectionCandidateCallback &CCC, 3269ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection &Candidate) { 3270ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Candidate.setCallbackDistance(CCC.RankCandidate(Candidate)); 3271ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance; 3272ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 3273d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3274ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic void LookupPotentialTypoResult(Sema &SemaRef, 3275ef8225444452a1486bd721f3285301fe84643b00Stephen Hines LookupResult &Res, 3276ef8225444452a1486bd721f3285301fe84643b00Stephen Hines IdentifierInfo *Name, 3277ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Scope *S, CXXScopeSpec *SS, 3278ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *MemberContext, 3279ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool EnteringContext, 3280ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool isObjCIvarLookup, 3281ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool FindHidden); 3282d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3283ef8225444452a1486bd721f3285301fe84643b00Stephen Hines// Fill the supplied vector with the IdentifierInfo pointers for each piece of 3284ef8225444452a1486bd721f3285301fe84643b00Stephen Hines// the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::", 3285ef8225444452a1486bd721f3285301fe84643b00Stephen Hines// fill the vector with the IdentifierInfo pointers for "foo" and "bar"). 3286ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic void getNestedNameSpecifierIdentifiers( 3287ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NestedNameSpecifier *NNS, 3288ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SmallVectorImpl<const IdentifierInfo*> &Identifiers) { 3289ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NestedNameSpecifier *Prefix = NNS->getPrefix()) 3290ef8225444452a1486bd721f3285301fe84643b00Stephen Hines getNestedNameSpecifierIdentifiers(Prefix, Identifiers); 3291ef8225444452a1486bd721f3285301fe84643b00Stephen Hines else 3292ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Identifiers.clear(); 3293d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3294ef8225444452a1486bd721f3285301fe84643b00Stephen Hines const IdentifierInfo *II = nullptr; 3295546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3296ef8225444452a1486bd721f3285301fe84643b00Stephen Hines switch (NNS->getKind()) { 3297ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::Identifier: 3298ef8225444452a1486bd721f3285301fe84643b00Stephen Hines II = NNS->getAsIdentifier(); 3299ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3300546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3301ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::Namespace: 3302ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NNS->getAsNamespace()->isAnonymousNamespace()) 3303ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return; 3304ef8225444452a1486bd721f3285301fe84643b00Stephen Hines II = NNS->getAsNamespace()->getIdentifier(); 3305ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3306ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3307ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::NamespaceAlias: 3308ef8225444452a1486bd721f3285301fe84643b00Stephen Hines II = NNS->getAsNamespaceAlias()->getIdentifier(); 3309ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3310ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3311ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::TypeSpecWithTemplate: 3312ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::TypeSpec: 3313ef8225444452a1486bd721f3285301fe84643b00Stephen Hines II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier(); 3314ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3315ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3316ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case NestedNameSpecifier::Global: 3317ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return; 3318ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3319ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3320ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (II) 3321ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Identifiers.push_back(II); 3322ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 3323ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3324ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesnamespace { 3325ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3326ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic const unsigned MaxTypoDistanceResultSets = 5; 3327ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3328ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesclass TypoCorrectionConsumer : public VisibleDeclConsumer { 3329ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef SmallVector<TypoCorrection, 1> TypoResultList; 3330ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef llvm::StringMap<TypoResultList> TypoResultsMap; 3331ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap; 3332dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3333546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregorpublic: 3334ef8225444452a1486bd721f3285301fe84643b00Stephen Hines explicit TypoCorrectionConsumer(Sema &SemaRef, 3335ef8225444452a1486bd721f3285301fe84643b00Stephen Hines const DeclarationNameInfo &TypoName, 3336ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Sema::LookupNameKind LookupKind, 3337ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Scope *S, CXXScopeSpec *SS, 3338ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CorrectionCandidateCallback &CCC, 3339ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *MemberContext, 3340ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool EnteringContext) 3341ef8225444452a1486bd721f3285301fe84643b00Stephen Hines : Typo(TypoName.getName().getAsIdentifierInfo()), SemaRef(SemaRef), S(S), 3342ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SS(SS), CorrectionValidator(CCC), MemberContext(MemberContext), 3343ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Result(SemaRef, TypoName, LookupKind), 3344ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Namespaces(SemaRef.Context, SemaRef.CurContext, SS), 3345ef8225444452a1486bd721f3285301fe84643b00Stephen Hines EnteringContext(EnteringContext), SearchNamespaces(false) { 3346ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Result.suppressDiagnostics(); 3347ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3348d67679d7439bd17b06574781b908630f4640c662Richard Smith 3349651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool includeHiddenDecls() const override { return true; } 3350546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3351ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Methods for adding potential corrections to the consumer. 3352651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, 3353651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool InBaseClass) override; 33545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner void FoundName(StringRef Name); 33555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner void addKeywordResult(StringRef Keyword); 3356d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor void addCorrection(TypoCorrection Correction); 3357d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3358396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain bool empty() const { return CorrectionResults.empty(); } 3359e24b575f06985336973bb721e198bd184bf7bdd3Douglas Gregor 3360ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Return the list of TypoCorrections for the given identifier from 3361ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// the set of corrections that have the closest edit distance, if any. 3362784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain TypoResultList &operator[](StringRef Name) { 3363396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain return CorrectionResults.begin()->second[Name]; 33647b824e854b8ffc0d46337d95425825015528f35cDouglas Gregor } 33657b824e854b8ffc0d46337d95425825015528f35cDouglas Gregor 3366ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Return the edit distance of the corrections that have the 3367ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// closest/best edit distance from the original typop. 336863aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain unsigned getBestEditDistance(bool Normalized) { 3369396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain if (CorrectionResults.empty()) 337063aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain return (std::numeric_limits<unsigned>::max)(); 337163aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain 3372396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain unsigned BestED = CorrectionResults.begin()->first; 337363aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : BestED; 3374d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3375396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain 3376ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Set-up method to add to the consumer the set of namespaces to use 3377ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// in performing corrections to nested name specifiers. This method also 3378ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// implicitly adds all of the known classes in the current AST context to the 3379ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// to the consumer for correcting nested name specifiers. 3380ef8225444452a1486bd721f3285301fe84643b00Stephen Hines void 3381ef8225444452a1486bd721f3285301fe84643b00Stephen Hines addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces); 3382ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3383ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Return the next typo correction that passes all internal filters 3384ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// and is deemed valid by the consumer's CorrectionCandidateCallback, 3385ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// starting with the corrections that have the closest edit distance. An 3386ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// empty TypoCorrection is returned once no more viable corrections remain 3387ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// in the consumer. 3388ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection getNextCorrection(); 3389ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3390ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesprivate: 3391ef8225444452a1486bd721f3285301fe84643b00Stephen Hines class NamespaceSpecifierSet { 3392ef8225444452a1486bd721f3285301fe84643b00Stephen Hines struct SpecifierInfo { 3393ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext* DeclCtx; 3394ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NestedNameSpecifier* NameSpecifier; 3395ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned EditDistance; 3396ef8225444452a1486bd721f3285301fe84643b00Stephen Hines }; 3397ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3398ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef SmallVector<DeclContext*, 4> DeclContextList; 3399ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList; 3400ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3401ef8225444452a1486bd721f3285301fe84643b00Stephen Hines ASTContext &Context; 3402ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContextList CurContextChain; 3403ef8225444452a1486bd721f3285301fe84643b00Stephen Hines std::string CurNameSpecifier; 3404ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers; 3405ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers; 3406ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool isSorted; 3407ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3408ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SpecifierInfoList Specifiers; 3409ef8225444452a1486bd721f3285301fe84643b00Stephen Hines llvm::SmallSetVector<unsigned, 4> Distances; 3410ef8225444452a1486bd721f3285301fe84643b00Stephen Hines llvm::DenseMap<unsigned, SpecifierInfoList> DistanceMap; 3411ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3412ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Helper for building the list of DeclContexts between the current 3413ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// context and the top of the translation unit 3414ef8225444452a1486bd721f3285301fe84643b00Stephen Hines static DeclContextList buildContextChain(DeclContext *Start); 3415ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3416ef8225444452a1486bd721f3285301fe84643b00Stephen Hines void sortNamespaces(); 3417ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3418ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned buildNestedNameSpecifier(DeclContextList &DeclChain, 3419ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NestedNameSpecifier *&NNS); 3420ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3421ef8225444452a1486bd721f3285301fe84643b00Stephen Hines public: 3422ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext, 3423ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CXXScopeSpec *CurScopeSpec); 3424ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3425ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Add the DeclContext (a namespace or record) to the set, computing 3426ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// the corresponding NestedNameSpecifier and its distance in the process. 3427ef8225444452a1486bd721f3285301fe84643b00Stephen Hines void addNameSpecifier(DeclContext *Ctx); 3428396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain 3429ef8225444452a1486bd721f3285301fe84643b00Stephen Hines typedef SpecifierInfoList::iterator iterator; 3430ef8225444452a1486bd721f3285301fe84643b00Stephen Hines iterator begin() { 3431ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!isSorted) sortNamespaces(); 3432ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return Specifiers.begin(); 3433ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3434ef8225444452a1486bd721f3285301fe84643b00Stephen Hines iterator end() { return Specifiers.end(); } 3435ef8225444452a1486bd721f3285301fe84643b00Stephen Hines }; 3436ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3437ef8225444452a1486bd721f3285301fe84643b00Stephen Hines void addName(StringRef Name, NamedDecl *ND, 3438ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NestedNameSpecifier *NNS = nullptr, bool isKeyword = false); 3439ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3440ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Find any visible decls for the given typo correction candidate. 3441ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// If none are found, it to the set of candidates for which qualified lookups 3442ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// will be performed to find possible nested name specifier changes. 3443ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool resolveCorrection(TypoCorrection &Candidate); 3444ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3445ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief Perform qualified lookups on the queued set of typo correction 3446ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// candidates and add the nested name specifier changes to each candidate if 3447ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// a lookup succeeds (at which point the candidate will be returned to the 3448ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// main pool of potential corrections). 3449ef8225444452a1486bd721f3285301fe84643b00Stephen Hines void performQualifiedLookups(); 3450ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3451ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief The name written that is a typo in the source. 3452ef8225444452a1486bd721f3285301fe84643b00Stephen Hines IdentifierInfo *Typo; 3453ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3454ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \brief The results found that have the smallest edit distance 3455ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// found (so far) with the typo name. 3456ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// 3457ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// The pointer value being set to the current DeclContext indicates 3458ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// whether there is a keyword with this name. 3459ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoEditDistanceMap CorrectionResults; 3460ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3461ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Sema &SemaRef; 3462ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Scope *S; 3463ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CXXScopeSpec *SS; 3464ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CorrectionCandidateCallback &CorrectionValidator; 3465ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *MemberContext; 3466ef8225444452a1486bd721f3285301fe84643b00Stephen Hines LookupResult Result; 3467ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NamespaceSpecifierSet Namespaces; 3468ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SmallVector<TypoCorrection, 2> QualifiedResults; 3469ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool EnteringContext; 3470ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool SearchNamespaces; 3471546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor}; 3472546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3473546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3474546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3475dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, 3476d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen DeclContext *Ctx, bool InBaseClass) { 3477546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Don't consider hidden names for typo correction. 3478546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (Hiding) 3479546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return; 3480dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3481546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Only consider entities with identifiers for names, ignoring 3482546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // special names (constructors, overloaded operators, selectors, 3483546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // etc.). 3484546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor IdentifierInfo *Name = ND->getIdentifier(); 3485546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (!Name) 3486546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor return; 3487546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 3488d67679d7439bd17b06574781b908630f4640c662Richard Smith // Only consider visible declarations and declarations from modules with 3489d67679d7439bd17b06574781b908630f4640c662Richard Smith // names that exactly match. 3490ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo && 3491d67679d7439bd17b06574781b908630f4640c662Richard Smith !findAcceptableDecl(SemaRef, ND)) 3492d67679d7439bd17b06574781b908630f4640c662Richard Smith return; 3493d67679d7439bd17b06574781b908630f4640c662Richard Smith 349495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor FoundName(Name->getName()); 349595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor} 349695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor 34975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid TypoCorrectionConsumer::FoundName(StringRef Name) { 3498546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Compute the edit distance between the typo and the name of this 3499bb3d9976fd0e037ec9864baeaf23dc138bcb6df0Kaelyn Uhrain // entity, and add the identifier to the list of results. 35006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines addName(Name, nullptr); 3501546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 3502546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 35035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) { 3504bb3d9976fd0e037ec9864baeaf23dc138bcb6df0Kaelyn Uhrain // Compute the edit distance between the typo and this keyword, 3505bb3d9976fd0e037ec9864baeaf23dc138bcb6df0Kaelyn Uhrain // and add the keyword to the list of results. 35066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines addName(Keyword, nullptr, nullptr, true); 3507d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3508d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3509fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrainvoid TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND, 3510fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain NestedNameSpecifier *NNS, bool isKeyword) { 3511fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // Use a simple length-based heuristic to determine the minimum possible 3512fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // edit distance. If the minimum isn't good enough, bail out early. 3513ef8225444452a1486bd721f3285301fe84643b00Stephen Hines StringRef TypoStr = Typo->getName(); 3514ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned MinED = abs((int)Name.size() - (int)TypoStr.size()); 3515ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (MinED && TypoStr.size() / MinED < 3) 3516fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain return; 3517fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain 3518fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // Compute an upper bound on the allowable edit distance, so that the 3519fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // edit-distance algorithm can short-circuit. 3520ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned UpperBound = (TypoStr.size() + 2) / 3 + 1; 3521ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned ED = TypoStr.edit_distance(Name, true, UpperBound); 3522fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain if (ED >= UpperBound) return; 3523fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain 3524fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED); 3525f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain if (isKeyword) TC.makeKeyword(); 3526f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain addCorrection(TC); 3527d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3528d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3529d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorvoid TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) { 3530ef8225444452a1486bd721f3285301fe84643b00Stephen Hines StringRef TypoStr = Typo->getName(); 35315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName(); 3532ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3533ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // For very short typos, ignore potential corrections that have a different 3534ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // base identifier from the typo or which have a normalized edit distance 3535ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // longer than the typo itself. 3536ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (TypoStr.size() < 3 && 3537ef8225444452a1486bd721f3285301fe84643b00Stephen Hines (Name != TypoStr || Correction.getEditDistance(true) > TypoStr.size())) 3538ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return; 3539ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3540ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // If the correction is resolved but is not viable, ignore it. 3541ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (Correction.isResolved() && 3542ef8225444452a1486bd721f3285301fe84643b00Stephen Hines !isCandidateViable(CorrectionValidator, Correction)) 3543ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return; 3544ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3545784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain TypoResultList &CList = 3546784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain CorrectionResults[Correction.getEditDistance(false)][Name]; 3547784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain 3548784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain if (!CList.empty() && !CList.back().isResolved()) 3549784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain CList.pop_back(); 3550784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain if (NamedDecl *NewND = Correction.getCorrectionDecl()) { 3551784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain std::string CorrectionStr = Correction.getAsString(SemaRef.getLangOpts()); 3552784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain for (TypoResultList::iterator RI = CList.begin(), RIEnd = CList.end(); 3553784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain RI != RIEnd; ++RI) { 3554784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain // If the Correction refers to a decl already in the result list, 3555784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain // replace the existing result if the string representation of Correction 3556784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain // comes before the current result alphabetically, then stop as there is 3557784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain // nothing more to be done to add Correction to the candidate set. 3558784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain if (RI->getCorrectionDecl() == NewND) { 3559784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain if (CorrectionStr < RI->getAsString(SemaRef.getLangOpts())) 3560784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain *RI = Correction; 3561784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain return; 3562784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain } 3563784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain } 3564784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain } 3565784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain if (CList.empty() || Correction.isResolved()) 3566784ae8e5c6b557e2395991c6008293660f5afe66Kaelyn Uhrain CList.push_back(Correction); 3567d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3568396e0a8cfa3fbdd5fde83a19cc87b7af62cc1dddKaelyn Uhrain while (CorrectionResults.size() > MaxTypoDistanceResultSets) 3569ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CorrectionResults.erase(std::prev(CorrectionResults.end())); 3570d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3571d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3572ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid TypoCorrectionConsumer::addNamespaces( 3573ef8225444452a1486bd721f3285301fe84643b00Stephen Hines const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) { 3574ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SearchNamespaces = true; 35752d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3576ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (auto KNPair : KnownNamespaces) 3577ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Namespaces.addNameSpecifier(KNPair.first); 35782d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3579ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool SSIsTemplate = false; 3580ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NestedNameSpecifier *NNS = 3581ef8225444452a1486bd721f3285301fe84643b00Stephen Hines (SS && SS->isValid()) ? SS->getScopeRep() : nullptr) { 3582ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (const Type *T = NNS->getAsType()) 3583ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization; 3584ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3585ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (const auto *TI : SemaRef.getASTContext().types()) { 3586ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) { 3587ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CD = CD->getCanonicalDecl(); 3588ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() && 3589ef8225444452a1486bd721f3285301fe84643b00Stephen Hines !CD->isUnion() && CD->getIdentifier() && 3590ef8225444452a1486bd721f3285301fe84643b00Stephen Hines (SSIsTemplate || !isa<ClassTemplateSpecializationDecl>(CD)) && 3591ef8225444452a1486bd721f3285301fe84643b00Stephen Hines (CD->isBeingDefined() || CD->isCompleteDefinition())) 3592ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Namespaces.addNameSpecifier(CD); 3593ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3594ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3595ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 35962d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3597ef8225444452a1486bd721f3285301fe84643b00Stephen HinesTypoCorrection TypoCorrectionConsumer::getNextCorrection() { 3598ef8225444452a1486bd721f3285301fe84643b00Stephen Hines while (!CorrectionResults.empty()) { 3599ef8225444452a1486bd721f3285301fe84643b00Stephen Hines auto DI = CorrectionResults.begin(); 3600ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (DI->second.empty()) { 3601ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CorrectionResults.erase(DI); 3602ef8225444452a1486bd721f3285301fe84643b00Stephen Hines continue; 3603ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 36042d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3605ef8225444452a1486bd721f3285301fe84643b00Stephen Hines auto RI = DI->second.begin(); 3606ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (RI->second.empty()) { 3607ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DI->second.erase(RI); 3608ef8225444452a1486bd721f3285301fe84643b00Stephen Hines performQualifiedLookups(); 3609ef8225444452a1486bd721f3285301fe84643b00Stephen Hines continue; 3610ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3611ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3612ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection TC = RI->second.pop_back_val(); 3613ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (TC.isResolved() || resolveCorrection(TC)) 3614ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return TC; 3615ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3616ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return TypoCorrection(); 3617ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 3618ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3619ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesbool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) { 3620ef8225444452a1486bd721f3285301fe84643b00Stephen Hines IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo(); 3621ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *TempMemberContext = MemberContext; 3622ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CXXScopeSpec *TempSS = SS; 3623ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesretry_lookup: 3624ef8225444452a1486bd721f3285301fe84643b00Stephen Hines LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext, 3625ef8225444452a1486bd721f3285301fe84643b00Stephen Hines EnteringContext, 3626ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CorrectionValidator.IsObjCIvarLookup, 3627ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Name == Typo && !Candidate.WillReplaceSpecifier()); 3628ef8225444452a1486bd721f3285301fe84643b00Stephen Hines switch (Result.getResultKind()) { 3629ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::NotFound: 3630ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::NotFoundInCurrentInstantiation: 3631ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::FoundUnresolvedValue: 3632ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (TempSS) { 3633ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Immediately retry the lookup without the given CXXScopeSpec 3634ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TempSS = nullptr; 3635ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Candidate.WillReplaceSpecifier(true); 3636ef8225444452a1486bd721f3285301fe84643b00Stephen Hines goto retry_lookup; 3637ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3638ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (TempMemberContext) { 3639ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SS && !TempSS) 3640ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TempSS = SS; 3641ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TempMemberContext = nullptr; 3642ef8225444452a1486bd721f3285301fe84643b00Stephen Hines goto retry_lookup; 3643ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3644ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SearchNamespaces) 3645ef8225444452a1486bd721f3285301fe84643b00Stephen Hines QualifiedResults.push_back(Candidate); 36462d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain break; 36472d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3648ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::Ambiguous: 3649ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // We don't deal with ambiguities. 36502d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain break; 36512d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3652ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::Found: 3653ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::FoundOverloaded: 3654ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Store all of the Decls for overloaded symbols 3655ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (auto *TRD : Result) 3656ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Candidate.addCorrectionDecl(TRD); 3657ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!isCandidateViable(CorrectionValidator, Candidate)) { 3658ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SearchNamespaces) 3659ef8225444452a1486bd721f3285301fe84643b00Stephen Hines QualifiedResults.push_back(Candidate); 3660ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3661ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3662ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return true; 36632d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain } 3664ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return false; 36652d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain} 36662d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain 3667ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid TypoCorrectionConsumer::performQualifiedLookups() { 3668ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned TypoLen = Typo->getName().size(); 3669ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (auto QR : QualifiedResults) { 3670ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (auto NSI : Namespaces) { 3671ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *Ctx = NSI.DeclCtx; 3672ef8225444452a1486bd721f3285301fe84643b00Stephen Hines const Type *NSType = NSI.NameSpecifier->getAsType(); 3673ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3674ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // If the current NestedNameSpecifier refers to a class and the 3675ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // current correction candidate is the name of that class, then skip 3676ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // it as it is unlikely a qualified version of the class' constructor 3677ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // is an appropriate correction. 3678ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() : 0) { 3679ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo()) 3680ef8225444452a1486bd721f3285301fe84643b00Stephen Hines continue; 3681ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3682d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3683ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection TC(QR); 3684ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TC.ClearCorrectionDecls(); 3685ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TC.setCorrectionSpecifier(NSI.NameSpecifier); 3686ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TC.setQualifierDistance(NSI.EditDistance); 3687ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TC.setCallbackDistance(0); // Reset the callback distance 3688ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 3689ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // If the current correction candidate and namespace combination are 3690ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // too far away from the original typo based on the normalized edit 3691ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // distance, then skip performing a qualified name lookup. 3692ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned TmpED = TC.getEditDistance(true); 3693ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED && 3694ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoLen / TmpED < 3) 3695ef8225444452a1486bd721f3285301fe84643b00Stephen Hines continue; 3696d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3697ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Result.clear(); 3698ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Result.setLookupName(QR.getCorrectionAsIdentifierInfo()); 3699ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!SemaRef.LookupQualifiedName(Result, Ctx)) 3700ef8225444452a1486bd721f3285301fe84643b00Stephen Hines continue; 37016e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain 3702ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Any corrections added below will be validated in subsequent 3703ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // iterations of the main while() loop over the Consumer's contents. 3704ef8225444452a1486bd721f3285301fe84643b00Stephen Hines switch (Result.getResultKind()) { 3705ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::Found: 3706ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::FoundOverloaded: { 3707ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SS && SS->isValid()) { 3708ef8225444452a1486bd721f3285301fe84643b00Stephen Hines std::string NewQualified = TC.getAsString(SemaRef.getLangOpts()); 3709ef8225444452a1486bd721f3285301fe84643b00Stephen Hines std::string OldQualified; 3710ef8225444452a1486bd721f3285301fe84643b00Stephen Hines llvm::raw_string_ostream OldOStream(OldQualified); 3711ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy()); 3712ef8225444452a1486bd721f3285301fe84643b00Stephen Hines OldOStream << Typo->getName(); 3713ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // If correction candidate would be an identical written qualified 3714ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // identifer, then the existing CXXScopeSpec probably included a 3715ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // typedef that didn't get accounted for properly. 3716ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (OldOStream.str() == NewQualified) 3717ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3718ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3719ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end(); 3720ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TRD != TRDEnd; ++TRD) { 3721ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(), 3722ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NSType ? NSType->getAsCXXRecordDecl() 3723ef8225444452a1486bd721f3285301fe84643b00Stephen Hines : nullptr, 3724ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TRD.getPair()) == Sema::AR_accessible) 3725ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TC.addCorrectionDecl(*TRD); 3726ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3727ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (TC.isResolved()) 3728ef8225444452a1486bd721f3285301fe84643b00Stephen Hines addCorrection(TC); 3729ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3730ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3731ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::NotFound: 3732ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::NotFoundInCurrentInstantiation: 3733ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::Ambiguous: 3734ef8225444452a1486bd721f3285301fe84643b00Stephen Hines case LookupResult::FoundUnresolvedValue: 3735ef8225444452a1486bd721f3285301fe84643b00Stephen Hines break; 3736ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 37372d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain } 37382d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain } 3739ef8225444452a1486bd721f3285301fe84643b00Stephen Hines QualifiedResults.clear(); 3740ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 3741d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3742ef8225444452a1486bd721f3285301fe84643b00Stephen HinesTypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet( 3743ef8225444452a1486bd721f3285301fe84643b00Stephen Hines ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec) 3744ef8225444452a1486bd721f3285301fe84643b00Stephen Hines : Context(Context), CurContextChain(buildContextChain(CurContext)), 3745ef8225444452a1486bd721f3285301fe84643b00Stephen Hines isSorted(false) { 3746ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NestedNameSpecifier *NNS = 3747ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) { 3748ef8225444452a1486bd721f3285301fe84643b00Stephen Hines llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier); 3749ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NNS->print(SpecifierOStream, Context.getPrintingPolicy()); 37503d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 3751ef8225444452a1486bd721f3285301fe84643b00Stephen Hines getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers); 3752ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 3753ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Build the list of identifiers that would be used for an absolute 3754ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // (from the global context) NestedNameSpecifier referring to the current 3755ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // context. 3756ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), 3757ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CEnd = CurContextChain.rend(); 3758ef8225444452a1486bd721f3285301fe84643b00Stephen Hines C != CEnd; ++C) { 3759ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) 3760ef8225444452a1486bd721f3285301fe84643b00Stephen Hines CurContextIdentifiers.push_back(ND->getIdentifier()); 3761d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3762d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3763ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Add the global context as a NestedNameSpecifier 3764ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Distances.insert(1); 3765ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()), 3766ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NestedNameSpecifier::GlobalSpecifier(Context), 1}; 3767ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DistanceMap[1].push_back(SI); 3768d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3769d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3770ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesauto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain( 3771ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *Start) -> DeclContextList { 37720db9d2066318f3721a45954e23b26ac8ef0723bbNick Lewycky assert(Start && "Building a context chain from a null context"); 3773d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DeclContextList Chain; 37746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr; 3775d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DC = DC->getLookupParent()) { 3776d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC); 3777d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (!DC->isInlineNamespace() && !DC->isTransparentContext() && 3778d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor !(ND && ND->isAnonymousNamespace())) 3779d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Chain.push_back(DC->getPrimaryContext()); 3780d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3781d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return Chain; 3782d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3783d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3784ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid TypoCorrectionConsumer::NamespaceSpecifierSet::sortNamespaces() { 37855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<unsigned, 4> sortedDistances; 3786d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor sortedDistances.append(Distances.begin(), Distances.end()); 3787d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3788d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (sortedDistances.size() > 1) 3789d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor std::sort(sortedDistances.begin(), sortedDistances.end()); 3790d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3791d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Specifiers.clear(); 379209d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper for (SmallVectorImpl<unsigned>::iterator DI = sortedDistances.begin(), 379309d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper DIEnd = sortedDistances.end(); 3794d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DI != DIEnd; ++DI) { 3795d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor SpecifierInfoList &SpecList = DistanceMap[*DI]; 3796d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Specifiers.append(SpecList.begin(), SpecList.end()); 3797d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3798d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3799d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor isSorted = true; 3800d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 3801d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3802ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesunsigned 3803ef8225444452a1486bd721f3285301fe84643b00Stephen HinesTypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier( 3804ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContextList &DeclChain, NestedNameSpecifier *&NNS) { 38056e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain unsigned NumSpecifiers = 0; 38066e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain for (DeclContextList::reverse_iterator C = DeclChain.rbegin(), 38076e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain CEnd = DeclChain.rend(); 38086e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain C != CEnd; ++C) { 38096e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) { 38106e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain NNS = NestedNameSpecifier::Create(Context, NNS, ND); 38116e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain ++NumSpecifiers; 38126e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain } else if (RecordDecl *RD = dyn_cast_or_null<RecordDecl>(*C)) { 38136e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(), 38146e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain RD->getTypeForDecl()); 38156e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain ++NumSpecifiers; 38166e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain } 38176e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain } 38186e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain return NumSpecifiers; 38196e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain} 38206e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain 3821ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier( 3822ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContext *Ctx) { 38236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines NestedNameSpecifier *NNS = nullptr; 38243d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain unsigned NumSpecifiers = 0; 3825ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DeclContextList NamespaceDeclChain(buildContextChain(Ctx)); 38263d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain DeclContextList FullNamespaceDeclChain(NamespaceDeclChain); 38273d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 38283d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // Eliminate common elements from the two DeclContext chains. 38293d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(), 38303d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain CEnd = CurContextChain.rend(); 38313d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain C != CEnd && !NamespaceDeclChain.empty() && 38323d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain NamespaceDeclChain.back() == *C; ++C) { 38333d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain NamespaceDeclChain.pop_back(); 38343d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain } 38353d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 38366e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain 3837ef8225444452a1486bd721f3285301fe84643b00Stephen Hines NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS); 38386e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain 38393d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // Add an explicit leading '::' specifier if needed. 38403d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain if (NamespaceDeclChain.empty()) { 38416e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain // Rebuild the NestedNameSpecifier as a globally-qualified specifier. 38423d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain NNS = NestedNameSpecifier::GlobalSpecifier(Context); 38436e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain NumSpecifiers = 3844ef8225444452a1486bd721f3285301fe84643b00Stephen Hines buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); 3845fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain } else if (NamedDecl *ND = 3846fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) { 38473d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain IdentifierInfo *Name = ND->getIdentifier(); 38486e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain bool SameNameSpecifier = false; 38496e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain if (std::find(CurNameSpecifierIdentifiers.begin(), 38503d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain CurNameSpecifierIdentifiers.end(), 38513d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain Name) != CurNameSpecifierIdentifiers.end()) { 38526e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain std::string NewNameSpecifier; 38536e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier); 38546e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers; 38556e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); 38566e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain NNS->print(SpecifierOStream, Context.getPrintingPolicy()); 38576e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain SpecifierOStream.flush(); 38586e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain SameNameSpecifier = NewNameSpecifier == CurNameSpecifier; 38596e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain } 38606e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain if (SameNameSpecifier || 38616e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain std::find(CurContextIdentifiers.begin(), CurContextIdentifiers.end(), 38626e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain Name) != CurContextIdentifiers.end()) { 38636e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain // Rebuild the NestedNameSpecifier as a globally-qualified specifier. 38643d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain NNS = NestedNameSpecifier::GlobalSpecifier(Context); 38656e4f6f865010649bc3969e57436b5501544a0c39Kaelyn Uhrain NumSpecifiers = 3866ef8225444452a1486bd721f3285301fe84643b00Stephen Hines buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); 38673d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain } 38683d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain } 38693d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 38703d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // If the built NestedNameSpecifier would be replacing an existing 38713d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // NestedNameSpecifier, use the number of component identifiers that 38723d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // would need to be changed as the edit distance instead of the number 38733d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain // of components in the built NestedNameSpecifier. 38743d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain if (NNS && !CurNameSpecifierIdentifiers.empty()) { 38753d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers; 38763d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); 38773d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain NumSpecifiers = llvm::ComputeEditDistance( 38783d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain ArrayRef<const IdentifierInfo *>(CurNameSpecifierIdentifiers), 38793d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain ArrayRef<const IdentifierInfo *>(NewNameSpecifierIdentifiers)); 38803d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain } 38813d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 38823d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain isSorted = false; 38833d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain Distances.insert(NumSpecifiers); 3884ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SpecifierInfo SI = {Ctx, NNS, NumSpecifiers}; 3885ef8225444452a1486bd721f3285301fe84643b00Stephen Hines DistanceMap[NumSpecifiers].push_back(SI); 38863d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain} 38873d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 38889a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor/// \brief Perform name lookup for a possible result for typo correction. 38899a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregorstatic void LookupPotentialTypoResult(Sema &SemaRef, 38909a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor LookupResult &Res, 38919a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor IdentifierInfo *Name, 38929a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Scope *S, CXXScopeSpec *SS, 38939a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor DeclContext *MemberContext, 38949a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor bool EnteringContext, 3895d67679d7439bd17b06574781b908630f4640c662Richard Smith bool isObjCIvarLookup, 3896d67679d7439bd17b06574781b908630f4640c662Richard Smith bool FindHidden) { 38979a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.suppressDiagnostics(); 38989a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.clear(); 38999a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.setLookupName(Name); 3900d67679d7439bd17b06574781b908630f4640c662Richard Smith Res.setAllowHidden(FindHidden); 3901dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (MemberContext) { 39029a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { 39037d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain if (isObjCIvarLookup) { 39049a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { 39059a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.addDecl(Ivar); 39069a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.resolveKind(); 39079a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor return; 39089a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 39099a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 3910dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 39119a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(Name)) { 39129a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.addDecl(Prop); 39139a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.resolveKind(); 39149a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor return; 39159a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 39169a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 3917dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 39189a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor SemaRef.LookupQualifiedName(Res, MemberContext); 39199a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor return; 39209a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 3921dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 3922dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, 39239a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor EnteringContext); 3924dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 39259a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor // Fake ivar lookup; this should really be part of 39269a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor // LookupParsedName. 39279a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { 39289a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor if (Method->isInstanceMethod() && Method->getClassInterface() && 3929dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi (Res.empty() || 39309a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor (Res.isSingleResult() && 39319a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) { 3932dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (ObjCIvarDecl *IV 39339a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor = Method->getClassInterface()->lookupInstanceVariable(Name)) { 39349a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.addDecl(IV); 39359a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor Res.resolveKind(); 39369a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 39379a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 39389a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor } 39399a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor} 39409a632eaa0ee73e4db701a8df74e92909d1fa350eDouglas Gregor 3941d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// \brief Add keywords to the consumer as possible typo corrections. 3942d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorstatic void AddKeywordsToConsumer(Sema &SemaRef, 3943d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor TypoCorrectionConsumer &Consumer, 39440f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith Scope *S, CorrectionCandidateCallback &CCC, 39450f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith bool AfterNestedNameSpecifier) { 39460f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith if (AfterNestedNameSpecifier) { 39470f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith // For 'X::', we know exactly which keywords can appear next. 39480f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith Consumer.addKeywordResult("template"); 39490f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith if (CCC.WantExpressionKeywords) 39500f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith Consumer.addKeywordResult("operator"); 39510f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith return; 39520f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith } 39530f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith 39547d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain if (CCC.WantObjCSuper) 39557d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain Consumer.addKeywordResult("super"); 3956d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 39577d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain if (CCC.WantTypeSpecifiers) { 3958d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // Add type-specifier keywords to the set of results. 39593aa29df37b140f9c6786b6863a0cac195071b598Craig Topper static const char *const CTypeSpecs[] = { 3960d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor "char", "const", "double", "enum", "float", "int", "long", "short", 396107f4a06c402a2ccdd9eae7a3d710990b22040001Douglas Gregor "signed", "struct", "union", "unsigned", "void", "volatile", 3962d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor "_Complex", "_Imaginary", 3963d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // storage-specifiers as well 3964d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor "extern", "inline", "static", "typedef" 3965d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor }; 3966d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 3967b960232518a1cd79c5f64ab5ef54c88e34660191Craig Topper const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs); 3968d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor for (unsigned I = 0; I != NumCTypeSpecs; ++I) 3969d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult(CTypeSpecs[I]); 3970d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 39714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().C99) 3972d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("restrict"); 39734e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) 3974d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("bool"); 39754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie else if (SemaRef.getLangOpts().C99) 397607f4a06c402a2ccdd9eae7a3d710990b22040001Douglas Gregor Consumer.addKeywordResult("_Bool"); 397707f4a06c402a2ccdd9eae7a3d710990b22040001Douglas Gregor 39784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().CPlusPlus) { 3979d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("class"); 3980d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("typename"); 3981d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("wchar_t"); 3982d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 398380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (SemaRef.getLangOpts().CPlusPlus11) { 3984d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("char16_t"); 3985d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("char32_t"); 3986d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("constexpr"); 3987d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("decltype"); 3988d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("thread_local"); 3989d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3990d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3991d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 39924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().GNUMode) 3993d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("typeof"); 3994d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 3995d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 39964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) { 3997d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("const_cast"); 3998d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("dynamic_cast"); 3999d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("reinterpret_cast"); 4000d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("static_cast"); 4001d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4002d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 40037d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain if (CCC.WantExpressionKeywords) { 4004d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("sizeof"); 40054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) { 4006d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("false"); 4007d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("true"); 4008d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4009d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 40104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().CPlusPlus) { 40113aa29df37b140f9c6786b6863a0cac195071b598Craig Topper static const char *const CXXExprs[] = { 4012d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor "delete", "new", "operator", "throw", "typeid" 4013d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor }; 4014b960232518a1cd79c5f64ab5ef54c88e34660191Craig Topper const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs); 4015d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor for (unsigned I = 0; I != NumCXXExprs; ++I) 4016d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult(CXXExprs[I]); 4017d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4018d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (isa<CXXMethodDecl>(SemaRef.CurContext) && 4019d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance()) 4020d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("this"); 4021d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 402280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (SemaRef.getLangOpts().CPlusPlus11) { 4023d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("alignof"); 4024d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("nullptr"); 4025d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4026d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4027f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose 4028f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose if (SemaRef.getLangOpts().C11) { 4029f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose // FIXME: We should not suggest _Alignof if the alignof macro 4030f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose // is present. 4031f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose Consumer.addKeywordResult("_Alignof"); 4032f70a88612a4bc2de2872a518267b9b4891ab6431Jordan Rose } 4033d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4034d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 40357d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain if (CCC.WantRemainingKeywords) { 4036d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) { 4037d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // Statements. 40383aa29df37b140f9c6786b6863a0cac195071b598Craig Topper static const char *const CStmts[] = { 4039d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor "do", "else", "for", "goto", "if", "return", "switch", "while" }; 4040b960232518a1cd79c5f64ab5ef54c88e34660191Craig Topper const unsigned NumCStmts = llvm::array_lengthof(CStmts); 4041d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor for (unsigned I = 0; I != NumCStmts; ++I) 4042d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult(CStmts[I]); 4043d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 40444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().CPlusPlus) { 4045d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("catch"); 4046d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("try"); 4047d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4048d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4049d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (S && S->getBreakParent()) 4050d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("break"); 4051d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4052d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (S && S->getContinueParent()) 4053d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("continue"); 4054d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4055d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (!SemaRef.getCurFunction()->SwitchStack.empty()) { 4056d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("case"); 4057d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("default"); 4058d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4059d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } else { 40604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().CPlusPlus) { 4061d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("namespace"); 4062d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("template"); 4063d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4064d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4065d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (S && S->isClassScope()) { 4066d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("explicit"); 4067d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("friend"); 4068d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("mutable"); 4069d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("private"); 4070d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("protected"); 4071d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("public"); 4072d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("virtual"); 4073d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4074d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4075d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 40764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().CPlusPlus) { 4077d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("using"); 4078d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 407980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (SemaRef.getLangOpts().CPlusPlus11) 4080d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Consumer.addKeywordResult("static_assert"); 4081d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4082d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4083d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 4084d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4085d67679d7439bd17b06574781b908630f4640c662Richard Smith/// \brief Check whether the declarations found for a typo correction are 4086d67679d7439bd17b06574781b908630f4640c662Richard Smith/// visible, and if none of them are, convert the correction to an 'import 4087d67679d7439bd17b06574781b908630f4640c662Richard Smith/// a module' correction. 4088ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) { 4089d67679d7439bd17b06574781b908630f4640c662Richard Smith if (TC.begin() == TC.end()) 4090d67679d7439bd17b06574781b908630f4640c662Richard Smith return; 4091d67679d7439bd17b06574781b908630f4640c662Richard Smith 4092d67679d7439bd17b06574781b908630f4640c662Richard Smith TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end(); 4093d67679d7439bd17b06574781b908630f4640c662Richard Smith 4094d67679d7439bd17b06574781b908630f4640c662Richard Smith for (/**/; DI != DE; ++DI) 4095d67679d7439bd17b06574781b908630f4640c662Richard Smith if (!LookupResult::isVisible(SemaRef, *DI)) 4096d67679d7439bd17b06574781b908630f4640c662Richard Smith break; 4097d67679d7439bd17b06574781b908630f4640c662Richard Smith // Nothing to do if all decls are visible. 4098d67679d7439bd17b06574781b908630f4640c662Richard Smith if (DI == DE) 4099d67679d7439bd17b06574781b908630f4640c662Richard Smith return; 4100d67679d7439bd17b06574781b908630f4640c662Richard Smith 4101d67679d7439bd17b06574781b908630f4640c662Richard Smith llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI); 4102d67679d7439bd17b06574781b908630f4640c662Richard Smith bool AnyVisibleDecls = !NewDecls.empty(); 4103d67679d7439bd17b06574781b908630f4640c662Richard Smith 4104d67679d7439bd17b06574781b908630f4640c662Richard Smith for (/**/; DI != DE; ++DI) { 4105d67679d7439bd17b06574781b908630f4640c662Richard Smith NamedDecl *VisibleDecl = *DI; 4106d67679d7439bd17b06574781b908630f4640c662Richard Smith if (!LookupResult::isVisible(SemaRef, *DI)) 4107d67679d7439bd17b06574781b908630f4640c662Richard Smith VisibleDecl = findAcceptableDecl(SemaRef, *DI); 4108d67679d7439bd17b06574781b908630f4640c662Richard Smith 4109d67679d7439bd17b06574781b908630f4640c662Richard Smith if (VisibleDecl) { 4110d67679d7439bd17b06574781b908630f4640c662Richard Smith if (!AnyVisibleDecls) { 4111d67679d7439bd17b06574781b908630f4640c662Richard Smith // Found a visible decl, discard all hidden ones. 4112d67679d7439bd17b06574781b908630f4640c662Richard Smith AnyVisibleDecls = true; 4113d67679d7439bd17b06574781b908630f4640c662Richard Smith NewDecls.clear(); 4114d67679d7439bd17b06574781b908630f4640c662Richard Smith } 4115d67679d7439bd17b06574781b908630f4640c662Richard Smith NewDecls.push_back(VisibleDecl); 4116d67679d7439bd17b06574781b908630f4640c662Richard Smith } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate()) 4117d67679d7439bd17b06574781b908630f4640c662Richard Smith NewDecls.push_back(*DI); 4118d67679d7439bd17b06574781b908630f4640c662Richard Smith } 4119d67679d7439bd17b06574781b908630f4640c662Richard Smith 4120d67679d7439bd17b06574781b908630f4640c662Richard Smith if (NewDecls.empty()) 4121d67679d7439bd17b06574781b908630f4640c662Richard Smith TC = TypoCorrection(); 4122d67679d7439bd17b06574781b908630f4640c662Richard Smith else { 4123d67679d7439bd17b06574781b908630f4640c662Richard Smith TC.setCorrectionDecls(NewDecls); 4124d67679d7439bd17b06574781b908630f4640c662Richard Smith TC.setRequiresImport(!AnyVisibleDecls); 4125d67679d7439bd17b06574781b908630f4640c662Richard Smith } 4126d67679d7439bd17b06574781b908630f4640c662Richard Smith} 4127d67679d7439bd17b06574781b908630f4640c662Richard Smith 4128546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// \brief Try to "correct" a typo in the source code by finding 4129546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// visible declarations whose names are similar to the name that was 4130546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// present in the source code. 4131546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// 4132d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// \param TypoName the \c DeclarationNameInfo structure that contains 4133d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// the name that was present in the source code along with its location. 4134d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// 4135d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// \param LookupKind the name-lookup criteria used to search for the name. 4136546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// 4137546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// \param S the scope in which name lookup occurs. 4138546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// 4139546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// \param SS the nested-name-specifier that precedes the name we're 4140546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// looking for, if present. 4141546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor/// 41427d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain/// \param CCC A CorrectionCandidateCallback object that provides further 41437d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain/// validation of typo correction candidates. It also provides flags for 41447d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain/// determining the set of keywords permitted. 41457d5e6948e6a4e04ee67b607f931d90d3063579f2Kaelyn Uhrain/// 41462dcc01195b7850692b9e25c65f77978e6a5a69a5Douglas Gregor/// \param MemberContext if non-NULL, the context in which to look for 41472dcc01195b7850692b9e25c65f77978e6a5a69a5Douglas Gregor/// a member access expression. 41482dcc01195b7850692b9e25c65f77978e6a5a69a5Douglas Gregor/// 4149dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param EnteringContext whether we're entering the context described by 4150bb092bafa984e9fa05136b5cef40fd4374dea0f6Douglas Gregor/// the nested-name-specifier SS. 4151bb092bafa984e9fa05136b5cef40fd4374dea0f6Douglas Gregor/// 4152f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor/// \param OPT when non-NULL, the search for visible declarations will 4153f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor/// also walk the protocols in the qualified interfaces of \p OPT. 4154f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor/// 4155d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// \returns a \c TypoCorrection containing the corrected name if the typo 4156d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// along with information such as the \c NamedDecl where the corrected name 4157d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// was declared, and any additional \c NestedNameSpecifier needed to access 4158d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor/// it (C++ only). The \c TypoCorrection is empty if there is no correction. 4159d8bba9c15230d2b1b3893e272106aa79efc50251Douglas GregorTypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName, 4160d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Sema::LookupNameKind LookupKind, 4161d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Scope *S, CXXScopeSpec *SS, 416216e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain CorrectionCandidateCallback &CCC, 41636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CorrectTypoKind Mode, 4164d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DeclContext *MemberContext, 4165d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor bool EnteringContext, 41662b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain const ObjCObjectPointerType *OPT, 41672b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain bool RecordFailure) { 416870571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain // Always let the ExternalSource have the first chance at correction, even 416970571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain // if we would otherwise have given up. 417070571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain if (ExternalSource) { 417170571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain if (TypoCorrection Correction = ExternalSource->CorrectTypo( 417270571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain TypoName, LookupKind, S, SS, CCC, MemberContext, EnteringContext, OPT)) 417370571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain return Correction; 417470571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain } 417570571f43ab9ae399cd005eaee02f4ca1ecbc3a81Kaelyn Uhrain 41769bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking || 41779bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith DisableTypoCorrection) 4178d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4179dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 41804d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet // In Microsoft mode, don't perform typo correction in a template member 41814d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet // function dependent context because it interferes with the "lookup into 41824d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet // dependent bases of class templates" feature. 4183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (getLangOpts().MSVCCompat && CurContext->isDependentContext() && 41844d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet isa<CXXMethodDecl>(CurContext)) 41854d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet return TypoCorrection(); 41864d604d6f1d413864e7dad8e368ba9a69a3f478c7Francois Pichet 4187546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // We only attempt to correct typos for identifiers. 4188d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); 4189546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (!Typo) 4190d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4191546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 4192546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // If the scope specifier itself was invalid, don't try to correct 4193546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // typos. 4194546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (SS && SS->isInvalid()) 4195d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4196546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 4197546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // Never try to correct typos during template deduction or 4198546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor // instantiation. 4199546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor if (!ActiveTemplateInstantiations.empty()) 4200d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4201d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 420257f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis // Don't try to correct 'super'. 420357f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier()) 420457f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis return TypoCorrection(); 420557f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis 42062b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain // Abort if typo correction already failed for this specific typo. 42072b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo); 42082b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain if (locs != TypoCorrectionFailures.end() && 42096dfc04b4cc5db5f631243d0d8543d1dbc9cd58edAaron Ballman locs->second.count(TypoName.getLoc())) 42102b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return TypoCorrection(); 42112b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain 4212fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // Don't try to correct the identifier "vector" when in AltiVec mode. 4213fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // TODO: Figure out why typo correction misbehaves in this case, fix it, and 4214fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain // remove this workaround. 4215fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain if (getLangOpts().AltiVec && Typo->isStr("vector")) 4216fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain return TypoCorrection(); 4217fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain 42186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // If we're handling a missing symbol error, using modules, and the 42196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // special search all modules option is used, look for a missing import. 42206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if ((Mode == CTK_ErrorRecovery) && getLangOpts().Modules && 42216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines getLangOpts().ModulesSearchAll) { 42226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // The following has the side effect of loading the missing module. 42236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines getModuleLoader().lookupMissingImports(Typo->getName(), 42246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines TypoName.getLocStart()); 42256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines } 42266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 4227ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrectionConsumer Consumer(*this, TypoName, LookupKind, S, SS, CCC, 4228ef8225444452a1486bd721f3285301fe84643b00Stephen Hines MemberContext, EnteringContext); 42296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 423063aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain // If a callback object considers an empty typo correction candidate to be 423163aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain // viable, assume it does not do any actual validation of the candidates. 4232438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain TypoCorrection EmptyCorrection; 423363aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain bool ValidatingCallback = !isCandidateViable(CCC, EmptyCorrection); 4234438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain 4235aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor // Perform name lookup to find visible, similarly-named entities. 42363eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor bool IsUnqualifiedLookup = false; 42372d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain DeclContext *QualifiedDC = MemberContext; 4238f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (MemberContext) { 4239d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor LookupVisibleDecls(MemberContext, LookupKind, Consumer); 4240f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor 4241f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor // Look in qualified interfaces. 4242f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor if (OPT) { 4243651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (auto *I : OPT->quals()) 4244651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LookupVisibleDecls(I, LookupKind, Consumer); 4245f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } 4246f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor } else if (SS && SS->isSet()) { 42472d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain QualifiedDC = computeDeclContext(*SS, EnteringContext); 42482d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain if (!QualifiedDC) 4249d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4250dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 42513eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // Provide a stop gap for files that are just seriously broken. Trying 42523eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // to correct all typos can turn into a HUGE performance penalty, causing 42533eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // some files to take minutes to get rejected by the parser. 42543eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) 4255d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 42563eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor ++TyposCorrected; 42573eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor 42582d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain LookupVisibleDecls(QualifiedDC, LookupKind, Consumer); 4259546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } else { 42603eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor IsUnqualifiedLookup = true; 42613eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor UnqualifiedTyposCorrectedMap::iterator Cached 42623eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor = UnqualifiedTyposCorrected.find(Typo); 4263438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (Cached != UnqualifiedTyposCorrected.end()) { 4264438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // Add the cached value, unless it's a keyword or fails validation. In the 4265438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // keyword case, we'll end up adding the keyword below. 4266438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (Cached->second) { 4267438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (!Cached->second.isKeyword() && 426881e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov isCandidateViable(CCC, Cached->second)) { 426981e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov // Do not use correction that is unaccessible in the given scope. 42700ccadb2835d169e834ab9bb44bdb299fbf7de660Serge Pavlov NamedDecl *CorrectionDecl = Cached->second.getCorrectionDecl(); 427181e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov DeclarationNameInfo NameInfo(CorrectionDecl->getDeclName(), 427281e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov CorrectionDecl->getLocation()); 427381e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov LookupResult R(*this, NameInfo, LookupOrdinaryName); 427481e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov if (LookupName(R, S)) 427581e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov Consumer.addCorrection(Cached->second); 427681e34b1c137ffaa9b9c7d488744bdaa0cee8e845Serge Pavlov } 4277438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain } else { 4278438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // Only honor no-correction cache hits when a callback that will validate 4279438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // correction candidates is not being used. 4280438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (!ValidatingCallback) 4281438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain return TypoCorrection(); 4282438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain } 4283438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain } 4284438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (Cached == UnqualifiedTyposCorrected.end()) { 42853eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // Provide a stop gap for files that are just seriously broken. Trying 42863eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // to correct all typos can turn into a HUGE performance penalty, causing 42873eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // some files to take minutes to get rejected by the parser. 42883eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor if (TyposCorrected + UnqualifiedTyposCorrected.size() >= 20) 4289d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return TypoCorrection(); 4290438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain } 42912d4d7fd0ca37b61406dfe58acbefa8cf20ee050aKaelyn Uhrain } 4292dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 42930179868cce096101236261ff64eca30224b1f233Douglas Gregor // Determine whether we are going to search in the various namespaces for 42940179868cce096101236261ff64eca30224b1f233Douglas Gregor // corrections. 42950179868cce096101236261ff64eca30224b1f233Douglas Gregor bool SearchNamespaces 42966d858d96ea294baecdf3e7ad3f3c5395c00dec8eKaelyn Uhrain = getLangOpts().CPlusPlus && 4297b5c7768a74936d4e2c7a484570a638cb74702d8bKaelyn Uhrain (IsUnqualifiedLookup || (SS && SS->isSet())); 4298d67679d7439bd17b06574781b908630f4640c662Richard Smith // In a few cases we *only* want to search for corrections based on just 42998d3607bf22a6a70acce3722a758f02d4ea84fc3aKaelyn Uhrain // adding or changing the nested name specifier. 4300fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain unsigned TypoLen = Typo->getName().size(); 4301fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain bool AllowOnlyNNSChanges = TypoLen < 3; 4302fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain 43030179868cce096101236261ff64eca30224b1f233Douglas Gregor if (IsUnqualifiedLookup || SearchNamespaces) { 4304438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // For unqualified lookup, look through all of the names that we have 4305438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // seen in this translation unit. 4306bb3d9976fd0e037ec9864baeaf23dc138bcb6df0Kaelyn Uhrain // FIXME: Re-add the ability to skip very unlikely potential corrections. 4307438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain for (IdentifierTable::iterator I = Context.Idents.begin(), 4308438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain IEnd = Context.Idents.end(); 4309438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain I != IEnd; ++I) 4310438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain Consumer.FoundName(I->getKey()); 431195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor 4312438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // Walk through identifiers in external identifier sources. 4313bb3d9976fd0e037ec9864baeaf23dc138bcb6df0Kaelyn Uhrain // FIXME: Re-add the ability to skip very unlikely potential corrections. 4314438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (IdentifierInfoLookup *External 4315438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain = Context.Idents.getExternalIdentifierLookup()) { 4316651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); 4317438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain do { 4318438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain StringRef Name = Iter->Next(); 4319438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain if (Name.empty()) 4320438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain break; 4321dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4322438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain Consumer.FoundName(Name); 4323438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain } while (true); 432495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor } 4325546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor } 4326546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor 43270f4b5be4a3b3e1c18e611e5a5c262ef028e8320aRichard Smith AddKeywordsToConsumer(*this, Consumer, S, CCC, SS && SS->isNotEmpty()); 4328dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4329d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // If we haven't found anything, we're done. 43302b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain if (Consumer.empty()) 43312b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 43322b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain IsUnqualifiedLookup); 4333aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor 43348d3607bf22a6a70acce3722a758f02d4ea84fc3aKaelyn Uhrain // Make sure the best edit distance (prior to adding any namespace qualifiers) 43358d3607bf22a6a70acce3722a758f02d4ea84fc3aKaelyn Uhrain // is not more that about a third of the length of the typo's identifier. 433663aae82bb12bbbe9028e597fb77e40fa8d348c12Kaelyn Uhrain unsigned ED = Consumer.getBestEditDistance(true); 4337fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain if (ED > 0 && TypoLen / ED < 3) 43382b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 43392b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain IsUnqualifiedLookup); 4340dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 43410179868cce096101236261ff64eca30224b1f233Douglas Gregor // Build the NestedNameSpecifiers for the KnownNamespaces, if we're going 43420179868cce096101236261ff64eca30224b1f233Douglas Gregor // to search those namespaces. 43430179868cce096101236261ff64eca30224b1f233Douglas Gregor if (SearchNamespaces) { 4344d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // Load any externally-known namespaces. 4345d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (ExternalSource && !LoadedExternalKnownNamespaces) { 43465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces; 4347d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor LoadedExternalKnownNamespaces = true; 4348d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces); 4349d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor for (unsigned I = 0, N = ExternalKnownNamespaces.size(); I != N; ++I) 4350d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor KnownNamespaces[ExternalKnownNamespaces[I]] = true; 4351d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 43523d9559b91c00757b296354cc6ca93e899266c7d2Kaelyn Uhrain 4353ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Consumer.addNamespaces(KnownNamespaces); 4354aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor } 4355dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4356ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection BestTC = Consumer.getNextCorrection(); 4357ef8225444452a1486bd721f3285301fe84643b00Stephen Hines TypoCorrection SecondBestTC = Consumer.getNextCorrection(); 4358ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!BestTC) 43592b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4360d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4361ef8225444452a1486bd721f3285301fe84643b00Stephen Hines ED = BestTC.getEditDistance(); 436253e4b55d2c534cfd59021ff18349ac58979ce43fDouglas Gregor 4363fb61e462277751f519c14515b05799e62e2dcdfaKaelyn Uhrain if (!AllowOnlyNNSChanges && ED > 0 && TypoLen / ED < 3) { 4364438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // If this was an unqualified lookup and we believe the callback 4365438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // object wouldn't have filtered out possible corrections, note 4366438ee1fc5e5baaa204faede83cb999e45bb6b57eKaelyn Uhrain // that no correction was found. 43672b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 43682b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain IsUnqualifiedLookup && !ValidatingCallback); 43693eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor } 4370e24b575f06985336973bb721e198bd184bf7bdd3Douglas Gregor 4371e24b575f06985336973bb721e198bd184bf7bdd3Douglas Gregor // If only a single name remains, return that result. 4372ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!SecondBestTC || 4373ef8225444452a1486bd721f3285301fe84643b00Stephen Hines SecondBestTC.getEditDistance(false) > BestTC.getEditDistance(false)) { 4374ef8225444452a1486bd721f3285301fe84643b00Stephen Hines const TypoCorrection &Result = BestTC; 4375dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4376d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // Don't correct to a keyword that's the same as the typo; the keyword 4377d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // wasn't actually in scope. 43782b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain if (ED == 0 && Result.isKeyword()) 43792b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 43806eaac8b3e68177f56090ddb0fcfc8fbfd49150a6Douglas Gregor 43813eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // Record the correction for unqualified lookup. 43823eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor if (IsUnqualifiedLookup) 4383d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor UnqualifiedTyposCorrected[Typo] = Result; 4384dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 43856952c018318a8ce57e336d7ed2a4819a98182fa2David Blaikie TypoCorrection TC = Result; 43866952c018318a8ce57e336d7ed2a4819a98182fa2David Blaikie TC.setCorrectionRange(SS, TypoName); 4387ef8225444452a1486bd721f3285301fe84643b00Stephen Hines checkCorrectionVisibility(*this, TC); 43886952c018318a8ce57e336d7ed2a4819a98182fa2David Blaikie return TC; 43896eaac8b3e68177f56090ddb0fcfc8fbfd49150a6Douglas Gregor } 4390ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver; 4391ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // WantObjCSuper is only true for CTC_ObjCMessageReceiver and for 4392ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // some instances of CTC_Unknown, while WantRemainingKeywords is true 4393ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // for CTC_Unknown but not for CTC_ObjCMessageReceiver. 4394ef8225444452a1486bd721f3285301fe84643b00Stephen Hines else if (SecondBestTC && CCC.WantObjCSuper && !CCC.WantRemainingKeywords) { 4395d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor // Prefer 'super' when we're completing in a message-receiver 43967b824e854b8ffc0d46337d95425825015528f35cDouglas Gregor // context. 4397dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4398ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (BestTC.getCorrection().getAsString() != "super") { 4399ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (SecondBestTC.getCorrection().getAsString() == "super") 4400ef8225444452a1486bd721f3285301fe84643b00Stephen Hines BestTC = SecondBestTC; 4401ef8225444452a1486bd721f3285301fe84643b00Stephen Hines else if (Consumer["super"].front().isKeyword()) 4402ef8225444452a1486bd721f3285301fe84643b00Stephen Hines BestTC = Consumer["super"].front(); 4403ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 440453e4b55d2c534cfd59021ff18349ac58979ce43fDouglas Gregor // Don't correct to a keyword that's the same as the typo; the keyword 440553e4b55d2c534cfd59021ff18349ac58979ce43fDouglas Gregor // wasn't actually in scope. 4406ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (BestTC.getEditDistance() == 0 || 4407ef8225444452a1486bd721f3285301fe84643b00Stephen Hines BestTC.getCorrection().getAsString() != "super") 44082b17b472d2f503ee61c460ca23f226c2aebcdd62Kaelyn Uhrain return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); 4409dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 44103eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor // Record the correction for unqualified lookup. 44113eedbb06dd35b021bb614250585d4a6a5b7936e7Douglas Gregor if (IsUnqualifiedLookup) 4412ef8225444452a1486bd721f3285301fe84643b00Stephen Hines UnqualifiedTyposCorrected[Typo] = BestTC; 4413dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4414ef8225444452a1486bd721f3285301fe84643b00Stephen Hines BestTC.setCorrectionRange(SS, TypoName); 4415ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return BestTC; 44167b824e854b8ffc0d46337d95425825015528f35cDouglas Gregor } 4417dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4418ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // Record the failure's location if needed and return an empty correction. If 4419ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // this was an unqualified lookup and we believe the callback object did not 4420ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // filter out possible corrections, also cache the failure for the typo. 4421ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure, 4422ef8225444452a1486bd721f3285301fe84643b00Stephen Hines IsUnqualifiedLookup && !ValidatingCallback); 4423d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor} 4424d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4425f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrainvoid TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) { 4426f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain if (!CDecl) return; 4427f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain 4428f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain if (isKeyword()) 4429f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain CorrectionDecls.clear(); 4430f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain 4431728948fba8ce2249db0a6fd228d883f38434289aKaelyn Uhrain CorrectionDecls.push_back(CDecl->getUnderlyingDecl()); 4432f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain 4433f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain if (!CorrectionName) 4434f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain CorrectionName = CDecl->getDeclName(); 4435f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain} 4436f0c1d8f804e7854bedf3f241409185951ee67866Kaelyn Uhrain 4437d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorstd::string TypoCorrection::getAsString(const LangOptions &LO) const { 4438d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (CorrectionNameSpec) { 4439d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor std::string tmpBuffer; 4440d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor llvm::raw_string_ostream PrefixOStream(tmpBuffer); 4441d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO)); 444217828ca5857d5d9cadfffd339f888de58182c8f1David Blaikie PrefixOStream << CorrectionName; 444334f9dc46d3207f3d6e649082e61f7ec0226632daBenjamin Kramer return PrefixOStream.str(); 4444d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } 4445d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor 4446d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor return CorrectionName.getAsString(); 4447546be3c5c000626c8cdf65e32e8ed9b90c424eddDouglas Gregor} 444820a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain 444920a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrainbool CorrectionCandidateCallback::ValidateCandidate(const TypoCorrection &candidate) { 445020a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain if (!candidate.isResolved()) 445120a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain return true; 445220a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain 445320a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain if (candidate.isKeyword()) 445420a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts || 445520a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain WantRemainingKeywords || WantObjCSuper; 445620a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain 4457ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool HasNonType = false; 4458ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool HasStaticMethod = false; 4459ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool HasNonStaticMethod = false; 4460ef8225444452a1486bd721f3285301fe84643b00Stephen Hines for (Decl *D : candidate) { 4461ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) 4462ef8225444452a1486bd721f3285301fe84643b00Stephen Hines D = FTD->getTemplatedDecl(); 4463ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 4464ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (Method->isStatic()) 4465ef8225444452a1486bd721f3285301fe84643b00Stephen Hines HasStaticMethod = true; 4466ef8225444452a1486bd721f3285301fe84643b00Stephen Hines else 4467ef8225444452a1486bd721f3285301fe84643b00Stephen Hines HasNonStaticMethod = true; 4468ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 4469ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!isa<TypeDecl>(D)) 4470ef8225444452a1486bd721f3285301fe84643b00Stephen Hines HasNonType = true; 447120a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain } 447220a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain 4473ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (IsAddressOfOperand && HasNonStaticMethod && !HasStaticMethod && 4474ef8225444452a1486bd721f3285301fe84643b00Stephen Hines !candidate.getCorrectionSpecifier()) 4475ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return false; 4476ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 4477ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return WantTypeSpecifiers || HasNonType; 447820a7cf4d782e32b2752e651a11ed3712d2debd65Kaelyn Uhrain} 4479761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain 4480761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn UhrainFunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs, 4481651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool HasExplicitTemplateArgs, 44826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines MemberExpr *ME) 4483651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs), 44846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CurContext(SemaRef.CurContext), MemberFn(ME) { 4485761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain WantTypeSpecifiers = SemaRef.getLangOpts().CPlusPlus; 4486761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain WantRemainingKeywords = false; 4487761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain} 4488761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain 4489761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrainbool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) { 4490761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (!candidate.getCorrectionDecl()) 4491761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain return candidate.isKeyword(); 4492761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain 4493761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain for (TypoCorrection::const_decl_iterator DI = candidate.begin(), 4494761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain DIEnd = candidate.end(); 4495761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain DI != DIEnd; ++DI) { 44966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines FunctionDecl *FD = nullptr; 4497761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain NamedDecl *ND = (*DI)->getUnderlyingDecl(); 4498761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 4499761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain FD = FTD->getTemplatedDecl(); 4500761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (!HasExplicitTemplateArgs && !FD) { 4501761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { 4502761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain // If the Decl is neither a function nor a template function, 4503761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain // determine if it is a pointer or reference to a function. If so, 4504761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain // check against the number of arguments expected for the pointee. 4505761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain QualType ValType = cast<ValueDecl>(ND)->getType(); 4506761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (ValType->isAnyPointerType() || ValType->isReferenceType()) 4507761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain ValType = ValType->getPointeeType(); 4508761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) 4509651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (FPT->getNumParams() == NumArgs) 4510761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain return true; 4511761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain } 4512761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain } 4513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 4514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Skip the current candidate if it is not a FunctionDecl or does not accept 4515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // the current number of arguments. 4516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!FD || !(FD->getNumParams() >= NumArgs && 4517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines FD->getMinRequiredArguments() <= NumArgs)) 4518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines continue; 4519651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 45206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // If the current candidate is a non-static C++ method, skip the candidate 45216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // unless the method being corrected--or the current DeclContext, if the 45226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // function being corrected is not a method--is a method in the same class 45236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // or a descendent class of the candidate's parent class. 4524651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 45256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (MemberFn || !MD->isStatic()) { 45266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CXXMethodDecl *CurMD = 45276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines MemberFn 45286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ? dyn_cast_or_null<CXXMethodDecl>(MemberFn->getMemberDecl()) 45296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines : dyn_cast_or_null<CXXMethodDecl>(CurContext); 4530651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CXXRecordDecl *CurRD = 45316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr; 4532651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl(); 4533651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD))) 4534651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines continue; 4535651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 4536651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 4537651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return true; 4538761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain } 4539761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain return false; 4540761695fec3e4fe5aaae1544d489389bcf6cd9be4Kaelyn Uhrain} 45412d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith 45422d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smithvoid Sema::diagnoseTypo(const TypoCorrection &Correction, 45432d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith const PartialDiagnostic &TypoDiag, 45442d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith bool ErrorRecovery) { 45452d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl), 45462d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith ErrorRecovery); 45472d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith} 45482d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith 4549d67679d7439bd17b06574781b908630f4640c662Richard Smith/// Find which declaration we should import to provide the definition of 4550d67679d7439bd17b06574781b908630f4640c662Richard Smith/// the given declaration. 4551d67679d7439bd17b06574781b908630f4640c662Richard Smithstatic const NamedDecl *getDefinitionToImport(const NamedDecl *D) { 4552d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 4553d67679d7439bd17b06574781b908630f4640c662Richard Smith return VD->getDefinition(); 4554d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 45556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return FD->isDefined(FD) ? FD : nullptr; 4556d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const TagDecl *TD = dyn_cast<TagDecl>(D)) 4557d67679d7439bd17b06574781b908630f4640c662Richard Smith return TD->getDefinition(); 4558d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 4559d67679d7439bd17b06574781b908630f4640c662Richard Smith return ID->getDefinition(); 4560d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 4561d67679d7439bd17b06574781b908630f4640c662Richard Smith return PD->getDefinition(); 4562d67679d7439bd17b06574781b908630f4640c662Richard Smith if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) 4563d67679d7439bd17b06574781b908630f4640c662Richard Smith return getDefinitionToImport(TD->getTemplatedDecl()); 45646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 4565d67679d7439bd17b06574781b908630f4640c662Richard Smith} 4566d67679d7439bd17b06574781b908630f4640c662Richard Smith 45672d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// \brief Diagnose a successfully-corrected typo. Separated from the correction 45682d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// itself to allow external validation of the result, etc. 45692d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// 45702d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// \param Correction The result of performing typo correction. 45712d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// \param TypoDiag The diagnostic to produce. This will have the corrected 45722d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// string added to it (and usually also a fixit). 45732d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// \param PrevNote A note to use when indicating the location of the entity to 45742d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// which we are correcting. Will have the correction string added to it. 45752d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// \param ErrorRecovery If \c true (the default), the caller is going to 45762d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// recover from the typo as if the corrected string had been typed. 45772d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// In this case, \c PDiag must be an error, and we will attach a fixit 45782d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith/// to it. 45792d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smithvoid Sema::diagnoseTypo(const TypoCorrection &Correction, 45802d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith const PartialDiagnostic &TypoDiag, 45812d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith const PartialDiagnostic &PrevNote, 45822d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith bool ErrorRecovery) { 45832d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith std::string CorrectedStr = Correction.getAsString(getLangOpts()); 45842d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts()); 45852d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith FixItHint FixTypo = FixItHint::CreateReplacement( 45862d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith Correction.getCorrectionRange(), CorrectedStr); 45872d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith 4588d67679d7439bd17b06574781b908630f4640c662Richard Smith // Maybe we're just missing a module import. 4589d67679d7439bd17b06574781b908630f4640c662Richard Smith if (Correction.requiresImport()) { 4590d67679d7439bd17b06574781b908630f4640c662Richard Smith NamedDecl *Decl = Correction.getCorrectionDecl(); 4591d67679d7439bd17b06574781b908630f4640c662Richard Smith assert(Decl && "import required but no declaration to import"); 4592d67679d7439bd17b06574781b908630f4640c662Richard Smith 4593d67679d7439bd17b06574781b908630f4640c662Richard Smith // Suggest importing a module providing the definition of this entity, if 4594d67679d7439bd17b06574781b908630f4640c662Richard Smith // possible. 4595d67679d7439bd17b06574781b908630f4640c662Richard Smith const NamedDecl *Def = getDefinitionToImport(Decl); 4596d67679d7439bd17b06574781b908630f4640c662Richard Smith if (!Def) 4597d67679d7439bd17b06574781b908630f4640c662Richard Smith Def = Decl; 4598d67679d7439bd17b06574781b908630f4640c662Richard Smith Module *Owner = Def->getOwningModule(); 4599d67679d7439bd17b06574781b908630f4640c662Richard Smith assert(Owner && "definition of hidden declaration is not in a module"); 4600d67679d7439bd17b06574781b908630f4640c662Richard Smith 4601d67679d7439bd17b06574781b908630f4640c662Richard Smith Diag(Correction.getCorrectionRange().getBegin(), 4602d67679d7439bd17b06574781b908630f4640c662Richard Smith diag::err_module_private_declaration) 4603d67679d7439bd17b06574781b908630f4640c662Richard Smith << Def << Owner->getFullModuleName(); 4604d67679d7439bd17b06574781b908630f4640c662Richard Smith Diag(Def->getLocation(), diag::note_previous_declaration); 4605d67679d7439bd17b06574781b908630f4640c662Richard Smith 4606d67679d7439bd17b06574781b908630f4640c662Richard Smith // Recover by implicitly importing this module. 46076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (ErrorRecovery) 46086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines createImplicitModuleImportForErrorRecovery( 46096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Correction.getCorrectionRange().getBegin(), Owner); 4610d67679d7439bd17b06574781b908630f4640c662Richard Smith return; 4611d67679d7439bd17b06574781b908630f4640c662Richard Smith } 4612d67679d7439bd17b06574781b908630f4640c662Richard Smith 46132d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith Diag(Correction.getCorrectionRange().getBegin(), TypoDiag) 46142d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith << CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint()); 46152d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith 46162d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith NamedDecl *ChosenDecl = 46176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Correction.isKeyword() ? nullptr : Correction.getCorrectionDecl(); 46182d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith if (PrevNote.getDiagID() && ChosenDecl) 46192d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith Diag(ChosenDecl->getLocation(), PrevNote) 46202d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo); 46212d67097ad41f4c2fe82ebce3f587e06498f1bd71Richard Smith} 4622