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