Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- 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 defines the code-completion semantic actions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "Sema.h" |
| 14 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
| 17 | #include <list> |
| 18 | #include <map> |
| 19 | #include <vector> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | /// \brief Set the code-completion consumer for semantic analysis. |
| 24 | void Sema::setCodeCompleteConsumer(CodeCompleteConsumer *CCC) { |
| 25 | assert(((CodeCompleter != 0) != (CCC != 0)) && |
| 26 | "Already set or cleared a code-completion consumer?"); |
| 27 | CodeCompleter = CCC; |
| 28 | } |
| 29 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | /// \brief A container of code-completion results. |
| 32 | class ResultBuilder { |
| 33 | public: |
| 34 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 35 | /// name-lookup routines to specify which declarations should be included in |
| 36 | /// the result set (when it returns true) and which declarations should be |
| 37 | /// filtered out (returns false). |
| 38 | typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const; |
| 39 | |
| 40 | typedef CodeCompleteConsumer::Result Result; |
| 41 | |
| 42 | private: |
| 43 | /// \brief The actual results we have found. |
| 44 | std::vector<Result> Results; |
| 45 | |
| 46 | /// \brief A record of all of the declarations we have found and placed |
| 47 | /// into the result set, used to ensure that no declaration ever gets into |
| 48 | /// the result set twice. |
| 49 | llvm::SmallPtrSet<Decl*, 16> AllDeclsFound; |
| 50 | |
| 51 | /// \brief A mapping from declaration names to the declarations that have |
| 52 | /// this name within a particular scope and their index within the list of |
| 53 | /// results. |
| 54 | typedef std::multimap<DeclarationName, |
| 55 | std::pair<NamedDecl *, unsigned> > ShadowMap; |
| 56 | |
| 57 | /// \brief The semantic analysis object for which results are being |
| 58 | /// produced. |
| 59 | Sema &SemaRef; |
| 60 | |
| 61 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 62 | /// results that are not desirable. |
| 63 | LookupFilter Filter; |
| 64 | |
| 65 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 66 | /// different levels of, e.g., the inheritance hierarchy. |
| 67 | std::list<ShadowMap> ShadowMaps; |
| 68 | |
| 69 | public: |
| 70 | explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0) |
| 71 | : SemaRef(SemaRef), Filter(Filter) { } |
| 72 | |
| 73 | /// \brief Set the filter used for code-completion results. |
| 74 | void setFilter(LookupFilter Filter) { |
| 75 | this->Filter = Filter; |
| 76 | } |
| 77 | |
| 78 | typedef std::vector<Result>::iterator iterator; |
| 79 | iterator begin() { return Results.begin(); } |
| 80 | iterator end() { return Results.end(); } |
| 81 | |
| 82 | Result *data() { return Results.empty()? 0 : &Results.front(); } |
| 83 | unsigned size() const { return Results.size(); } |
| 84 | bool empty() const { return Results.empty(); } |
| 85 | |
| 86 | /// \brief Add a new result to this result set (if it isn't already in one |
| 87 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 88 | /// redeclaration). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 89 | /// |
| 90 | /// \param R the result to add (if it is unique). |
| 91 | /// |
| 92 | /// \param R the context in which this result will be named. |
| 93 | void MaybeAddResult(Result R, DeclContext *CurContext = 0); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 94 | |
| 95 | /// \brief Enter into a new scope. |
| 96 | void EnterNewScope(); |
| 97 | |
| 98 | /// \brief Exit from the current scope. |
| 99 | void ExitScope(); |
| 100 | |
| 101 | /// \name Name lookup predicates |
| 102 | /// |
| 103 | /// These predicates can be passed to the name lookup functions to filter the |
| 104 | /// results of name lookup. All of the predicates have the same type, so that |
| 105 | /// |
| 106 | //@{ |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 107 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 108 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 109 | bool IsEnum(NamedDecl *ND) const; |
| 110 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 111 | bool IsUnion(NamedDecl *ND) const; |
| 112 | bool IsNamespace(NamedDecl *ND) const; |
| 113 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 114 | bool IsType(NamedDecl *ND) const; |
| 115 | //@} |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | /// \brief Determines whether the given hidden result could be found with |
| 120 | /// some extra work, e.g., by qualifying the name. |
| 121 | /// |
| 122 | /// \param Hidden the declaration that is hidden by the currenly \p Visible |
| 123 | /// declaration. |
| 124 | /// |
| 125 | /// \param Visible the declaration with the same name that is already visible. |
| 126 | /// |
| 127 | /// \returns true if the hidden result can be found by some mechanism, |
| 128 | /// false otherwise. |
| 129 | static bool canHiddenResultBeFound(const LangOptions &LangOpts, |
| 130 | NamedDecl *Hidden, NamedDecl *Visible) { |
| 131 | // In C, there is no way to refer to a hidden name. |
| 132 | if (!LangOpts.CPlusPlus) |
| 133 | return false; |
| 134 | |
| 135 | DeclContext *HiddenCtx = Hidden->getDeclContext()->getLookupContext(); |
| 136 | |
| 137 | // There is no way to qualify a name declared in a function or method. |
| 138 | if (HiddenCtx->isFunctionOrMethod()) |
| 139 | return false; |
| 140 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 141 | return HiddenCtx != Visible->getDeclContext()->getLookupContext(); |
| 142 | } |
| 143 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 144 | /// \brief Compute the qualification required to get from the current context |
| 145 | /// (\p CurContext) to the target context (\p TargetContext). |
| 146 | /// |
| 147 | /// \param Context the AST context in which the qualification will be used. |
| 148 | /// |
| 149 | /// \param CurContext the context where an entity is being named, which is |
| 150 | /// typically based on the current scope. |
| 151 | /// |
| 152 | /// \param TargetContext the context in which the named entity actually |
| 153 | /// resides. |
| 154 | /// |
| 155 | /// \returns a nested name specifier that refers into the target context, or |
| 156 | /// NULL if no qualification is needed. |
| 157 | static NestedNameSpecifier * |
| 158 | getRequiredQualification(ASTContext &Context, |
| 159 | DeclContext *CurContext, |
| 160 | DeclContext *TargetContext) { |
| 161 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 162 | |
| 163 | for (DeclContext *CommonAncestor = TargetContext; |
| 164 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 165 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 166 | if (CommonAncestor->isTransparentContext() || |
| 167 | CommonAncestor->isFunctionOrMethod()) |
| 168 | continue; |
| 169 | |
| 170 | TargetParents.push_back(CommonAncestor); |
| 171 | } |
| 172 | |
| 173 | NestedNameSpecifier *Result = 0; |
| 174 | while (!TargetParents.empty()) { |
| 175 | DeclContext *Parent = TargetParents.back(); |
| 176 | TargetParents.pop_back(); |
| 177 | |
| 178 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) |
| 179 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
| 180 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 181 | Result = NestedNameSpecifier::Create(Context, Result, |
| 182 | false, |
| 183 | Context.getTypeDeclType(TD).getTypePtr()); |
| 184 | else |
| 185 | assert(Parent->isTranslationUnit()); |
| 186 | } |
| 187 | |
| 188 | return Result; |
| 189 | } |
| 190 | |
| 191 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 192 | if (R.Kind != Result::RK_Declaration) { |
| 193 | // For non-declaration results, just add the result. |
| 194 | Results.push_back(R); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // Look through using declarations. |
| 199 | if (UsingDecl *Using = dyn_cast<UsingDecl>(R.Declaration)) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 200 | return MaybeAddResult(Result(Using->getTargetDecl(), R.Rank, R.Qualifier), |
| 201 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 202 | |
| 203 | // Handle each declaration in an overload set separately. |
| 204 | if (OverloadedFunctionDecl *Ovl |
| 205 | = dyn_cast<OverloadedFunctionDecl>(R.Declaration)) { |
| 206 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 207 | FEnd = Ovl->function_end(); |
| 208 | F != FEnd; ++F) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 209 | MaybeAddResult(Result(*F, R.Rank, R.Qualifier), CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 215 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 216 | |
| 217 | // Friend declarations and declarations introduced due to friends are never |
| 218 | // added as results. |
| 219 | if (isa<FriendDecl>(CanonDecl) || |
| 220 | (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))) |
| 221 | return; |
| 222 | |
| 223 | if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) { |
| 224 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 225 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
| 226 | return; |
| 227 | |
| 228 | // FIXME: Should we filter out other names in the implementation's |
| 229 | // namespace, e.g., those containing a __ or that start with _[A-Z]? |
| 230 | } |
| 231 | |
| 232 | // C++ constructors are never found by name lookup. |
| 233 | if (isa<CXXConstructorDecl>(CanonDecl)) |
| 234 | return; |
| 235 | |
| 236 | // Filter out any unwanted results. |
| 237 | if (Filter && !(this->*Filter)(R.Declaration)) |
| 238 | return; |
| 239 | |
| 240 | ShadowMap &SMap = ShadowMaps.back(); |
| 241 | ShadowMap::iterator I, IEnd; |
| 242 | for (llvm::tie(I, IEnd) = SMap.equal_range(R.Declaration->getDeclName()); |
| 243 | I != IEnd; ++I) { |
| 244 | NamedDecl *ND = I->second.first; |
| 245 | unsigned Index = I->second.second; |
| 246 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 247 | // This is a redeclaration. Always pick the newer declaration. |
| 248 | I->second.first = R.Declaration; |
| 249 | Results[Index].Declaration = R.Declaration; |
| 250 | |
| 251 | // Pick the best rank of the two. |
| 252 | Results[Index].Rank = std::min(Results[Index].Rank, R.Rank); |
| 253 | |
| 254 | // We're done. |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // This is a new declaration in this scope. However, check whether this |
| 260 | // declaration name is hidden by a similarly-named declaration in an outer |
| 261 | // scope. |
| 262 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 263 | --SMEnd; |
| 264 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
| 265 | for (llvm::tie(I, IEnd) = SM->equal_range(R.Declaration->getDeclName()); |
| 266 | I != IEnd; ++I) { |
| 267 | // A tag declaration does not hide a non-tag declaration. |
| 268 | if (I->second.first->getIdentifierNamespace() == Decl::IDNS_Tag && |
| 269 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 270 | Decl::IDNS_ObjCProtocol))) |
| 271 | continue; |
| 272 | |
| 273 | // Protocols are in distinct namespaces from everything else. |
| 274 | if (((I->second.first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 275 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 276 | I->second.first->getIdentifierNamespace() != IDNS) |
| 277 | continue; |
| 278 | |
| 279 | // The newly-added result is hidden by an entry in the shadow map. |
| 280 | if (canHiddenResultBeFound(SemaRef.getLangOptions(), R.Declaration, |
| 281 | I->second.first)) { |
| 282 | // Note that this result was hidden. |
| 283 | R.Hidden = true; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 284 | |
| 285 | if (!R.Qualifier) |
| 286 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 287 | CurContext, |
| 288 | R.Declaration->getDeclContext()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 289 | } else { |
| 290 | // This result was hidden and cannot be found; don't bother adding |
| 291 | // it. |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Make sure that any given declaration only shows up in the result set once. |
| 300 | if (!AllDeclsFound.insert(CanonDecl)) |
| 301 | return; |
| 302 | |
| 303 | // Insert this result into the set of results and into the current shadow |
| 304 | // map. |
| 305 | SMap.insert(std::make_pair(R.Declaration->getDeclName(), |
| 306 | std::make_pair(R.Declaration, Results.size()))); |
| 307 | Results.push_back(R); |
| 308 | } |
| 309 | |
| 310 | /// \brief Enter into a new scope. |
| 311 | void ResultBuilder::EnterNewScope() { |
| 312 | ShadowMaps.push_back(ShadowMap()); |
| 313 | } |
| 314 | |
| 315 | /// \brief Exit from the current scope. |
| 316 | void ResultBuilder::ExitScope() { |
| 317 | ShadowMaps.pop_back(); |
| 318 | } |
| 319 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 320 | /// \brief Determines whether this given declaration will be found by |
| 321 | /// ordinary name lookup. |
| 322 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
| 323 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 324 | if (SemaRef.getLangOptions().CPlusPlus) |
| 325 | IDNS |= Decl::IDNS_Tag; |
| 326 | |
| 327 | return ND->getIdentifierNamespace() & IDNS; |
| 328 | } |
| 329 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 330 | /// \brief Determines whether the given declaration is suitable as the |
| 331 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 332 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 333 | // Allow us to find class templates, too. |
| 334 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 335 | ND = ClassTemplate->getTemplatedDecl(); |
| 336 | |
| 337 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 338 | } |
| 339 | |
| 340 | /// \brief Determines whether the given declaration is an enumeration. |
| 341 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 342 | return isa<EnumDecl>(ND); |
| 343 | } |
| 344 | |
| 345 | /// \brief Determines whether the given declaration is a class or struct. |
| 346 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 347 | // Allow us to find class templates, too. |
| 348 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 349 | ND = ClassTemplate->getTemplatedDecl(); |
| 350 | |
| 351 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 352 | return RD->getTagKind() == TagDecl::TK_class || |
| 353 | RD->getTagKind() == TagDecl::TK_struct; |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | /// \brief Determines whether the given declaration is a union. |
| 359 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 360 | // Allow us to find class templates, too. |
| 361 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 362 | ND = ClassTemplate->getTemplatedDecl(); |
| 363 | |
| 364 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 365 | return RD->getTagKind() == TagDecl::TK_union; |
| 366 | |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | /// \brief Determines whether the given declaration is a namespace. |
| 371 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 372 | return isa<NamespaceDecl>(ND); |
| 373 | } |
| 374 | |
| 375 | /// \brief Determines whether the given declaration is a namespace or |
| 376 | /// namespace alias. |
| 377 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 378 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 379 | } |
| 380 | |
| 381 | /// \brief Brief determines whether the given declaration is a namespace or |
| 382 | /// namespace alias. |
| 383 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
| 384 | return isa<TypeDecl>(ND); |
| 385 | } |
| 386 | |
| 387 | // Find the next outer declaration context corresponding to this scope. |
| 388 | static DeclContext *findOuterContext(Scope *S) { |
| 389 | for (S = S->getParent(); S; S = S->getParent()) |
| 390 | if (S->getEntity()) |
| 391 | return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext(); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /// \brief Collect the results of searching for members within the given |
| 397 | /// declaration context. |
| 398 | /// |
| 399 | /// \param Ctx the declaration context from which we will gather results. |
| 400 | /// |
| 401 | /// \param InitialRank the initial rank given to results in this declaration |
| 402 | /// context. Larger rank values will be used for, e.g., members found in |
| 403 | /// base classes. |
| 404 | /// |
| 405 | /// \param Visited the set of declaration contexts that have already been |
| 406 | /// visited. Declaration contexts will only be visited once. |
| 407 | /// |
| 408 | /// \param Results the result set that will be extended with any results |
| 409 | /// found within this declaration context (and, for a C++ class, its bases). |
| 410 | /// |
| 411 | /// \returns the next higher rank value, after considering all of the |
| 412 | /// names within this declaration context. |
| 413 | static unsigned CollectMemberLookupResults(DeclContext *Ctx, |
| 414 | unsigned InitialRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 415 | DeclContext *CurContext, |
| 416 | llvm::SmallPtrSet<DeclContext *, 16> &Visited, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 417 | ResultBuilder &Results) { |
| 418 | // Make sure we don't visit the same context twice. |
| 419 | if (!Visited.insert(Ctx->getPrimaryContext())) |
| 420 | return InitialRank; |
| 421 | |
| 422 | // Enumerate all of the results in this context. |
| 423 | Results.EnterNewScope(); |
| 424 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 425 | CurCtx = CurCtx->getNextContext()) { |
| 426 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
| 427 | DEnd = CurCtx->decls_end(); |
| 428 | D != DEnd; ++D) { |
| 429 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 430 | Results.MaybeAddResult(CodeCompleteConsumer::Result(ND, InitialRank), |
| 431 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | // Traverse the contexts of inherited classes. |
| 436 | unsigned NextRank = InitialRank; |
| 437 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 438 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
| 439 | BEnd = Record->bases_end(); |
| 440 | B != BEnd; ++B) { |
| 441 | QualType BaseType = B->getType(); |
| 442 | |
| 443 | // Don't look into dependent bases, because name lookup can't look |
| 444 | // there anyway. |
| 445 | if (BaseType->isDependentType()) |
| 446 | continue; |
| 447 | |
| 448 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 449 | if (!Record) |
| 450 | continue; |
| 451 | |
| 452 | // FIXME: It would be nice to be able to determine whether referencing |
| 453 | // a particular member would be ambiguous. For example, given |
| 454 | // |
| 455 | // struct A { int member; }; |
| 456 | // struct B { int member; }; |
| 457 | // struct C : A, B { }; |
| 458 | // |
| 459 | // void f(C *c) { c->### } |
| 460 | // accessing 'member' would result in an ambiguity. However, code |
| 461 | // completion could be smart enough to qualify the member with the |
| 462 | // base class, e.g., |
| 463 | // |
| 464 | // c->B::member |
| 465 | // |
| 466 | // or |
| 467 | // |
| 468 | // c->A::member |
| 469 | |
| 470 | // Collect results from this base class (and its bases). |
| 471 | NextRank = std::max(NextRank, |
| 472 | CollectMemberLookupResults(Record->getDecl(), |
| 473 | InitialRank + 1, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 474 | CurContext, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 475 | Visited, |
| 476 | Results)); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // FIXME: Look into base classes in Objective-C! |
| 481 | |
| 482 | Results.ExitScope(); |
| 483 | return NextRank; |
| 484 | } |
| 485 | |
| 486 | /// \brief Collect the results of searching for members within the given |
| 487 | /// declaration context. |
| 488 | /// |
| 489 | /// \param Ctx the declaration context from which we will gather results. |
| 490 | /// |
| 491 | /// \param InitialRank the initial rank given to results in this declaration |
| 492 | /// context. Larger rank values will be used for, e.g., members found in |
| 493 | /// base classes. |
| 494 | /// |
| 495 | /// \param Results the result set that will be extended with any results |
| 496 | /// found within this declaration context (and, for a C++ class, its bases). |
| 497 | /// |
| 498 | /// \returns the next higher rank value, after considering all of the |
| 499 | /// names within this declaration context. |
| 500 | static unsigned CollectMemberLookupResults(DeclContext *Ctx, |
| 501 | unsigned InitialRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 502 | DeclContext *CurContext, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 503 | ResultBuilder &Results) { |
| 504 | llvm::SmallPtrSet<DeclContext *, 16> Visited; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 505 | return CollectMemberLookupResults(Ctx, InitialRank, CurContext, Visited, |
| 506 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /// \brief Collect the results of searching for declarations within the given |
| 510 | /// scope and its parent scopes. |
| 511 | /// |
| 512 | /// \param S the scope in which we will start looking for declarations. |
| 513 | /// |
| 514 | /// \param InitialRank the initial rank given to results in this scope. |
| 515 | /// Larger rank values will be used for results found in parent scopes. |
| 516 | /// |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 517 | /// \param CurContext the context from which lookup results will be found. |
| 518 | /// |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 519 | /// \param Results the builder object that will receive each result. |
| 520 | static unsigned CollectLookupResults(Scope *S, |
| 521 | TranslationUnitDecl *TranslationUnit, |
| 522 | unsigned InitialRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 523 | DeclContext *CurContext, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 524 | ResultBuilder &Results) { |
| 525 | if (!S) |
| 526 | return InitialRank; |
| 527 | |
| 528 | // FIXME: Using directives! |
| 529 | |
| 530 | unsigned NextRank = InitialRank; |
| 531 | Results.EnterNewScope(); |
| 532 | if (S->getEntity() && |
| 533 | !((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 534 | // Look into this scope's declaration context, along with any of its |
| 535 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 536 | // where we hit the context stored in the next outer scope. |
| 537 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 538 | DeclContext *OuterCtx = findOuterContext(S); |
| 539 | |
| 540 | for (; Ctx && Ctx->getPrimaryContext() != OuterCtx; |
| 541 | Ctx = Ctx->getLookupParent()) { |
| 542 | if (Ctx->isFunctionOrMethod()) |
| 543 | continue; |
| 544 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 545 | NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, CurContext, |
| 546 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 547 | } |
| 548 | } else if (!S->getParent()) { |
| 549 | // Look into the translation unit scope. We walk through the translation |
| 550 | // unit's declaration context, because the Scope itself won't have all of |
| 551 | // the declarations if we loaded a precompiled header. |
| 552 | // FIXME: We would like the translation unit's Scope object to point to the |
| 553 | // translation unit, so we don't need this special "if" branch. However, |
| 554 | // doing so would force the normal C++ name-lookup code to look into the |
| 555 | // translation unit decl when the IdentifierInfo chains would suffice. |
| 556 | // Once we fix that problem (which is part of a more general "don't look |
| 557 | // in DeclContexts unless we have to" optimization), we can eliminate the |
| 558 | // TranslationUnit parameter entirely. |
| 559 | NextRank = CollectMemberLookupResults(TranslationUnit, NextRank + 1, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 560 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 561 | } else { |
| 562 | // Walk through the declarations in this Scope. |
| 563 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 564 | D != DEnd; ++D) { |
| 565 | if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 566 | Results.MaybeAddResult(CodeCompleteConsumer::Result(ND, NextRank), |
| 567 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | NextRank = NextRank + 1; |
| 571 | } |
| 572 | |
| 573 | // Lookup names in the parent scope. |
| 574 | NextRank = CollectLookupResults(S->getParent(), TranslationUnit, NextRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 575 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 576 | Results.ExitScope(); |
| 577 | |
| 578 | return NextRank; |
| 579 | } |
| 580 | |
| 581 | /// \brief Add type specifiers for the current language as keyword results. |
| 582 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, unsigned Rank, |
| 583 | ResultBuilder &Results) { |
| 584 | typedef CodeCompleteConsumer::Result Result; |
| 585 | Results.MaybeAddResult(Result("short", Rank)); |
| 586 | Results.MaybeAddResult(Result("long", Rank)); |
| 587 | Results.MaybeAddResult(Result("signed", Rank)); |
| 588 | Results.MaybeAddResult(Result("unsigned", Rank)); |
| 589 | Results.MaybeAddResult(Result("void", Rank)); |
| 590 | Results.MaybeAddResult(Result("char", Rank)); |
| 591 | Results.MaybeAddResult(Result("int", Rank)); |
| 592 | Results.MaybeAddResult(Result("float", Rank)); |
| 593 | Results.MaybeAddResult(Result("double", Rank)); |
| 594 | Results.MaybeAddResult(Result("enum", Rank)); |
| 595 | Results.MaybeAddResult(Result("struct", Rank)); |
| 596 | Results.MaybeAddResult(Result("union", Rank)); |
| 597 | |
| 598 | if (LangOpts.C99) { |
| 599 | // C99-specific |
| 600 | Results.MaybeAddResult(Result("_Complex", Rank)); |
| 601 | Results.MaybeAddResult(Result("_Imaginary", Rank)); |
| 602 | Results.MaybeAddResult(Result("_Bool", Rank)); |
| 603 | } |
| 604 | |
| 605 | if (LangOpts.CPlusPlus) { |
| 606 | // C++-specific |
| 607 | Results.MaybeAddResult(Result("bool", Rank)); |
| 608 | Results.MaybeAddResult(Result("class", Rank)); |
| 609 | Results.MaybeAddResult(Result("typename", Rank)); |
| 610 | Results.MaybeAddResult(Result("wchar_t", Rank)); |
| 611 | |
| 612 | if (LangOpts.CPlusPlus0x) { |
| 613 | Results.MaybeAddResult(Result("char16_t", Rank)); |
| 614 | Results.MaybeAddResult(Result("char32_t", Rank)); |
| 615 | Results.MaybeAddResult(Result("decltype", Rank)); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // GNU extensions |
| 620 | if (LangOpts.GNUMode) { |
| 621 | // FIXME: Enable when we actually support decimal floating point. |
| 622 | // Results.MaybeAddResult(Result("_Decimal32", Rank)); |
| 623 | // Results.MaybeAddResult(Result("_Decimal64", Rank)); |
| 624 | // Results.MaybeAddResult(Result("_Decimal128", Rank)); |
| 625 | Results.MaybeAddResult(Result("typeof", Rank)); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /// \brief Add function parameter chunks to the given code completion string. |
| 630 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 631 | FunctionDecl *Function, |
| 632 | CodeCompletionString *Result) { |
| 633 | CodeCompletionString *CCStr = Result; |
| 634 | |
| 635 | for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) { |
| 636 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 637 | |
| 638 | if (Param->hasDefaultArg()) { |
| 639 | // When we see an optional default argument, put that argument and |
| 640 | // the remaining default arguments into a new, optional string. |
| 641 | CodeCompletionString *Opt = new CodeCompletionString; |
| 642 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 643 | CCStr = Opt; |
| 644 | } |
| 645 | |
| 646 | if (P != 0) |
| 647 | CCStr->AddTextChunk(", "); |
| 648 | |
| 649 | // Format the placeholder string. |
| 650 | std::string PlaceholderStr; |
| 651 | if (Param->getIdentifier()) |
| 652 | PlaceholderStr = Param->getIdentifier()->getName(); |
| 653 | |
| 654 | Param->getType().getAsStringInternal(PlaceholderStr, |
| 655 | Context.PrintingPolicy); |
| 656 | |
| 657 | // Add the placeholder string. |
| 658 | CCStr->AddPlaceholderChunk(PlaceholderStr.c_str()); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /// \brief Add template parameter chunks to the given code completion string. |
| 663 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 664 | TemplateDecl *Template, |
| 665 | CodeCompletionString *Result, |
| 666 | unsigned MaxParameters = 0) { |
| 667 | CodeCompletionString *CCStr = Result; |
| 668 | bool FirstParameter = true; |
| 669 | |
| 670 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 671 | TemplateParameterList::iterator PEnd = Params->end(); |
| 672 | if (MaxParameters) |
| 673 | PEnd = Params->begin() + MaxParameters; |
| 674 | for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) { |
| 675 | bool HasDefaultArg = false; |
| 676 | std::string PlaceholderStr; |
| 677 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 678 | if (TTP->wasDeclaredWithTypename()) |
| 679 | PlaceholderStr = "typename"; |
| 680 | else |
| 681 | PlaceholderStr = "class"; |
| 682 | |
| 683 | if (TTP->getIdentifier()) { |
| 684 | PlaceholderStr += ' '; |
| 685 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 686 | } |
| 687 | |
| 688 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 689 | } else if (NonTypeTemplateParmDecl *NTTP |
| 690 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 691 | if (NTTP->getIdentifier()) |
| 692 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 693 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 694 | Context.PrintingPolicy); |
| 695 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 696 | } else { |
| 697 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 698 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 699 | |
| 700 | // Since putting the template argument list into the placeholder would |
| 701 | // be very, very long, we just use an abbreviation. |
| 702 | PlaceholderStr = "template<...> class"; |
| 703 | if (TTP->getIdentifier()) { |
| 704 | PlaceholderStr += ' '; |
| 705 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 706 | } |
| 707 | |
| 708 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 709 | } |
| 710 | |
| 711 | if (HasDefaultArg) { |
| 712 | // When we see an optional default argument, put that argument and |
| 713 | // the remaining default arguments into a new, optional string. |
| 714 | CodeCompletionString *Opt = new CodeCompletionString; |
| 715 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 716 | CCStr = Opt; |
| 717 | } |
| 718 | |
| 719 | if (FirstParameter) |
| 720 | FirstParameter = false; |
| 721 | else |
| 722 | CCStr->AddTextChunk(", "); |
| 723 | |
| 724 | // Add the placeholder string. |
| 725 | CCStr->AddPlaceholderChunk(PlaceholderStr.c_str()); |
| 726 | } |
| 727 | } |
| 728 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 729 | /// \brief Add a qualifier to the given code-completion string, if the |
| 730 | /// provided nested-name-specifier is non-NULL. |
| 731 | void AddQualifierToCompletionString(CodeCompletionString *Result, |
| 732 | NestedNameSpecifier *Qualifier, |
| 733 | ASTContext &Context) { |
| 734 | if (!Qualifier) |
| 735 | return; |
| 736 | |
| 737 | std::string PrintedNNS; |
| 738 | { |
| 739 | llvm::raw_string_ostream OS(PrintedNNS); |
| 740 | Qualifier->print(OS, Context.PrintingPolicy); |
| 741 | } |
| 742 | Result->AddTextChunk(PrintedNNS.c_str()); |
| 743 | } |
| 744 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 745 | /// \brief If possible, create a new code completion string for the given |
| 746 | /// result. |
| 747 | /// |
| 748 | /// \returns Either a new, heap-allocated code completion string describing |
| 749 | /// how to use this result, or NULL to indicate that the string or name of the |
| 750 | /// result is all that is needed. |
| 751 | CodeCompletionString * |
| 752 | CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) { |
| 753 | if (Kind != RK_Declaration) |
| 754 | return 0; |
| 755 | |
| 756 | NamedDecl *ND = Declaration; |
| 757 | |
| 758 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
| 759 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 760 | AddQualifierToCompletionString(Result, Qualifier, S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 761 | Result->AddTextChunk(Function->getNameAsString().c_str()); |
| 762 | Result->AddTextChunk("("); |
| 763 | AddFunctionParameterChunks(S.Context, Function, Result); |
| 764 | Result->AddTextChunk(")"); |
| 765 | return Result; |
| 766 | } |
| 767 | |
| 768 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
| 769 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 770 | AddQualifierToCompletionString(Result, Qualifier, S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 771 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 772 | Result->AddTextChunk(Function->getNameAsString().c_str()); |
| 773 | |
| 774 | // Figure out which template parameters are deduced (or have default |
| 775 | // arguments). |
| 776 | llvm::SmallVector<bool, 16> Deduced; |
| 777 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 778 | unsigned LastDeducibleArgument; |
| 779 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 780 | --LastDeducibleArgument) { |
| 781 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 782 | // C++0x: Figure out if the template argument has a default. If so, |
| 783 | // the user doesn't need to type this argument. |
| 784 | // FIXME: We need to abstract template parameters better! |
| 785 | bool HasDefaultArg = false; |
| 786 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| 787 | LastDeducibleArgument - 1); |
| 788 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 789 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 790 | else if (NonTypeTemplateParmDecl *NTTP |
| 791 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 792 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 793 | else { |
| 794 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 795 | HasDefaultArg |
| 796 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
| 797 | } |
| 798 | |
| 799 | if (!HasDefaultArg) |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | if (LastDeducibleArgument) { |
| 805 | // Some of the function template arguments cannot be deduced from a |
| 806 | // function call, so we introduce an explicit template argument list |
| 807 | // containing all of the arguments up to the first deducible argument. |
| 808 | Result->AddTextChunk("<"); |
| 809 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 810 | LastDeducibleArgument); |
| 811 | Result->AddTextChunk(">"); |
| 812 | } |
| 813 | |
| 814 | // Add the function parameters |
| 815 | Result->AddTextChunk("("); |
| 816 | AddFunctionParameterChunks(S.Context, Function, Result); |
| 817 | Result->AddTextChunk(")"); |
| 818 | return Result; |
| 819 | } |
| 820 | |
| 821 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
| 822 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 823 | AddQualifierToCompletionString(Result, Qualifier, S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 824 | Result->AddTextChunk(Template->getNameAsString().c_str()); |
| 825 | Result->AddTextChunk("<"); |
| 826 | AddTemplateParameterChunks(S.Context, Template, Result); |
| 827 | Result->AddTextChunk(">"); |
| 828 | return Result; |
| 829 | } |
| 830 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 831 | if (Qualifier) { |
| 832 | CodeCompletionString *Result = new CodeCompletionString; |
| 833 | AddQualifierToCompletionString(Result, Qualifier, S.Context); |
| 834 | Result->AddTextChunk(ND->getNameAsString().c_str()); |
| 835 | return Result; |
| 836 | } |
| 837 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | namespace { |
| 842 | struct SortCodeCompleteResult { |
| 843 | typedef CodeCompleteConsumer::Result Result; |
| 844 | |
| 845 | bool operator()(const Result &X, const Result &Y) const { |
| 846 | // Sort first by rank. |
| 847 | if (X.Rank < Y.Rank) |
| 848 | return true; |
| 849 | else if (X.Rank > Y.Rank) |
| 850 | return false; |
| 851 | |
| 852 | // Result kinds are ordered by decreasing importance. |
| 853 | if (X.Kind < Y.Kind) |
| 854 | return true; |
| 855 | else if (X.Kind > Y.Kind) |
| 856 | return false; |
| 857 | |
| 858 | // Non-hidden names precede hidden names. |
| 859 | if (X.Hidden != Y.Hidden) |
| 860 | return !X.Hidden; |
| 861 | |
| 862 | // Ordering depends on the kind of result. |
| 863 | switch (X.Kind) { |
| 864 | case Result::RK_Declaration: |
| 865 | // Order based on the declaration names. |
| 866 | return X.Declaration->getDeclName() < Y.Declaration->getDeclName(); |
| 867 | |
| 868 | case Result::RK_Keyword: |
| 869 | return strcmp(X.Keyword, Y.Keyword) == -1; |
| 870 | } |
| 871 | |
| 872 | // Silence GCC warning. |
| 873 | return false; |
| 874 | } |
| 875 | }; |
| 876 | } |
| 877 | |
| 878 | static void HandleCodeCompleteResults(CodeCompleteConsumer *CodeCompleter, |
| 879 | CodeCompleteConsumer::Result *Results, |
| 880 | unsigned NumResults) { |
| 881 | // Sort the results by rank/kind/etc. |
| 882 | std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult()); |
| 883 | |
| 884 | if (CodeCompleter) |
| 885 | CodeCompleter->ProcessCodeCompleteResults(Results, NumResults); |
| 886 | } |
| 887 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 888 | void Sema::CodeCompleteOrdinaryName(Scope *S) { |
| 889 | ResultBuilder Results(*this, &ResultBuilder::IsOrdinaryName); |
| 890 | CollectLookupResults(S, Context.getTranslationUnitDecl(), 0, CurContext, |
| 891 | Results); |
| 892 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
| 893 | } |
| 894 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 895 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 896 | SourceLocation OpLoc, |
| 897 | bool IsArrow) { |
| 898 | if (!BaseE || !CodeCompleter) |
| 899 | return; |
| 900 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 901 | typedef CodeCompleteConsumer::Result Result; |
| 902 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 903 | Expr *Base = static_cast<Expr *>(BaseE); |
| 904 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 905 | |
| 906 | if (IsArrow) { |
| 907 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 908 | BaseType = Ptr->getPointeeType(); |
| 909 | else if (BaseType->isObjCObjectPointerType()) |
| 910 | /*Do nothing*/ ; |
| 911 | else |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | ResultBuilder Results(*this); |
| 916 | unsigned NextRank = 0; |
| 917 | |
| 918 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 919 | NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank, |
| 920 | Record->getDecl(), Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 921 | |
| 922 | if (getLangOptions().CPlusPlus) { |
| 923 | if (!Results.empty()) { |
| 924 | // The "template" keyword can follow "->" or "." in the grammar. |
| 925 | // However, we only want to suggest the template keyword if something |
| 926 | // is dependent. |
| 927 | bool IsDependent = BaseType->isDependentType(); |
| 928 | if (!IsDependent) { |
| 929 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 930 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 931 | IsDependent = Ctx->isDependentContext(); |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | if (IsDependent) |
| 937 | Results.MaybeAddResult(Result("template", NextRank++)); |
| 938 | } |
| 939 | |
| 940 | // We could have the start of a nested-name-specifier. Add those |
| 941 | // results as well. |
| 942 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 943 | CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 944 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | // Hand off the results found for code completion. |
| 948 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
| 949 | |
| 950 | // We're done! |
| 951 | return; |
| 952 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 955 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 956 | if (!CodeCompleter) |
| 957 | return; |
| 958 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 959 | typedef CodeCompleteConsumer::Result Result; |
| 960 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 961 | switch ((DeclSpec::TST)TagSpec) { |
| 962 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 963 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 964 | break; |
| 965 | |
| 966 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 967 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 968 | break; |
| 969 | |
| 970 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 971 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 972 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 973 | break; |
| 974 | |
| 975 | default: |
| 976 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 977 | return; |
| 978 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 979 | |
| 980 | ResultBuilder Results(*this, Filter); |
| 981 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 982 | 0, CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 983 | |
| 984 | if (getLangOptions().CPlusPlus) { |
| 985 | // We could have the start of a nested-name-specifier. Add those |
| 986 | // results as well. |
| 987 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 988 | CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 989 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 995 | void Sema::CodeCompleteCase(Scope *S) { |
| 996 | if (getSwitchStack().empty() || !CodeCompleter) |
| 997 | return; |
| 998 | |
| 999 | SwitchStmt *Switch = getSwitchStack().back(); |
| 1000 | if (!Switch->getCond()->getType()->isEnumeralType()) |
| 1001 | return; |
| 1002 | |
| 1003 | // Code-complete the cases of a switch statement over an enumeration type |
| 1004 | // by providing the list of |
| 1005 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 1006 | |
| 1007 | // Determine which enumerators we have already seen in the switch statement. |
| 1008 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 1009 | // token, in case we are code-completing in the middle of the switch and not |
| 1010 | // at the end. However, we aren't able to do so at the moment. |
| 1011 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1012 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1013 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 1014 | SC = SC->getNextSwitchCase()) { |
| 1015 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 1016 | if (!Case) |
| 1017 | continue; |
| 1018 | |
| 1019 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 1020 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 1021 | if (EnumConstantDecl *Enumerator |
| 1022 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 1023 | // We look into the AST of the case statement to determine which |
| 1024 | // enumerator was named. Alternatively, we could compute the value of |
| 1025 | // the integral constant expression, then compare it against the |
| 1026 | // values of each enumerator. However, value-based approach would not |
| 1027 | // work as well with C++ templates where enumerators declared within a |
| 1028 | // template are type- and value-dependent. |
| 1029 | EnumeratorsSeen.insert(Enumerator); |
| 1030 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1031 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 1032 | // so that we can reproduce it as part of code completion, e.g., |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1033 | // |
| 1034 | // switch (TagD.getKind()) { |
| 1035 | // case TagDecl::TK_enum: |
| 1036 | // break; |
| 1037 | // case XXX |
| 1038 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1039 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1040 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 1041 | // TK_struct, and TK_class. |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1042 | if (QualifiedDeclRefExpr *QDRE = dyn_cast<QualifiedDeclRefExpr>(DRE)) |
| 1043 | Qualifier = QDRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1047 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 1048 | // If there are no prior enumerators in C++, check whether we have to |
| 1049 | // qualify the names of the enumerators that we suggest, because they |
| 1050 | // may not be visible in this scope. |
| 1051 | Qualifier = getRequiredQualification(Context, CurContext, |
| 1052 | Enum->getDeclContext()); |
| 1053 | |
| 1054 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 1055 | } |
| 1056 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1057 | // Add any enumerators that have not yet been mentioned. |
| 1058 | ResultBuilder Results(*this); |
| 1059 | Results.EnterNewScope(); |
| 1060 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 1061 | EEnd = Enum->enumerator_end(); |
| 1062 | E != EEnd; ++E) { |
| 1063 | if (EnumeratorsSeen.count(*E)) |
| 1064 | continue; |
| 1065 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1066 | Results.MaybeAddResult(CodeCompleteConsumer::Result(*E, 0, Qualifier)); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1067 | } |
| 1068 | Results.ExitScope(); |
| 1069 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1070 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
| 1071 | } |
| 1072 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1073 | namespace { |
| 1074 | struct IsBetterOverloadCandidate { |
| 1075 | Sema &S; |
| 1076 | |
| 1077 | public: |
| 1078 | explicit IsBetterOverloadCandidate(Sema &S) : S(S) { } |
| 1079 | |
| 1080 | bool |
| 1081 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
| 1082 | return S.isBetterOverloadCandidate(X, Y); |
| 1083 | } |
| 1084 | }; |
| 1085 | } |
| 1086 | |
| 1087 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 1088 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 1089 | if (!CodeCompleter) |
| 1090 | return; |
| 1091 | |
| 1092 | Expr *Fn = (Expr *)FnIn; |
| 1093 | Expr **Args = (Expr **)ArgsIn; |
| 1094 | |
| 1095 | // Ignore type-dependent call expressions entirely. |
| 1096 | if (Fn->isTypeDependent() || |
| 1097 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 1098 | return; |
| 1099 | |
| 1100 | NamedDecl *Function; |
| 1101 | DeclarationName UnqualifiedName; |
| 1102 | NestedNameSpecifier *Qualifier; |
| 1103 | SourceRange QualifierRange; |
| 1104 | bool ArgumentDependentLookup; |
| 1105 | bool HasExplicitTemplateArgs; |
| 1106 | const TemplateArgument *ExplicitTemplateArgs; |
| 1107 | unsigned NumExplicitTemplateArgs; |
| 1108 | |
| 1109 | DeconstructCallFunction(Fn, |
| 1110 | Function, UnqualifiedName, Qualifier, QualifierRange, |
| 1111 | ArgumentDependentLookup, HasExplicitTemplateArgs, |
| 1112 | ExplicitTemplateArgs, NumExplicitTemplateArgs); |
| 1113 | |
| 1114 | |
| 1115 | // FIXME: What if we're calling something that isn't a function declaration? |
| 1116 | // FIXME: What if we're calling a pseudo-destructor? |
| 1117 | // FIXME: What if we're calling a member function? |
| 1118 | |
| 1119 | // Build an overload candidate set based on the functions we find. |
| 1120 | OverloadCandidateSet CandidateSet; |
| 1121 | AddOverloadedCallCandidates(Function, UnqualifiedName, |
| 1122 | ArgumentDependentLookup, HasExplicitTemplateArgs, |
| 1123 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 1124 | Args, NumArgs, |
| 1125 | CandidateSet, |
| 1126 | /*PartialOverloading=*/true); |
| 1127 | |
| 1128 | // Sort the overload candidate set by placing the best overloads first. |
| 1129 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
| 1130 | IsBetterOverloadCandidate(*this)); |
| 1131 | |
| 1132 | // Add the remaining viable overload candidates as code-completion reslults. |
| 1133 | typedef CodeCompleteConsumer::Result Result; |
| 1134 | ResultBuilder Results(*this); |
Anders Carlsson | 9075630 | 2009-09-22 17:29:51 +0000 | [diff] [blame^] | 1135 | Results.EnterNewScope(); |
| 1136 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1137 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 1138 | CandEnd = CandidateSet.end(); |
| 1139 | Cand != CandEnd; ++Cand) { |
| 1140 | if (Cand->Viable) |
| 1141 | Results.MaybeAddResult(Result(Cand->Function, 0), 0); |
| 1142 | } |
| 1143 | |
Anders Carlsson | 9075630 | 2009-09-22 17:29:51 +0000 | [diff] [blame^] | 1144 | Results.ExitScope(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1145 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
| 1146 | } |
| 1147 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1148 | void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS, |
| 1149 | bool EnteringContext) { |
| 1150 | if (!SS.getScopeRep() || !CodeCompleter) |
| 1151 | return; |
| 1152 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1153 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 1154 | if (!Ctx) |
| 1155 | return; |
| 1156 | |
| 1157 | ResultBuilder Results(*this); |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1158 | unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Ctx, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1159 | |
| 1160 | // The "template" keyword can follow "::" in the grammar, but only |
| 1161 | // put it into the grammar if the nested-name-specifier is dependent. |
| 1162 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1163 | if (!Results.empty() && NNS->isDependent()) |
| 1164 | Results.MaybeAddResult(CodeCompleteConsumer::Result("template", NextRank)); |
| 1165 | |
| 1166 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1167 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1168 | |
| 1169 | void Sema::CodeCompleteUsing(Scope *S) { |
| 1170 | if (!CodeCompleter) |
| 1171 | return; |
| 1172 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1173 | ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier); |
| 1174 | |
| 1175 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 1176 | if (!S->isClassScope()) |
| 1177 | Results.MaybeAddResult(CodeCompleteConsumer::Result("namespace", 0)); |
| 1178 | |
| 1179 | // After "using", we can see anything that would start a |
| 1180 | // nested-name-specifier. |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1181 | CollectLookupResults(S, Context.getTranslationUnitDecl(), 0, |
| 1182 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1183 | |
| 1184 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 1188 | if (!CodeCompleter) |
| 1189 | return; |
| 1190 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1191 | // After "using namespace", we expect to see a namespace name or namespace |
| 1192 | // alias. |
| 1193 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1194 | CollectLookupResults(S, Context.getTranslationUnitDecl(), 0, CurContext, |
| 1195 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1196 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 1200 | if (!CodeCompleter) |
| 1201 | return; |
| 1202 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1203 | ResultBuilder Results(*this, &ResultBuilder::IsNamespace); |
| 1204 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 1205 | if (!S->getParent()) |
| 1206 | Ctx = Context.getTranslationUnitDecl(); |
| 1207 | |
| 1208 | if (Ctx && Ctx->isFileContext()) { |
| 1209 | // We only want to see those namespaces that have already been defined |
| 1210 | // within this scope, because its likely that the user is creating an |
| 1211 | // extended namespace declaration. Keep track of the most recent |
| 1212 | // definition of each namespace. |
| 1213 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 1214 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 1215 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 1216 | NS != NSEnd; ++NS) |
| 1217 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 1218 | |
| 1219 | // Add the most recent definition (or extended definition) of each |
| 1220 | // namespace to the list of results. |
| 1221 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 1222 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 1223 | NS != NSEnd; ++NS) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1224 | Results.MaybeAddResult(CodeCompleteConsumer::Result(NS->second, 0), |
| 1225 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 1232 | if (!CodeCompleter) |
| 1233 | return; |
| 1234 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1235 | // After "namespace", we expect to see a namespace or alias. |
| 1236 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1237 | CollectLookupResults(S, Context.getTranslationUnitDecl(), 0, CurContext, |
| 1238 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1239 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1242 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 1243 | if (!CodeCompleter) |
| 1244 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1245 | |
| 1246 | typedef CodeCompleteConsumer::Result Result; |
| 1247 | ResultBuilder Results(*this, &ResultBuilder::IsType); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1248 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1249 | // Add the names of overloadable operators. |
| 1250 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 1251 | if (std::strcmp(Spelling, "?")) \ |
| 1252 | Results.MaybeAddResult(Result(Spelling, 0)); |
| 1253 | #include "clang/Basic/OperatorKinds.def" |
| 1254 | |
| 1255 | // Add any type names visible from the current scope |
| 1256 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1257 | 0, CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Add any type specifiers |
| 1260 | AddTypeSpecifierResults(getLangOptions(), 0, Results); |
| 1261 | |
| 1262 | // Add any nested-name-specifiers |
| 1263 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 1264 | CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank + 1, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1265 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1266 | |
| 1267 | HandleCodeCompleteResults(CodeCompleter, Results.data(), Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1268 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1269 | |