Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1 | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements name lookup for C, C++, Objective-C, and |
| 11 | // Objective-C++. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "Sema.h" |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/Parse/DeclSpec.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 23 | #include <set> |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | /// MaybeConstructOverloadSet - Name lookup has determined that the |
| 28 | /// elements in [I, IEnd) have the name that we are looking for, and |
| 29 | /// *I is a match for the namespace. This routine returns an |
| 30 | /// appropriate Decl for name lookup, which may either be *I or an |
| 31 | /// OverloadeFunctionDecl that represents the overloaded functions in |
| 32 | /// [I, IEnd). |
| 33 | /// |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 34 | /// The existance of this routine is temporary; users of LookupResult |
| 35 | /// should be able to handle multiple results, to deal with cases of |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 36 | /// ambiguity and overloaded functions without needing to create a |
| 37 | /// Decl node. |
| 38 | template<typename DeclIterator> |
| 39 | static Decl * |
| 40 | MaybeConstructOverloadSet(ASTContext &Context, |
| 41 | DeclIterator I, DeclIterator IEnd) { |
| 42 | assert(I != IEnd && "Iterator range cannot be empty"); |
| 43 | assert(!isa<OverloadedFunctionDecl>(*I) && |
| 44 | "Cannot have an overloaded function"); |
| 45 | |
| 46 | if (isa<FunctionDecl>(*I)) { |
| 47 | // If we found a function, there might be more functions. If |
| 48 | // so, collect them into an overload set. |
| 49 | DeclIterator Last = I; |
| 50 | OverloadedFunctionDecl *Ovl = 0; |
| 51 | for (++Last; Last != IEnd && isa<FunctionDecl>(*Last); ++Last) { |
| 52 | if (!Ovl) { |
| 53 | // FIXME: We leak this overload set. Eventually, we want to |
| 54 | // stop building the declarations for these overload sets, so |
| 55 | // there will be nothing to leak. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 56 | Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(), |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 57 | (*I)->getDeclName()); |
| 58 | Ovl->addOverload(cast<FunctionDecl>(*I)); |
| 59 | } |
| 60 | Ovl->addOverload(cast<FunctionDecl>(*Last)); |
| 61 | } |
| 62 | |
| 63 | // If we had more than one function, we built an overload |
| 64 | // set. Return it. |
| 65 | if (Ovl) |
| 66 | return Ovl; |
| 67 | } |
| 68 | |
| 69 | return *I; |
| 70 | } |
| 71 | |
Douglas Gregor | 6beddfe | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 72 | /// @brief Moves the name-lookup results from Other to this LookupResult. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 73 | Sema::LookupResult |
| 74 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 75 | IdentifierResolver::iterator F, |
| 76 | IdentifierResolver::iterator L) { |
| 77 | LookupResult Result; |
| 78 | Result.Context = &Context; |
| 79 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 80 | if (F != L && isa<FunctionDecl>(*F)) { |
| 81 | IdentifierResolver::iterator Next = F; |
| 82 | ++Next; |
| 83 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 84 | Result.StoredKind = OverloadedDeclFromIdResolver; |
| 85 | Result.First = F.getAsOpaqueValue(); |
| 86 | Result.Last = L.getAsOpaqueValue(); |
| 87 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 91 | Result.StoredKind = SingleDecl; |
| 92 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 93 | Result.Last = 0; |
| 94 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 97 | Sema::LookupResult |
| 98 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 99 | DeclContext::lookup_iterator F, |
| 100 | DeclContext::lookup_iterator L) { |
| 101 | LookupResult Result; |
| 102 | Result.Context = &Context; |
| 103 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 104 | if (F != L && isa<FunctionDecl>(*F)) { |
| 105 | DeclContext::lookup_iterator Next = F; |
| 106 | ++Next; |
| 107 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 108 | Result.StoredKind = OverloadedDeclFromDeclContext; |
| 109 | Result.First = reinterpret_cast<uintptr_t>(F); |
| 110 | Result.Last = reinterpret_cast<uintptr_t>(L); |
| 111 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 115 | Result.StoredKind = SingleDecl; |
| 116 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 117 | Result.Last = 0; |
| 118 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 121 | /// @brief Determine the result of name lookup. |
| 122 | Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const { |
| 123 | switch (StoredKind) { |
| 124 | case SingleDecl: |
| 125 | return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound; |
| 126 | |
| 127 | case OverloadedDeclFromIdResolver: |
| 128 | case OverloadedDeclFromDeclContext: |
| 129 | return FoundOverloaded; |
| 130 | |
| 131 | case AmbiguousLookup: |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 132 | return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 135 | // We can't ever get here. |
| 136 | return NotFound; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /// @brief Converts the result of name lookup into a single (possible |
| 140 | /// NULL) pointer to a declaration. |
| 141 | /// |
| 142 | /// The resulting declaration will either be the declaration we found |
| 143 | /// (if only a single declaration was found), an |
| 144 | /// OverloadedFunctionDecl (if an overloaded function was found), or |
| 145 | /// NULL (if no declaration was found). This conversion must not be |
| 146 | /// used anywhere where name lookup could result in an ambiguity. |
| 147 | /// |
| 148 | /// The OverloadedFunctionDecl conversion is meant as a stop-gap |
| 149 | /// solution, since it causes the OverloadedFunctionDecl to be |
| 150 | /// leaked. FIXME: Eventually, there will be a better way to iterate |
| 151 | /// over the set of overloaded functions returned by name lookup. |
| 152 | Decl *Sema::LookupResult::getAsDecl() const { |
| 153 | switch (StoredKind) { |
| 154 | case SingleDecl: |
| 155 | return reinterpret_cast<Decl *>(First); |
| 156 | |
| 157 | case OverloadedDeclFromIdResolver: |
| 158 | return MaybeConstructOverloadSet(*Context, |
| 159 | IdentifierResolver::iterator::getFromOpaqueValue(First), |
| 160 | IdentifierResolver::iterator::getFromOpaqueValue(Last)); |
| 161 | |
| 162 | case OverloadedDeclFromDeclContext: |
| 163 | return MaybeConstructOverloadSet(*Context, |
| 164 | reinterpret_cast<DeclContext::lookup_iterator>(First), |
| 165 | reinterpret_cast<DeclContext::lookup_iterator>(Last)); |
| 166 | |
| 167 | case AmbiguousLookup: |
| 168 | assert(false && |
| 169 | "Name lookup returned an ambiguity that could not be handled"); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 176 | /// @brief Retrieves the BasePaths structure describing an ambiguous |
| 177 | /// name lookup. |
| 178 | BasePaths *Sema::LookupResult::getBasePaths() const { |
| 179 | assert((StoredKind == AmbiguousLookup) && |
| 180 | "getBasePaths can only be used on an ambiguous lookup"); |
| 181 | return reinterpret_cast<BasePaths *>(First); |
| 182 | } |
| 183 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 184 | // Retrieve the set of identifier namespaces that correspond to a |
| 185 | // specific kind of name lookup. |
| 186 | inline unsigned |
| 187 | getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind, |
| 188 | bool CPlusPlus) { |
| 189 | unsigned IDNS = 0; |
| 190 | switch (NameKind) { |
| 191 | case Sema::LookupOrdinaryName: |
| 192 | IDNS = Decl::IDNS_Ordinary; |
| 193 | if (CPlusPlus) |
| 194 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
| 195 | break; |
| 196 | |
| 197 | case Sema::LookupTagName: |
| 198 | IDNS = Decl::IDNS_Tag; |
| 199 | break; |
| 200 | |
| 201 | case Sema::LookupMemberName: |
| 202 | IDNS = Decl::IDNS_Member; |
| 203 | if (CPlusPlus) |
| 204 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
| 205 | break; |
| 206 | |
| 207 | case Sema::LookupNestedNameSpecifierName: |
| 208 | case Sema::LookupNamespaceName: |
| 209 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 210 | break; |
| 211 | } |
| 212 | return IDNS; |
| 213 | } |
| 214 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 215 | /// @brief Perform unqualified name lookup starting from a given |
| 216 | /// scope. |
| 217 | /// |
| 218 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 219 | /// used to find names within the current scope. For example, 'x' in |
| 220 | /// @code |
| 221 | /// int x; |
| 222 | /// int f() { |
| 223 | /// return x; // unqualified name look finds 'x' in the global scope |
| 224 | /// } |
| 225 | /// @endcode |
| 226 | /// |
| 227 | /// Different lookup criteria can find different names. For example, a |
| 228 | /// particular scope can have both a struct and a function of the same |
| 229 | /// name, and each can be found by certain lookup criteria. For more |
| 230 | /// information about lookup criteria, see the documentation for the |
| 231 | /// class LookupCriteria. |
| 232 | /// |
| 233 | /// @param S The scope from which unqualified name lookup will |
| 234 | /// begin. If the lookup criteria permits, name lookup may also search |
| 235 | /// in the parent scopes. |
| 236 | /// |
| 237 | /// @param Name The name of the entity that we are searching for. |
| 238 | /// |
| 239 | /// @param Criteria The criteria that this routine will use to |
| 240 | /// determine which names are visible and which names will be |
| 241 | /// found. Note that name lookup will find a name that is visible by |
| 242 | /// the given criteria, but the entity itself may not be semantically |
| 243 | /// correct or even the kind of entity expected based on the |
| 244 | /// lookup. For example, searching for a nested-name-specifier name |
| 245 | /// might result in an EnumDecl, which is visible but is not permitted |
| 246 | /// as a nested-name-specifier in C++03. |
| 247 | /// |
| 248 | /// @returns The result of name lookup, which includes zero or more |
| 249 | /// declarations and possibly additional information used to diagnose |
| 250 | /// ambiguities. |
| 251 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 252 | Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind, |
| 253 | bool RedeclarationOnly) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 254 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 255 | |
| 256 | if (!getLangOptions().CPlusPlus) { |
| 257 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 258 | // search in the declarations attached to the name. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 259 | unsigned IDNS = 0; |
| 260 | switch (NameKind) { |
| 261 | case Sema::LookupOrdinaryName: |
| 262 | IDNS = Decl::IDNS_Ordinary; |
| 263 | break; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 265 | case Sema::LookupTagName: |
| 266 | IDNS = Decl::IDNS_Tag; |
| 267 | break; |
| 268 | |
| 269 | case Sema::LookupMemberName: |
| 270 | IDNS = Decl::IDNS_Member; |
| 271 | break; |
| 272 | |
| 273 | case Sema::LookupNestedNameSpecifierName: |
| 274 | case Sema::LookupNamespaceName: |
| 275 | assert(false && "C does not perform these kinds of name lookup"); |
| 276 | break; |
| 277 | } |
| 278 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 279 | // Scan up the scope chain looking for a decl that matches this |
| 280 | // identifier that is in the appropriate namespace. This search |
| 281 | // should not take long, as shadowing of names is uncommon, and |
| 282 | // deep shadowing is extremely uncommon. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 283 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
| 284 | IEnd = IdResolver.end(); |
| 285 | I != IEnd; ++I) |
| 286 | if ((*I)->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 287 | return LookupResult::CreateLookupResult(Context, *I); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 288 | } else { |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 289 | unsigned IDNS |
| 290 | = getIdentifierNamespacesFromLookupNameKind(NameKind, |
| 291 | getLangOptions().CPlusPlus); |
| 292 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 293 | // Unqualified name lookup in C++ requires looking into scopes |
| 294 | // that aren't strictly lexical, and therefore we walk through the |
| 295 | // context as well as walking through the scopes. |
| 296 | |
| 297 | // FIXME: does "true" for LookInParentCtx actually make sense? |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 298 | IdentifierResolver::iterator I = IdResolver.begin(Name), |
| 299 | IEnd = IdResolver.end(); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 300 | for (; S; S = S->getParent()) { |
| 301 | // Check whether the IdResolver has anything in this scope. |
| 302 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 303 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 304 | // We found something. Look for anything else in our scope |
| 305 | // with this same name and in an acceptable identifier |
| 306 | // namespace, so that we can construct an overload set if we |
| 307 | // need to. |
| 308 | IdentifierResolver::iterator LastI = I; |
| 309 | for (++LastI; LastI != IEnd; ++LastI) { |
| 310 | if (!S->isDeclScope(*LastI)) |
| 311 | break; |
| 312 | } |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 313 | return LookupResult::CreateLookupResult(Context, I, LastI); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | // If there is an entity associated with this scope, it's a |
| 318 | // DeclContext. We might need to perform qualified lookup into |
| 319 | // it. |
| 320 | // FIXME: We're performing redundant lookups here, where the |
| 321 | // scope stack mirrors the semantic nested of classes and |
| 322 | // namespaces. We can save some work by checking the lexical |
| 323 | // scope against the semantic scope and avoiding any lookups |
| 324 | // when they are the same. |
| 325 | // FIXME: In some cases, we know that every name that could be |
| 326 | // found by this qualified name lookup will also be on the |
| 327 | // identifier chain. For example, inside a class without any |
| 328 | // base classes, we never need to perform qualified lookup |
| 329 | // because all of the members are on top of the identifier |
| 330 | // chain. However, we cannot perform this optimization when the |
| 331 | // lexical and semantic scopes don't line up, e.g., in an |
| 332 | // out-of-line member definition. |
| 333 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 334 | while (Ctx && Ctx->isFunctionOrMethod()) |
| 335 | Ctx = Ctx->getParent(); |
| 336 | while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) { |
| 337 | // Look for declarations of this name in this scope. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 338 | if (LookupResult Result = LookupQualifiedName(Ctx, Name, NameKind, |
| 339 | RedeclarationOnly)) |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 340 | return Result; |
| 341 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 342 | if (RedeclarationOnly && !Ctx->isTransparentContext()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 343 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 344 | |
| 345 | Ctx = Ctx->getParent(); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // If we didn't find a use of this identifier, and if the identifier |
| 351 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 352 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 353 | if (NameKind == LookupOrdinaryName) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 354 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 355 | if (II) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 356 | // If this is a builtin on this (or all) targets, create the decl. |
| 357 | if (unsigned BuiltinID = II->getBuiltinID()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 358 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 359 | LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 360 | S)); |
| 361 | } |
| 362 | if (getLangOptions().ObjC1 && II) { |
| 363 | // @interface and @compatibility_alias introduce typedef-like names. |
| 364 | // Unlike typedef's, they can only be introduced at file-scope (and are |
| 365 | // therefore not scoped decls). They can, however, be shadowed by |
| 366 | // other names in IDNS_Ordinary. |
| 367 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 368 | if (IDI != ObjCInterfaceDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 369 | return LookupResult::CreateLookupResult(Context, IDI->second); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 370 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 371 | if (I != ObjCAliasDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 372 | return LookupResult::CreateLookupResult(Context, |
| 373 | I->second->getClassInterface()); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 376 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /// @brief Perform qualified name lookup into a given context. |
| 380 | /// |
| 381 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 382 | /// names when the context of those names is explicit specified, e.g., |
| 383 | /// "std::vector" or "x->member". |
| 384 | /// |
| 385 | /// Different lookup criteria can find different names. For example, a |
| 386 | /// particular scope can have both a struct and a function of the same |
| 387 | /// name, and each can be found by certain lookup criteria. For more |
| 388 | /// information about lookup criteria, see the documentation for the |
| 389 | /// class LookupCriteria. |
| 390 | /// |
| 391 | /// @param LookupCtx The context in which qualified name lookup will |
| 392 | /// search. If the lookup criteria permits, name lookup may also search |
| 393 | /// in the parent contexts or (for C++ classes) base classes. |
| 394 | /// |
| 395 | /// @param Name The name of the entity that we are searching for. |
| 396 | /// |
| 397 | /// @param Criteria The criteria that this routine will use to |
| 398 | /// determine which names are visible and which names will be |
| 399 | /// found. Note that name lookup will find a name that is visible by |
| 400 | /// the given criteria, but the entity itself may not be semantically |
| 401 | /// correct or even the kind of entity expected based on the |
| 402 | /// lookup. For example, searching for a nested-name-specifier name |
| 403 | /// might result in an EnumDecl, which is visible but is not permitted |
| 404 | /// as a nested-name-specifier in C++03. |
| 405 | /// |
| 406 | /// @returns The result of name lookup, which includes zero or more |
| 407 | /// declarations and possibly additional information used to diagnose |
| 408 | /// ambiguities. |
| 409 | Sema::LookupResult |
| 410 | Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 411 | LookupNameKind NameKind, bool RedeclarationOnly) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 412 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
| 413 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 414 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 415 | |
| 416 | // If we're performing qualified name lookup (e.g., lookup into a |
| 417 | // struct), find fields as part of ordinary name lookup. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 418 | unsigned IDNS |
| 419 | = getIdentifierNamespacesFromLookupNameKind(NameKind, |
| 420 | getLangOptions().CPlusPlus); |
| 421 | if (NameKind == LookupOrdinaryName) |
| 422 | IDNS |= Decl::IDNS_Member; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 423 | |
| 424 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 425 | DeclContext::lookup_iterator I, E; |
| 426 | for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 427 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 428 | return LookupResult::CreateLookupResult(Context, I, E); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 429 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 430 | // If this isn't a C++ class or we aren't allowed to look into base |
| 431 | // classes, we're done. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 432 | if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 433 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 434 | |
| 435 | // Perform lookup into our base classes. |
| 436 | BasePaths Paths; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 437 | Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx))); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 438 | |
| 439 | // Look for this member in our base classes |
| 440 | if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 441 | MemberLookupCriteria(Name, NameKind, IDNS), Paths)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 442 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 443 | |
| 444 | // C++ [class.member.lookup]p2: |
| 445 | // [...] If the resulting set of declarations are not all from |
| 446 | // sub-objects of the same type, or the set has a nonstatic member |
| 447 | // and includes members from distinct sub-objects, there is an |
| 448 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 449 | // the result of the lookup. |
| 450 | // FIXME: support using declarations! |
| 451 | QualType SubobjectType; |
Daniel Dunbar | ddebeca | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 452 | int SubobjectNumber = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 453 | for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
| 454 | Path != PathEnd; ++Path) { |
| 455 | const BasePathElement &PathElement = Path->back(); |
| 456 | |
| 457 | // Determine whether we're looking at a distinct sub-object or not. |
| 458 | if (SubobjectType.isNull()) { |
| 459 | // This is the first subobject we've looked at. Record it's type. |
| 460 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 461 | SubobjectNumber = PathElement.SubobjectNumber; |
| 462 | } else if (SubobjectType |
| 463 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 464 | // We found members of the given name in two subobjects of |
| 465 | // different types. This lookup is ambiguous. |
| 466 | BasePaths *PathsOnHeap = new BasePaths; |
| 467 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 468 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, true); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 469 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 470 | // We have a different subobject of the same type. |
| 471 | |
| 472 | // C++ [class.member.lookup]p5: |
| 473 | // A static member, a nested type or an enumerator defined in |
| 474 | // a base class T can unambiguously be found even if an object |
| 475 | // has more than one base class subobject of type T. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 476 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 477 | if (isa<VarDecl>(FirstDecl) || |
| 478 | isa<TypeDecl>(FirstDecl) || |
| 479 | isa<EnumConstantDecl>(FirstDecl)) |
| 480 | continue; |
| 481 | |
| 482 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 483 | // Determine whether all of the methods are static. |
| 484 | bool AllMethodsAreStatic = true; |
| 485 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 486 | Func != Path->Decls.second; ++Func) { |
| 487 | if (!isa<CXXMethodDecl>(*Func)) { |
| 488 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 493 | AllMethodsAreStatic = false; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (AllMethodsAreStatic) |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | // We have found a nonstatic member name in multiple, distinct |
| 503 | // subobjects. Name lookup is ambiguous. |
| 504 | BasePaths *PathsOnHeap = new BasePaths; |
| 505 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 506 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | // Lookup in a base class succeeded; return these results. |
| 511 | |
| 512 | // If we found a function declaration, return an overload set. |
| 513 | if (isa<FunctionDecl>(*Paths.front().Decls.first)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 514 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 515 | Paths.front().Decls.first, Paths.front().Decls.second); |
| 516 | |
| 517 | // We found a non-function declaration; return a single declaration. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 518 | return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /// @brief Performs name lookup for a name that was parsed in the |
| 522 | /// source code, and may contain a C++ scope specifier. |
| 523 | /// |
| 524 | /// This routine is a convenience routine meant to be called from |
| 525 | /// contexts that receive a name and an optional C++ scope specifier |
| 526 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 527 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 528 | /// respectively) on the given name and return those results. |
| 529 | /// |
| 530 | /// @param S The scope from which unqualified name lookup will |
| 531 | /// begin. |
| 532 | /// |
| 533 | /// @param SS An optional C++ scope-specified, e.g., "::N::M". |
| 534 | /// |
| 535 | /// @param Name The name of the entity that name lookup will |
| 536 | /// search for. |
| 537 | /// |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 538 | /// @returns The result of qualified or unqualified name lookup. |
| 539 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 540 | Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS, |
| 541 | DeclarationName Name, LookupNameKind NameKind, |
| 542 | bool RedeclarationOnly) { |
| 543 | if (SS) { |
| 544 | if (SS->isInvalid()) |
| 545 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 547 | if (SS->isSet()) |
| 548 | return LookupQualifiedName(static_cast<DeclContext *>(SS->getScopeRep()), |
| 549 | Name, NameKind, RedeclarationOnly); |
| 550 | } |
| 551 | |
| 552 | return LookupName(S, Name, NameKind, RedeclarationOnly); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 555 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 556 | /// from name lookup. |
| 557 | /// |
| 558 | /// @param Result The ambiguous name lookup result. |
| 559 | /// |
| 560 | /// @param Name The name of the entity that name lookup was |
| 561 | /// searching for. |
| 562 | /// |
| 563 | /// @param NameLoc The location of the name within the source code. |
| 564 | /// |
| 565 | /// @param LookupRange A source range that provides more |
| 566 | /// source-location information concerning the lookup itself. For |
| 567 | /// example, this range might highlight a nested-name-specifier that |
| 568 | /// precedes the name. |
| 569 | /// |
| 570 | /// @returns true |
| 571 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name, |
| 572 | SourceLocation NameLoc, |
| 573 | SourceRange LookupRange) { |
| 574 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 575 | |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 576 | BasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 577 | if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) { |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 578 | QualType SubobjectType = Paths->front().back().Base->getType(); |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 579 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 580 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 581 | << LookupRange; |
| 582 | |
| 583 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 584 | while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic()) |
| 585 | ++Found; |
| 586 | |
| 587 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 589 | return true; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes && |
| 593 | "Unhandled form of name lookup ambiguity"); |
| 594 | |
| 595 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 596 | << Name << LookupRange; |
| 597 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 598 | std::set<Decl *> DeclsPrinted; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 599 | for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end(); |
| 600 | Path != PathEnd; ++Path) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 601 | Decl *D = *Path->Decls.first; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 602 | if (DeclsPrinted.insert(D).second) |
| 603 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 604 | } |
| 605 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 606 | delete Paths; |
| 607 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 608 | return true; |
| 609 | } |