Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1 | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements name lookup for C, C++, Objective-C, and |
| 11 | // Objective-C++. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "Sema.h" |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "clang/Basic/LangOptions.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 25 | #include <set> |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <iterator> |
| 28 | #include <utility> |
| 29 | #include <algorithm> |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 33 | typedef llvm::SmallVector<UsingDirectiveDecl*, 4> UsingDirectivesTy; |
| 34 | typedef llvm::DenseSet<NamespaceDecl*> NamespaceSet; |
| 35 | typedef llvm::SmallVector<Sema::LookupResult, 3> LookupResultsTy; |
| 36 | |
| 37 | /// UsingDirAncestorCompare - Implements strict weak ordering of |
| 38 | /// UsingDirectives. It orders them by address of its common ancestor. |
| 39 | struct UsingDirAncestorCompare { |
| 40 | |
| 41 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 42 | bool operator () (UsingDirectiveDecl *U, const DeclContext *Ctx) const { |
| 43 | return U->getCommonAncestor() < Ctx; |
| 44 | } |
| 45 | |
| 46 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 47 | bool operator () (const DeclContext *Ctx, UsingDirectiveDecl *U) const { |
| 48 | return Ctx < U->getCommonAncestor(); |
| 49 | } |
| 50 | |
| 51 | /// @brief Compares UsingDirectiveDecl common ancestors. |
| 52 | bool operator () (UsingDirectiveDecl *U1, UsingDirectiveDecl *U2) const { |
| 53 | return U1->getCommonAncestor() < U2->getCommonAncestor(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | /// AddNamespaceUsingDirectives - Adds all UsingDirectiveDecl's to heap UDirs |
| 58 | /// (ordered by common ancestors), found in namespace NS, |
| 59 | /// including all found (recursively) in their nominated namespaces. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 60 | void AddNamespaceUsingDirectives(ASTContext &Context, |
| 61 | DeclContext *NS, |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 62 | UsingDirectivesTy &UDirs, |
| 63 | NamespaceSet &Visited) { |
| 64 | DeclContext::udir_iterator I, End; |
| 65 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 66 | for (llvm::tie(I, End) = NS->getUsingDirectives(Context); I !=End; ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 67 | UDirs.push_back(*I); |
| 68 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 69 | NamespaceDecl *Nominated = (*I)->getNominatedNamespace(); |
| 70 | if (Visited.insert(Nominated).second) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 71 | AddNamespaceUsingDirectives(Context, Nominated, UDirs, /*ref*/ Visited); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | /// AddScopeUsingDirectives - Adds all UsingDirectiveDecl's found in Scope S, |
| 76 | /// including all found in the namespaces they nominate. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 77 | static void AddScopeUsingDirectives(ASTContext &Context, Scope *S, |
| 78 | UsingDirectivesTy &UDirs) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 79 | NamespaceSet VisitedNS; |
| 80 | |
| 81 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 82 | |
| 83 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 84 | VisitedNS.insert(NS); |
| 85 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 86 | AddNamespaceUsingDirectives(Context, Ctx, UDirs, /*ref*/ VisitedNS); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 87 | |
| 88 | } else { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 89 | Scope::udir_iterator I = S->using_directives_begin(), |
| 90 | End = S->using_directives_end(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 91 | |
| 92 | for (; I != End; ++I) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 93 | UsingDirectiveDecl *UD = I->getAs<UsingDirectiveDecl>(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 94 | UDirs.push_back(UD); |
| 95 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 96 | |
| 97 | NamespaceDecl *Nominated = UD->getNominatedNamespace(); |
| 98 | if (!VisitedNS.count(Nominated)) { |
| 99 | VisitedNS.insert(Nominated); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 100 | AddNamespaceUsingDirectives(Context, Nominated, UDirs, |
| 101 | /*ref*/ VisitedNS); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 107 | /// MaybeConstructOverloadSet - Name lookup has determined that the |
| 108 | /// elements in [I, IEnd) have the name that we are looking for, and |
| 109 | /// *I is a match for the namespace. This routine returns an |
| 110 | /// appropriate Decl for name lookup, which may either be *I or an |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 111 | /// OverloadedFunctionDecl that represents the overloaded functions in |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 112 | /// [I, IEnd). |
| 113 | /// |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 114 | /// The existance of this routine is temporary; users of LookupResult |
| 115 | /// should be able to handle multiple results, to deal with cases of |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 116 | /// ambiguity and overloaded functions without needing to create a |
| 117 | /// Decl node. |
| 118 | template<typename DeclIterator> |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 119 | static NamedDecl * |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 120 | MaybeConstructOverloadSet(ASTContext &Context, |
| 121 | DeclIterator I, DeclIterator IEnd) { |
| 122 | assert(I != IEnd && "Iterator range cannot be empty"); |
| 123 | assert(!isa<OverloadedFunctionDecl>(*I) && |
| 124 | "Cannot have an overloaded function"); |
| 125 | |
| 126 | if (isa<FunctionDecl>(*I)) { |
| 127 | // If we found a function, there might be more functions. If |
| 128 | // so, collect them into an overload set. |
| 129 | DeclIterator Last = I; |
| 130 | OverloadedFunctionDecl *Ovl = 0; |
| 131 | for (++Last; Last != IEnd && isa<FunctionDecl>(*Last); ++Last) { |
| 132 | if (!Ovl) { |
| 133 | // FIXME: We leak this overload set. Eventually, we want to |
| 134 | // stop building the declarations for these overload sets, so |
| 135 | // there will be nothing to leak. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 136 | Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(), |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 137 | (*I)->getDeclName()); |
| 138 | Ovl->addOverload(cast<FunctionDecl>(*I)); |
| 139 | } |
| 140 | Ovl->addOverload(cast<FunctionDecl>(*Last)); |
| 141 | } |
| 142 | |
| 143 | // If we had more than one function, we built an overload |
| 144 | // set. Return it. |
| 145 | if (Ovl) |
| 146 | return Ovl; |
| 147 | } |
| 148 | |
| 149 | return *I; |
| 150 | } |
| 151 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 152 | /// Merges together multiple LookupResults dealing with duplicated Decl's. |
| 153 | static Sema::LookupResult |
| 154 | MergeLookupResults(ASTContext &Context, LookupResultsTy &Results) { |
| 155 | typedef Sema::LookupResult LResult; |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 156 | typedef llvm::SmallPtrSet<NamedDecl*, 4> DeclsSetTy; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 157 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 158 | // Remove duplicated Decl pointing at same Decl, by storing them in |
| 159 | // associative collection. This might be case for code like: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 160 | // |
| 161 | // namespace A { int i; } |
| 162 | // namespace B { using namespace A; } |
| 163 | // namespace C { using namespace A; } |
| 164 | // |
| 165 | // void foo() { |
| 166 | // using namespace B; |
| 167 | // using namespace C; |
| 168 | // ++i; // finds A::i, from both namespace B and C at global scope |
| 169 | // } |
| 170 | // |
| 171 | // C++ [namespace.qual].p3: |
| 172 | // The same declaration found more than once is not an ambiguity |
| 173 | // (because it is still a unique declaration). |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 174 | DeclsSetTy FoundDecls; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 176 | // Counter of tag names, and functions for resolving ambiguity |
| 177 | // and name hiding. |
| 178 | std::size_t TagNames = 0, Functions = 0, OrdinaryNonFunc = 0; |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 180 | LookupResultsTy::iterator I = Results.begin(), End = Results.end(); |
| 181 | |
| 182 | // No name lookup results, return early. |
| 183 | if (I == End) return LResult::CreateLookupResult(Context, 0); |
| 184 | |
| 185 | // Keep track of the tag declaration we found. We only use this if |
| 186 | // we find a single tag declaration. |
| 187 | TagDecl *TagFound = 0; |
| 188 | |
| 189 | for (; I != End; ++I) { |
| 190 | switch (I->getKind()) { |
| 191 | case LResult::NotFound: |
| 192 | assert(false && |
| 193 | "Should be always successful name lookup result here."); |
| 194 | break; |
| 195 | |
| 196 | case LResult::AmbiguousReference: |
| 197 | case LResult::AmbiguousBaseSubobjectTypes: |
| 198 | case LResult::AmbiguousBaseSubobjects: |
| 199 | assert(false && "Shouldn't get ambiguous lookup here."); |
| 200 | break; |
| 201 | |
| 202 | case LResult::Found: { |
| 203 | NamedDecl *ND = I->getAsDecl(); |
| 204 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) { |
| 205 | TagFound = Context.getCanonicalDecl(TD); |
| 206 | TagNames += FoundDecls.insert(TagFound)? 1 : 0; |
| 207 | } else if (isa<FunctionDecl>(ND)) |
| 208 | Functions += FoundDecls.insert(ND)? 1 : 0; |
| 209 | else |
| 210 | FoundDecls.insert(ND); |
| 211 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 214 | case LResult::FoundOverloaded: |
| 215 | for (LResult::iterator FI = I->begin(), FEnd = I->end(); FI != FEnd; ++FI) |
| 216 | Functions += FoundDecls.insert(*FI)? 1 : 0; |
| 217 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 218 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 219 | } |
| 220 | OrdinaryNonFunc = FoundDecls.size() - TagNames - Functions; |
| 221 | bool Ambiguous = false, NameHidesTags = false; |
| 222 | |
| 223 | if (FoundDecls.size() == 1) { |
| 224 | // 1) Exactly one result. |
| 225 | } else if (TagNames > 1) { |
| 226 | // 2) Multiple tag names (even though they may be hidden by an |
| 227 | // object name). |
| 228 | Ambiguous = true; |
| 229 | } else if (FoundDecls.size() - TagNames == 1) { |
| 230 | // 3) Ordinary name hides (optional) tag. |
| 231 | NameHidesTags = TagFound; |
| 232 | } else if (Functions) { |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 233 | // C++ [basic.lookup].p1: |
| 234 | // ... Name lookup may associate more than one declaration with |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 235 | // a name if it finds the name to be a function name; the declarations |
| 236 | // are said to form a set of overloaded functions (13.1). |
| 237 | // Overload resolution (13.3) takes place after name lookup has succeeded. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 238 | // |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 239 | if (!OrdinaryNonFunc) { |
| 240 | // 4) Functions hide tag names. |
| 241 | NameHidesTags = TagFound; |
| 242 | } else { |
| 243 | // 5) Functions + ordinary names. |
| 244 | Ambiguous = true; |
| 245 | } |
| 246 | } else { |
| 247 | // 6) Multiple non-tag names |
| 248 | Ambiguous = true; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 251 | if (Ambiguous) |
| 252 | return LResult::CreateLookupResult(Context, |
| 253 | FoundDecls.begin(), FoundDecls.size()); |
| 254 | if (NameHidesTags) { |
| 255 | // There's only one tag, TagFound. Remove it. |
| 256 | assert(TagFound && FoundDecls.count(TagFound) && "No tag name found?"); |
| 257 | FoundDecls.erase(TagFound); |
| 258 | } |
| 259 | |
| 260 | // Return successful name lookup result. |
| 261 | return LResult::CreateLookupResult(Context, |
| 262 | MaybeConstructOverloadSet(Context, |
| 263 | FoundDecls.begin(), |
| 264 | FoundDecls.end())); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Retrieve the set of identifier namespaces that correspond to a |
| 268 | // specific kind of name lookup. |
| 269 | inline unsigned |
| 270 | getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind, |
| 271 | bool CPlusPlus) { |
| 272 | unsigned IDNS = 0; |
| 273 | switch (NameKind) { |
| 274 | case Sema::LookupOrdinaryName: |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 275 | case Sema::LookupOperatorName: |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 276 | case Sema::LookupRedeclarationWithLinkage: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 277 | IDNS = Decl::IDNS_Ordinary; |
| 278 | if (CPlusPlus) |
| 279 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
| 280 | break; |
| 281 | |
| 282 | case Sema::LookupTagName: |
| 283 | IDNS = Decl::IDNS_Tag; |
| 284 | break; |
| 285 | |
| 286 | case Sema::LookupMemberName: |
| 287 | IDNS = Decl::IDNS_Member; |
| 288 | if (CPlusPlus) |
| 289 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
| 290 | break; |
| 291 | |
| 292 | case Sema::LookupNestedNameSpecifierName: |
| 293 | case Sema::LookupNamespaceName: |
| 294 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 295 | break; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame^] | 296 | |
| 297 | case Sema::LookupProtocolName: |
| 298 | IDNS = Decl::IDNS_Protocol; |
| 299 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 300 | } |
| 301 | return IDNS; |
| 302 | } |
| 303 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 304 | Sema::LookupResult |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 305 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, NamedDecl *D) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 306 | LookupResult Result; |
| 307 | Result.StoredKind = (D && isa<OverloadedFunctionDecl>(D))? |
| 308 | OverloadedDeclSingleDecl : SingleDecl; |
| 309 | Result.First = reinterpret_cast<uintptr_t>(D); |
| 310 | Result.Last = 0; |
| 311 | Result.Context = &Context; |
| 312 | return Result; |
| 313 | } |
| 314 | |
Douglas Gregor | 6beddfe | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 315 | /// @brief Moves the name-lookup results from Other to this LookupResult. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 316 | Sema::LookupResult |
| 317 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 318 | IdentifierResolver::iterator F, |
| 319 | IdentifierResolver::iterator L) { |
| 320 | LookupResult Result; |
| 321 | Result.Context = &Context; |
| 322 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 323 | if (F != L && isa<FunctionDecl>(*F)) { |
| 324 | IdentifierResolver::iterator Next = F; |
| 325 | ++Next; |
| 326 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 327 | Result.StoredKind = OverloadedDeclFromIdResolver; |
| 328 | Result.First = F.getAsOpaqueValue(); |
| 329 | Result.Last = L.getAsOpaqueValue(); |
| 330 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 334 | Result.StoredKind = SingleDecl; |
| 335 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 336 | Result.Last = 0; |
| 337 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 340 | Sema::LookupResult |
| 341 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 342 | DeclContext::lookup_iterator F, |
| 343 | DeclContext::lookup_iterator L) { |
| 344 | LookupResult Result; |
| 345 | Result.Context = &Context; |
| 346 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 347 | if (F != L && isa<FunctionDecl>(*F)) { |
| 348 | DeclContext::lookup_iterator Next = F; |
| 349 | ++Next; |
| 350 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 351 | Result.StoredKind = OverloadedDeclFromDeclContext; |
| 352 | Result.First = reinterpret_cast<uintptr_t>(F); |
| 353 | Result.Last = reinterpret_cast<uintptr_t>(L); |
| 354 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 358 | Result.StoredKind = SingleDecl; |
| 359 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 360 | Result.Last = 0; |
| 361 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 364 | /// @brief Determine the result of name lookup. |
| 365 | Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const { |
| 366 | switch (StoredKind) { |
| 367 | case SingleDecl: |
| 368 | return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound; |
| 369 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 370 | case OverloadedDeclSingleDecl: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 371 | case OverloadedDeclFromIdResolver: |
| 372 | case OverloadedDeclFromDeclContext: |
| 373 | return FoundOverloaded; |
| 374 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 375 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 376 | return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 377 | |
| 378 | case AmbiguousLookupStoresDecls: |
| 379 | return AmbiguousReference; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 382 | // We can't ever get here. |
| 383 | return NotFound; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /// @brief Converts the result of name lookup into a single (possible |
| 387 | /// NULL) pointer to a declaration. |
| 388 | /// |
| 389 | /// The resulting declaration will either be the declaration we found |
| 390 | /// (if only a single declaration was found), an |
| 391 | /// OverloadedFunctionDecl (if an overloaded function was found), or |
| 392 | /// NULL (if no declaration was found). This conversion must not be |
| 393 | /// used anywhere where name lookup could result in an ambiguity. |
| 394 | /// |
| 395 | /// The OverloadedFunctionDecl conversion is meant as a stop-gap |
| 396 | /// solution, since it causes the OverloadedFunctionDecl to be |
| 397 | /// leaked. FIXME: Eventually, there will be a better way to iterate |
| 398 | /// over the set of overloaded functions returned by name lookup. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 399 | NamedDecl *Sema::LookupResult::getAsDecl() const { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 400 | switch (StoredKind) { |
| 401 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 402 | return reinterpret_cast<NamedDecl *>(First); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 403 | |
| 404 | case OverloadedDeclFromIdResolver: |
| 405 | return MaybeConstructOverloadSet(*Context, |
| 406 | IdentifierResolver::iterator::getFromOpaqueValue(First), |
| 407 | IdentifierResolver::iterator::getFromOpaqueValue(Last)); |
| 408 | |
| 409 | case OverloadedDeclFromDeclContext: |
| 410 | return MaybeConstructOverloadSet(*Context, |
| 411 | reinterpret_cast<DeclContext::lookup_iterator>(First), |
| 412 | reinterpret_cast<DeclContext::lookup_iterator>(Last)); |
| 413 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 414 | case OverloadedDeclSingleDecl: |
| 415 | return reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 416 | |
| 417 | case AmbiguousLookupStoresDecls: |
| 418 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 419 | assert(false && |
| 420 | "Name lookup returned an ambiguity that could not be handled"); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 427 | /// @brief Retrieves the BasePaths structure describing an ambiguous |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 428 | /// name lookup, or null. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 429 | BasePaths *Sema::LookupResult::getBasePaths() const { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 430 | if (StoredKind == AmbiguousLookupStoresBasePaths) |
| 431 | return reinterpret_cast<BasePaths *>(First); |
| 432 | return 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 435 | Sema::LookupResult::iterator::reference |
| 436 | Sema::LookupResult::iterator::operator*() const { |
| 437 | switch (Result->StoredKind) { |
| 438 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 439 | return reinterpret_cast<NamedDecl*>(Current); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 441 | case OverloadedDeclSingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 442 | return *reinterpret_cast<NamedDecl**>(Current); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 444 | case OverloadedDeclFromIdResolver: |
| 445 | return *IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 446 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 447 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 448 | if (Result->Last) |
| 449 | return *reinterpret_cast<NamedDecl**>(Current); |
| 450 | |
| 451 | // Fall through to handle the DeclContext::lookup_iterator we're |
| 452 | // storing. |
| 453 | |
| 454 | case OverloadedDeclFromDeclContext: |
| 455 | case AmbiguousLookupStoresDecls: |
| 456 | return *reinterpret_cast<DeclContext::lookup_iterator>(Current); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | Sema::LookupResult::iterator& Sema::LookupResult::iterator::operator++() { |
| 463 | switch (Result->StoredKind) { |
| 464 | case SingleDecl: |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 465 | Current = reinterpret_cast<uintptr_t>((NamedDecl*)0); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 466 | break; |
| 467 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 468 | case OverloadedDeclSingleDecl: { |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 469 | NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 470 | ++I; |
| 471 | Current = reinterpret_cast<uintptr_t>(I); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 472 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 475 | case OverloadedDeclFromIdResolver: { |
| 476 | IdentifierResolver::iterator I |
| 477 | = IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 478 | ++I; |
| 479 | Current = I.getAsOpaqueValue(); |
| 480 | break; |
| 481 | } |
| 482 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 483 | case AmbiguousLookupStoresBasePaths: |
| 484 | if (Result->Last) { |
| 485 | NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current); |
| 486 | ++I; |
| 487 | Current = reinterpret_cast<uintptr_t>(I); |
| 488 | break; |
| 489 | } |
| 490 | // Fall through to handle the DeclContext::lookup_iterator we're |
| 491 | // storing. |
| 492 | |
| 493 | case OverloadedDeclFromDeclContext: |
| 494 | case AmbiguousLookupStoresDecls: { |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 495 | DeclContext::lookup_iterator I |
| 496 | = reinterpret_cast<DeclContext::lookup_iterator>(Current); |
| 497 | ++I; |
| 498 | Current = reinterpret_cast<uintptr_t>(I); |
| 499 | break; |
| 500 | } |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return *this; |
| 504 | } |
| 505 | |
| 506 | Sema::LookupResult::iterator Sema::LookupResult::begin() { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 507 | switch (StoredKind) { |
| 508 | case SingleDecl: |
| 509 | case OverloadedDeclFromIdResolver: |
| 510 | case OverloadedDeclFromDeclContext: |
| 511 | case AmbiguousLookupStoresDecls: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 512 | return iterator(this, First); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 513 | |
| 514 | case OverloadedDeclSingleDecl: { |
| 515 | OverloadedFunctionDecl * Ovl = |
| 516 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 517 | return iterator(this, |
| 518 | reinterpret_cast<uintptr_t>(&(*Ovl->function_begin()))); |
| 519 | } |
| 520 | |
| 521 | case AmbiguousLookupStoresBasePaths: |
| 522 | if (Last) |
| 523 | return iterator(this, |
| 524 | reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_begin())); |
| 525 | else |
| 526 | return iterator(this, |
| 527 | reinterpret_cast<uintptr_t>(getBasePaths()->front().Decls.first)); |
| 528 | } |
| 529 | |
| 530 | // Required to suppress GCC warning. |
| 531 | return iterator(); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | Sema::LookupResult::iterator Sema::LookupResult::end() { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 535 | switch (StoredKind) { |
| 536 | case SingleDecl: |
| 537 | case OverloadedDeclFromIdResolver: |
| 538 | case OverloadedDeclFromDeclContext: |
| 539 | case AmbiguousLookupStoresDecls: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 540 | return iterator(this, Last); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 541 | |
| 542 | case OverloadedDeclSingleDecl: { |
| 543 | OverloadedFunctionDecl * Ovl = |
| 544 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 545 | return iterator(this, |
| 546 | reinterpret_cast<uintptr_t>(&(*Ovl->function_end()))); |
| 547 | } |
| 548 | |
| 549 | case AmbiguousLookupStoresBasePaths: |
| 550 | if (Last) |
| 551 | return iterator(this, |
| 552 | reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_end())); |
| 553 | else |
| 554 | return iterator(this, reinterpret_cast<uintptr_t>( |
| 555 | getBasePaths()->front().Decls.second)); |
| 556 | } |
| 557 | |
| 558 | // Required to suppress GCC warning. |
| 559 | return iterator(); |
| 560 | } |
| 561 | |
| 562 | void Sema::LookupResult::Destroy() { |
| 563 | if (BasePaths *Paths = getBasePaths()) |
| 564 | delete Paths; |
| 565 | else if (getKind() == AmbiguousReference) |
| 566 | delete[] reinterpret_cast<NamedDecl **>(First); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 569 | static void |
| 570 | CppNamespaceLookup(ASTContext &Context, DeclContext *NS, |
| 571 | DeclarationName Name, Sema::LookupNameKind NameKind, |
| 572 | unsigned IDNS, LookupResultsTy &Results, |
| 573 | UsingDirectivesTy *UDirs = 0) { |
| 574 | |
| 575 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!"); |
| 576 | |
| 577 | // Perform qualified name lookup into the LookupCtx. |
| 578 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 579 | for (llvm::tie(I, E) = NS->lookup(Context, Name); I != E; ++I) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 580 | if (Sema::isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 581 | Results.push_back(Sema::LookupResult::CreateLookupResult(Context, I, E)); |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | if (UDirs) { |
| 586 | // For each UsingDirectiveDecl, which common ancestor is equal |
| 587 | // to NS, we preform qualified name lookup into namespace nominated by it. |
| 588 | UsingDirectivesTy::const_iterator UI, UEnd; |
| 589 | llvm::tie(UI, UEnd) = |
| 590 | std::equal_range(UDirs->begin(), UDirs->end(), NS, |
| 591 | UsingDirAncestorCompare()); |
| 592 | |
| 593 | for (; UI != UEnd; ++UI) |
| 594 | CppNamespaceLookup(Context, (*UI)->getNominatedNamespace(), |
| 595 | Name, NameKind, IDNS, Results); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 600 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 601 | return Ctx->isFileContext(); |
| 602 | return false; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 603 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 605 | std::pair<bool, Sema::LookupResult> |
| 606 | Sema::CppLookupName(Scope *S, DeclarationName Name, |
| 607 | LookupNameKind NameKind, bool RedeclarationOnly) { |
| 608 | assert(getLangOptions().CPlusPlus && |
| 609 | "Can perform only C++ lookup"); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 610 | unsigned IDNS |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 611 | = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 612 | Scope *Initial = S; |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 613 | DeclContext *OutOfLineCtx = 0; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 614 | IdentifierResolver::iterator |
| 615 | I = IdResolver.begin(Name), |
| 616 | IEnd = IdResolver.end(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 618 | // First we lookup local scope. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 619 | // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 620 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 621 | // they were declared in the nearest enclosing namespace which contains |
| 622 | // both the using-directive and the nominated namespace. |
| 623 | // [Note: in this context, “contains” means “contains directly or |
| 624 | // indirectly”. |
| 625 | // |
| 626 | // For example: |
| 627 | // namespace A { int i; } |
| 628 | // void foo() { |
| 629 | // int i; |
| 630 | // { |
| 631 | // using namespace A; |
| 632 | // ++i; // finds local 'i', A::i appears at global scope |
| 633 | // } |
| 634 | // } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 635 | // |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 636 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 637 | // Check whether the IdResolver has anything in this scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 638 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 639 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 640 | // We found something. Look for anything else in our scope |
| 641 | // with this same name and in an acceptable identifier |
| 642 | // namespace, so that we can construct an overload set if we |
| 643 | // need to. |
| 644 | IdentifierResolver::iterator LastI = I; |
| 645 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 646 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | LookupResult Result = |
| 650 | LookupResult::CreateLookupResult(Context, I, LastI); |
| 651 | return std::make_pair(true, Result); |
| 652 | } |
| 653 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 654 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 655 | LookupResult R; |
| 656 | // Perform member lookup into struct. |
| 657 | // FIXME: In some cases, we know that every name that could be |
| 658 | // found by this qualified name lookup will also be on the |
| 659 | // identifier chain. For example, inside a class without any |
| 660 | // base classes, we never need to perform qualified lookup |
| 661 | // because all of the members are on top of the identifier |
| 662 | // chain. |
Douglas Gregor | d3c7859 | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 663 | if (isa<RecordDecl>(Ctx)) { |
| 664 | R = LookupQualifiedName(Ctx, Name, NameKind, RedeclarationOnly); |
| 665 | if (R || RedeclarationOnly) |
| 666 | return std::make_pair(true, R); |
| 667 | } |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 668 | if (Ctx->getParent() != Ctx->getLexicalParent()) { |
| 669 | // It is out of line defined C++ method or struct, we continue |
| 670 | // doing name lookup in parent context. Once we will find namespace |
| 671 | // or translation-unit we save it for possible checking |
| 672 | // using-directives later. |
| 673 | for (OutOfLineCtx = Ctx; OutOfLineCtx && !OutOfLineCtx->isFileContext(); |
| 674 | OutOfLineCtx = OutOfLineCtx->getParent()) { |
Douglas Gregor | d3c7859 | 2009-03-27 04:21:56 +0000 | [diff] [blame] | 675 | R = LookupQualifiedName(OutOfLineCtx, Name, NameKind, RedeclarationOnly); |
| 676 | if (R || RedeclarationOnly) |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 677 | return std::make_pair(true, R); |
| 678 | } |
| 679 | } |
| 680 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 681 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 683 | // Collect UsingDirectiveDecls in all scopes, and recursively all |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 684 | // nominated namespaces by those using-directives. |
| 685 | // UsingDirectives are pushed to heap, in common ancestor pointer |
| 686 | // value order. |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 687 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, |
| 688 | // so we don't build it for each lookup! |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 689 | UsingDirectivesTy UDirs; |
| 690 | for (Scope *SC = Initial; SC; SC = SC->getParent()) |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 691 | if (SC->getFlags() & Scope::DeclScope) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 692 | AddScopeUsingDirectives(Context, SC, UDirs); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 693 | |
| 694 | // Sort heapified UsingDirectiveDecls. |
| 695 | std::sort_heap(UDirs.begin(), UDirs.end()); |
| 696 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 697 | // Lookup namespace scope, and global scope. |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 698 | // Unqualified name lookup in C++ requires looking into scopes |
| 699 | // that aren't strictly lexical, and therefore we walk through the |
| 700 | // context as well as walking through the scopes. |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 701 | |
| 702 | LookupResultsTy LookupResults; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 703 | assert((!OutOfLineCtx || OutOfLineCtx->isFileContext()) && |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 704 | "We should have been looking only at file context here already."); |
| 705 | bool LookedInCtx = false; |
| 706 | LookupResult Result; |
| 707 | while (OutOfLineCtx && |
| 708 | OutOfLineCtx != S->getEntity() && |
| 709 | OutOfLineCtx->isNamespace()) { |
| 710 | LookedInCtx = true; |
| 711 | |
| 712 | // Look into context considering using-directives. |
| 713 | CppNamespaceLookup(Context, OutOfLineCtx, Name, NameKind, IDNS, |
| 714 | LookupResults, &UDirs); |
| 715 | |
| 716 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 717 | (RedeclarationOnly && !OutOfLineCtx->isTransparentContext())) |
| 718 | return std::make_pair(true, Result); |
| 719 | |
| 720 | OutOfLineCtx = OutOfLineCtx->getParent(); |
| 721 | } |
| 722 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 723 | for (; S; S = S->getParent()) { |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 724 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 725 | assert(Ctx && Ctx->isFileContext() && |
| 726 | "We should have been looking only at file context here already."); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 727 | |
| 728 | // Check whether the IdResolver has anything in this scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 729 | for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 730 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 731 | // We found something. Look for anything else in our scope |
| 732 | // with this same name and in an acceptable identifier |
| 733 | // namespace, so that we can construct an overload set if we |
| 734 | // need to. |
| 735 | IdentifierResolver::iterator LastI = I; |
| 736 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 737 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 738 | break; |
| 739 | } |
| 740 | |
| 741 | // We store name lookup result, and continue trying to look into |
| 742 | // associated context, and maybe namespaces nominated by |
| 743 | // using-directives. |
| 744 | LookupResults.push_back( |
| 745 | LookupResult::CreateLookupResult(Context, I, LastI)); |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 750 | LookedInCtx = true; |
| 751 | // Look into context considering using-directives. |
| 752 | CppNamespaceLookup(Context, Ctx, Name, NameKind, IDNS, |
| 753 | LookupResults, &UDirs); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 755 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 756 | (RedeclarationOnly && !Ctx->isTransparentContext())) |
| 757 | return std::make_pair(true, Result); |
| 758 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | b96b92d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 760 | if (!(LookedInCtx || LookupResults.empty())) { |
| 761 | // We didn't Performed lookup in Scope entity, so we return |
| 762 | // result form IdentifierResolver. |
| 763 | assert((LookupResults.size() == 1) && "Wrong size!"); |
| 764 | return std::make_pair(true, LookupResults.front()); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 765 | } |
| 766 | return std::make_pair(false, LookupResult()); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 769 | /// @brief Perform unqualified name lookup starting from a given |
| 770 | /// scope. |
| 771 | /// |
| 772 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 773 | /// used to find names within the current scope. For example, 'x' in |
| 774 | /// @code |
| 775 | /// int x; |
| 776 | /// int f() { |
| 777 | /// return x; // unqualified name look finds 'x' in the global scope |
| 778 | /// } |
| 779 | /// @endcode |
| 780 | /// |
| 781 | /// Different lookup criteria can find different names. For example, a |
| 782 | /// particular scope can have both a struct and a function of the same |
| 783 | /// name, and each can be found by certain lookup criteria. For more |
| 784 | /// information about lookup criteria, see the documentation for the |
| 785 | /// class LookupCriteria. |
| 786 | /// |
| 787 | /// @param S The scope from which unqualified name lookup will |
| 788 | /// begin. If the lookup criteria permits, name lookup may also search |
| 789 | /// in the parent scopes. |
| 790 | /// |
| 791 | /// @param Name The name of the entity that we are searching for. |
| 792 | /// |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 793 | /// @param Loc If provided, the source location where we're performing |
| 794 | /// name lookup. At present, this is only used to produce diagnostics when |
| 795 | /// C library functions (like "malloc") are implicitly declared. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 796 | /// |
| 797 | /// @returns The result of name lookup, which includes zero or more |
| 798 | /// declarations and possibly additional information used to diagnose |
| 799 | /// ambiguities. |
| 800 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 801 | Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 802 | bool RedeclarationOnly, bool AllowBuiltinCreation, |
| 803 | SourceLocation Loc) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 804 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 805 | |
| 806 | if (!getLangOptions().CPlusPlus) { |
| 807 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 808 | // search in the declarations attached to the name. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 809 | unsigned IDNS = 0; |
| 810 | switch (NameKind) { |
| 811 | case Sema::LookupOrdinaryName: |
| 812 | IDNS = Decl::IDNS_Ordinary; |
| 813 | break; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 815 | case Sema::LookupTagName: |
| 816 | IDNS = Decl::IDNS_Tag; |
| 817 | break; |
| 818 | |
| 819 | case Sema::LookupMemberName: |
| 820 | IDNS = Decl::IDNS_Member; |
| 821 | break; |
| 822 | |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 823 | case Sema::LookupOperatorName: |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 824 | case Sema::LookupNestedNameSpecifierName: |
| 825 | case Sema::LookupNamespaceName: |
| 826 | assert(false && "C does not perform these kinds of name lookup"); |
| 827 | break; |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 828 | |
| 829 | case Sema::LookupRedeclarationWithLinkage: |
| 830 | // Find the nearest non-transparent declaration scope. |
| 831 | while (!(S->getFlags() & Scope::DeclScope) || |
| 832 | (S->getEntity() && |
| 833 | static_cast<DeclContext *>(S->getEntity()) |
| 834 | ->isTransparentContext())) |
| 835 | S = S->getParent(); |
| 836 | IDNS = Decl::IDNS_Ordinary; |
| 837 | break; |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame^] | 838 | |
| 839 | case Sema::LookupProtocolName: |
| 840 | IDNS = Decl::IDNS_Protocol; |
| 841 | break; |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 844 | // Scan up the scope chain looking for a decl that matches this |
| 845 | // identifier that is in the appropriate namespace. This search |
| 846 | // should not take long, as shadowing of names is uncommon, and |
| 847 | // deep shadowing is extremely uncommon. |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 848 | bool LeftStartingScope = false; |
| 849 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 850 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
| 851 | IEnd = IdResolver.end(); |
| 852 | I != IEnd; ++I) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 853 | if ((*I)->isInIdentifierNamespace(IDNS)) { |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 854 | if (NameKind == LookupRedeclarationWithLinkage) { |
| 855 | // Determine whether this (or a previous) declaration is |
| 856 | // out-of-scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 857 | if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 858 | LeftStartingScope = true; |
| 859 | |
| 860 | // If we found something outside of our starting scope that |
| 861 | // does not have linkage, skip it. |
| 862 | if (LeftStartingScope && !((*I)->hasLinkage())) |
| 863 | continue; |
| 864 | } |
| 865 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 866 | if ((*I)->getAttr<OverloadableAttr>()) { |
| 867 | // If this declaration has the "overloadable" attribute, we |
| 868 | // might have a set of overloaded functions. |
| 869 | |
| 870 | // Figure out what scope the identifier is in. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 871 | while (!(S->getFlags() & Scope::DeclScope) || |
| 872 | !S->isDeclScope(DeclPtrTy::make(*I))) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 873 | S = S->getParent(); |
| 874 | |
| 875 | // Find the last declaration in this scope (with the same |
| 876 | // name, naturally). |
| 877 | IdentifierResolver::iterator LastI = I; |
| 878 | for (++LastI; LastI != IEnd; ++LastI) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 879 | if (!S->isDeclScope(DeclPtrTy::make(*LastI))) |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | |
| 883 | return LookupResult::CreateLookupResult(Context, I, LastI); |
| 884 | } |
| 885 | |
| 886 | // We have a single lookup result. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 887 | return LookupResult::CreateLookupResult(Context, *I); |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 888 | } |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 889 | } else { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 890 | // Perform C++ unqualified name lookup. |
| 891 | std::pair<bool, LookupResult> MaybeResult = |
| 892 | CppLookupName(S, Name, NameKind, RedeclarationOnly); |
| 893 | if (MaybeResult.first) |
| 894 | return MaybeResult.second; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | // If we didn't find a use of this identifier, and if the identifier |
| 898 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 899 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 1c52c63 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 900 | if (NameKind == LookupOrdinaryName || |
| 901 | NameKind == LookupRedeclarationWithLinkage) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 902 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 903 | if (II && AllowBuiltinCreation) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 904 | // If this is a builtin on this (or all) targets, create the decl. |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 905 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 906 | // In C++, we don't have any predefined library functions like |
| 907 | // 'malloc'. Instead, we'll just error. |
| 908 | if (getLangOptions().CPlusPlus && |
| 909 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 910 | return LookupResult::CreateLookupResult(Context, 0); |
| 911 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 912 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 913 | LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 914 | S, RedeclarationOnly, Loc)); |
| 915 | } |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 916 | } |
| 917 | if (getLangOptions().ObjC1 && II) { |
| 918 | // @interface and @compatibility_alias introduce typedef-like names. |
| 919 | // Unlike typedef's, they can only be introduced at file-scope (and are |
| 920 | // therefore not scoped decls). They can, however, be shadowed by |
| 921 | // other names in IDNS_Ordinary. |
| 922 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 923 | if (IDI != ObjCInterfaceDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 924 | return LookupResult::CreateLookupResult(Context, IDI->second); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 925 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 926 | if (I != ObjCAliasDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 927 | return LookupResult::CreateLookupResult(Context, |
| 928 | I->second->getClassInterface()); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 931 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /// @brief Perform qualified name lookup into a given context. |
| 935 | /// |
| 936 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 937 | /// names when the context of those names is explicit specified, e.g., |
| 938 | /// "std::vector" or "x->member". |
| 939 | /// |
| 940 | /// Different lookup criteria can find different names. For example, a |
| 941 | /// particular scope can have both a struct and a function of the same |
| 942 | /// name, and each can be found by certain lookup criteria. For more |
| 943 | /// information about lookup criteria, see the documentation for the |
| 944 | /// class LookupCriteria. |
| 945 | /// |
| 946 | /// @param LookupCtx The context in which qualified name lookup will |
| 947 | /// search. If the lookup criteria permits, name lookup may also search |
| 948 | /// in the parent contexts or (for C++ classes) base classes. |
| 949 | /// |
| 950 | /// @param Name The name of the entity that we are searching for. |
| 951 | /// |
| 952 | /// @param Criteria The criteria that this routine will use to |
| 953 | /// determine which names are visible and which names will be |
| 954 | /// found. Note that name lookup will find a name that is visible by |
| 955 | /// the given criteria, but the entity itself may not be semantically |
| 956 | /// correct or even the kind of entity expected based on the |
| 957 | /// lookup. For example, searching for a nested-name-specifier name |
| 958 | /// might result in an EnumDecl, which is visible but is not permitted |
| 959 | /// as a nested-name-specifier in C++03. |
| 960 | /// |
| 961 | /// @returns The result of name lookup, which includes zero or more |
| 962 | /// declarations and possibly additional information used to diagnose |
| 963 | /// ambiguities. |
| 964 | Sema::LookupResult |
| 965 | Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 966 | LookupNameKind NameKind, bool RedeclarationOnly) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 967 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
| 968 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 969 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 970 | |
| 971 | // If we're performing qualified name lookup (e.g., lookup into a |
| 972 | // struct), find fields as part of ordinary name lookup. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 973 | unsigned IDNS |
| 974 | = getIdentifierNamespacesFromLookupNameKind(NameKind, |
| 975 | getLangOptions().CPlusPlus); |
| 976 | if (NameKind == LookupOrdinaryName) |
| 977 | IDNS |= Decl::IDNS_Member; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 978 | |
| 979 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 980 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 981 | for (llvm::tie(I, E) = LookupCtx->lookup(Context, Name); I != E; ++I) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 982 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 983 | return LookupResult::CreateLookupResult(Context, I, E); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 985 | // If this isn't a C++ class or we aren't allowed to look into base |
| 986 | // classes, we're done. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 987 | if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 988 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 989 | |
| 990 | // Perform lookup into our base classes. |
| 991 | BasePaths Paths; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 992 | Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx))); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 993 | |
| 994 | // Look for this member in our base classes |
| 995 | if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 996 | MemberLookupCriteria(Name, NameKind, IDNS), Paths)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 997 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 998 | |
| 999 | // C++ [class.member.lookup]p2: |
| 1000 | // [...] If the resulting set of declarations are not all from |
| 1001 | // sub-objects of the same type, or the set has a nonstatic member |
| 1002 | // and includes members from distinct sub-objects, there is an |
| 1003 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 1004 | // the result of the lookup. |
| 1005 | // FIXME: support using declarations! |
| 1006 | QualType SubobjectType; |
Daniel Dunbar | ddebeca | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1007 | int SubobjectNumber = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1008 | for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
| 1009 | Path != PathEnd; ++Path) { |
| 1010 | const BasePathElement &PathElement = Path->back(); |
| 1011 | |
| 1012 | // Determine whether we're looking at a distinct sub-object or not. |
| 1013 | if (SubobjectType.isNull()) { |
| 1014 | // This is the first subobject we've looked at. Record it's type. |
| 1015 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 1016 | SubobjectNumber = PathElement.SubobjectNumber; |
| 1017 | } else if (SubobjectType |
| 1018 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 1019 | // We found members of the given name in two subobjects of |
| 1020 | // different types. This lookup is ambiguous. |
| 1021 | BasePaths *PathsOnHeap = new BasePaths; |
| 1022 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1023 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, true); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1024 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 1025 | // We have a different subobject of the same type. |
| 1026 | |
| 1027 | // C++ [class.member.lookup]p5: |
| 1028 | // A static member, a nested type or an enumerator defined in |
| 1029 | // a base class T can unambiguously be found even if an object |
| 1030 | // has more than one base class subobject of type T. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1031 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1032 | if (isa<VarDecl>(FirstDecl) || |
| 1033 | isa<TypeDecl>(FirstDecl) || |
| 1034 | isa<EnumConstantDecl>(FirstDecl)) |
| 1035 | continue; |
| 1036 | |
| 1037 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 1038 | // Determine whether all of the methods are static. |
| 1039 | bool AllMethodsAreStatic = true; |
| 1040 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 1041 | Func != Path->Decls.second; ++Func) { |
| 1042 | if (!isa<CXXMethodDecl>(*Func)) { |
| 1043 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 1048 | AllMethodsAreStatic = false; |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (AllMethodsAreStatic) |
| 1054 | continue; |
| 1055 | } |
| 1056 | |
| 1057 | // We have found a nonstatic member name in multiple, distinct |
| 1058 | // subobjects. Name lookup is ambiguous. |
| 1059 | BasePaths *PathsOnHeap = new BasePaths; |
| 1060 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1061 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // Lookup in a base class succeeded; return these results. |
| 1066 | |
| 1067 | // If we found a function declaration, return an overload set. |
| 1068 | if (isa<FunctionDecl>(*Paths.front().Decls.first)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1069 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1070 | Paths.front().Decls.first, Paths.front().Decls.second); |
| 1071 | |
| 1072 | // We found a non-function declaration; return a single declaration. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1073 | return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /// @brief Performs name lookup for a name that was parsed in the |
| 1077 | /// source code, and may contain a C++ scope specifier. |
| 1078 | /// |
| 1079 | /// This routine is a convenience routine meant to be called from |
| 1080 | /// contexts that receive a name and an optional C++ scope specifier |
| 1081 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1082 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1083 | /// respectively) on the given name and return those results. |
| 1084 | /// |
| 1085 | /// @param S The scope from which unqualified name lookup will |
| 1086 | /// begin. |
| 1087 | /// |
| 1088 | /// @param SS An optional C++ scope-specified, e.g., "::N::M". |
| 1089 | /// |
| 1090 | /// @param Name The name of the entity that name lookup will |
| 1091 | /// search for. |
| 1092 | /// |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1093 | /// @param Loc If provided, the source location where we're performing |
| 1094 | /// name lookup. At present, this is only used to produce diagnostics when |
| 1095 | /// C library functions (like "malloc") are implicitly declared. |
| 1096 | /// |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1097 | /// @returns The result of qualified or unqualified name lookup. |
| 1098 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1099 | Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS, |
| 1100 | DeclarationName Name, LookupNameKind NameKind, |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1101 | bool RedeclarationOnly, bool AllowBuiltinCreation, |
| 1102 | SourceLocation Loc) { |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1103 | if (SS) { |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 1104 | if (SS->isInvalid() || RequireCompleteDeclContext(*SS)) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1105 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1107 | if (SS->isSet()) { |
| 1108 | return LookupQualifiedName(computeDeclContext(*SS), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1109 | Name, NameKind, RedeclarationOnly); |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1110 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1113 | return LookupName(S, Name, NameKind, RedeclarationOnly, |
| 1114 | AllowBuiltinCreation, Loc); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1118 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1119 | /// from name lookup. |
| 1120 | /// |
| 1121 | /// @param Result The ambiguous name lookup result. |
| 1122 | /// |
| 1123 | /// @param Name The name of the entity that name lookup was |
| 1124 | /// searching for. |
| 1125 | /// |
| 1126 | /// @param NameLoc The location of the name within the source code. |
| 1127 | /// |
| 1128 | /// @param LookupRange A source range that provides more |
| 1129 | /// source-location information concerning the lookup itself. For |
| 1130 | /// example, this range might highlight a nested-name-specifier that |
| 1131 | /// precedes the name. |
| 1132 | /// |
| 1133 | /// @returns true |
| 1134 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name, |
| 1135 | SourceLocation NameLoc, |
| 1136 | SourceRange LookupRange) { |
| 1137 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1138 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1139 | if (BasePaths *Paths = Result.getBasePaths()) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1140 | if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) { |
| 1141 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1142 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1143 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1144 | << LookupRange; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1146 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1147 | while (isa<CXXMethodDecl>(*Found) && |
| 1148 | cast<CXXMethodDecl>(*Found)->isStatic()) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1149 | ++Found; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1151 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1152 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1153 | Result.Destroy(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes && |
| 1158 | "Unhandled form of name lookup ambiguity"); |
| 1159 | |
| 1160 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1161 | << Name << LookupRange; |
| 1162 | |
| 1163 | std::set<Decl *> DeclsPrinted; |
| 1164 | for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end(); |
| 1165 | Path != PathEnd; ++Path) { |
| 1166 | Decl *D = *Path->Decls.first; |
| 1167 | if (DeclsPrinted.insert(D).second) |
| 1168 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1169 | } |
| 1170 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1171 | Result.Destroy(); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1172 | return true; |
| 1173 | } else if (Result.getKind() == LookupResult::AmbiguousReference) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1174 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
| 1175 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1176 | NamedDecl **DI = reinterpret_cast<NamedDecl **>(Result.First), |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1177 | **DEnd = reinterpret_cast<NamedDecl **>(Result.Last); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | b02f5f2 | 2009-02-03 21:29:32 +0000 | [diff] [blame] | 1179 | for (; DI != DEnd; ++DI) |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1180 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1182 | Result.Destroy(); |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1183 | return true; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1186 | assert(false && "Unhandled form of name lookup ambiguity"); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1187 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1188 | // We can't reach here. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1191 | |
| 1192 | // \brief Add the associated classes and namespaces for |
| 1193 | // argument-dependent lookup with an argument of class type |
| 1194 | // (C++ [basic.lookup.koenig]p2). |
| 1195 | static void |
| 1196 | addAssociatedClassesAndNamespaces(CXXRecordDecl *Class, |
| 1197 | ASTContext &Context, |
| 1198 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1199 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1200 | // C++ [basic.lookup.koenig]p2: |
| 1201 | // [...] |
| 1202 | // -- If T is a class type (including unions), its associated |
| 1203 | // classes are: the class itself; the class of which it is a |
| 1204 | // member, if any; and its direct and indirect base |
| 1205 | // classes. Its associated namespaces are the namespaces in |
| 1206 | // which its associated classes are defined. |
| 1207 | |
| 1208 | // Add the class of which it is a member, if any. |
| 1209 | DeclContext *Ctx = Class->getDeclContext(); |
| 1210 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1211 | AssociatedClasses.insert(EnclosingClass); |
| 1212 | |
| 1213 | // Add the associated namespace for this class. |
| 1214 | while (Ctx->isRecord()) |
| 1215 | Ctx = Ctx->getParent(); |
| 1216 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1217 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1218 | |
| 1219 | // Add the class itself. If we've already seen this class, we don't |
| 1220 | // need to visit base classes. |
| 1221 | if (!AssociatedClasses.insert(Class)) |
| 1222 | return; |
| 1223 | |
| 1224 | // FIXME: Handle class template specializations |
| 1225 | |
| 1226 | // Add direct and indirect base classes along with their associated |
| 1227 | // namespaces. |
| 1228 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1229 | Bases.push_back(Class); |
| 1230 | while (!Bases.empty()) { |
| 1231 | // Pop this class off the stack. |
| 1232 | Class = Bases.back(); |
| 1233 | Bases.pop_back(); |
| 1234 | |
| 1235 | // Visit the base classes. |
| 1236 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1237 | BaseEnd = Class->bases_end(); |
| 1238 | Base != BaseEnd; ++Base) { |
| 1239 | const RecordType *BaseType = Base->getType()->getAsRecordType(); |
| 1240 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 1241 | if (AssociatedClasses.insert(BaseDecl)) { |
| 1242 | // Find the associated namespace for this base class. |
| 1243 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
| 1244 | while (BaseCtx->isRecord()) |
| 1245 | BaseCtx = BaseCtx->getParent(); |
| 1246 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(BaseCtx)) |
| 1247 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1248 | |
| 1249 | // Make sure we visit the bases of this base class. |
| 1250 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1251 | Bases.push_back(BaseDecl); |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // \brief Add the associated classes and namespaces for |
| 1258 | // argument-dependent lookup with an argument of type T |
| 1259 | // (C++ [basic.lookup.koenig]p2). |
| 1260 | static void |
| 1261 | addAssociatedClassesAndNamespaces(QualType T, |
| 1262 | ASTContext &Context, |
| 1263 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1264 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1265 | // C++ [basic.lookup.koenig]p2: |
| 1266 | // |
| 1267 | // For each argument type T in the function call, there is a set |
| 1268 | // of zero or more associated namespaces and a set of zero or more |
| 1269 | // associated classes to be considered. The sets of namespaces and |
| 1270 | // classes is determined entirely by the types of the function |
| 1271 | // arguments (and the namespace of any template template |
| 1272 | // argument). Typedef names and using-declarations used to specify |
| 1273 | // the types do not contribute to this set. The sets of namespaces |
| 1274 | // and classes are determined in the following way: |
| 1275 | T = Context.getCanonicalType(T).getUnqualifiedType(); |
| 1276 | |
| 1277 | // -- If T is a pointer to U or an array of U, its associated |
| 1278 | // namespaces and classes are those associated with U. |
| 1279 | // |
| 1280 | // We handle this by unwrapping pointer and array types immediately, |
| 1281 | // to avoid unnecessary recursion. |
| 1282 | while (true) { |
| 1283 | if (const PointerType *Ptr = T->getAsPointerType()) |
| 1284 | T = Ptr->getPointeeType(); |
| 1285 | else if (const ArrayType *Ptr = Context.getAsArrayType(T)) |
| 1286 | T = Ptr->getElementType(); |
| 1287 | else |
| 1288 | break; |
| 1289 | } |
| 1290 | |
| 1291 | // -- If T is a fundamental type, its associated sets of |
| 1292 | // namespaces and classes are both empty. |
| 1293 | if (T->getAsBuiltinType()) |
| 1294 | return; |
| 1295 | |
| 1296 | // -- If T is a class type (including unions), its associated |
| 1297 | // classes are: the class itself; the class of which it is a |
| 1298 | // member, if any; and its direct and indirect base |
| 1299 | // classes. Its associated namespaces are the namespaces in |
| 1300 | // which its associated classes are defined. |
Douglas Gregor | 2e04759 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1301 | if (const RecordType *ClassType = T->getAsRecordType()) |
| 1302 | if (CXXRecordDecl *ClassDecl |
| 1303 | = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) { |
| 1304 | addAssociatedClassesAndNamespaces(ClassDecl, Context, |
| 1305 | AssociatedNamespaces, |
| 1306 | AssociatedClasses); |
| 1307 | return; |
| 1308 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1309 | |
| 1310 | // -- If T is an enumeration type, its associated namespace is |
| 1311 | // the namespace in which it is defined. If it is class |
| 1312 | // member, its associated class is the member’s class; else |
| 1313 | // it has no associated class. |
| 1314 | if (const EnumType *EnumT = T->getAsEnumType()) { |
| 1315 | EnumDecl *Enum = EnumT->getDecl(); |
| 1316 | |
| 1317 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1318 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1319 | AssociatedClasses.insert(EnclosingClass); |
| 1320 | |
| 1321 | // Add the associated namespace for this class. |
| 1322 | while (Ctx->isRecord()) |
| 1323 | Ctx = Ctx->getParent(); |
| 1324 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1325 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1326 | |
| 1327 | return; |
| 1328 | } |
| 1329 | |
| 1330 | // -- If T is a function type, its associated namespaces and |
| 1331 | // classes are those associated with the function parameter |
| 1332 | // types and those associated with the return type. |
| 1333 | if (const FunctionType *FunctionType = T->getAsFunctionType()) { |
| 1334 | // Return type |
| 1335 | addAssociatedClassesAndNamespaces(FunctionType->getResultType(), |
| 1336 | Context, |
| 1337 | AssociatedNamespaces, AssociatedClasses); |
| 1338 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1339 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FunctionType); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1340 | if (!Proto) |
| 1341 | return; |
| 1342 | |
| 1343 | // Argument types |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1344 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1345 | ArgEnd = Proto->arg_type_end(); |
| 1346 | Arg != ArgEnd; ++Arg) |
| 1347 | addAssociatedClassesAndNamespaces(*Arg, Context, |
| 1348 | AssociatedNamespaces, AssociatedClasses); |
| 1349 | |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | // -- If T is a pointer to a member function of a class X, its |
| 1354 | // associated namespaces and classes are those associated |
| 1355 | // with the function parameter types and return type, |
| 1356 | // together with those associated with X. |
| 1357 | // |
| 1358 | // -- If T is a pointer to a data member of class X, its |
| 1359 | // associated namespaces and classes are those associated |
| 1360 | // with the member type together with those associated with |
| 1361 | // X. |
| 1362 | if (const MemberPointerType *MemberPtr = T->getAsMemberPointerType()) { |
| 1363 | // Handle the type that the pointer to member points to. |
| 1364 | addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(), |
| 1365 | Context, |
| 1366 | AssociatedNamespaces, AssociatedClasses); |
| 1367 | |
| 1368 | // Handle the class type into which this points. |
| 1369 | if (const RecordType *Class = MemberPtr->getClass()->getAsRecordType()) |
| 1370 | addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()), |
| 1371 | Context, |
| 1372 | AssociatedNamespaces, AssociatedClasses); |
| 1373 | |
| 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | // FIXME: What about block pointers? |
| 1378 | // FIXME: What about Objective-C message sends? |
| 1379 | } |
| 1380 | |
| 1381 | /// \brief Find the associated classes and namespaces for |
| 1382 | /// argument-dependent lookup for a call with the given set of |
| 1383 | /// arguments. |
| 1384 | /// |
| 1385 | /// This routine computes the sets of associated classes and associated |
| 1386 | /// namespaces searched by argument-dependent lookup |
| 1387 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
| 1388 | void |
| 1389 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1390 | AssociatedNamespaceSet &AssociatedNamespaces, |
| 1391 | AssociatedClassSet &AssociatedClasses) { |
| 1392 | AssociatedNamespaces.clear(); |
| 1393 | AssociatedClasses.clear(); |
| 1394 | |
| 1395 | // C++ [basic.lookup.koenig]p2: |
| 1396 | // For each argument type T in the function call, there is a set |
| 1397 | // of zero or more associated namespaces and a set of zero or more |
| 1398 | // associated classes to be considered. The sets of namespaces and |
| 1399 | // classes is determined entirely by the types of the function |
| 1400 | // arguments (and the namespace of any template template |
| 1401 | // argument). |
| 1402 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1403 | Expr *Arg = Args[ArgIdx]; |
| 1404 | |
| 1405 | if (Arg->getType() != Context.OverloadTy) { |
| 1406 | addAssociatedClassesAndNamespaces(Arg->getType(), Context, |
| 1407 | AssociatedNamespaces, AssociatedClasses); |
| 1408 | continue; |
| 1409 | } |
| 1410 | |
| 1411 | // [...] In addition, if the argument is the name or address of a |
| 1412 | // set of overloaded functions and/or function templates, its |
| 1413 | // associated classes and namespaces are the union of those |
| 1414 | // associated with each of the members of the set: the namespace |
| 1415 | // in which the function or function template is defined and the |
| 1416 | // classes and namespaces associated with its (non-dependent) |
| 1417 | // parameter types and return type. |
| 1418 | DeclRefExpr *DRE = 0; |
| 1419 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1420 | if (unaryOp->getOpcode() == UnaryOperator::AddrOf) |
| 1421 | DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr()); |
| 1422 | } else |
| 1423 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 1424 | if (!DRE) |
| 1425 | continue; |
| 1426 | |
| 1427 | OverloadedFunctionDecl *Ovl |
| 1428 | = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl()); |
| 1429 | if (!Ovl) |
| 1430 | continue; |
| 1431 | |
| 1432 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 1433 | FuncEnd = Ovl->function_end(); |
| 1434 | Func != FuncEnd; ++Func) { |
| 1435 | FunctionDecl *FDecl = cast<FunctionDecl>(*Func); |
| 1436 | |
| 1437 | // Add the namespace in which this function was defined. Note |
| 1438 | // that, if this is a member function, we do *not* consider the |
| 1439 | // enclosing namespace of its class. |
| 1440 | DeclContext *Ctx = FDecl->getDeclContext(); |
| 1441 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1442 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1443 | |
| 1444 | // Add the classes and namespaces associated with the parameter |
| 1445 | // types and return type of this function. |
| 1446 | addAssociatedClassesAndNamespaces(FDecl->getType(), Context, |
| 1447 | AssociatedNamespaces, AssociatedClasses); |
| 1448 | } |
| 1449 | } |
| 1450 | } |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1451 | |
| 1452 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 1453 | /// an acceptable non-member overloaded operator for a call whose |
| 1454 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 1455 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 1456 | /// enumeration types. |
| 1457 | static bool |
| 1458 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 1459 | QualType T1, QualType T2, |
| 1460 | ASTContext &Context) { |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1461 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 1462 | return true; |
| 1463 | |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1464 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 1465 | return true; |
| 1466 | |
| 1467 | const FunctionProtoType *Proto = Fn->getType()->getAsFunctionProtoType(); |
| 1468 | if (Proto->getNumArgs() < 1) |
| 1469 | return false; |
| 1470 | |
| 1471 | if (T1->isEnumeralType()) { |
| 1472 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
| 1473 | if (Context.getCanonicalType(T1).getUnqualifiedType() |
| 1474 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1475 | return true; |
| 1476 | } |
| 1477 | |
| 1478 | if (Proto->getNumArgs() < 2) |
| 1479 | return false; |
| 1480 | |
| 1481 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 1482 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
| 1483 | if (Context.getCanonicalType(T2).getUnqualifiedType() |
| 1484 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
Douglas Gregor | e57c8cc | 2009-04-23 23:18:26 +0000 | [diff] [blame^] | 1491 | /// \brief Find the protocol with the given name, if any. |
| 1492 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) { |
| 1493 | Decl *D = LookupName(TUScope, II, LookupProtocolName).getAsDecl(); |
| 1494 | return cast_or_null<ObjCProtocolDecl>(D); |
| 1495 | } |
| 1496 | |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1497 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, |
| 1498 | QualType T1, QualType T2, |
| 1499 | FunctionSet &Functions) { |
| 1500 | // C++ [over.match.oper]p3: |
| 1501 | // -- The set of non-member candidates is the result of the |
| 1502 | // unqualified lookup of operator@ in the context of the |
| 1503 | // expression according to the usual rules for name lookup in |
| 1504 | // unqualified function calls (3.4.2) except that all member |
| 1505 | // functions are ignored. However, if no operand has a class |
| 1506 | // type, only those non-member functions in the lookup set |
| 1507 | // that have a first parameter of type T1 or “reference to |
| 1508 | // (possibly cv-qualified) T1”, when T1 is an enumeration |
| 1509 | // type, or (if there is a right operand) a second parameter |
| 1510 | // of type T2 or “reference to (possibly cv-qualified) T2”, |
| 1511 | // when T2 is an enumeration type, are candidate functions. |
| 1512 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 1513 | LookupResult Operators = LookupName(S, OpName, LookupOperatorName); |
| 1514 | |
| 1515 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 1516 | |
| 1517 | if (!Operators) |
| 1518 | return; |
| 1519 | |
| 1520 | for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end(); |
| 1521 | Op != OpEnd; ++Op) { |
| 1522 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) |
| 1523 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
| 1524 | Functions.insert(FD); // FIXME: canonical FD |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | void Sema::ArgumentDependentLookup(DeclarationName Name, |
| 1529 | Expr **Args, unsigned NumArgs, |
| 1530 | FunctionSet &Functions) { |
| 1531 | // Find all of the associated namespaces and classes based on the |
| 1532 | // arguments we have. |
| 1533 | AssociatedNamespaceSet AssociatedNamespaces; |
| 1534 | AssociatedClassSet AssociatedClasses; |
| 1535 | FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 1536 | AssociatedNamespaces, AssociatedClasses); |
| 1537 | |
| 1538 | // C++ [basic.lookup.argdep]p3: |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1539 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 1540 | // and let Y be the lookup set produced by argument dependent |
| 1541 | // lookup (defined as follows). If X contains [...] then Y is |
| 1542 | // empty. Otherwise Y is the set of declarations found in the |
| 1543 | // namespaces associated with the argument types as described |
| 1544 | // below. The set of declarations found by the lookup of the name |
| 1545 | // is the union of X and Y. |
| 1546 | // |
| 1547 | // Here, we compute Y and add its members to the overloaded |
| 1548 | // candidate set. |
| 1549 | for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(), |
| 1550 | NSEnd = AssociatedNamespaces.end(); |
| 1551 | NS != NSEnd; ++NS) { |
| 1552 | // When considering an associated namespace, the lookup is the |
| 1553 | // same as the lookup performed when the associated namespace is |
| 1554 | // used as a qualifier (3.4.3.2) except that: |
| 1555 | // |
| 1556 | // -- Any using-directives in the associated namespace are |
| 1557 | // ignored. |
| 1558 | // |
| 1559 | // -- FIXME: Any namespace-scope friend functions declared in |
| 1560 | // associated classes are visible within their respective |
| 1561 | // namespaces even if they are not visible during an ordinary |
| 1562 | // lookup (11.4). |
| 1563 | DeclContext::lookup_iterator I, E; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1564 | for (llvm::tie(I, E) = (*NS)->lookup(Context, Name); I != E; ++I) { |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 1565 | FunctionDecl *Func = dyn_cast<FunctionDecl>(*I); |
| 1566 | if (!Func) |
| 1567 | break; |
| 1568 | |
| 1569 | Functions.insert(Func); |
| 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | |