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