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