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