Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---- CodeCompleteConsumer.h - Code Completion Interface ----*- C++ -*-===// |
| 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 the CodeCompleteConsumer class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | 56c2dbc | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "Sema.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <algorithm> |
| 22 | #include <string.h> |
| 23 | using namespace clang; |
| 24 | |
| 25 | CodeCompleteConsumer::CodeCompleteConsumer(Sema &S) : SemaRef(S) { |
| 26 | SemaRef.setCodeCompleteConsumer(this); |
| 27 | } |
| 28 | |
| 29 | CodeCompleteConsumer::~CodeCompleteConsumer() { |
| 30 | SemaRef.setCodeCompleteConsumer(0); |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | CodeCompleteConsumer::CodeCompleteMemberReferenceExpr(Scope *S, |
| 35 | QualType BaseType, |
| 36 | bool IsArrow) { |
| 37 | if (IsArrow) { |
| 38 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 39 | BaseType = Ptr->getPointeeType(); |
| 40 | else if (BaseType->isObjCObjectPointerType()) |
| 41 | /*Do nothing*/ ; |
| 42 | else |
| 43 | return; |
| 44 | } |
| 45 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 46 | ResultSet Results(*this); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 47 | unsigned NextRank = 0; |
| 48 | |
| 49 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 50 | NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank, Results); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 51 | |
| 52 | if (getSema().getLangOptions().CPlusPlus) { |
| 53 | if (!Results.empty()) |
| 54 | // The "template" keyword can follow "->" or "." in the grammar. |
| 55 | Results.MaybeAddResult(Result("template", NextRank++)); |
| 56 | |
Douglas Gregor | 945e8d9 | 2009-09-18 17:42:29 +0000 | [diff] [blame] | 57 | // We could have the start of a nested-name-specifier. Add those |
| 58 | // results as well. |
| 59 | Results.setFilter(&CodeCompleteConsumer::IsNestedNameSpecifier); |
| 60 | CollectLookupResults(S, NextRank, Results); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Hand off the results found for code completion. |
| 64 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 65 | |
| 66 | // We're done! |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 71 | void CodeCompleteConsumer::CodeCompleteTag(Scope *S, ElaboratedType::TagKind TK) { |
| 72 | ResultSet::LookupFilter Filter = 0; |
| 73 | switch (TK) { |
| 74 | case ElaboratedType::TK_enum: |
| 75 | Filter = &CodeCompleteConsumer::IsEnum; |
| 76 | break; |
| 77 | |
| 78 | case ElaboratedType::TK_class: |
| 79 | case ElaboratedType::TK_struct: |
| 80 | Filter = &CodeCompleteConsumer::IsClassOrStruct; |
| 81 | break; |
| 82 | |
| 83 | case ElaboratedType::TK_union: |
| 84 | Filter = &CodeCompleteConsumer::IsUnion; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | ResultSet Results(*this, Filter); |
Douglas Gregor | 945e8d9 | 2009-09-18 17:42:29 +0000 | [diff] [blame] | 89 | unsigned NextRank = CollectLookupResults(S, 0, Results); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 945e8d9 | 2009-09-18 17:42:29 +0000 | [diff] [blame] | 91 | if (getSema().getLangOptions().CPlusPlus) { |
| 92 | // We could have the start of a nested-name-specifier. Add those |
| 93 | // results as well. |
| 94 | Results.setFilter(&CodeCompleteConsumer::IsNestedNameSpecifier); |
| 95 | CollectLookupResults(S, NextRank, Results); |
| 96 | } |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 97 | |
| 98 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 99 | } |
| 100 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 101 | void |
| 102 | CodeCompleteConsumer::CodeCompleteQualifiedId(Scope *S, |
| 103 | NestedNameSpecifier *NNS, |
| 104 | bool EnteringContext) { |
| 105 | CXXScopeSpec SS; |
| 106 | SS.setScopeRep(NNS); |
| 107 | DeclContext *Ctx = getSema().computeDeclContext(SS, EnteringContext); |
| 108 | if (!Ctx) |
| 109 | return; |
| 110 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 111 | ResultSet Results(*this); |
| 112 | unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Results); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 113 | |
| 114 | // The "template" keyword can follow "::" in the grammar |
| 115 | if (!Results.empty()) |
| 116 | Results.MaybeAddResult(Result("template", NextRank)); |
| 117 | |
| 118 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 119 | } |
| 120 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame^] | 121 | void CodeCompleteConsumer::CodeCompleteUsing(Scope *S) { |
| 122 | ResultSet Results(*this, &CodeCompleteConsumer::IsNestedNameSpecifier); |
| 123 | |
| 124 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 125 | if (!S->isClassScope()) |
| 126 | Results.MaybeAddResult(Result("namespace", 0)); |
| 127 | |
| 128 | // After "using", we can see anything that would start a |
| 129 | // nested-name-specifier. |
| 130 | CollectLookupResults(S, 0, Results); |
| 131 | |
| 132 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 133 | } |
| 134 | |
| 135 | void CodeCompleteConsumer::CodeCompleteUsingDirective(Scope *S) { |
| 136 | // After "using namespace", we expect to see a namespace name or namespace |
| 137 | // alias. |
| 138 | ResultSet Results(*this, &CodeCompleteConsumer::IsNamespaceOrAlias); |
| 139 | CollectLookupResults(S, 0, Results); |
| 140 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 141 | } |
| 142 | |
| 143 | void CodeCompleteConsumer::CodeCompleteNamespaceDecl(Scope *S) { |
| 144 | ResultSet Results(*this, &CodeCompleteConsumer::IsNamespace); |
| 145 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 146 | if (!S->getParent()) |
| 147 | Ctx = getSema().Context.getTranslationUnitDecl(); |
| 148 | |
| 149 | if (Ctx && Ctx->isFileContext()) { |
| 150 | // We only want to see those namespaces that have already been defined |
| 151 | // within this scope, because its likely that the user is creating an |
| 152 | // extended namespace declaration. Keep track of the most recent |
| 153 | // definition of each namespace. |
| 154 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 155 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 156 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 157 | NS != NSEnd; ++NS) |
| 158 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 159 | |
| 160 | // Add the most recent definition (or extended definition) of each |
| 161 | // namespace to the list of results. |
| 162 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 163 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 164 | NS != NSEnd; ++NS) |
| 165 | Results.MaybeAddResult(Result(NS->second, 0)); |
| 166 | } |
| 167 | |
| 168 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 169 | } |
| 170 | |
| 171 | void CodeCompleteConsumer::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 172 | // After "namespace", we expect to see a namespace or alias. |
| 173 | ResultSet Results(*this, &CodeCompleteConsumer::IsNamespaceOrAlias); |
| 174 | CollectLookupResults(S, 0, Results); |
| 175 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 176 | } |
| 177 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 178 | void CodeCompleteConsumer::ResultSet::MaybeAddResult(Result R) { |
| 179 | if (R.Kind != Result::RK_Declaration) { |
| 180 | // For non-declaration results, just add the result. |
| 181 | Results.push_back(R); |
| 182 | return; |
| 183 | } |
Douglas Gregor | 56c2dbc | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 184 | |
| 185 | // Look through using declarations. |
| 186 | if (UsingDecl *Using = dyn_cast<UsingDecl>(R.Declaration)) |
| 187 | return MaybeAddResult(Result(Using->getTargetDecl(), R.Rank)); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 56c2dbc | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 189 | // Handle each declaration in an overload set separately. |
| 190 | if (OverloadedFunctionDecl *Ovl |
| 191 | = dyn_cast<OverloadedFunctionDecl>(R.Declaration)) { |
| 192 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 193 | FEnd = Ovl->function_end(); |
| 194 | F != FEnd; ++F) |
| 195 | MaybeAddResult(Result(*F, R.Rank)); |
| 196 | |
| 197 | return; |
| 198 | } |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 200 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 201 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 202 | |
| 203 | // Friend declarations and declarations introduced due to friends are never |
| 204 | // added as results. |
| 205 | if (isa<FriendDecl>(CanonDecl) || |
| 206 | (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))) |
| 207 | return; |
| 208 | |
Douglas Gregor | 945e8d9 | 2009-09-18 17:42:29 +0000 | [diff] [blame] | 209 | if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) { |
| 210 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 211 | if (Id->isStr("__va_list_tag")) |
| 212 | return; |
| 213 | |
| 214 | // FIXME: Should we filter out other names in the implementation's |
| 215 | // namespace, e.g., those containing a __ or that start with _[A-Z]? |
| 216 | } |
| 217 | |
| 218 | // C++ constructors are never found by name lookup. |
| 219 | if (isa<CXXConstructorDecl>(CanonDecl)) |
| 220 | return; |
| 221 | |
| 222 | // Filter out any unwanted results. |
| 223 | if (Filter && !(Completer.*Filter)(R.Declaration)) |
| 224 | return; |
| 225 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 226 | ShadowMap &SMap = ShadowMaps.back(); |
| 227 | ShadowMap::iterator I, IEnd; |
| 228 | for (llvm::tie(I, IEnd) = SMap.equal_range(R.Declaration->getDeclName()); |
| 229 | I != IEnd; ++I) { |
| 230 | NamedDecl *ND = I->second.first; |
| 231 | unsigned Index = I->second.second; |
| 232 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 233 | // This is a redeclaration. Always pick the newer declaration. |
| 234 | I->second.first = R.Declaration; |
| 235 | Results[Index].Declaration = R.Declaration; |
| 236 | |
| 237 | // Pick the best rank of the two. |
| 238 | Results[Index].Rank = std::min(Results[Index].Rank, R.Rank); |
| 239 | |
| 240 | // We're done. |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // This is a new declaration in this scope. However, check whether this |
| 246 | // declaration name is hidden by a similarly-named declaration in an outer |
| 247 | // scope. |
| 248 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 249 | --SMEnd; |
| 250 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
| 251 | for (llvm::tie(I, IEnd) = SM->equal_range(R.Declaration->getDeclName()); |
| 252 | I != IEnd; ++I) { |
| 253 | // A tag declaration does not hide a non-tag declaration. |
| 254 | if (I->second.first->getIdentifierNamespace() == Decl::IDNS_Tag && |
| 255 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 256 | Decl::IDNS_ObjCProtocol))) |
| 257 | continue; |
| 258 | |
| 259 | // Protocols are in distinct namespaces from everything else. |
| 260 | if (((I->second.first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 261 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 262 | I->second.first->getIdentifierNamespace() != IDNS) |
| 263 | continue; |
| 264 | |
| 265 | // The newly-added result is hidden by an entry in the shadow map. |
Douglas Gregor | 2da1108 | 2009-09-18 15:51:54 +0000 | [diff] [blame] | 266 | if (Completer.canHiddenResultBeFound(R.Declaration, I->second.first)) { |
| 267 | // Note that this result was hidden. |
| 268 | R.Hidden = true; |
| 269 | } else { |
| 270 | // This result was hidden and cannot be found; don't bother adding |
| 271 | // it. |
| 272 | return; |
| 273 | } |
| 274 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
Douglas Gregor | 945e8d9 | 2009-09-18 17:42:29 +0000 | [diff] [blame] | 279 | // Make sure that any given declaration only shows up in the result set once. |
| 280 | if (!AllDeclsFound.insert(CanonDecl)) |
| 281 | return; |
| 282 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 283 | // Insert this result into the set of results and into the current shadow |
| 284 | // map. |
| 285 | SMap.insert(std::make_pair(R.Declaration->getDeclName(), |
| 286 | std::make_pair(R.Declaration, Results.size()))); |
| 287 | Results.push_back(R); |
| 288 | } |
| 289 | |
| 290 | /// \brief Enter into a new scope. |
| 291 | void CodeCompleteConsumer::ResultSet::EnterNewScope() { |
| 292 | ShadowMaps.push_back(ShadowMap()); |
| 293 | } |
| 294 | |
| 295 | /// \brief Exit from the current scope. |
| 296 | void CodeCompleteConsumer::ResultSet::ExitScope() { |
| 297 | ShadowMaps.pop_back(); |
| 298 | } |
| 299 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 300 | // Find the next outer declaration context corresponding to this scope. |
| 301 | static DeclContext *findOuterContext(Scope *S) { |
| 302 | for (S = S->getParent(); S; S = S->getParent()) |
| 303 | if (S->getEntity()) |
| 304 | return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext(); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /// \brief Collect the results of searching for declarations within the given |
| 310 | /// scope and its parent scopes. |
| 311 | /// |
| 312 | /// \param S the scope in which we will start looking for declarations. |
| 313 | /// |
| 314 | /// \param InitialRank the initial rank given to results in this scope. |
| 315 | /// Larger rank values will be used for results found in parent scopes. |
| 316 | unsigned CodeCompleteConsumer::CollectLookupResults(Scope *S, |
| 317 | unsigned InitialRank, |
| 318 | ResultSet &Results) { |
| 319 | if (!S) |
| 320 | return InitialRank; |
| 321 | |
| 322 | // FIXME: Using directives! |
| 323 | |
| 324 | unsigned NextRank = InitialRank; |
| 325 | Results.EnterNewScope(); |
| 326 | if (S->getEntity() && |
| 327 | !((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 328 | // Look into this scope's declaration context, along with any of its |
| 329 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 330 | // where we hit the context stored in the next outer scope. |
| 331 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 332 | DeclContext *OuterCtx = findOuterContext(S); |
| 333 | |
| 334 | for (; Ctx && Ctx->getPrimaryContext() != OuterCtx; |
| 335 | Ctx = Ctx->getLookupParent()) { |
| 336 | if (Ctx->isFunctionOrMethod()) |
| 337 | continue; |
| 338 | |
| 339 | NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, Results); |
| 340 | } |
| 341 | } else if (!S->getParent()) { |
| 342 | // Look into the translation unit scope. We walk through the translation |
| 343 | // unit's declaration context, because the Scope itself won't have all of |
| 344 | // the declarations if |
| 345 | NextRank = CollectMemberLookupResults( |
| 346 | getSema().Context.getTranslationUnitDecl(), |
| 347 | NextRank + 1, Results); |
| 348 | } else { |
| 349 | // Walk through the declarations in this Scope. |
| 350 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 351 | D != DEnd; ++D) { |
| 352 | if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) |
| 353 | Results.MaybeAddResult(Result(ND, NextRank)); |
| 354 | } |
| 355 | |
| 356 | NextRank = NextRank + 1; |
| 357 | } |
| 358 | |
| 359 | // Lookup names in the parent scope. |
| 360 | NextRank = CollectLookupResults(S->getParent(), NextRank, Results); |
| 361 | Results.ExitScope(); |
| 362 | |
| 363 | return NextRank; |
| 364 | } |
| 365 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 366 | /// \brief Collect the results of searching for members within the given |
| 367 | /// declaration context. |
| 368 | /// |
| 369 | /// \param Ctx the declaration context from which we will gather results. |
| 370 | /// |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 371 | /// \param InitialRank the initial rank given to results in this declaration |
| 372 | /// context. Larger rank values will be used for, e.g., members found in |
| 373 | /// base classes. |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 374 | /// |
| 375 | /// \param Results the result set that will be extended with any results |
| 376 | /// found within this declaration context (and, for a C++ class, its bases). |
| 377 | /// |
| 378 | /// \returns the next higher rank value, after considering all of the |
| 379 | /// names within this declaration context. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 380 | unsigned CodeCompleteConsumer::CollectMemberLookupResults(DeclContext *Ctx, |
Douglas Gregor | 2ecab75 | 2009-09-18 18:07:23 +0000 | [diff] [blame] | 381 | unsigned InitialRank, |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 382 | ResultSet &Results) { |
Douglas Gregor | 2ecab75 | 2009-09-18 18:07:23 +0000 | [diff] [blame] | 383 | llvm::SmallPtrSet<DeclContext *, 16> Visited; |
| 384 | return CollectMemberLookupResults(Ctx, InitialRank, Visited, Results); |
| 385 | } |
| 386 | |
| 387 | /// \brief Collect the results of searching for members within the given |
| 388 | /// declaration context. |
| 389 | /// |
| 390 | /// \param Ctx the declaration context from which we will gather results. |
| 391 | /// |
| 392 | /// \param InitialRank the initial rank given to results in this declaration |
| 393 | /// context. Larger rank values will be used for, e.g., members found in |
| 394 | /// base classes. |
| 395 | /// |
| 396 | /// \param Visited the set of declaration contexts that have already been |
| 397 | /// visited. Declaration contexts will only be visited once. |
| 398 | /// |
| 399 | /// \param Results the result set that will be extended with any results |
| 400 | /// found within this declaration context (and, for a C++ class, its bases). |
| 401 | /// |
| 402 | /// \returns the next higher rank value, after considering all of the |
| 403 | /// names within this declaration context. |
| 404 | unsigned CodeCompleteConsumer::CollectMemberLookupResults(DeclContext *Ctx, |
| 405 | unsigned InitialRank, |
| 406 | llvm::SmallPtrSet<DeclContext *, 16> &Visited, |
| 407 | ResultSet &Results) { |
| 408 | // Make sure we don't visit the same context twice. |
| 409 | if (!Visited.insert(Ctx->getPrimaryContext())) |
| 410 | return InitialRank; |
| 411 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 412 | // Enumerate all of the results in this context. |
| 413 | Results.EnterNewScope(); |
| 414 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 415 | CurCtx = CurCtx->getNextContext()) { |
| 416 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
| 417 | DEnd = CurCtx->decls_end(); |
| 418 | D != DEnd; ++D) { |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 419 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 420 | Results.MaybeAddResult(Result(ND, InitialRank)); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | // Traverse the contexts of inherited classes. |
| 425 | unsigned NextRank = InitialRank; |
| 426 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 427 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
| 428 | BEnd = Record->bases_end(); |
| 429 | B != BEnd; ++B) { |
| 430 | QualType BaseType = B->getType(); |
| 431 | |
| 432 | // Don't look into dependent bases, because name lookup can't look |
| 433 | // there anyway. |
| 434 | if (BaseType->isDependentType()) |
| 435 | continue; |
| 436 | |
| 437 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 438 | if (!Record) |
| 439 | continue; |
| 440 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 441 | // FIXME: It would be nice to be able to determine whether referencing |
| 442 | // a particular member would be ambiguous. For example, given |
| 443 | // |
| 444 | // struct A { int member; }; |
| 445 | // struct B { int member; }; |
| 446 | // struct C : A, B { }; |
| 447 | // |
| 448 | // void f(C *c) { c->### } |
| 449 | // accessing 'member' would result in an ambiguity. However, code |
| 450 | // completion could be smart enough to qualify the member with the |
| 451 | // base class, e.g., |
| 452 | // |
| 453 | // c->B::member |
| 454 | // |
| 455 | // or |
| 456 | // |
| 457 | // c->A::member |
| 458 | |
| 459 | // Collect results from this base class (and its bases). |
| 460 | NextRank = std::max(NextRank, |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 461 | CollectMemberLookupResults(Record->getDecl(), |
Douglas Gregor | 2ecab75 | 2009-09-18 18:07:23 +0000 | [diff] [blame] | 462 | InitialRank + 1, |
| 463 | Visited, |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 464 | Results)); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | // FIXME: Look into base classes in Objective-C! |
| 469 | |
| 470 | Results.ExitScope(); |
| 471 | return NextRank; |
| 472 | } |
| 473 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 474 | /// \brief Determines whether the given declaration is suitable as the |
| 475 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 476 | bool CodeCompleteConsumer::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 477 | // Allow us to find class templates, too. |
| 478 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 479 | ND = ClassTemplate->getTemplatedDecl(); |
| 480 | |
| 481 | return getSema().isAcceptableNestedNameSpecifier(ND); |
| 482 | } |
| 483 | |
| 484 | /// \brief Determines whether the given declaration is an enumeration. |
| 485 | bool CodeCompleteConsumer::IsEnum(NamedDecl *ND) const { |
| 486 | return isa<EnumDecl>(ND); |
| 487 | } |
| 488 | |
| 489 | /// \brief Determines whether the given declaration is a class or struct. |
| 490 | bool CodeCompleteConsumer::IsClassOrStruct(NamedDecl *ND) const { |
| 491 | // Allow us to find class templates, too. |
| 492 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 493 | ND = ClassTemplate->getTemplatedDecl(); |
| 494 | |
| 495 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 496 | return RD->getTagKind() == TagDecl::TK_class || |
| 497 | RD->getTagKind() == TagDecl::TK_struct; |
| 498 | |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | /// \brief Determines whether the given declaration is a union. |
| 503 | bool CodeCompleteConsumer::IsUnion(NamedDecl *ND) const { |
| 504 | // Allow us to find class templates, too. |
| 505 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 506 | ND = ClassTemplate->getTemplatedDecl(); |
| 507 | |
| 508 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 509 | return RD->getTagKind() == TagDecl::TK_union; |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame^] | 514 | /// \brief Determines whether the given declaration is a namespace. |
| 515 | bool CodeCompleteConsumer::IsNamespace(NamedDecl *ND) const { |
| 516 | return isa<NamespaceDecl>(ND); |
| 517 | } |
| 518 | |
| 519 | /// \brief Determines whether the given declaration is a namespace or |
| 520 | /// namespace alias. |
| 521 | bool CodeCompleteConsumer::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 522 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 523 | } |
| 524 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 525 | namespace { |
| 526 | struct VISIBILITY_HIDDEN SortCodeCompleteResult { |
| 527 | typedef CodeCompleteConsumer::Result Result; |
| 528 | |
| 529 | bool operator()(const Result &X, const Result &Y) const { |
| 530 | // Sort first by rank. |
| 531 | if (X.Rank < Y.Rank) |
| 532 | return true; |
| 533 | else if (X.Rank > Y.Rank) |
| 534 | return false; |
| 535 | |
| 536 | // Result kinds are ordered by decreasing importance. |
| 537 | if (X.Kind < Y.Kind) |
| 538 | return true; |
| 539 | else if (X.Kind > Y.Kind) |
| 540 | return false; |
| 541 | |
| 542 | // Non-hidden names precede hidden names. |
| 543 | if (X.Hidden != Y.Hidden) |
| 544 | return !X.Hidden; |
| 545 | |
| 546 | // Ordering depends on the kind of result. |
| 547 | switch (X.Kind) { |
| 548 | case Result::RK_Declaration: |
| 549 | // Order based on the declaration names. |
| 550 | return X.Declaration->getDeclName() < Y.Declaration->getDeclName(); |
| 551 | |
| 552 | case Result::RK_Keyword: |
| 553 | return strcmp(X.Keyword, Y.Keyword) == -1; |
| 554 | } |
| 555 | |
| 556 | // If only our C++ compiler did control-flow warnings properly. |
| 557 | return false; |
| 558 | } |
| 559 | }; |
| 560 | } |
| 561 | |
Douglas Gregor | 2da1108 | 2009-09-18 15:51:54 +0000 | [diff] [blame] | 562 | /// \brief Determines whether the given hidden result could be found with |
| 563 | /// some extra work, e.g., by qualifying the name. |
| 564 | /// |
| 565 | /// \param Hidden the declaration that is hidden by the currenly \p Visible |
| 566 | /// declaration. |
| 567 | /// |
| 568 | /// \param Visible the declaration with the same name that is already visible. |
| 569 | /// |
| 570 | /// \returns true if the hidden result can be found by some mechanism, |
| 571 | /// false otherwise. |
| 572 | bool CodeCompleteConsumer::canHiddenResultBeFound(NamedDecl *Hidden, |
| 573 | NamedDecl *Visible) { |
| 574 | // In C, there is no way to refer to a hidden name. |
| 575 | if (!getSema().getLangOptions().CPlusPlus) |
| 576 | return false; |
| 577 | |
| 578 | DeclContext *HiddenCtx = Hidden->getDeclContext()->getLookupContext(); |
| 579 | |
| 580 | // There is no way to qualify a name declared in a function or method. |
| 581 | if (HiddenCtx->isFunctionOrMethod()) |
| 582 | return false; |
| 583 | |
| 584 | // If the hidden and visible declarations are in different name-lookup |
| 585 | // contexts, then we can qualify the name of the hidden declaration. |
| 586 | // FIXME: Optionally compute the string needed to refer to the hidden |
| 587 | // name. |
| 588 | return HiddenCtx != Visible->getDeclContext()->getLookupContext(); |
| 589 | } |
| 590 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 591 | void |
| 592 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results, |
| 593 | unsigned NumResults) { |
| 594 | // Sort the results by rank/kind/etc. |
| 595 | std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult()); |
| 596 | |
| 597 | // Print the results. |
| 598 | for (unsigned I = 0; I != NumResults; ++I) { |
| 599 | switch (Results[I].Kind) { |
| 600 | case Result::RK_Declaration: |
| 601 | OS << Results[I].Declaration->getNameAsString() << " : " |
| 602 | << Results[I].Rank; |
| 603 | if (Results[I].Hidden) |
| 604 | OS << " (Hidden)"; |
| 605 | OS << '\n'; |
| 606 | break; |
| 607 | |
| 608 | case Result::RK_Keyword: |
| 609 | OS << Results[I].Keyword << " : " << Results[I].Rank << '\n'; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // Once we've printed the code-completion results, suppress remaining |
| 615 | // diagnostics. |
| 616 | // FIXME: Move this somewhere else! |
| 617 | getSema().PP.getDiagnostics().setSuppressAllDiagnostics(); |
| 618 | } |