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 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 98 | /// \brief Ignore this declaration, if it is seen again. |
| 99 | void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 100 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 101 | /// \name Name lookup predicates |
| 102 | /// |
| 103 | /// These predicates can be passed to the name lookup functions to filter the |
| 104 | /// results of name lookup. All of the predicates have the same type, so that |
| 105 | /// |
| 106 | //@{ |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 107 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 108 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 109 | bool IsEnum(NamedDecl *ND) const; |
| 110 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 111 | bool IsUnion(NamedDecl *ND) const; |
| 112 | bool IsNamespace(NamedDecl *ND) const; |
| 113 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 114 | bool IsType(NamedDecl *ND) const; |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 115 | bool IsMember(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 116 | //@} |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | /// \brief Determines whether the given hidden result could be found with |
| 121 | /// some extra work, e.g., by qualifying the name. |
| 122 | /// |
| 123 | /// \param Hidden the declaration that is hidden by the currenly \p Visible |
| 124 | /// declaration. |
| 125 | /// |
| 126 | /// \param Visible the declaration with the same name that is already visible. |
| 127 | /// |
| 128 | /// \returns true if the hidden result can be found by some mechanism, |
| 129 | /// false otherwise. |
| 130 | static bool canHiddenResultBeFound(const LangOptions &LangOpts, |
| 131 | NamedDecl *Hidden, NamedDecl *Visible) { |
| 132 | // In C, there is no way to refer to a hidden name. |
| 133 | if (!LangOpts.CPlusPlus) |
| 134 | return false; |
| 135 | |
| 136 | DeclContext *HiddenCtx = Hidden->getDeclContext()->getLookupContext(); |
| 137 | |
| 138 | // There is no way to qualify a name declared in a function or method. |
| 139 | if (HiddenCtx->isFunctionOrMethod()) |
| 140 | return false; |
| 141 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 142 | return HiddenCtx != Visible->getDeclContext()->getLookupContext(); |
| 143 | } |
| 144 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 145 | /// \brief Compute the qualification required to get from the current context |
| 146 | /// (\p CurContext) to the target context (\p TargetContext). |
| 147 | /// |
| 148 | /// \param Context the AST context in which the qualification will be used. |
| 149 | /// |
| 150 | /// \param CurContext the context where an entity is being named, which is |
| 151 | /// typically based on the current scope. |
| 152 | /// |
| 153 | /// \param TargetContext the context in which the named entity actually |
| 154 | /// resides. |
| 155 | /// |
| 156 | /// \returns a nested name specifier that refers into the target context, or |
| 157 | /// NULL if no qualification is needed. |
| 158 | static NestedNameSpecifier * |
| 159 | getRequiredQualification(ASTContext &Context, |
| 160 | DeclContext *CurContext, |
| 161 | DeclContext *TargetContext) { |
| 162 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 163 | |
| 164 | for (DeclContext *CommonAncestor = TargetContext; |
| 165 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 166 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 167 | if (CommonAncestor->isTransparentContext() || |
| 168 | CommonAncestor->isFunctionOrMethod()) |
| 169 | continue; |
| 170 | |
| 171 | TargetParents.push_back(CommonAncestor); |
| 172 | } |
| 173 | |
| 174 | NestedNameSpecifier *Result = 0; |
| 175 | while (!TargetParents.empty()) { |
| 176 | DeclContext *Parent = TargetParents.back(); |
| 177 | TargetParents.pop_back(); |
| 178 | |
| 179 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) |
| 180 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
| 181 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 182 | Result = NestedNameSpecifier::Create(Context, Result, |
| 183 | false, |
| 184 | Context.getTypeDeclType(TD).getTypePtr()); |
| 185 | else |
| 186 | assert(Parent->isTranslationUnit()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 187 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 188 | return Result; |
| 189 | } |
| 190 | |
| 191 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 192 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 193 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 194 | if (R.Kind != Result::RK_Declaration) { |
| 195 | // For non-declaration results, just add the result. |
| 196 | Results.push_back(R); |
| 197 | return; |
| 198 | } |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 199 | |
| 200 | // Skip unnamed entities. |
| 201 | if (!R.Declaration->getDeclName()) |
| 202 | return; |
| 203 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 204 | // Look through using declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 205 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 206 | MaybeAddResult(Result(Using->getTargetDecl(), R.Rank, R.Qualifier), |
| 207 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 208 | |
| 209 | // Handle each declaration in an overload set separately. |
| 210 | if (OverloadedFunctionDecl *Ovl |
| 211 | = dyn_cast<OverloadedFunctionDecl>(R.Declaration)) { |
| 212 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 213 | FEnd = Ovl->function_end(); |
| 214 | F != FEnd; ++F) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 215 | MaybeAddResult(Result(*F, R.Rank, R.Qualifier), CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 216 | |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 221 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 222 | |
| 223 | // Friend declarations and declarations introduced due to friends are never |
| 224 | // added as results. |
| 225 | if (isa<FriendDecl>(CanonDecl) || |
| 226 | (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))) |
| 227 | return; |
| 228 | |
| 229 | if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) { |
| 230 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 231 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
| 232 | return; |
| 233 | |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 234 | // Filter out names reserved for the implementation (C99 7.1.3, |
| 235 | // C++ [lib.global.names]). Users don't need to see those. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 236 | // |
| 237 | // FIXME: Add predicate for this. |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 238 | if (Id->getLength() >= 2) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 239 | const char *Name = Id->getNameStart(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 240 | if (Name[0] == '_' && |
| 241 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z'))) |
| 242 | return; |
| 243 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // C++ constructors are never found by name lookup. |
| 247 | if (isa<CXXConstructorDecl>(CanonDecl)) |
| 248 | return; |
| 249 | |
| 250 | // Filter out any unwanted results. |
| 251 | if (Filter && !(this->*Filter)(R.Declaration)) |
| 252 | return; |
| 253 | |
| 254 | ShadowMap &SMap = ShadowMaps.back(); |
| 255 | ShadowMap::iterator I, IEnd; |
| 256 | for (llvm::tie(I, IEnd) = SMap.equal_range(R.Declaration->getDeclName()); |
| 257 | I != IEnd; ++I) { |
| 258 | NamedDecl *ND = I->second.first; |
| 259 | unsigned Index = I->second.second; |
| 260 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 261 | // This is a redeclaration. Always pick the newer declaration. |
| 262 | I->second.first = R.Declaration; |
| 263 | Results[Index].Declaration = R.Declaration; |
| 264 | |
| 265 | // Pick the best rank of the two. |
| 266 | Results[Index].Rank = std::min(Results[Index].Rank, R.Rank); |
| 267 | |
| 268 | // We're done. |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // This is a new declaration in this scope. However, check whether this |
| 274 | // declaration name is hidden by a similarly-named declaration in an outer |
| 275 | // scope. |
| 276 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 277 | --SMEnd; |
| 278 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
| 279 | for (llvm::tie(I, IEnd) = SM->equal_range(R.Declaration->getDeclName()); |
| 280 | I != IEnd; ++I) { |
| 281 | // A tag declaration does not hide a non-tag declaration. |
| 282 | if (I->second.first->getIdentifierNamespace() == Decl::IDNS_Tag && |
| 283 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 284 | Decl::IDNS_ObjCProtocol))) |
| 285 | continue; |
| 286 | |
| 287 | // Protocols are in distinct namespaces from everything else. |
| 288 | if (((I->second.first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| 289 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| 290 | I->second.first->getIdentifierNamespace() != IDNS) |
| 291 | continue; |
| 292 | |
| 293 | // The newly-added result is hidden by an entry in the shadow map. |
| 294 | if (canHiddenResultBeFound(SemaRef.getLangOptions(), R.Declaration, |
| 295 | I->second.first)) { |
| 296 | // Note that this result was hidden. |
| 297 | R.Hidden = true; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 298 | R.QualifierIsInformative = false; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 299 | |
| 300 | if (!R.Qualifier) |
| 301 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 302 | CurContext, |
| 303 | R.Declaration->getDeclContext()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 304 | } else { |
| 305 | // This result was hidden and cannot be found; don't bother adding |
| 306 | // it. |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Make sure that any given declaration only shows up in the result set once. |
| 315 | if (!AllDeclsFound.insert(CanonDecl)) |
| 316 | return; |
| 317 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 318 | // If the filter is for nested-name-specifiers, then this result starts a |
| 319 | // nested-name-specifier. |
| 320 | if ((Filter == &ResultBuilder::IsNestedNameSpecifier) || |
| 321 | (Filter == &ResultBuilder::IsMember && |
| 322 | isa<CXXRecordDecl>(R.Declaration) && |
| 323 | cast<CXXRecordDecl>(R.Declaration)->isInjectedClassName())) |
| 324 | R.StartsNestedNameSpecifier = true; |
| 325 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 326 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 327 | if (R.QualifierIsInformative && !R.Qualifier && |
| 328 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 329 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 330 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 331 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 332 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 333 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 334 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 335 | else |
| 336 | R.QualifierIsInformative = false; |
| 337 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 339 | // Insert this result into the set of results and into the current shadow |
| 340 | // map. |
| 341 | SMap.insert(std::make_pair(R.Declaration->getDeclName(), |
| 342 | std::make_pair(R.Declaration, Results.size()))); |
| 343 | Results.push_back(R); |
| 344 | } |
| 345 | |
| 346 | /// \brief Enter into a new scope. |
| 347 | void ResultBuilder::EnterNewScope() { |
| 348 | ShadowMaps.push_back(ShadowMap()); |
| 349 | } |
| 350 | |
| 351 | /// \brief Exit from the current scope. |
| 352 | void ResultBuilder::ExitScope() { |
| 353 | ShadowMaps.pop_back(); |
| 354 | } |
| 355 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 356 | /// \brief Determines whether this given declaration will be found by |
| 357 | /// ordinary name lookup. |
| 358 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
| 359 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 360 | if (SemaRef.getLangOptions().CPlusPlus) |
| 361 | IDNS |= Decl::IDNS_Tag; |
| 362 | |
| 363 | return ND->getIdentifierNamespace() & IDNS; |
| 364 | } |
| 365 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 366 | /// \brief Determines whether the given declaration is suitable as the |
| 367 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 368 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 369 | // Allow us to find class templates, too. |
| 370 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 371 | ND = ClassTemplate->getTemplatedDecl(); |
| 372 | |
| 373 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 374 | } |
| 375 | |
| 376 | /// \brief Determines whether the given declaration is an enumeration. |
| 377 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 378 | return isa<EnumDecl>(ND); |
| 379 | } |
| 380 | |
| 381 | /// \brief Determines whether the given declaration is a class or struct. |
| 382 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 383 | // Allow us to find class templates, too. |
| 384 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 385 | ND = ClassTemplate->getTemplatedDecl(); |
| 386 | |
| 387 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 388 | return RD->getTagKind() == TagDecl::TK_class || |
| 389 | RD->getTagKind() == TagDecl::TK_struct; |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | /// \brief Determines whether the given declaration is a union. |
| 395 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 396 | // Allow us to find class templates, too. |
| 397 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 398 | ND = ClassTemplate->getTemplatedDecl(); |
| 399 | |
| 400 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 401 | return RD->getTagKind() == TagDecl::TK_union; |
| 402 | |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | /// \brief Determines whether the given declaration is a namespace. |
| 407 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 408 | return isa<NamespaceDecl>(ND); |
| 409 | } |
| 410 | |
| 411 | /// \brief Determines whether the given declaration is a namespace or |
| 412 | /// namespace alias. |
| 413 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 414 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 415 | } |
| 416 | |
| 417 | /// \brief Brief determines whether the given declaration is a namespace or |
| 418 | /// namespace alias. |
| 419 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
| 420 | return isa<TypeDecl>(ND); |
| 421 | } |
| 422 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 423 | /// \brief Since every declaration found within a class is a member that we |
| 424 | /// care about, always returns true. This predicate exists mostly to |
| 425 | /// communicate to the result builder that we are performing a lookup for |
| 426 | /// member access. |
| 427 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
| 428 | return true; |
| 429 | } |
| 430 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 431 | // Find the next outer declaration context corresponding to this scope. |
| 432 | static DeclContext *findOuterContext(Scope *S) { |
| 433 | for (S = S->getParent(); S; S = S->getParent()) |
| 434 | if (S->getEntity()) |
| 435 | return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext(); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /// \brief Collect the results of searching for members within the given |
| 441 | /// declaration context. |
| 442 | /// |
| 443 | /// \param Ctx the declaration context from which we will gather results. |
| 444 | /// |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 445 | /// \param Rank the rank given to results in this declaration context. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 446 | /// |
| 447 | /// \param Visited the set of declaration contexts that have already been |
| 448 | /// visited. Declaration contexts will only be visited once. |
| 449 | /// |
| 450 | /// \param Results the result set that will be extended with any results |
| 451 | /// found within this declaration context (and, for a C++ class, its bases). |
| 452 | /// |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 453 | /// \param InBaseClass whether we are in a base class. |
| 454 | /// |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 455 | /// \returns the next higher rank value, after considering all of the |
| 456 | /// names within this declaration context. |
| 457 | static unsigned CollectMemberLookupResults(DeclContext *Ctx, |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 458 | unsigned Rank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 459 | DeclContext *CurContext, |
| 460 | llvm::SmallPtrSet<DeclContext *, 16> &Visited, |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 461 | ResultBuilder &Results, |
| 462 | bool InBaseClass = false) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 463 | // Make sure we don't visit the same context twice. |
| 464 | if (!Visited.insert(Ctx->getPrimaryContext())) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 465 | return Rank; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 466 | |
| 467 | // Enumerate all of the results in this context. |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 468 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 469 | Results.EnterNewScope(); |
| 470 | for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx; |
| 471 | CurCtx = CurCtx->getNextContext()) { |
| 472 | for (DeclContext::decl_iterator D = CurCtx->decls_begin(), |
Douglas Gregor | ff4393c | 2009-11-09 21:35:27 +0000 | [diff] [blame] | 473 | DEnd = CurCtx->decls_end(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 474 | D != DEnd; ++D) { |
| 475 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 476 | Results.MaybeAddResult(Result(ND, Rank, 0, InBaseClass), CurContext); |
Douglas Gregor | ff4393c | 2009-11-09 21:35:27 +0000 | [diff] [blame] | 477 | |
| 478 | // Visit transparent contexts inside this context. |
| 479 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) { |
| 480 | if (InnerCtx->isTransparentContext()) |
| 481 | CollectMemberLookupResults(InnerCtx, Rank, CurContext, Visited, |
| 482 | Results, InBaseClass); |
| 483 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | // Traverse the contexts of inherited classes. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 488 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 489 | for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(), |
Douglas Gregor | ff4393c | 2009-11-09 21:35:27 +0000 | [diff] [blame] | 490 | BEnd = Record->bases_end(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 491 | B != BEnd; ++B) { |
| 492 | QualType BaseType = B->getType(); |
| 493 | |
| 494 | // Don't look into dependent bases, because name lookup can't look |
| 495 | // there anyway. |
| 496 | if (BaseType->isDependentType()) |
| 497 | continue; |
| 498 | |
| 499 | const RecordType *Record = BaseType->getAs<RecordType>(); |
| 500 | if (!Record) |
| 501 | continue; |
| 502 | |
| 503 | // FIXME: It would be nice to be able to determine whether referencing |
| 504 | // a particular member would be ambiguous. For example, given |
| 505 | // |
| 506 | // struct A { int member; }; |
| 507 | // struct B { int member; }; |
| 508 | // struct C : A, B { }; |
| 509 | // |
| 510 | // void f(C *c) { c->### } |
| 511 | // accessing 'member' would result in an ambiguity. However, code |
| 512 | // completion could be smart enough to qualify the member with the |
| 513 | // base class, e.g., |
| 514 | // |
| 515 | // c->B::member |
| 516 | // |
| 517 | // or |
| 518 | // |
| 519 | // c->A::member |
| 520 | |
| 521 | // Collect results from this base class (and its bases). |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 522 | CollectMemberLookupResults(Record->getDecl(), Rank, CurContext, Visited, |
| 523 | Results, /*InBaseClass=*/true); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | // FIXME: Look into base classes in Objective-C! |
| 528 | |
| 529 | Results.ExitScope(); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 530 | return Rank + 1; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /// \brief Collect the results of searching for members within the given |
| 534 | /// declaration context. |
| 535 | /// |
| 536 | /// \param Ctx the declaration context from which we will gather results. |
| 537 | /// |
| 538 | /// \param InitialRank the initial rank given to results in this declaration |
| 539 | /// context. Larger rank values will be used for, e.g., members found in |
| 540 | /// base classes. |
| 541 | /// |
| 542 | /// \param Results the result set that will be extended with any results |
| 543 | /// found within this declaration context (and, for a C++ class, its bases). |
| 544 | /// |
| 545 | /// \returns the next higher rank value, after considering all of the |
| 546 | /// names within this declaration context. |
| 547 | static unsigned CollectMemberLookupResults(DeclContext *Ctx, |
| 548 | unsigned InitialRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 549 | DeclContext *CurContext, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 550 | ResultBuilder &Results) { |
| 551 | llvm::SmallPtrSet<DeclContext *, 16> Visited; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 552 | return CollectMemberLookupResults(Ctx, InitialRank, CurContext, Visited, |
| 553 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /// \brief Collect the results of searching for declarations within the given |
| 557 | /// scope and its parent scopes. |
| 558 | /// |
| 559 | /// \param S the scope in which we will start looking for declarations. |
| 560 | /// |
| 561 | /// \param InitialRank the initial rank given to results in this scope. |
| 562 | /// Larger rank values will be used for results found in parent scopes. |
| 563 | /// |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 564 | /// \param CurContext the context from which lookup results will be found. |
| 565 | /// |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 566 | /// \param Results the builder object that will receive each result. |
| 567 | static unsigned CollectLookupResults(Scope *S, |
| 568 | TranslationUnitDecl *TranslationUnit, |
| 569 | unsigned InitialRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 570 | DeclContext *CurContext, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 571 | ResultBuilder &Results) { |
| 572 | if (!S) |
| 573 | return InitialRank; |
| 574 | |
| 575 | // FIXME: Using directives! |
| 576 | |
| 577 | unsigned NextRank = InitialRank; |
| 578 | Results.EnterNewScope(); |
| 579 | if (S->getEntity() && |
| 580 | !((DeclContext *)S->getEntity())->isFunctionOrMethod()) { |
| 581 | // Look into this scope's declaration context, along with any of its |
| 582 | // parent lookup contexts (e.g., enclosing classes), up to the point |
| 583 | // where we hit the context stored in the next outer scope. |
| 584 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 585 | DeclContext *OuterCtx = findOuterContext(S); |
| 586 | |
| 587 | for (; Ctx && Ctx->getPrimaryContext() != OuterCtx; |
| 588 | Ctx = Ctx->getLookupParent()) { |
| 589 | if (Ctx->isFunctionOrMethod()) |
| 590 | continue; |
| 591 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 592 | NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, CurContext, |
| 593 | Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 594 | } |
| 595 | } else if (!S->getParent()) { |
| 596 | // Look into the translation unit scope. We walk through the translation |
| 597 | // unit's declaration context, because the Scope itself won't have all of |
| 598 | // the declarations if we loaded a precompiled header. |
| 599 | // FIXME: We would like the translation unit's Scope object to point to the |
| 600 | // translation unit, so we don't need this special "if" branch. However, |
| 601 | // doing so would force the normal C++ name-lookup code to look into the |
| 602 | // translation unit decl when the IdentifierInfo chains would suffice. |
| 603 | // Once we fix that problem (which is part of a more general "don't look |
| 604 | // in DeclContexts unless we have to" optimization), we can eliminate the |
| 605 | // TranslationUnit parameter entirely. |
| 606 | NextRank = CollectMemberLookupResults(TranslationUnit, NextRank + 1, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 607 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 608 | } else { |
| 609 | // Walk through the declarations in this Scope. |
| 610 | for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 611 | D != DEnd; ++D) { |
| 612 | if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get()))) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 613 | Results.MaybeAddResult(CodeCompleteConsumer::Result(ND, NextRank), |
| 614 | CurContext); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | NextRank = NextRank + 1; |
| 618 | } |
| 619 | |
| 620 | // Lookup names in the parent scope. |
| 621 | NextRank = CollectLookupResults(S->getParent(), TranslationUnit, NextRank, |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 622 | CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 623 | Results.ExitScope(); |
| 624 | |
| 625 | return NextRank; |
| 626 | } |
| 627 | |
| 628 | /// \brief Add type specifiers for the current language as keyword results. |
| 629 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, unsigned Rank, |
| 630 | ResultBuilder &Results) { |
| 631 | typedef CodeCompleteConsumer::Result Result; |
| 632 | Results.MaybeAddResult(Result("short", Rank)); |
| 633 | Results.MaybeAddResult(Result("long", Rank)); |
| 634 | Results.MaybeAddResult(Result("signed", Rank)); |
| 635 | Results.MaybeAddResult(Result("unsigned", Rank)); |
| 636 | Results.MaybeAddResult(Result("void", Rank)); |
| 637 | Results.MaybeAddResult(Result("char", Rank)); |
| 638 | Results.MaybeAddResult(Result("int", Rank)); |
| 639 | Results.MaybeAddResult(Result("float", Rank)); |
| 640 | Results.MaybeAddResult(Result("double", Rank)); |
| 641 | Results.MaybeAddResult(Result("enum", Rank)); |
| 642 | Results.MaybeAddResult(Result("struct", Rank)); |
| 643 | Results.MaybeAddResult(Result("union", Rank)); |
| 644 | |
| 645 | if (LangOpts.C99) { |
| 646 | // C99-specific |
| 647 | Results.MaybeAddResult(Result("_Complex", Rank)); |
| 648 | Results.MaybeAddResult(Result("_Imaginary", Rank)); |
| 649 | Results.MaybeAddResult(Result("_Bool", Rank)); |
| 650 | } |
| 651 | |
| 652 | if (LangOpts.CPlusPlus) { |
| 653 | // C++-specific |
| 654 | Results.MaybeAddResult(Result("bool", Rank)); |
| 655 | Results.MaybeAddResult(Result("class", Rank)); |
| 656 | Results.MaybeAddResult(Result("typename", Rank)); |
| 657 | Results.MaybeAddResult(Result("wchar_t", Rank)); |
| 658 | |
| 659 | if (LangOpts.CPlusPlus0x) { |
| 660 | Results.MaybeAddResult(Result("char16_t", Rank)); |
| 661 | Results.MaybeAddResult(Result("char32_t", Rank)); |
| 662 | Results.MaybeAddResult(Result("decltype", Rank)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // GNU extensions |
| 667 | if (LangOpts.GNUMode) { |
| 668 | // FIXME: Enable when we actually support decimal floating point. |
| 669 | // Results.MaybeAddResult(Result("_Decimal32", Rank)); |
| 670 | // Results.MaybeAddResult(Result("_Decimal64", Rank)); |
| 671 | // Results.MaybeAddResult(Result("_Decimal128", Rank)); |
| 672 | Results.MaybeAddResult(Result("typeof", Rank)); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /// \brief Add function parameter chunks to the given code completion string. |
| 677 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 678 | FunctionDecl *Function, |
| 679 | CodeCompletionString *Result) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 680 | typedef CodeCompletionString::Chunk Chunk; |
| 681 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 682 | CodeCompletionString *CCStr = Result; |
| 683 | |
| 684 | for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) { |
| 685 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 686 | |
| 687 | if (Param->hasDefaultArg()) { |
| 688 | // When we see an optional default argument, put that argument and |
| 689 | // the remaining default arguments into a new, optional string. |
| 690 | CodeCompletionString *Opt = new CodeCompletionString; |
| 691 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 692 | CCStr = Opt; |
| 693 | } |
| 694 | |
| 695 | if (P != 0) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 696 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 697 | |
| 698 | // Format the placeholder string. |
| 699 | std::string PlaceholderStr; |
| 700 | if (Param->getIdentifier()) |
| 701 | PlaceholderStr = Param->getIdentifier()->getName(); |
| 702 | |
| 703 | Param->getType().getAsStringInternal(PlaceholderStr, |
| 704 | Context.PrintingPolicy); |
| 705 | |
| 706 | // Add the placeholder string. |
| 707 | CCStr->AddPlaceholderChunk(PlaceholderStr.c_str()); |
| 708 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 709 | |
| 710 | if (const FunctionProtoType *Proto |
| 711 | = Function->getType()->getAs<FunctionProtoType>()) |
| 712 | if (Proto->isVariadic()) |
| 713 | CCStr->AddPlaceholderChunk(", ..."); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// \brief Add template parameter chunks to the given code completion string. |
| 717 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 718 | TemplateDecl *Template, |
| 719 | CodeCompletionString *Result, |
| 720 | unsigned MaxParameters = 0) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 721 | typedef CodeCompletionString::Chunk Chunk; |
| 722 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 723 | CodeCompletionString *CCStr = Result; |
| 724 | bool FirstParameter = true; |
| 725 | |
| 726 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 727 | TemplateParameterList::iterator PEnd = Params->end(); |
| 728 | if (MaxParameters) |
| 729 | PEnd = Params->begin() + MaxParameters; |
| 730 | for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) { |
| 731 | bool HasDefaultArg = false; |
| 732 | std::string PlaceholderStr; |
| 733 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 734 | if (TTP->wasDeclaredWithTypename()) |
| 735 | PlaceholderStr = "typename"; |
| 736 | else |
| 737 | PlaceholderStr = "class"; |
| 738 | |
| 739 | if (TTP->getIdentifier()) { |
| 740 | PlaceholderStr += ' '; |
| 741 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 742 | } |
| 743 | |
| 744 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 745 | } else if (NonTypeTemplateParmDecl *NTTP |
| 746 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 747 | if (NTTP->getIdentifier()) |
| 748 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 749 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 750 | Context.PrintingPolicy); |
| 751 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 752 | } else { |
| 753 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 754 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 755 | |
| 756 | // Since putting the template argument list into the placeholder would |
| 757 | // be very, very long, we just use an abbreviation. |
| 758 | PlaceholderStr = "template<...> class"; |
| 759 | if (TTP->getIdentifier()) { |
| 760 | PlaceholderStr += ' '; |
| 761 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 762 | } |
| 763 | |
| 764 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 765 | } |
| 766 | |
| 767 | if (HasDefaultArg) { |
| 768 | // When we see an optional default argument, put that argument and |
| 769 | // the remaining default arguments into a new, optional string. |
| 770 | CodeCompletionString *Opt = new CodeCompletionString; |
| 771 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 772 | CCStr = Opt; |
| 773 | } |
| 774 | |
| 775 | if (FirstParameter) |
| 776 | FirstParameter = false; |
| 777 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 778 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 779 | |
| 780 | // Add the placeholder string. |
| 781 | CCStr->AddPlaceholderChunk(PlaceholderStr.c_str()); |
| 782 | } |
| 783 | } |
| 784 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 785 | /// \brief Add a qualifier to the given code-completion string, if the |
| 786 | /// provided nested-name-specifier is non-NULL. |
| 787 | void AddQualifierToCompletionString(CodeCompletionString *Result, |
| 788 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 789 | bool QualifierIsInformative, |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 790 | ASTContext &Context) { |
| 791 | if (!Qualifier) |
| 792 | return; |
| 793 | |
| 794 | std::string PrintedNNS; |
| 795 | { |
| 796 | llvm::raw_string_ostream OS(PrintedNNS); |
| 797 | Qualifier->print(OS, Context.PrintingPolicy); |
| 798 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 799 | if (QualifierIsInformative) |
| 800 | Result->AddInformativeChunk(PrintedNNS.c_str()); |
| 801 | else |
| 802 | Result->AddTextChunk(PrintedNNS.c_str()); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 805 | /// \brief If possible, create a new code completion string for the given |
| 806 | /// result. |
| 807 | /// |
| 808 | /// \returns Either a new, heap-allocated code completion string describing |
| 809 | /// how to use this result, or NULL to indicate that the string or name of the |
| 810 | /// result is all that is needed. |
| 811 | CodeCompletionString * |
| 812 | CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 813 | typedef CodeCompletionString::Chunk Chunk; |
| 814 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 815 | if (Kind == RK_Keyword) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 816 | return 0; |
| 817 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 818 | if (Kind == RK_Macro) { |
| 819 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
| 820 | if (!MI || !MI->isFunctionLike()) |
| 821 | return 0; |
| 822 | |
| 823 | // Format a function-like macro with placeholders for the arguments. |
| 824 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 825 | Result->AddTypedTextChunk(Macro->getName().str().c_str()); |
| 826 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 827 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 828 | A != AEnd; ++A) { |
| 829 | if (A != MI->arg_begin()) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 830 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 831 | |
| 832 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 833 | // Non-variadic argument. |
| 834 | Result->AddPlaceholderChunk((*A)->getName().str().c_str()); |
| 835 | continue; |
| 836 | } |
| 837 | |
| 838 | // Variadic argument; cope with the different between GNU and C99 |
| 839 | // variadic macros, providing a single placeholder for the rest of the |
| 840 | // arguments. |
| 841 | if ((*A)->isStr("__VA_ARGS__")) |
| 842 | Result->AddPlaceholderChunk("..."); |
| 843 | else { |
| 844 | std::string Arg = (*A)->getName(); |
| 845 | Arg += "..."; |
| 846 | Result->AddPlaceholderChunk(Arg.c_str()); |
| 847 | } |
| 848 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 849 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 850 | return Result; |
| 851 | } |
| 852 | |
| 853 | assert(Kind == RK_Declaration && "Missed a macro kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 854 | NamedDecl *ND = Declaration; |
| 855 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 856 | if (StartsNestedNameSpecifier) { |
| 857 | CodeCompletionString *Result = new CodeCompletionString; |
| 858 | Result->AddTypedTextChunk(ND->getNameAsString().c_str()); |
| 859 | Result->AddTextChunk("::"); |
| 860 | return Result; |
| 861 | } |
| 862 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 863 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
| 864 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 865 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 866 | S.Context); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 867 | Result->AddTypedTextChunk(Function->getNameAsString().c_str()); |
| 868 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 869 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 870 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 871 | return Result; |
| 872 | } |
| 873 | |
| 874 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
| 875 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 876 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 877 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 878 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 879 | Result->AddTypedTextChunk(Function->getNameAsString().c_str()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 880 | |
| 881 | // Figure out which template parameters are deduced (or have default |
| 882 | // arguments). |
| 883 | llvm::SmallVector<bool, 16> Deduced; |
| 884 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 885 | unsigned LastDeducibleArgument; |
| 886 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 887 | --LastDeducibleArgument) { |
| 888 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 889 | // C++0x: Figure out if the template argument has a default. If so, |
| 890 | // the user doesn't need to type this argument. |
| 891 | // FIXME: We need to abstract template parameters better! |
| 892 | bool HasDefaultArg = false; |
| 893 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| 894 | LastDeducibleArgument - 1); |
| 895 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 896 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 897 | else if (NonTypeTemplateParmDecl *NTTP |
| 898 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 899 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 900 | else { |
| 901 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 902 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 903 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | if (!HasDefaultArg) |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | if (LastDeducibleArgument) { |
| 912 | // Some of the function template arguments cannot be deduced from a |
| 913 | // function call, so we introduce an explicit template argument list |
| 914 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 915 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 916 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 917 | LastDeducibleArgument); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 918 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | // Add the function parameters |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 922 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 923 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 924 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 925 | return Result; |
| 926 | } |
| 927 | |
| 928 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
| 929 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 930 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 931 | S.Context); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 932 | Result->AddTypedTextChunk(Template->getNameAsString().c_str()); |
| 933 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 934 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 935 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 936 | return Result; |
| 937 | } |
| 938 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 939 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 940 | CodeCompletionString *Result = new CodeCompletionString; |
| 941 | Selector Sel = Method->getSelector(); |
| 942 | if (Sel.isUnarySelector()) { |
| 943 | Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 944 | return Result; |
| 945 | } |
| 946 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 947 | std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); |
| 948 | SelName += ':'; |
| 949 | if (StartParameter == 0) |
| 950 | Result->AddTypedTextChunk(SelName); |
| 951 | else { |
| 952 | Result->AddInformativeChunk(SelName); |
| 953 | |
| 954 | // If there is only one parameter, and we're past it, add an empty |
| 955 | // typed-text chunk since there is nothing to type. |
| 956 | if (Method->param_size() == 1) |
| 957 | Result->AddTypedTextChunk(""); |
| 958 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 959 | unsigned Idx = 0; |
| 960 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 961 | PEnd = Method->param_end(); |
| 962 | P != PEnd; (void)++P, ++Idx) { |
| 963 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 964 | std::string Keyword; |
| 965 | if (Idx > StartParameter) |
| 966 | Keyword = " "; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 967 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 968 | Keyword += II->getName().str(); |
| 969 | Keyword += ":"; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 970 | if (Idx < StartParameter || AllParametersAreInformative) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 971 | Result->AddInformativeChunk(Keyword); |
| 972 | } else if (Idx == StartParameter) |
| 973 | Result->AddTypedTextChunk(Keyword); |
| 974 | else |
| 975 | Result->AddTextChunk(Keyword); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 976 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 977 | |
| 978 | // If we're before the starting parameter, skip the placeholder. |
| 979 | if (Idx < StartParameter) |
| 980 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 981 | |
| 982 | std::string Arg; |
| 983 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 984 | Arg = "(" + Arg + ")"; |
| 985 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
| 986 | Arg += II->getName().str(); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 987 | if (AllParametersAreInformative) |
| 988 | Result->AddInformativeChunk(Arg); |
| 989 | else |
| 990 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return Result; |
| 994 | } |
| 995 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 996 | if (Qualifier) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 997 | CodeCompletionString *Result = new CodeCompletionString; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 998 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 999 | S.Context); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1000 | Result->AddTypedTextChunk(ND->getNameAsString().c_str()); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1001 | return Result; |
| 1002 | } |
| 1003 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
| 1006 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1007 | CodeCompletionString * |
| 1008 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 1009 | unsigned CurrentArg, |
| 1010 | Sema &S) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1011 | typedef CodeCompletionString::Chunk Chunk; |
| 1012 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1013 | CodeCompletionString *Result = new CodeCompletionString; |
| 1014 | FunctionDecl *FDecl = getFunction(); |
| 1015 | const FunctionProtoType *Proto |
| 1016 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 1017 | if (!FDecl && !Proto) { |
| 1018 | // Function without a prototype. Just give the return type and a |
| 1019 | // highlighted ellipsis. |
| 1020 | const FunctionType *FT = getFunctionType(); |
| 1021 | Result->AddTextChunk( |
| 1022 | FT->getResultType().getAsString(S.Context.PrintingPolicy).c_str()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1023 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 1024 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 1025 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1026 | return Result; |
| 1027 | } |
| 1028 | |
| 1029 | if (FDecl) |
| 1030 | Result->AddTextChunk(FDecl->getNameAsString().c_str()); |
| 1031 | else |
| 1032 | Result->AddTextChunk( |
| 1033 | Proto->getResultType().getAsString(S.Context.PrintingPolicy).c_str()); |
| 1034 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1035 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1036 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 1037 | for (unsigned I = 0; I != NumParams; ++I) { |
| 1038 | if (I) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1039 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1040 | |
| 1041 | std::string ArgString; |
| 1042 | QualType ArgType; |
| 1043 | |
| 1044 | if (FDecl) { |
| 1045 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 1046 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 1047 | } else { |
| 1048 | ArgType = Proto->getArgType(I); |
| 1049 | } |
| 1050 | |
| 1051 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 1052 | |
| 1053 | if (I == CurrentArg) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1054 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
| 1055 | ArgString.c_str())); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1056 | else |
| 1057 | Result->AddTextChunk(ArgString.c_str()); |
| 1058 | } |
| 1059 | |
| 1060 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1061 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1062 | if (CurrentArg < NumParams) |
| 1063 | Result->AddTextChunk("..."); |
| 1064 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1065 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1066 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1067 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1068 | |
| 1069 | return Result; |
| 1070 | } |
| 1071 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1072 | namespace { |
| 1073 | struct SortCodeCompleteResult { |
| 1074 | typedef CodeCompleteConsumer::Result Result; |
| 1075 | |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1076 | bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const { |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1077 | if (!X.getObjCSelector().isNull() && !Y.getObjCSelector().isNull()) { |
| 1078 | // Consider all selector kinds to be equivalent. |
| 1079 | } else if (X.getNameKind() != Y.getNameKind()) |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1080 | return X.getNameKind() < Y.getNameKind(); |
| 1081 | |
| 1082 | return llvm::LowercaseString(X.getAsString()) |
| 1083 | < llvm::LowercaseString(Y.getAsString()); |
| 1084 | } |
| 1085 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1086 | bool operator()(const Result &X, const Result &Y) const { |
| 1087 | // Sort first by rank. |
| 1088 | if (X.Rank < Y.Rank) |
| 1089 | return true; |
| 1090 | else if (X.Rank > Y.Rank) |
| 1091 | return false; |
| 1092 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1093 | // We use a special ordering for keywords and patterns, based on the |
| 1094 | // typed text. |
| 1095 | if ((X.Kind == Result::RK_Keyword || X.Kind == Result::RK_Pattern) && |
| 1096 | (Y.Kind == Result::RK_Keyword || Y.Kind == Result::RK_Pattern)) { |
| 1097 | const char *XStr = (X.Kind == Result::RK_Keyword)? X.Keyword |
| 1098 | : X.Pattern->getTypedText(); |
| 1099 | const char *YStr = (Y.Kind == Result::RK_Keyword)? Y.Keyword |
| 1100 | : Y.Pattern->getTypedText(); |
| 1101 | return strcmp(XStr, YStr) < 0; |
| 1102 | } |
| 1103 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1104 | // Result kinds are ordered by decreasing importance. |
| 1105 | if (X.Kind < Y.Kind) |
| 1106 | return true; |
| 1107 | else if (X.Kind > Y.Kind) |
| 1108 | return false; |
| 1109 | |
| 1110 | // Non-hidden names precede hidden names. |
| 1111 | if (X.Hidden != Y.Hidden) |
| 1112 | return !X.Hidden; |
| 1113 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1114 | // Non-nested-name-specifiers precede nested-name-specifiers. |
| 1115 | if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier) |
| 1116 | return !X.StartsNestedNameSpecifier; |
| 1117 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1118 | // Ordering depends on the kind of result. |
| 1119 | switch (X.Kind) { |
| 1120 | case Result::RK_Declaration: |
| 1121 | // Order based on the declaration names. |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1122 | return isEarlierDeclarationName(X.Declaration->getDeclName(), |
| 1123 | Y.Declaration->getDeclName()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1125 | case Result::RK_Macro: |
| 1126 | return llvm::LowercaseString(X.Macro->getName()) < |
| 1127 | llvm::LowercaseString(Y.Macro->getName()); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1128 | |
| 1129 | case Result::RK_Keyword: |
| 1130 | case Result::RK_Pattern: |
| 1131 | llvm::llvm_unreachable("Result kinds handled above"); |
| 1132 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | // Silence GCC warning. |
| 1136 | return false; |
| 1137 | } |
| 1138 | }; |
| 1139 | } |
| 1140 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1141 | static void AddMacroResults(Preprocessor &PP, unsigned Rank, |
| 1142 | ResultBuilder &Results) { |
| 1143 | Results.EnterNewScope(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1144 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 1145 | MEnd = PP.macro_end(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1146 | M != MEnd; ++M) |
| 1147 | Results.MaybeAddResult(CodeCompleteConsumer::Result(M->first, Rank)); |
| 1148 | Results.ExitScope(); |
| 1149 | } |
| 1150 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1151 | static void HandleCodeCompleteResults(Sema *S, |
| 1152 | CodeCompleteConsumer *CodeCompleter, |
| 1153 | CodeCompleteConsumer::Result *Results, |
| 1154 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1155 | // Sort the results by rank/kind/etc. |
| 1156 | std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult()); |
| 1157 | |
| 1158 | if (CodeCompleter) |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1159 | CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1160 | |
| 1161 | for (unsigned I = 0; I != NumResults; ++I) |
| 1162 | Results[I].Destroy(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1165 | void Sema::CodeCompleteOrdinaryName(Scope *S) { |
| 1166 | ResultBuilder Results(*this, &ResultBuilder::IsOrdinaryName); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1167 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1168 | 0, CurContext, Results); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1169 | if (CodeCompleter->includeMacros()) |
| 1170 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1171 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1174 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1175 | bool AllowCategories, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1176 | DeclContext *CurContext, |
| 1177 | ResultBuilder &Results) { |
| 1178 | typedef CodeCompleteConsumer::Result Result; |
| 1179 | |
| 1180 | // Add properties in this container. |
| 1181 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 1182 | PEnd = Container->prop_end(); |
| 1183 | P != PEnd; |
| 1184 | ++P) |
| 1185 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 1186 | |
| 1187 | // Add properties in referenced protocols. |
| 1188 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 1189 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 1190 | PEnd = Protocol->protocol_end(); |
| 1191 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1192 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1193 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1194 | if (AllowCategories) { |
| 1195 | // Look through categories. |
| 1196 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 1197 | Category; Category = Category->getNextClassCategory()) |
| 1198 | AddObjCProperties(Category, AllowCategories, CurContext, Results); |
| 1199 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Look through protocols. |
| 1202 | for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(), |
| 1203 | E = IFace->protocol_end(); |
| 1204 | I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1205 | AddObjCProperties(*I, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Look in the superclass. |
| 1208 | if (IFace->getSuperClass()) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1209 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
| 1210 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1211 | } else if (const ObjCCategoryDecl *Category |
| 1212 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 1213 | // Look through protocols. |
| 1214 | for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(), |
| 1215 | PEnd = Category->protocol_end(); |
| 1216 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1217 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1221 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 1222 | SourceLocation OpLoc, |
| 1223 | bool IsArrow) { |
| 1224 | if (!BaseE || !CodeCompleter) |
| 1225 | return; |
| 1226 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1227 | typedef CodeCompleteConsumer::Result Result; |
| 1228 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1229 | Expr *Base = static_cast<Expr *>(BaseE); |
| 1230 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1231 | |
| 1232 | if (IsArrow) { |
| 1233 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 1234 | BaseType = Ptr->getPointeeType(); |
| 1235 | else if (BaseType->isObjCObjectPointerType()) |
| 1236 | /*Do nothing*/ ; |
| 1237 | else |
| 1238 | return; |
| 1239 | } |
| 1240 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1241 | ResultBuilder Results(*this, &ResultBuilder::IsMember); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1242 | unsigned NextRank = 0; |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1244 | Results.EnterNewScope(); |
| 1245 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 1246 | // Access to a C/C++ class, struct, or union. |
| 1247 | NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank, |
| 1248 | Record->getDecl(), Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1250 | if (getLangOptions().CPlusPlus) { |
| 1251 | if (!Results.empty()) { |
| 1252 | // The "template" keyword can follow "->" or "." in the grammar. |
| 1253 | // However, we only want to suggest the template keyword if something |
| 1254 | // is dependent. |
| 1255 | bool IsDependent = BaseType->isDependentType(); |
| 1256 | if (!IsDependent) { |
| 1257 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 1258 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 1259 | IsDependent = Ctx->isDependentContext(); |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1264 | if (IsDependent) |
| 1265 | Results.MaybeAddResult(Result("template", NextRank++)); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1268 | // We could have the start of a nested-name-specifier. Add those |
| 1269 | // results as well. |
| 1270 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 1271 | CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank, |
| 1272 | CurContext, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1273 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1274 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 1275 | // Objective-C property reference. |
| 1276 | |
| 1277 | // Add property results based on our interface. |
| 1278 | const ObjCObjectPointerType *ObjCPtr |
| 1279 | = BaseType->getAsObjCInterfacePointerType(); |
| 1280 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1281 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Add properties from the protocols in a qualified interface. |
| 1284 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 1285 | E = ObjCPtr->qual_end(); |
| 1286 | I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1287 | AddObjCProperties(*I, true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1288 | |
| 1289 | // FIXME: We could (should?) also look for "implicit" properties, identified |
| 1290 | // only by the presence of nullary and unary selectors. |
| 1291 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 1292 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
| 1293 | // Objective-C instance variable access. |
| 1294 | ObjCInterfaceDecl *Class = 0; |
| 1295 | if (const ObjCObjectPointerType *ObjCPtr |
| 1296 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 1297 | Class = ObjCPtr->getInterfaceDecl(); |
| 1298 | else |
| 1299 | Class = BaseType->getAs<ObjCInterfaceType>()->getDecl(); |
| 1300 | |
| 1301 | // Add all ivars from this class and its superclasses. |
| 1302 | for (; Class; Class = Class->getSuperClass()) { |
| 1303 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 1304 | IVarEnd = Class->ivar_end(); |
| 1305 | IVar != IVarEnd; ++IVar) |
| 1306 | Results.MaybeAddResult(Result(*IVar, 0), CurContext); |
| 1307 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1308 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1309 | |
| 1310 | // FIXME: How do we cope with isa? |
| 1311 | |
| 1312 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1313 | |
| 1314 | // Add macros |
| 1315 | if (CodeCompleter->includeMacros()) |
| 1316 | AddMacroResults(PP, NextRank, Results); |
| 1317 | |
| 1318 | // Hand off the results found for code completion. |
| 1319 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1322 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 1323 | if (!CodeCompleter) |
| 1324 | return; |
| 1325 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1326 | typedef CodeCompleteConsumer::Result Result; |
| 1327 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1328 | switch ((DeclSpec::TST)TagSpec) { |
| 1329 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1330 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | |
| 1333 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1334 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | |
| 1337 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1338 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1339 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | |
| 1342 | default: |
| 1343 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 1344 | return; |
| 1345 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1346 | |
| 1347 | ResultBuilder Results(*this, Filter); |
| 1348 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1349 | 0, CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1350 | |
| 1351 | if (getLangOptions().CPlusPlus) { |
| 1352 | // We could have the start of a nested-name-specifier. Add those |
| 1353 | // results as well. |
| 1354 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1355 | NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1356 | NextRank, CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1359 | if (CodeCompleter->includeMacros()) |
| 1360 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1361 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1364 | void Sema::CodeCompleteCase(Scope *S) { |
| 1365 | if (getSwitchStack().empty() || !CodeCompleter) |
| 1366 | return; |
| 1367 | |
| 1368 | SwitchStmt *Switch = getSwitchStack().back(); |
| 1369 | if (!Switch->getCond()->getType()->isEnumeralType()) |
| 1370 | return; |
| 1371 | |
| 1372 | // Code-complete the cases of a switch statement over an enumeration type |
| 1373 | // by providing the list of |
| 1374 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 1375 | |
| 1376 | // Determine which enumerators we have already seen in the switch statement. |
| 1377 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 1378 | // token, in case we are code-completing in the middle of the switch and not |
| 1379 | // at the end. However, we aren't able to do so at the moment. |
| 1380 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1381 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1382 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 1383 | SC = SC->getNextSwitchCase()) { |
| 1384 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 1385 | if (!Case) |
| 1386 | continue; |
| 1387 | |
| 1388 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 1389 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 1390 | if (EnumConstantDecl *Enumerator |
| 1391 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 1392 | // We look into the AST of the case statement to determine which |
| 1393 | // enumerator was named. Alternatively, we could compute the value of |
| 1394 | // the integral constant expression, then compare it against the |
| 1395 | // values of each enumerator. However, value-based approach would not |
| 1396 | // work as well with C++ templates where enumerators declared within a |
| 1397 | // template are type- and value-dependent. |
| 1398 | EnumeratorsSeen.insert(Enumerator); |
| 1399 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1400 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 1401 | // 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] | 1402 | // |
| 1403 | // switch (TagD.getKind()) { |
| 1404 | // case TagDecl::TK_enum: |
| 1405 | // break; |
| 1406 | // case XXX |
| 1407 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1408 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1409 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 1410 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1411 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1415 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 1416 | // If there are no prior enumerators in C++, check whether we have to |
| 1417 | // qualify the names of the enumerators that we suggest, because they |
| 1418 | // may not be visible in this scope. |
| 1419 | Qualifier = getRequiredQualification(Context, CurContext, |
| 1420 | Enum->getDeclContext()); |
| 1421 | |
| 1422 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 1423 | } |
| 1424 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1425 | // Add any enumerators that have not yet been mentioned. |
| 1426 | ResultBuilder Results(*this); |
| 1427 | Results.EnterNewScope(); |
| 1428 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 1429 | EEnd = Enum->enumerator_end(); |
| 1430 | E != EEnd; ++E) { |
| 1431 | if (EnumeratorsSeen.count(*E)) |
| 1432 | continue; |
| 1433 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1434 | Results.MaybeAddResult(CodeCompleteConsumer::Result(*E, 0, Qualifier)); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1435 | } |
| 1436 | Results.ExitScope(); |
| 1437 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1438 | if (CodeCompleter->includeMacros()) |
| 1439 | AddMacroResults(PP, 1, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1440 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1443 | namespace { |
| 1444 | struct IsBetterOverloadCandidate { |
| 1445 | Sema &S; |
| 1446 | |
| 1447 | public: |
| 1448 | explicit IsBetterOverloadCandidate(Sema &S) : S(S) { } |
| 1449 | |
| 1450 | bool |
| 1451 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
| 1452 | return S.isBetterOverloadCandidate(X, Y); |
| 1453 | } |
| 1454 | }; |
| 1455 | } |
| 1456 | |
| 1457 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 1458 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 1459 | if (!CodeCompleter) |
| 1460 | return; |
| 1461 | |
| 1462 | Expr *Fn = (Expr *)FnIn; |
| 1463 | Expr **Args = (Expr **)ArgsIn; |
| 1464 | |
| 1465 | // Ignore type-dependent call expressions entirely. |
| 1466 | if (Fn->isTypeDependent() || |
| 1467 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 1468 | return; |
| 1469 | |
| 1470 | NamedDecl *Function; |
| 1471 | DeclarationName UnqualifiedName; |
| 1472 | NestedNameSpecifier *Qualifier; |
| 1473 | SourceRange QualifierRange; |
| 1474 | bool ArgumentDependentLookup; |
| 1475 | bool HasExplicitTemplateArgs; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1476 | const TemplateArgumentLoc *ExplicitTemplateArgs; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1477 | unsigned NumExplicitTemplateArgs; |
| 1478 | |
| 1479 | DeconstructCallFunction(Fn, |
| 1480 | Function, UnqualifiedName, Qualifier, QualifierRange, |
| 1481 | ArgumentDependentLookup, HasExplicitTemplateArgs, |
| 1482 | ExplicitTemplateArgs, NumExplicitTemplateArgs); |
| 1483 | |
| 1484 | |
| 1485 | // FIXME: What if we're calling something that isn't a function declaration? |
| 1486 | // FIXME: What if we're calling a pseudo-destructor? |
| 1487 | // FIXME: What if we're calling a member function? |
| 1488 | |
| 1489 | // Build an overload candidate set based on the functions we find. |
| 1490 | OverloadCandidateSet CandidateSet; |
| 1491 | AddOverloadedCallCandidates(Function, UnqualifiedName, |
| 1492 | ArgumentDependentLookup, HasExplicitTemplateArgs, |
| 1493 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 1494 | Args, NumArgs, |
| 1495 | CandidateSet, |
| 1496 | /*PartialOverloading=*/true); |
| 1497 | |
| 1498 | // Sort the overload candidate set by placing the best overloads first. |
| 1499 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
| 1500 | IsBetterOverloadCandidate(*this)); |
| 1501 | |
| 1502 | // Add the remaining viable overload candidates as code-completion reslults. |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 1503 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 1504 | llvm::SmallVector<ResultCandidate, 8> Results; |
Anders Carlsson | 9075630 | 2009-09-22 17:29:51 +0000 | [diff] [blame] | 1505 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1506 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 1507 | CandEnd = CandidateSet.end(); |
| 1508 | Cand != CandEnd; ++Cand) { |
| 1509 | if (Cand->Viable) |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 1510 | Results.push_back(ResultCandidate(Cand->Function)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1511 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1512 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 1513 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1516 | void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS, |
| 1517 | bool EnteringContext) { |
| 1518 | if (!SS.getScopeRep() || !CodeCompleter) |
| 1519 | return; |
| 1520 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1521 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 1522 | if (!Ctx) |
| 1523 | return; |
| 1524 | |
| 1525 | ResultBuilder Results(*this); |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1526 | unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Ctx, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1527 | |
| 1528 | // The "template" keyword can follow "::" in the grammar, but only |
| 1529 | // put it into the grammar if the nested-name-specifier is dependent. |
| 1530 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1531 | if (!Results.empty() && NNS->isDependent()) |
| 1532 | Results.MaybeAddResult(CodeCompleteConsumer::Result("template", NextRank)); |
| 1533 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1534 | if (CodeCompleter->includeMacros()) |
| 1535 | AddMacroResults(PP, NextRank + 1, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1536 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1537 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1538 | |
| 1539 | void Sema::CodeCompleteUsing(Scope *S) { |
| 1540 | if (!CodeCompleter) |
| 1541 | return; |
| 1542 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1543 | ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1544 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1545 | |
| 1546 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 1547 | if (!S->isClassScope()) |
| 1548 | Results.MaybeAddResult(CodeCompleteConsumer::Result("namespace", 0)); |
| 1549 | |
| 1550 | // After "using", we can see anything that would start a |
| 1551 | // nested-name-specifier. |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1552 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1553 | 0, CurContext, Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1554 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1555 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1556 | if (CodeCompleter->includeMacros()) |
| 1557 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1558 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 1562 | if (!CodeCompleter) |
| 1563 | return; |
| 1564 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1565 | // After "using namespace", we expect to see a namespace name or namespace |
| 1566 | // alias. |
| 1567 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1568 | Results.EnterNewScope(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1569 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1570 | 0, CurContext, Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1571 | Results.ExitScope(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1572 | if (CodeCompleter->includeMacros()) |
| 1573 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1574 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 1578 | if (!CodeCompleter) |
| 1579 | return; |
| 1580 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1581 | ResultBuilder Results(*this, &ResultBuilder::IsNamespace); |
| 1582 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 1583 | if (!S->getParent()) |
| 1584 | Ctx = Context.getTranslationUnitDecl(); |
| 1585 | |
| 1586 | if (Ctx && Ctx->isFileContext()) { |
| 1587 | // We only want to see those namespaces that have already been defined |
| 1588 | // within this scope, because its likely that the user is creating an |
| 1589 | // extended namespace declaration. Keep track of the most recent |
| 1590 | // definition of each namespace. |
| 1591 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 1592 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 1593 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 1594 | NS != NSEnd; ++NS) |
| 1595 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 1596 | |
| 1597 | // Add the most recent definition (or extended definition) of each |
| 1598 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1599 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1600 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 1601 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 1602 | NS != NSEnd; ++NS) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1603 | Results.MaybeAddResult(CodeCompleteConsumer::Result(NS->second, 0), |
| 1604 | CurContext); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1605 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1608 | if (CodeCompleter->includeMacros()) |
| 1609 | AddMacroResults(PP, 1, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1610 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 1614 | if (!CodeCompleter) |
| 1615 | return; |
| 1616 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1617 | // After "namespace", we expect to see a namespace or alias. |
| 1618 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1619 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1620 | 0, CurContext, Results); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1621 | if (CodeCompleter->includeMacros()) |
| 1622 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1623 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1626 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 1627 | if (!CodeCompleter) |
| 1628 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1629 | |
| 1630 | typedef CodeCompleteConsumer::Result Result; |
| 1631 | ResultBuilder Results(*this, &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1632 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1633 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1634 | // Add the names of overloadable operators. |
| 1635 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 1636 | if (std::strcmp(Spelling, "?")) \ |
| 1637 | Results.MaybeAddResult(Result(Spelling, 0)); |
| 1638 | #include "clang/Basic/OperatorKinds.def" |
| 1639 | |
| 1640 | // Add any type names visible from the current scope |
| 1641 | unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 1642 | 0, CurContext, Results); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1643 | |
| 1644 | // Add any type specifiers |
| 1645 | AddTypeSpecifierResults(getLangOptions(), 0, Results); |
| 1646 | |
| 1647 | // Add any nested-name-specifiers |
| 1648 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1649 | NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(), |
| 1650 | NextRank + 1, CurContext, Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 1651 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1653 | if (CodeCompleter->includeMacros()) |
| 1654 | AddMacroResults(PP, NextRank, Results); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1655 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1656 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1658 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 1659 | /// property's attributes will cause a conflict. |
| 1660 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 1661 | // Check if we've already added this flag. |
| 1662 | if (Attributes & NewFlag) |
| 1663 | return true; |
| 1664 | |
| 1665 | Attributes |= NewFlag; |
| 1666 | |
| 1667 | // Check for collisions with "readonly". |
| 1668 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1669 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1670 | ObjCDeclSpec::DQ_PR_assign | |
| 1671 | ObjCDeclSpec::DQ_PR_copy | |
| 1672 | ObjCDeclSpec::DQ_PR_retain))) |
| 1673 | return true; |
| 1674 | |
| 1675 | // Check for more than one of { assign, copy, retain }. |
| 1676 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 1677 | ObjCDeclSpec::DQ_PR_copy | |
| 1678 | ObjCDeclSpec::DQ_PR_retain); |
| 1679 | if (AssignCopyRetMask && |
| 1680 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 1681 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 1682 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 1683 | return true; |
| 1684 | |
| 1685 | return false; |
| 1686 | } |
| 1687 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 1688 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1689 | if (!CodeCompleter) |
| 1690 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1691 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1692 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 1693 | |
| 1694 | typedef CodeCompleteConsumer::Result Result; |
| 1695 | ResultBuilder Results(*this); |
| 1696 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1697 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1698 | Results.MaybeAddResult(CodeCompleteConsumer::Result("readonly", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1699 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1700 | Results.MaybeAddResult(CodeCompleteConsumer::Result("assign", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1701 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1702 | Results.MaybeAddResult(CodeCompleteConsumer::Result("readwrite", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1703 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1704 | Results.MaybeAddResult(CodeCompleteConsumer::Result("retain", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1705 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1706 | Results.MaybeAddResult(CodeCompleteConsumer::Result("copy", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1707 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1708 | Results.MaybeAddResult(CodeCompleteConsumer::Result("nonatomic", 0)); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1709 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1710 | CodeCompletionString *Setter = new CodeCompletionString; |
| 1711 | Setter->AddTypedTextChunk("setter"); |
| 1712 | Setter->AddTextChunk(" = "); |
| 1713 | Setter->AddPlaceholderChunk("method"); |
| 1714 | Results.MaybeAddResult(CodeCompleteConsumer::Result(Setter, 0)); |
| 1715 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 1716 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1717 | CodeCompletionString *Getter = new CodeCompletionString; |
| 1718 | Getter->AddTypedTextChunk("getter"); |
| 1719 | Getter->AddTextChunk(" = "); |
| 1720 | Getter->AddPlaceholderChunk("method"); |
| 1721 | Results.MaybeAddResult(CodeCompleteConsumer::Result(Getter, 0)); |
| 1722 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1723 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1724 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 1725 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1727 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 1728 | /// via code completion. |
| 1729 | enum ObjCMethodKind { |
| 1730 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 1731 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 1732 | MK_OneArgSelector //< One-argument selector. |
| 1733 | }; |
| 1734 | |
| 1735 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 1736 | ObjCMethodKind WantKind, |
| 1737 | IdentifierInfo **SelIdents, |
| 1738 | unsigned NumSelIdents) { |
| 1739 | Selector Sel = Method->getSelector(); |
| 1740 | if (NumSelIdents > Sel.getNumArgs()) |
| 1741 | return false; |
| 1742 | |
| 1743 | switch (WantKind) { |
| 1744 | case MK_Any: break; |
| 1745 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 1746 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 1747 | } |
| 1748 | |
| 1749 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 1750 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 1751 | return false; |
| 1752 | |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1756 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 1757 | /// container to the set of results. |
| 1758 | /// |
| 1759 | /// The container will be a class, protocol, category, or implementation of |
| 1760 | /// any of the above. This mether will recurse to include methods from |
| 1761 | /// the superclasses of classes along with their categories, protocols, and |
| 1762 | /// implementations. |
| 1763 | /// |
| 1764 | /// \param Container the container in which we'll look to find methods. |
| 1765 | /// |
| 1766 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 1767 | /// routine will add factory methods (only). |
| 1768 | /// |
| 1769 | /// \param CurContext the context in which we're performing the lookup that |
| 1770 | /// finds methods. |
| 1771 | /// |
| 1772 | /// \param Results the structure into which we'll add results. |
| 1773 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 1774 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1775 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1776 | IdentifierInfo **SelIdents, |
| 1777 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1778 | DeclContext *CurContext, |
| 1779 | ResultBuilder &Results) { |
| 1780 | typedef CodeCompleteConsumer::Result Result; |
| 1781 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 1782 | MEnd = Container->meth_end(); |
| 1783 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1784 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 1785 | // Check whether the selector identifiers we've been given are a |
| 1786 | // subset of the identifiers for this particular method. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1787 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1788 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1790 | Result R = Result(*M, 0); |
| 1791 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1792 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1793 | Results.MaybeAddResult(R, CurContext); |
| 1794 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 1798 | if (!IFace) |
| 1799 | return; |
| 1800 | |
| 1801 | // Add methods in protocols. |
| 1802 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 1803 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1804 | E = Protocols.end(); |
| 1805 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1806 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1807 | CurContext, Results); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1808 | |
| 1809 | // Add methods in categories. |
| 1810 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 1811 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1812 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
| 1813 | NumSelIdents, CurContext, Results); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Add a categories protocol methods. |
| 1816 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 1817 | = CatDecl->getReferencedProtocols(); |
| 1818 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1819 | E = Protocols.end(); |
| 1820 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1821 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
| 1822 | NumSelIdents, CurContext, Results); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1823 | |
| 1824 | // Add methods in category implementations. |
| 1825 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1826 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
| 1827 | NumSelIdents, CurContext, Results); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | // Add methods in superclass. |
| 1831 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1832 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 1833 | SelIdents, NumSelIdents, CurContext, Results); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Add methods in our implementation, if any. |
| 1836 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1837 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
| 1838 | NumSelIdents, CurContext, Results); |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, DeclPtrTy ClassDecl, |
| 1843 | DeclPtrTy *Methods, |
| 1844 | unsigned NumMethods) { |
| 1845 | typedef CodeCompleteConsumer::Result Result; |
| 1846 | |
| 1847 | // Try to find the interface where getters might live. |
| 1848 | ObjCInterfaceDecl *Class |
| 1849 | = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl.getAs<Decl>()); |
| 1850 | if (!Class) { |
| 1851 | if (ObjCCategoryDecl *Category |
| 1852 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl.getAs<Decl>())) |
| 1853 | Class = Category->getClassInterface(); |
| 1854 | |
| 1855 | if (!Class) |
| 1856 | return; |
| 1857 | } |
| 1858 | |
| 1859 | // Find all of the potential getters. |
| 1860 | ResultBuilder Results(*this); |
| 1861 | Results.EnterNewScope(); |
| 1862 | |
| 1863 | // FIXME: We need to do this because Objective-C methods don't get |
| 1864 | // pushed into DeclContexts early enough. Argh! |
| 1865 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 1866 | if (ObjCMethodDecl *Method |
| 1867 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>())) |
| 1868 | if (Method->isInstanceMethod() && |
| 1869 | isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) { |
| 1870 | Result R = Result(Method, 0); |
| 1871 | R.AllParametersAreInformative = true; |
| 1872 | Results.MaybeAddResult(R, CurContext); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results); |
| 1877 | Results.ExitScope(); |
| 1878 | HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size()); |
| 1879 | } |
| 1880 | |
| 1881 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, DeclPtrTy ObjCImplDecl, |
| 1882 | DeclPtrTy *Methods, |
| 1883 | unsigned NumMethods) { |
| 1884 | typedef CodeCompleteConsumer::Result Result; |
| 1885 | |
| 1886 | // Try to find the interface where setters might live. |
| 1887 | ObjCInterfaceDecl *Class |
| 1888 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl.getAs<Decl>()); |
| 1889 | if (!Class) { |
| 1890 | if (ObjCCategoryDecl *Category |
| 1891 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl.getAs<Decl>())) |
| 1892 | Class = Category->getClassInterface(); |
| 1893 | |
| 1894 | if (!Class) |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | // Find all of the potential getters. |
| 1899 | ResultBuilder Results(*this); |
| 1900 | Results.EnterNewScope(); |
| 1901 | |
| 1902 | // FIXME: We need to do this because Objective-C methods don't get |
| 1903 | // pushed into DeclContexts early enough. Argh! |
| 1904 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 1905 | if (ObjCMethodDecl *Method |
| 1906 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>())) |
| 1907 | if (Method->isInstanceMethod() && |
| 1908 | isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) { |
| 1909 | Result R = Result(Method, 0); |
| 1910 | R.AllParametersAreInformative = true; |
| 1911 | Results.MaybeAddResult(R, CurContext); |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results); |
| 1916 | |
| 1917 | Results.ExitScope(); |
| 1918 | HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Douglas Gregor | 60b01cc | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 1921 | void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1922 | SourceLocation FNameLoc, |
| 1923 | IdentifierInfo **SelIdents, |
| 1924 | unsigned NumSelIdents) { |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1925 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1926 | ObjCInterfaceDecl *CDecl = 0; |
| 1927 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1928 | if (FName->isStr("super")) { |
| 1929 | // We're sending a message to "super". |
| 1930 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 1931 | // Figure out which interface we're in. |
| 1932 | CDecl = CurMethod->getClassInterface(); |
| 1933 | if (!CDecl) |
| 1934 | return; |
| 1935 | |
| 1936 | // Find the superclass of this class. |
| 1937 | CDecl = CDecl->getSuperClass(); |
| 1938 | if (!CDecl) |
| 1939 | return; |
| 1940 | |
| 1941 | if (CurMethod->isInstanceMethod()) { |
| 1942 | // We are inside an instance method, which means that the message |
| 1943 | // send [super ...] is actually calling an instance method on the |
| 1944 | // current object. Build the super expression and handle this like |
| 1945 | // an instance method. |
| 1946 | QualType SuperTy = Context.getObjCInterfaceType(CDecl); |
| 1947 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
| 1948 | OwningExprResult Super |
Douglas Gregor | 60b01cc | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 1949 | = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1950 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
| 1951 | SelIdents, NumSelIdents); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | // Okay, we're calling a factory method in our superclass. |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | // If the given name refers to an interface type, retrieve the |
| 1959 | // corresponding declaration. |
| 1960 | if (!CDecl) |
Douglas Gregor | 60b01cc | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 1961 | if (TypeTy *Ty = getTypeName(*FName, FNameLoc, S, 0, false)) { |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1962 | QualType T = GetTypeFromParser(Ty, 0); |
| 1963 | if (!T.isNull()) |
| 1964 | if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>()) |
| 1965 | CDecl = Interface->getDecl(); |
| 1966 | } |
| 1967 | |
| 1968 | if (!CDecl && FName->isStr("super")) { |
| 1969 | // "super" may be the name of a variable, in which case we are |
| 1970 | // probably calling an instance method. |
Douglas Gregor | 60b01cc | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 1971 | OwningExprResult Super = ActOnDeclarationNameExpr(S, FNameLoc, FName, |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1972 | false, 0, false); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1973 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
| 1974 | SelIdents, NumSelIdents); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1977 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 1978 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1979 | ResultBuilder Results(*this); |
| 1980 | Results.EnterNewScope(); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1981 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 1982 | Results); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1983 | Results.ExitScope(); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1984 | |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1985 | // This also suppresses remaining diagnostics. |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1986 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1989 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 1990 | IdentifierInfo **SelIdents, |
| 1991 | unsigned NumSelIdents) { |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1992 | typedef CodeCompleteConsumer::Result Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1993 | |
| 1994 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
| 1995 | QualType RecType = RecExpr->getType(); |
| 1996 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 1997 | // If necessary, apply function/array conversion to the receiver. |
| 1998 | // C99 6.7.5.3p[7,8]. |
| 1999 | DefaultFunctionArrayConversion(RecExpr); |
| 2000 | QualType ReceiverType = RecExpr->getType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2002 | if (ReceiverType->isObjCIdType() || ReceiverType->isBlockPointerType()) { |
| 2003 | // FIXME: We're messaging 'id'. Do we actually want to look up every method |
| 2004 | // in the universe? |
| 2005 | return; |
| 2006 | } |
| 2007 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2008 | // Build the set of methods we can see. |
| 2009 | ResultBuilder Results(*this); |
| 2010 | Results.EnterNewScope(); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 2012 | // Handle messages to Class. This really isn't a message to an instance |
| 2013 | // method, so we treat it the same way we would treat a message send to a |
| 2014 | // class method. |
| 2015 | if (ReceiverType->isObjCClassType() || |
| 2016 | ReceiverType->isObjCQualifiedClassType()) { |
| 2017 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 2018 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2019 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
| 2020 | CurContext, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | // Handle messages to a qualified ID ("id<foo>"). |
| 2024 | else if (const ObjCObjectPointerType *QualID |
| 2025 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 2026 | // Search protocols for instance methods. |
| 2027 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 2028 | E = QualID->qual_end(); |
| 2029 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2030 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 2031 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 2032 | } |
| 2033 | // Handle messages to a pointer to interface type. |
| 2034 | else if (const ObjCObjectPointerType *IFacePtr |
| 2035 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2036 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2037 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
| 2038 | NumSelIdents, CurContext, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Search protocols for instance methods. |
| 2041 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 2042 | E = IFacePtr->qual_end(); |
| 2043 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2044 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 2045 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2048 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2049 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2050 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 2051 | |
| 2052 | /// \brief Add all of the protocol declarations that we find in the given |
| 2053 | /// (translation unit) context. |
| 2054 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 2055 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 2056 | ResultBuilder &Results) { |
| 2057 | typedef CodeCompleteConsumer::Result Result; |
| 2058 | |
| 2059 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 2060 | DEnd = Ctx->decls_end(); |
| 2061 | D != DEnd; ++D) { |
| 2062 | // Record any protocols we find. |
| 2063 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 2064 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
| 2065 | Results.MaybeAddResult(Result(Proto, 0), CurContext); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 2066 | |
| 2067 | // Record any forward-declared protocols we find. |
| 2068 | if (ObjCForwardProtocolDecl *Forward |
| 2069 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 2070 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 2071 | P = Forward->protocol_begin(), |
| 2072 | PEnd = Forward->protocol_end(); |
| 2073 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 2074 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
| 2075 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 2081 | unsigned NumProtocols) { |
| 2082 | ResultBuilder Results(*this); |
| 2083 | Results.EnterNewScope(); |
| 2084 | |
| 2085 | // Tell the result set to ignore all of the protocols we have |
| 2086 | // already seen. |
| 2087 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 2088 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first)) |
| 2089 | Results.Ignore(Protocol); |
| 2090 | |
| 2091 | // Add all protocols. |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 2092 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 2093 | Results); |
| 2094 | |
| 2095 | Results.ExitScope(); |
| 2096 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2097 | } |
| 2098 | |
| 2099 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
| 2100 | ResultBuilder Results(*this); |
| 2101 | Results.EnterNewScope(); |
| 2102 | |
| 2103 | // Add all protocols. |
| 2104 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 2105 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 2106 | |
| 2107 | Results.ExitScope(); |
| 2108 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2109 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 2110 | |
| 2111 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 2112 | /// the given (translation unit) context. |
| 2113 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 2114 | bool OnlyForwardDeclarations, |
| 2115 | bool OnlyUnimplemented, |
| 2116 | ResultBuilder &Results) { |
| 2117 | typedef CodeCompleteConsumer::Result Result; |
| 2118 | |
| 2119 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 2120 | DEnd = Ctx->decls_end(); |
| 2121 | D != DEnd; ++D) { |
| 2122 | // Record any interfaces we find. |
| 2123 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 2124 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 2125 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 2126 | Results.MaybeAddResult(Result(Class, 0), CurContext); |
| 2127 | |
| 2128 | // Record any forward-declared interfaces we find. |
| 2129 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 2130 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
| 2131 | C != CEnd; ++C) |
| 2132 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 2133 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 2134 | Results.MaybeAddResult(Result(C->getInterface(), 0), CurContext); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
| 2140 | ResultBuilder Results(*this); |
| 2141 | Results.EnterNewScope(); |
| 2142 | |
| 2143 | // Add all classes. |
| 2144 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 2145 | false, Results); |
| 2146 | |
| 2147 | Results.ExitScope(); |
| 2148 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2149 | } |
| 2150 | |
| 2151 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName) { |
| 2152 | ResultBuilder Results(*this); |
| 2153 | Results.EnterNewScope(); |
| 2154 | |
| 2155 | // Make sure that we ignore the class we're currently defining. |
| 2156 | NamedDecl *CurClass |
| 2157 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 2158 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 2159 | Results.Ignore(CurClass); |
| 2160 | |
| 2161 | // Add all classes. |
| 2162 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 2163 | false, Results); |
| 2164 | |
| 2165 | Results.ExitScope(); |
| 2166 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2167 | } |
| 2168 | |
| 2169 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
| 2170 | ResultBuilder Results(*this); |
| 2171 | Results.EnterNewScope(); |
| 2172 | |
| 2173 | // Add all unimplemented classes. |
| 2174 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 2175 | true, Results); |
| 2176 | |
| 2177 | Results.ExitScope(); |
| 2178 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2179 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 2180 | |
| 2181 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
| 2182 | IdentifierInfo *ClassName) { |
| 2183 | typedef CodeCompleteConsumer::Result Result; |
| 2184 | |
| 2185 | ResultBuilder Results(*this); |
| 2186 | |
| 2187 | // Ignore any categories we find that have already been implemented by this |
| 2188 | // interface. |
| 2189 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 2190 | NamedDecl *CurClass |
| 2191 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
| 2192 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 2193 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 2194 | Category = Category->getNextClassCategory()) |
| 2195 | CategoryNames.insert(Category->getIdentifier()); |
| 2196 | |
| 2197 | // Add all of the categories we know about. |
| 2198 | Results.EnterNewScope(); |
| 2199 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 2200 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 2201 | DEnd = TU->decls_end(); |
| 2202 | D != DEnd; ++D) |
| 2203 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 2204 | if (CategoryNames.insert(Category->getIdentifier())) |
| 2205 | Results.MaybeAddResult(Result(Category, 0), CurContext); |
| 2206 | Results.ExitScope(); |
| 2207 | |
| 2208 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2209 | } |
| 2210 | |
| 2211 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
| 2212 | IdentifierInfo *ClassName) { |
| 2213 | typedef CodeCompleteConsumer::Result Result; |
| 2214 | |
| 2215 | // Find the corresponding interface. If we couldn't find the interface, the |
| 2216 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 2217 | // providing the list of all of the categories we know about. |
| 2218 | NamedDecl *CurClass |
| 2219 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
| 2220 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 2221 | if (!Class) |
| 2222 | return CodeCompleteObjCInterfaceCategory(S, ClassName); |
| 2223 | |
| 2224 | ResultBuilder Results(*this); |
| 2225 | |
| 2226 | // Add all of the categories that have have corresponding interface |
| 2227 | // declarations in this class and any of its superclasses, except for |
| 2228 | // already-implemented categories in the class itself. |
| 2229 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 2230 | Results.EnterNewScope(); |
| 2231 | bool IgnoreImplemented = true; |
| 2232 | while (Class) { |
| 2233 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 2234 | Category = Category->getNextClassCategory()) |
| 2235 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 2236 | CategoryNames.insert(Category->getIdentifier())) |
| 2237 | Results.MaybeAddResult(Result(Category, 0), CurContext); |
| 2238 | |
| 2239 | Class = Class->getSuperClass(); |
| 2240 | IgnoreImplemented = false; |
| 2241 | } |
| 2242 | Results.ExitScope(); |
| 2243 | |
| 2244 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2245 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2246 | |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 2247 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2248 | typedef CodeCompleteConsumer::Result Result; |
| 2249 | ResultBuilder Results(*this); |
| 2250 | |
| 2251 | // Figure out where this @synthesize lives. |
| 2252 | ObjCContainerDecl *Container |
| 2253 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>()); |
| 2254 | if (!Container || |
| 2255 | (!isa<ObjCImplementationDecl>(Container) && |
| 2256 | !isa<ObjCCategoryImplDecl>(Container))) |
| 2257 | return; |
| 2258 | |
| 2259 | // Ignore any properties that have already been implemented. |
| 2260 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 2261 | DEnd = Container->decls_end(); |
| 2262 | D != DEnd; ++D) |
| 2263 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 2264 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 2265 | |
| 2266 | // Add any properties that we find. |
| 2267 | Results.EnterNewScope(); |
| 2268 | if (ObjCImplementationDecl *ClassImpl |
| 2269 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 2270 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
| 2271 | Results); |
| 2272 | else |
| 2273 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
| 2274 | false, CurContext, Results); |
| 2275 | Results.ExitScope(); |
| 2276 | |
| 2277 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2278 | } |
| 2279 | |
| 2280 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 2281 | IdentifierInfo *PropertyName, |
| 2282 | DeclPtrTy ObjCImpDecl) { |
| 2283 | typedef CodeCompleteConsumer::Result Result; |
| 2284 | ResultBuilder Results(*this); |
| 2285 | |
| 2286 | // Figure out where this @synthesize lives. |
| 2287 | ObjCContainerDecl *Container |
| 2288 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>()); |
| 2289 | if (!Container || |
| 2290 | (!isa<ObjCImplementationDecl>(Container) && |
| 2291 | !isa<ObjCCategoryImplDecl>(Container))) |
| 2292 | return; |
| 2293 | |
| 2294 | // Figure out which interface we're looking into. |
| 2295 | ObjCInterfaceDecl *Class = 0; |
| 2296 | if (ObjCImplementationDecl *ClassImpl |
| 2297 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 2298 | Class = ClassImpl->getClassInterface(); |
| 2299 | else |
| 2300 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 2301 | ->getClassInterface(); |
| 2302 | |
| 2303 | // Add all of the instance variables in this class and its superclasses. |
| 2304 | Results.EnterNewScope(); |
| 2305 | for(; Class; Class = Class->getSuperClass()) { |
| 2306 | // FIXME: We could screen the type of each ivar for compatibility with |
| 2307 | // the property, but is that being too paternal? |
| 2308 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 2309 | IVarEnd = Class->ivar_end(); |
| 2310 | IVar != IVarEnd; ++IVar) |
| 2311 | Results.MaybeAddResult(Result(*IVar, 0), CurContext); |
| 2312 | } |
| 2313 | Results.ExitScope(); |
| 2314 | |
| 2315 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2316 | } |