Douglas Gregor | 81b747b | 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 | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 14 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "Sema.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include <algorithm> |
| 21 | #include <string.h> |
| 22 | using namespace clang; |
| 23 | |
| 24 | CodeCompleteConsumer::CodeCompleteConsumer(Sema &S) : SemaRef(S) { |
| 25 | SemaRef.setCodeCompleteConsumer(this); |
| 26 | } |
| 27 | |
| 28 | CodeCompleteConsumer::~CodeCompleteConsumer() { |
| 29 | SemaRef.setCodeCompleteConsumer(0); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | CodeCompleteConsumer::CodeCompleteMemberReferenceExpr(Scope *S, |
| 34 | QualType BaseType, |
| 35 | bool IsArrow) { |
| 36 | if (IsArrow) { |
| 37 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 38 | BaseType = Ptr->getPointeeType(); |
| 39 | else if (BaseType->isObjCObjectPointerType()) |
| 40 | /*Do nothing*/ ; |
| 41 | else |
| 42 | return; |
| 43 | } |
| 44 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 45 | ResultSet Results(*this); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 46 | unsigned NextRank = 0; |
| 47 | |
| 48 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 49 | NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank, Results); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 50 | |
| 51 | if (getSema().getLangOptions().CPlusPlus) { |
| 52 | if (!Results.empty()) |
| 53 | // The "template" keyword can follow "->" or "." in the grammar. |
| 54 | Results.MaybeAddResult(Result("template", NextRank++)); |
| 55 | |
| 56 | // FIXME: For C++, we also need to look into the current scope, since |
| 57 | // we could have the start of a nested-name-specifier. |
| 58 | } |
| 59 | |
| 60 | // Hand off the results found for code completion. |
| 61 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 62 | |
| 63 | // We're done! |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 68 | void CodeCompleteConsumer::CodeCompleteTag(Scope *S, ElaboratedType::TagKind TK) { |
| 69 | ResultSet::LookupFilter Filter = 0; |
| 70 | switch (TK) { |
| 71 | case ElaboratedType::TK_enum: |
| 72 | Filter = &CodeCompleteConsumer::IsEnum; |
| 73 | break; |
| 74 | |
| 75 | case ElaboratedType::TK_class: |
| 76 | case ElaboratedType::TK_struct: |
| 77 | Filter = &CodeCompleteConsumer::IsClassOrStruct; |
| 78 | break; |
| 79 | |
| 80 | case ElaboratedType::TK_union: |
| 81 | Filter = &CodeCompleteConsumer::IsUnion; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | ResultSet Results(*this, Filter); |
| 86 | CollectLookupResults(S, 0, Results); |
| 87 | |
| 88 | // FIXME: In C++, we could have the start of a nested-name-specifier. |
| 89 | // Add those results (with a poorer rank, naturally). |
| 90 | |
| 91 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 92 | } |
| 93 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 94 | void |
| 95 | CodeCompleteConsumer::CodeCompleteQualifiedId(Scope *S, |
| 96 | NestedNameSpecifier *NNS, |
| 97 | bool EnteringContext) { |
| 98 | CXXScopeSpec SS; |
| 99 | SS.setScopeRep(NNS); |
| 100 | DeclContext *Ctx = getSema().computeDeclContext(SS, EnteringContext); |
| 101 | if (!Ctx) |
| 102 | return; |
| 103 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 104 | ResultSet Results(*this); |
| 105 | unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Results); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 106 | |
| 107 | // The "template" keyword can follow "::" in the grammar |
| 108 | if (!Results.empty()) |
| 109 | Results.MaybeAddResult(Result("template", NextRank)); |
| 110 | |
| 111 | ProcessCodeCompleteResults(Results.data(), Results.size()); |
| 112 | } |
| 113 | |
| 114 | void CodeCompleteConsumer::ResultSet::MaybeAddResult(Result R) { |
| 115 | if (R.Kind != Result::RK_Declaration) { |
| 116 | // For non-declaration results, just add the result. |
| 117 | Results.push_back(R); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // FIXME: Using declarations |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 122 | // FIXME: Separate overload sets |
| 123 | |
| 124 | // Filter out any unwanted results. |
| 125 | if (Filter && !(Completer.*Filter)(R.Declaration)) |
| 126 | return; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 127 | |
| 128 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 129 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 130 | |
| 131 | // Friend declarations and declarations introduced due to friends are never |
| 132 | // added as results. |
| 133 | if (isa<FriendDecl>(CanonDecl) || |
| 134 | (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))) |
| 135 | return; |
| 136 | |
| 137 | ShadowMap &SMap = ShadowMaps.back(); |
| 138 | ShadowMap::iterator I, IEnd; |
| 139 | for (llvm::tie(I, IEnd) = SMap.equal_range(R.Declaration->getDeclName()); |
| 140 | I != IEnd; ++I) { |
| 141 | NamedDecl *ND = I->second.first; |
| 142 | unsigned Index = I->second.second; |
| 143 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 144 | // This is a redeclaration. Always pick the newer declaration. |
| 145 | I->second.first = R.Declaration; |
| 146 | Results[Index].Declaration = R.Declaration; |
| 147 | |
| 148 | // Pick the best rank of the two. |
| 149 | Results[Index].Rank = std::min(Results[Index].Rank, R.Rank); |
| 150 | |
| 151 | // We're done. |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // This is a new declaration in this scope. However, check whether this |
| 157 | // declaration name is hidden by a similarly-named declaration in an outer |
| 158 | // scope. |
| 159 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 160 | --SMEnd; |
| 161 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
| 162 | for (llvm::tie(I, IEnd) = SM->equal_range(R.Declaration->getDeclName()); |
| 163 | I != IEnd; ++I) { |
| 164 | // A tag declaration does not hide a non-tag declaration. |
| 165 | if (I->second.first->getIdentifierNamespace() == Decl::IDNS_Tag && |
| 166 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 167 | Decl::IDNS_ObjCProtocol))) |
| 168 | continue; |
| 169 | |
| 170 | // Protocols are in distinct namespaces from everything else. |
| 171 | if (((I->second.first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 172 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 173 | I->second.first->getIdentifierNamespace() != IDNS) |
| 174 | continue; |
| 175 | |
| 176 | // The newly-added result is hidden by an entry in the shadow map. |
| 177 | R.Hidden = true; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Insert this result into the set of results and into the current shadow |
| 183 | // map. |
| 184 | SMap.insert(std::make_pair(R.Declaration->getDeclName(), |
| 185 | std::make_pair(R.Declaration, Results.size()))); |
| 186 | Results.push_back(R); |
| 187 | } |
| 188 | |
| 189 | /// \brief Enter into a new scope. |
| 190 | void CodeCompleteConsumer::ResultSet::EnterNewScope() { |
| 191 | ShadowMaps.push_back(ShadowMap()); |
| 192 | } |
| 193 | |
| 194 | /// \brief Exit from the current scope. |
| 195 | void CodeCompleteConsumer::ResultSet::ExitScope() { |
| 196 | ShadowMaps.pop_back(); |
| 197 | } |
| 198 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 199 | // Find the next outer declaration context corresponding to this scope. |
| 200 | static DeclContext *findOuterContext(Scope *S) { |
| 201 | for (S = S->getParent(); S; S = S->getParent()) |
| 202 | if (S->getEntity()) |
| 203 | return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext(); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /// \brief Collect the results of searching for declarations within the given |
| 209 | /// scope and its parent scopes. |
| 210 | /// |
| 211 | /// \param S the scope in which we will start looking for declarations. |
| 212 | /// |
| 213 | /// \param InitialRank the initial rank given to results in this scope. |
| 214 | /// Larger rank values will be used for results found in parent scopes. |
| 215 | unsigned CodeCompleteConsumer::CollectLookupResults(Scope *S, |
| 216 | unsigned InitialRank, |
| 217 | ResultSet &Results) { |
| 218 | if (!S) |
| 219 | return InitialRank; |
| 220 | |
| 221 | // FIXME: Using directives! |
| 222 | |
| 223 | unsigned NextRank = InitialRank; |
| 224 | Results.EnterNewScope(); |
| 225 | if (S->getEntity() && |
| 226 | !((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 227 | // Look into this scope's declaration context, along with any of its |
| 228 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 229 | // where we hit the context stored in the next outer scope. |
| 230 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 231 | DeclContext *OuterCtx = findOuterContext(S); |
| 232 | |
| 233 | for (; Ctx && Ctx->getPrimaryContext() != OuterCtx; |
| 234 | Ctx = Ctx->getLookupParent()) { |
| 235 | if (Ctx->isFunctionOrMethod()) |
| 236 | continue; |
| 237 | |
| 238 | NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, Results); |
| 239 | } |
| 240 | } else if (!S->getParent()) { |
| 241 | // Look into the translation unit scope. We walk through the translation |
| 242 | // unit's declaration context, because the Scope itself won't have all of |
| 243 | // the declarations if |
| 244 | NextRank = CollectMemberLookupResults( |
| 245 | getSema().Context.getTranslationUnitDecl(), |
| 246 | NextRank + 1, Results); |
| 247 | } else { |
| 248 | // Walk through the declarations in this Scope. |
| 249 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 250 | D != DEnd; ++D) { |
| 251 | if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) |
| 252 | Results.MaybeAddResult(Result(ND, NextRank)); |
| 253 | } |
| 254 | |
| 255 | NextRank = NextRank + 1; |
| 256 | } |
| 257 | |
| 258 | // Lookup names in the parent scope. |
| 259 | NextRank = CollectLookupResults(S->getParent(), NextRank, Results); |
| 260 | Results.ExitScope(); |
| 261 | |
| 262 | return NextRank; |
| 263 | } |
| 264 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 265 | /// \brief Collect the results of searching for members within the given |
| 266 | /// declaration context. |
| 267 | /// |
| 268 | /// \param Ctx the declaration context from which we will gather results. |
| 269 | /// |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 270 | /// \param InitialRank the initial rank given to results in this declaration |
| 271 | /// context. Larger rank values will be used for, e.g., members found in |
| 272 | /// base classes. |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 273 | /// |
| 274 | /// \param Results the result set that will be extended with any results |
| 275 | /// found within this declaration context (and, for a C++ class, its bases). |
| 276 | /// |
| 277 | /// \returns the next higher rank value, after considering all of the |
| 278 | /// names within this declaration context. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 279 | unsigned CodeCompleteConsumer::CollectMemberLookupResults(DeclContext *Ctx, |
| 280 | unsigned InitialRank, |
| 281 | ResultSet &Results) { |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 282 | // Enumerate all of the results in this context. |
| 283 | Results.EnterNewScope(); |
| 284 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 285 | CurCtx = CurCtx->getNextContext()) { |
| 286 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
| 287 | DEnd = CurCtx->decls_end(); |
| 288 | D != DEnd; ++D) { |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 289 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 290 | Results.MaybeAddResult(Result(ND, InitialRank)); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | // Traverse the contexts of inherited classes. |
| 295 | unsigned NextRank = InitialRank; |
| 296 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 297 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
| 298 | BEnd = Record->bases_end(); |
| 299 | B != BEnd; ++B) { |
| 300 | QualType BaseType = B->getType(); |
| 301 | |
| 302 | // Don't look into dependent bases, because name lookup can't look |
| 303 | // there anyway. |
| 304 | if (BaseType->isDependentType()) |
| 305 | continue; |
| 306 | |
| 307 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 308 | if (!Record) |
| 309 | continue; |
| 310 | |
| 311 | // FIXME: We should keep track of the virtual bases we visit, so |
| 312 | // that we don't visit them more than once. |
| 313 | |
| 314 | // FIXME: It would be nice to be able to determine whether referencing |
| 315 | // a particular member would be ambiguous. For example, given |
| 316 | // |
| 317 | // struct A { int member; }; |
| 318 | // struct B { int member; }; |
| 319 | // struct C : A, B { }; |
| 320 | // |
| 321 | // void f(C *c) { c->### } |
| 322 | // accessing 'member' would result in an ambiguity. However, code |
| 323 | // completion could be smart enough to qualify the member with the |
| 324 | // base class, e.g., |
| 325 | // |
| 326 | // c->B::member |
| 327 | // |
| 328 | // or |
| 329 | // |
| 330 | // c->A::member |
| 331 | |
| 332 | // Collect results from this base class (and its bases). |
| 333 | NextRank = std::max(NextRank, |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 334 | CollectMemberLookupResults(Record->getDecl(), |
| 335 | InitialRank + 1, |
| 336 | Results)); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | // FIXME: Look into base classes in Objective-C! |
| 341 | |
| 342 | Results.ExitScope(); |
| 343 | return NextRank; |
| 344 | } |
| 345 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame^] | 346 | /// \brief Determines whether the given declaration is suitable as the |
| 347 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 348 | bool CodeCompleteConsumer::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 349 | // Allow us to find class templates, too. |
| 350 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 351 | ND = ClassTemplate->getTemplatedDecl(); |
| 352 | |
| 353 | return getSema().isAcceptableNestedNameSpecifier(ND); |
| 354 | } |
| 355 | |
| 356 | /// \brief Determines whether the given declaration is an enumeration. |
| 357 | bool CodeCompleteConsumer::IsEnum(NamedDecl *ND) const { |
| 358 | return isa<EnumDecl>(ND); |
| 359 | } |
| 360 | |
| 361 | /// \brief Determines whether the given declaration is a class or struct. |
| 362 | bool CodeCompleteConsumer::IsClassOrStruct(NamedDecl *ND) const { |
| 363 | // Allow us to find class templates, too. |
| 364 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 365 | ND = ClassTemplate->getTemplatedDecl(); |
| 366 | |
| 367 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 368 | return RD->getTagKind() == TagDecl::TK_class || |
| 369 | RD->getTagKind() == TagDecl::TK_struct; |
| 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | /// \brief Determines whether the given declaration is a union. |
| 375 | bool CodeCompleteConsumer::IsUnion(NamedDecl *ND) const { |
| 376 | // Allow us to find class templates, too. |
| 377 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 378 | ND = ClassTemplate->getTemplatedDecl(); |
| 379 | |
| 380 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 381 | return RD->getTagKind() == TagDecl::TK_union; |
| 382 | |
| 383 | return false; |
| 384 | } |
| 385 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 386 | namespace { |
| 387 | struct VISIBILITY_HIDDEN SortCodeCompleteResult { |
| 388 | typedef CodeCompleteConsumer::Result Result; |
| 389 | |
| 390 | bool operator()(const Result &X, const Result &Y) const { |
| 391 | // Sort first by rank. |
| 392 | if (X.Rank < Y.Rank) |
| 393 | return true; |
| 394 | else if (X.Rank > Y.Rank) |
| 395 | return false; |
| 396 | |
| 397 | // Result kinds are ordered by decreasing importance. |
| 398 | if (X.Kind < Y.Kind) |
| 399 | return true; |
| 400 | else if (X.Kind > Y.Kind) |
| 401 | return false; |
| 402 | |
| 403 | // Non-hidden names precede hidden names. |
| 404 | if (X.Hidden != Y.Hidden) |
| 405 | return !X.Hidden; |
| 406 | |
| 407 | // Ordering depends on the kind of result. |
| 408 | switch (X.Kind) { |
| 409 | case Result::RK_Declaration: |
| 410 | // Order based on the declaration names. |
| 411 | return X.Declaration->getDeclName() < Y.Declaration->getDeclName(); |
| 412 | |
| 413 | case Result::RK_Keyword: |
| 414 | return strcmp(X.Keyword, Y.Keyword) == -1; |
| 415 | } |
| 416 | |
| 417 | // If only our C++ compiler did control-flow warnings properly. |
| 418 | return false; |
| 419 | } |
| 420 | }; |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results, |
| 425 | unsigned NumResults) { |
| 426 | // Sort the results by rank/kind/etc. |
| 427 | std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult()); |
| 428 | |
| 429 | // Print the results. |
| 430 | for (unsigned I = 0; I != NumResults; ++I) { |
| 431 | switch (Results[I].Kind) { |
| 432 | case Result::RK_Declaration: |
| 433 | OS << Results[I].Declaration->getNameAsString() << " : " |
| 434 | << Results[I].Rank; |
| 435 | if (Results[I].Hidden) |
| 436 | OS << " (Hidden)"; |
| 437 | OS << '\n'; |
| 438 | break; |
| 439 | |
| 440 | case Result::RK_Keyword: |
| 441 | OS << Results[I].Keyword << " : " << Results[I].Rank << '\n'; |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Once we've printed the code-completion results, suppress remaining |
| 447 | // diagnostics. |
| 448 | // FIXME: Move this somewhere else! |
| 449 | getSema().PP.getDiagnostics().setSuppressAllDiagnostics(); |
| 450 | } |