Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the code-completion semantic actions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Eric Liu | b91e081 | 2018-10-02 10:29:00 +0000 | [diff] [blame] | 12 | #include "clang/AST/Decl.h" |
Ilya Biryukov | 9839755 | 2018-12-05 17:38:39 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclBase.h" |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 18 | #include "clang/AST/QualTypeNames.h" |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 20 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 21 | #include "clang/Lex/MacroInfo.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
| 25 | #include "clang/Sema/Overload.h" |
| 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 28 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Twine.h" |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/iterator_range.h" |
| 37 | #include "llvm/Support/Path.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 38 | #include <list> |
| 39 | #include <map> |
Ilya Biryukov | cabab29 | 2019-05-24 10:18:39 +0000 | [diff] [blame] | 40 | #include <string> |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 41 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 44 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 46 | namespace { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 47 | /// A container of code-completion results. |
| 48 | class ResultBuilder { |
| 49 | public: |
| 50 | /// The type of a name-lookup filter, which can be provided to the |
| 51 | /// name-lookup routines to specify which declarations should be included in |
| 52 | /// the result set (when it returns true) and which declarations should be |
| 53 | /// filtered out (returns false). |
| 54 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Ivan Donchevskii | 13d9054 | 2017-10-27 11:05:40 +0000 | [diff] [blame] | 55 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 56 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 57 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 58 | private: |
| 59 | /// The actual results we have found. |
| 60 | std::vector<Result> Results; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 61 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 62 | /// A record of all of the declarations we have found and placed |
| 63 | /// into the result set, used to ensure that no declaration ever gets into |
| 64 | /// the result set twice. |
| 65 | llvm::SmallPtrSet<const Decl *, 16> AllDeclsFound; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 66 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 67 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 68 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 69 | /// An entry in the shadow map, which is optimized to store |
| 70 | /// a single (declaration, index) mapping (the common case) but |
| 71 | /// can also store a list of (declaration, index) mappings. |
| 72 | class ShadowMapEntry { |
| 73 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 74 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 75 | /// Contains either the solitary NamedDecl * or a vector |
| 76 | /// of (declaration, index) pairs. |
| 77 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector *> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 78 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 79 | /// When the entry contains a single declaration, this is |
| 80 | /// the index associated with that entry. |
| 81 | unsigned SingleDeclIndex; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 83 | public: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 84 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) {} |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 85 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 86 | void Add(const NamedDecl *ND, unsigned Index) { |
| 87 | if (DeclOrVector.isNull()) { |
| 88 | // 0 - > 1 elements: just set the single element information. |
| 89 | DeclOrVector = ND; |
| 90 | SingleDeclIndex = Index; |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (const NamedDecl *PrevND = |
| 95 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
| 96 | // 1 -> 2 elements: create the vector of results and push in the |
| 97 | // existing declaration. |
| 98 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 99 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 100 | DeclOrVector = Vec; |
| 101 | } |
| 102 | |
| 103 | // Add the new element to the end of the vector. |
| 104 | DeclOrVector.get<DeclIndexPairVector *>()->push_back( |
| 105 | DeclIndexPair(ND, Index)); |
| 106 | } |
| 107 | |
| 108 | void Destroy() { |
| 109 | if (DeclIndexPairVector *Vec = |
| 110 | DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 111 | delete Vec; |
| 112 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 115 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 116 | // Iteration. |
| 117 | class iterator; |
| 118 | iterator begin() const; |
| 119 | iterator end() const; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 120 | }; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 121 | |
| 122 | /// A mapping from declaration names to the declarations that have |
| 123 | /// this name within a particular scope and their index within the list of |
| 124 | /// results. |
| 125 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| 126 | |
| 127 | /// The semantic analysis object for which results are being |
| 128 | /// produced. |
| 129 | Sema &SemaRef; |
| 130 | |
| 131 | /// The allocator used to allocate new code-completion strings. |
| 132 | CodeCompletionAllocator &Allocator; |
| 133 | |
| 134 | CodeCompletionTUInfo &CCTUInfo; |
| 135 | |
| 136 | /// If non-NULL, a filter function used to remove any code-completion |
| 137 | /// results that are not desirable. |
| 138 | LookupFilter Filter; |
| 139 | |
| 140 | /// Whether we should allow declarations as |
| 141 | /// nested-name-specifiers that would otherwise be filtered out. |
| 142 | bool AllowNestedNameSpecifiers; |
| 143 | |
| 144 | /// If set, the type that we would prefer our resulting value |
| 145 | /// declarations to have. |
| 146 | /// |
| 147 | /// Closely matching the preferred type gives a boost to a result's |
| 148 | /// priority. |
| 149 | CanQualType PreferredType; |
| 150 | |
| 151 | /// A list of shadow maps, which is used to model name hiding at |
| 152 | /// different levels of, e.g., the inheritance hierarchy. |
| 153 | std::list<ShadowMap> ShadowMaps; |
| 154 | |
| 155 | /// If we're potentially referring to a C++ member function, the set |
| 156 | /// of qualifiers applied to the object type. |
| 157 | Qualifiers ObjectTypeQualifiers; |
| 158 | |
| 159 | /// Whether the \p ObjectTypeQualifiers field is active. |
| 160 | bool HasObjectTypeQualifiers; |
| 161 | |
| 162 | /// The selector that we prefer. |
| 163 | Selector PreferredSelector; |
| 164 | |
| 165 | /// The completion context in which we are gathering results. |
| 166 | CodeCompletionContext CompletionContext; |
| 167 | |
| 168 | /// If we are in an instance method definition, the \@implementation |
| 169 | /// object. |
| 170 | ObjCImplementationDecl *ObjCImplementation; |
| 171 | |
| 172 | void AdjustResultPriorityForDecl(Result &R); |
| 173 | |
| 174 | void MaybeAddConstructorResults(Result R); |
| 175 | |
| 176 | public: |
| 177 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
| 178 | CodeCompletionTUInfo &CCTUInfo, |
| 179 | const CodeCompletionContext &CompletionContext, |
| 180 | LookupFilter Filter = nullptr) |
| 181 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 182 | Filter(Filter), AllowNestedNameSpecifiers(false), |
| 183 | HasObjectTypeQualifiers(false), CompletionContext(CompletionContext), |
| 184 | ObjCImplementation(nullptr) { |
| 185 | // If this is an Objective-C instance method definition, dig out the |
| 186 | // corresponding implementation. |
| 187 | switch (CompletionContext.getKind()) { |
| 188 | case CodeCompletionContext::CCC_Expression: |
| 189 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 190 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 191 | case CodeCompletionContext::CCC_Statement: |
| 192 | case CodeCompletionContext::CCC_Recovery: |
| 193 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 194 | if (Method->isInstanceMethod()) |
| 195 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 196 | ObjCImplementation = Interface->getImplementation(); |
| 197 | break; |
| 198 | |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Determine the priority for a reference to the given declaration. |
| 205 | unsigned getBasePriority(const NamedDecl *D); |
| 206 | |
| 207 | /// Whether we should include code patterns in the completion |
| 208 | /// results. |
| 209 | bool includeCodePatterns() const { |
| 210 | return SemaRef.CodeCompleter && |
| 211 | SemaRef.CodeCompleter->includeCodePatterns(); |
| 212 | } |
| 213 | |
| 214 | /// Set the filter used for code-completion results. |
| 215 | void setFilter(LookupFilter Filter) { this->Filter = Filter; } |
| 216 | |
| 217 | Result *data() { return Results.empty() ? nullptr : &Results.front(); } |
| 218 | unsigned size() const { return Results.size(); } |
| 219 | bool empty() const { return Results.empty(); } |
| 220 | |
| 221 | /// Specify the preferred type. |
| 222 | void setPreferredType(QualType T) { |
| 223 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 224 | } |
| 225 | |
| 226 | /// Set the cv-qualifiers on the object type, for us in filtering |
| 227 | /// calls to member functions. |
| 228 | /// |
| 229 | /// When there are qualifiers in this set, they will be used to filter |
| 230 | /// out member functions that aren't available (because there will be a |
| 231 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 232 | /// match. |
| 233 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 234 | ObjectTypeQualifiers = Quals; |
| 235 | HasObjectTypeQualifiers = true; |
| 236 | } |
| 237 | |
| 238 | /// Set the preferred selector. |
| 239 | /// |
| 240 | /// When an Objective-C method declaration result is added, and that |
| 241 | /// method's selector matches this preferred selector, we give that method |
| 242 | /// a slight priority boost. |
| 243 | void setPreferredSelector(Selector Sel) { PreferredSelector = Sel; } |
| 244 | |
| 245 | /// Retrieve the code-completion context for which results are |
| 246 | /// being collected. |
| 247 | const CodeCompletionContext &getCompletionContext() const { |
| 248 | return CompletionContext; |
| 249 | } |
| 250 | |
| 251 | /// Specify whether nested-name-specifiers are allowed. |
| 252 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 253 | AllowNestedNameSpecifiers = Allow; |
| 254 | } |
| 255 | |
| 256 | /// Return the semantic analysis object for which we are collecting |
| 257 | /// code completion results. |
| 258 | Sema &getSema() const { return SemaRef; } |
| 259 | |
| 260 | /// Retrieve the allocator used to allocate code completion strings. |
| 261 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
| 262 | |
| 263 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
| 264 | |
| 265 | /// Determine whether the given declaration is at all interesting |
| 266 | /// as a code-completion result. |
| 267 | /// |
| 268 | /// \param ND the declaration that we are inspecting. |
| 269 | /// |
| 270 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 271 | /// only interesting when it is a nested-name-specifier. |
| 272 | bool isInterestingDecl(const NamedDecl *ND, |
| 273 | bool &AsNestedNameSpecifier) const; |
| 274 | |
| 275 | /// Check whether the result is hidden by the Hiding declaration. |
| 276 | /// |
| 277 | /// \returns true if the result is hidden and cannot be found, false if |
| 278 | /// the hidden result could still be found. When false, \p R may be |
| 279 | /// modified to describe how the result can be found (e.g., via extra |
| 280 | /// qualification). |
| 281 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 282 | const NamedDecl *Hiding); |
| 283 | |
| 284 | /// Add a new result to this result set (if it isn't already in one |
| 285 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 286 | /// redeclaration). |
| 287 | /// |
| 288 | /// \param R the result to add (if it is unique). |
| 289 | /// |
| 290 | /// \param CurContext the context in which this result will be named. |
| 291 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 292 | |
| 293 | /// Add a new result to this result set, where we already know |
| 294 | /// the hiding declaration (if any). |
| 295 | /// |
| 296 | /// \param R the result to add (if it is unique). |
| 297 | /// |
| 298 | /// \param CurContext the context in which this result will be named. |
| 299 | /// |
| 300 | /// \param Hiding the declaration that hides the result. |
| 301 | /// |
| 302 | /// \param InBaseClass whether the result was found in a base |
| 303 | /// class of the searched context. |
| 304 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 305 | bool InBaseClass); |
| 306 | |
| 307 | /// Add a new non-declaration result to this result set. |
| 308 | void AddResult(Result R); |
| 309 | |
| 310 | /// Enter into a new scope. |
| 311 | void EnterNewScope(); |
| 312 | |
| 313 | /// Exit from the current scope. |
| 314 | void ExitScope(); |
| 315 | |
| 316 | /// Ignore this declaration, if it is seen again. |
| 317 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 318 | |
| 319 | /// Add a visited context. |
| 320 | void addVisitedContext(DeclContext *Ctx) { |
| 321 | CompletionContext.addVisitedContext(Ctx); |
| 322 | } |
| 323 | |
| 324 | /// \name Name lookup predicates |
| 325 | /// |
| 326 | /// These predicates can be passed to the name lookup functions to filter the |
| 327 | /// results of name lookup. All of the predicates have the same type, so that |
| 328 | /// |
| 329 | //@{ |
| 330 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 331 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 332 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 333 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 334 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 335 | bool IsEnum(const NamedDecl *ND) const; |
| 336 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 337 | bool IsUnion(const NamedDecl *ND) const; |
| 338 | bool IsNamespace(const NamedDecl *ND) const; |
| 339 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 340 | bool IsType(const NamedDecl *ND) const; |
| 341 | bool IsMember(const NamedDecl *ND) const; |
| 342 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 343 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 344 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 345 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 346 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
| 347 | //@} |
| 348 | }; |
| 349 | } // namespace |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 350 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 351 | void PreferredTypeBuilder::enterReturn(Sema &S, SourceLocation Tok) { |
| 352 | if (isa<BlockDecl>(S.CurContext)) { |
| 353 | if (sema::BlockScopeInfo *BSI = S.getCurBlock()) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 354 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 355 | Type = BSI->ReturnType; |
| 356 | ExpectedLoc = Tok; |
| 357 | } |
| 358 | } else if (const auto *Function = dyn_cast<FunctionDecl>(S.CurContext)) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 359 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 360 | Type = Function->getReturnType(); |
| 361 | ExpectedLoc = Tok; |
| 362 | } else if (const auto *Method = dyn_cast<ObjCMethodDecl>(S.CurContext)) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 363 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 364 | Type = Method->getReturnType(); |
| 365 | ExpectedLoc = Tok; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void PreferredTypeBuilder::enterVariableInit(SourceLocation Tok, Decl *D) { |
| 370 | auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D); |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 371 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 372 | Type = VD ? VD->getType() : QualType(); |
| 373 | ExpectedLoc = Tok; |
| 374 | } |
| 375 | |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 376 | void PreferredTypeBuilder::enterFunctionArgument( |
| 377 | SourceLocation Tok, llvm::function_ref<QualType()> ComputeType) { |
| 378 | this->ComputeType = ComputeType; |
| 379 | Type = QualType(); |
| 380 | ExpectedLoc = Tok; |
| 381 | } |
| 382 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 383 | void PreferredTypeBuilder::enterParenExpr(SourceLocation Tok, |
| 384 | SourceLocation LParLoc) { |
| 385 | // expected type for parenthesized expression does not change. |
| 386 | if (ExpectedLoc == LParLoc) |
| 387 | ExpectedLoc = Tok; |
| 388 | } |
| 389 | |
| 390 | static QualType getPreferredTypeOfBinaryRHS(Sema &S, Expr *LHS, |
| 391 | tok::TokenKind Op) { |
| 392 | if (!LHS) |
| 393 | return QualType(); |
| 394 | |
| 395 | QualType LHSType = LHS->getType(); |
| 396 | if (LHSType->isPointerType()) { |
| 397 | if (Op == tok::plus || Op == tok::plusequal || Op == tok::minusequal) |
| 398 | return S.getASTContext().getPointerDiffType(); |
| 399 | // Pointer difference is more common than subtracting an int from a pointer. |
| 400 | if (Op == tok::minus) |
| 401 | return LHSType; |
| 402 | } |
| 403 | |
| 404 | switch (Op) { |
| 405 | // No way to infer the type of RHS from LHS. |
| 406 | case tok::comma: |
| 407 | return QualType(); |
| 408 | // Prefer the type of the left operand for all of these. |
| 409 | // Arithmetic operations. |
| 410 | case tok::plus: |
| 411 | case tok::plusequal: |
| 412 | case tok::minus: |
| 413 | case tok::minusequal: |
| 414 | case tok::percent: |
| 415 | case tok::percentequal: |
| 416 | case tok::slash: |
| 417 | case tok::slashequal: |
| 418 | case tok::star: |
| 419 | case tok::starequal: |
| 420 | // Assignment. |
| 421 | case tok::equal: |
| 422 | // Comparison operators. |
| 423 | case tok::equalequal: |
| 424 | case tok::exclaimequal: |
| 425 | case tok::less: |
| 426 | case tok::lessequal: |
| 427 | case tok::greater: |
| 428 | case tok::greaterequal: |
| 429 | case tok::spaceship: |
| 430 | return LHS->getType(); |
| 431 | // Binary shifts are often overloaded, so don't try to guess those. |
| 432 | case tok::greatergreater: |
| 433 | case tok::greatergreaterequal: |
| 434 | case tok::lessless: |
| 435 | case tok::lesslessequal: |
| 436 | if (LHSType->isIntegralOrEnumerationType()) |
| 437 | return S.getASTContext().IntTy; |
| 438 | return QualType(); |
| 439 | // Logical operators, assume we want bool. |
| 440 | case tok::ampamp: |
| 441 | case tok::pipepipe: |
| 442 | case tok::caretcaret: |
| 443 | return S.getASTContext().BoolTy; |
| 444 | // Operators often used for bit manipulation are typically used with the type |
| 445 | // of the left argument. |
| 446 | case tok::pipe: |
| 447 | case tok::pipeequal: |
| 448 | case tok::caret: |
| 449 | case tok::caretequal: |
| 450 | case tok::amp: |
| 451 | case tok::ampequal: |
| 452 | if (LHSType->isIntegralOrEnumerationType()) |
| 453 | return LHSType; |
| 454 | return QualType(); |
| 455 | // RHS should be a pointer to a member of the 'LHS' type, but we can't give |
| 456 | // any particular type here. |
| 457 | case tok::periodstar: |
| 458 | case tok::arrowstar: |
| 459 | return QualType(); |
| 460 | default: |
| 461 | // FIXME(ibiryukov): handle the missing op, re-add the assertion. |
| 462 | // assert(false && "unhandled binary op"); |
| 463 | return QualType(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /// Get preferred type for an argument of an unary expression. \p ContextType is |
| 468 | /// preferred type of the whole unary expression. |
| 469 | static QualType getPreferredTypeOfUnaryArg(Sema &S, QualType ContextType, |
| 470 | tok::TokenKind Op) { |
| 471 | switch (Op) { |
| 472 | case tok::exclaim: |
| 473 | return S.getASTContext().BoolTy; |
| 474 | case tok::amp: |
| 475 | if (!ContextType.isNull() && ContextType->isPointerType()) |
| 476 | return ContextType->getPointeeType(); |
| 477 | return QualType(); |
| 478 | case tok::star: |
| 479 | if (ContextType.isNull()) |
| 480 | return QualType(); |
| 481 | return S.getASTContext().getPointerType(ContextType.getNonReferenceType()); |
| 482 | case tok::plus: |
| 483 | case tok::minus: |
| 484 | case tok::tilde: |
| 485 | case tok::minusminus: |
| 486 | case tok::plusplus: |
| 487 | if (ContextType.isNull()) |
| 488 | return S.getASTContext().IntTy; |
| 489 | // leave as is, these operators typically return the same type. |
| 490 | return ContextType; |
| 491 | case tok::kw___real: |
| 492 | case tok::kw___imag: |
| 493 | return QualType(); |
| 494 | default: |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 495 | assert(false && "unhandled unary op"); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 496 | return QualType(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void PreferredTypeBuilder::enterBinary(Sema &S, SourceLocation Tok, Expr *LHS, |
| 501 | tok::TokenKind Op) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 502 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 503 | Type = getPreferredTypeOfBinaryRHS(S, LHS, Op); |
| 504 | ExpectedLoc = Tok; |
| 505 | } |
| 506 | |
| 507 | void PreferredTypeBuilder::enterMemAccess(Sema &S, SourceLocation Tok, |
| 508 | Expr *Base) { |
| 509 | if (!Base) |
| 510 | return; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 511 | // Do we have expected type for Base? |
| 512 | if (ExpectedLoc != Base->getBeginLoc()) |
| 513 | return; |
| 514 | // Keep the expected type, only update the location. |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 515 | ExpectedLoc = Tok; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 516 | return; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void PreferredTypeBuilder::enterUnary(Sema &S, SourceLocation Tok, |
| 520 | tok::TokenKind OpKind, |
| 521 | SourceLocation OpLoc) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 522 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 523 | Type = getPreferredTypeOfUnaryArg(S, this->get(OpLoc), OpKind); |
| 524 | ExpectedLoc = Tok; |
| 525 | } |
| 526 | |
| 527 | void PreferredTypeBuilder::enterSubscript(Sema &S, SourceLocation Tok, |
| 528 | Expr *LHS) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 529 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 530 | Type = S.getASTContext().IntTy; |
| 531 | ExpectedLoc = Tok; |
| 532 | } |
| 533 | |
| 534 | void PreferredTypeBuilder::enterTypeCast(SourceLocation Tok, |
| 535 | QualType CastType) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 536 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 537 | Type = !CastType.isNull() ? CastType.getCanonicalType() : QualType(); |
| 538 | ExpectedLoc = Tok; |
| 539 | } |
| 540 | |
| 541 | void PreferredTypeBuilder::enterCondition(Sema &S, SourceLocation Tok) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 542 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 543 | Type = S.getASTContext().BoolTy; |
| 544 | ExpectedLoc = Tok; |
| 545 | } |
| 546 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 547 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 548 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 549 | unsigned SingleDeclIndex; |
| 550 | |
| 551 | public: |
| 552 | typedef DeclIndexPair value_type; |
| 553 | typedef value_type reference; |
| 554 | typedef std::ptrdiff_t difference_type; |
| 555 | typedef std::input_iterator_tag iterator_category; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 557 | class pointer { |
| 558 | DeclIndexPair Value; |
| 559 | |
| 560 | public: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 561 | pointer(const DeclIndexPair &Value) : Value(Value) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 562 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 563 | const DeclIndexPair *operator->() const { return &Value; } |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 564 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 565 | |
| 566 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 567 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 568 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 569 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 570 | |
| 571 | iterator(const DeclIndexPair *Iterator) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 572 | : DeclOrIterator(Iterator), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 573 | |
| 574 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 575 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 576 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 577 | SingleDeclIndex = 0; |
| 578 | return *this; |
| 579 | } |
| 580 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 581 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair *>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 582 | ++I; |
| 583 | DeclOrIterator = I; |
| 584 | return *this; |
| 585 | } |
| 586 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 587 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 588 | iterator tmp(*this); |
| 589 | ++(*this); |
| 590 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 591 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 592 | |
| 593 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 594 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 595 | return reference(ND, SingleDeclIndex); |
| 596 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 597 | return *DeclOrIterator.get<const DeclIndexPair *>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 600 | pointer operator->() const { return pointer(**this); } |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 601 | |
| 602 | friend bool operator==(const iterator &X, const iterator &Y) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 603 | return X.DeclOrIterator.getOpaqueValue() == |
| 604 | Y.DeclOrIterator.getOpaqueValue() && |
| 605 | X.SingleDeclIndex == Y.SingleDeclIndex; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 609 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 610 | } |
| 611 | }; |
| 612 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 613 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 614 | ResultBuilder::ShadowMapEntry::begin() const { |
| 615 | if (DeclOrVector.isNull()) |
| 616 | return iterator(); |
| 617 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 618 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 619 | return iterator(ND, SingleDeclIndex); |
| 620 | |
| 621 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 622 | } |
| 623 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 624 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 625 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 626 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 627 | return iterator(); |
| 628 | |
| 629 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 630 | } |
| 631 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 632 | /// Compute the qualification required to get from the current context |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 633 | /// (\p CurContext) to the target context (\p TargetContext). |
| 634 | /// |
| 635 | /// \param Context the AST context in which the qualification will be used. |
| 636 | /// |
| 637 | /// \param CurContext the context where an entity is being named, which is |
| 638 | /// typically based on the current scope. |
| 639 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 640 | /// \param TargetContext the context in which the named entity actually |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 641 | /// resides. |
| 642 | /// |
| 643 | /// \returns a nested name specifier that refers into the target context, or |
| 644 | /// NULL if no qualification is needed. |
| 645 | static NestedNameSpecifier * |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 646 | getRequiredQualification(ASTContext &Context, const DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 647 | const DeclContext *TargetContext) { |
| 648 | SmallVector<const DeclContext *, 4> TargetParents; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 649 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 650 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 651 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 652 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 653 | if (CommonAncestor->isTransparentContext() || |
| 654 | CommonAncestor->isFunctionOrMethod()) |
| 655 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 657 | TargetParents.push_back(CommonAncestor); |
| 658 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 659 | |
| 660 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 661 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 662 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 663 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 664 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 665 | if (!Namespace->getIdentifier()) |
| 666 | continue; |
| 667 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 668 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 669 | } else if (const auto *TD = dyn_cast<TagDecl>(Parent)) |
| 670 | Result = NestedNameSpecifier::Create( |
| 671 | Context, Result, false, Context.getTypeDeclType(TD).getTypePtr()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 672 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 673 | return Result; |
| 674 | } |
| 675 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 676 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 677 | /// 7.1.3, C++ [lib.global.names]). |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 678 | static bool isReservedName(const IdentifierInfo *Id, |
| 679 | bool doubleUnderscoreOnly = false) { |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 680 | if (Id->getLength() < 2) |
| 681 | return false; |
| 682 | const char *Name = Id->getNameStart(); |
| 683 | return Name[0] == '_' && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 684 | (Name[1] == '_' || |
| 685 | (Name[1] >= 'A' && Name[1] <= 'Z' && !doubleUnderscoreOnly)); |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // Some declarations have reserved names that we don't want to ever show. |
| 689 | // Filter out names reserved for the implementation if they come from a |
| 690 | // system header. |
| 691 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 692 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 693 | if (!Id) |
| 694 | return false; |
| 695 | |
| 696 | // Ignore reserved names for compiler provided decls. |
| 697 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 698 | return true; |
| 699 | |
| 700 | // For system headers ignore only double-underscore names. |
| 701 | // This allows for system headers providing private symbols with a single |
| 702 | // underscore. |
| 703 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 704 | SemaRef.SourceMgr.isInSystemHeader( |
| 705 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 706 | return true; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 707 | |
| 708 | return false; |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 711 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 712 | bool &AsNestedNameSpecifier) const { |
| 713 | AsNestedNameSpecifier = false; |
| 714 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 715 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 716 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 717 | |
| 718 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 719 | if (!ND->getDeclName()) |
| 720 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 721 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 722 | // Friend declarations and declarations introduced due to friends are never |
| 723 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 724 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 725 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 727 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 728 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 729 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 730 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 732 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 733 | if (isa<UsingDecl>(ND)) |
| 734 | return false; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 735 | |
| 736 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 737 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 739 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 740 | (isa<NamespaceDecl>(ND) && Filter != &ResultBuilder::IsNamespace && |
| 741 | Filter != &ResultBuilder::IsNamespaceOrAlias && Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 742 | AsNestedNameSpecifier = true; |
| 743 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 744 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 745 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 746 | // Check whether it is interesting as a nested-name-specifier. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 747 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 748 | IsNestedNameSpecifier(ND) && |
| 749 | (Filter != &ResultBuilder::IsMember || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 750 | (isa<CXXRecordDecl>(ND) && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 751 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 752 | AsNestedNameSpecifier = true; |
| 753 | return true; |
| 754 | } |
| 755 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 756 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 757 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 758 | // ... then it must be interesting! |
| 759 | return true; |
| 760 | } |
| 761 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 762 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 763 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 764 | // In C, there is no way to refer to a hidden name. |
| 765 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 766 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 767 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 768 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 769 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 770 | const DeclContext *HiddenCtx = |
| 771 | R.Declaration->getDeclContext()->getRedeclContext(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 773 | // There is no way to qualify a name declared in a function or method. |
| 774 | if (HiddenCtx->isFunctionOrMethod()) |
| 775 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 776 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 777 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 778 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 780 | // We can refer to the result with the appropriate qualification. Do it. |
| 781 | R.Hidden = true; |
| 782 | R.QualifierIsInformative = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 784 | if (!R.Qualifier) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 785 | R.Qualifier = getRequiredQualification(SemaRef.Context, CurContext, |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 786 | R.Declaration->getDeclContext()); |
| 787 | return false; |
| 788 | } |
| 789 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 790 | /// A simplified classification of types used to determine whether two |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 791 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 792 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 793 | switch (T->getTypeClass()) { |
| 794 | case Type::Builtin: |
| 795 | switch (cast<BuiltinType>(T)->getKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 796 | case BuiltinType::Void: |
| 797 | return STC_Void; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 798 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 799 | case BuiltinType::NullPtr: |
| 800 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 801 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 802 | case BuiltinType::Overload: |
| 803 | case BuiltinType::Dependent: |
| 804 | return STC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 805 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 806 | case BuiltinType::ObjCId: |
| 807 | case BuiltinType::ObjCClass: |
| 808 | case BuiltinType::ObjCSel: |
| 809 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 810 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 811 | default: |
| 812 | return STC_Arithmetic; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 813 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 815 | case Type::Complex: |
| 816 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 818 | case Type::Pointer: |
| 819 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 820 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 821 | case Type::BlockPointer: |
| 822 | return STC_Block; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 824 | case Type::LValueReference: |
| 825 | case Type::RValueReference: |
| 826 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 828 | case Type::ConstantArray: |
| 829 | case Type::IncompleteArray: |
| 830 | case Type::VariableArray: |
| 831 | case Type::DependentSizedArray: |
| 832 | return STC_Array; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 834 | case Type::DependentSizedExtVector: |
| 835 | case Type::Vector: |
| 836 | case Type::ExtVector: |
| 837 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 839 | case Type::FunctionProto: |
| 840 | case Type::FunctionNoProto: |
| 841 | return STC_Function; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 843 | case Type::Record: |
| 844 | return STC_Record; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 846 | case Type::Enum: |
| 847 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 849 | case Type::ObjCObject: |
| 850 | case Type::ObjCInterface: |
| 851 | case Type::ObjCObjectPointer: |
| 852 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 854 | default: |
| 855 | return STC_Other; |
| 856 | } |
| 857 | } |
| 858 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 859 | /// Get the type that a given expression will have if this declaration |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 860 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 861 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 862 | ND = ND->getUnderlyingDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 863 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 864 | if (const auto *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 865 | return C.getTypeDeclType(Type); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 866 | if (const auto *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 867 | return C.getObjCInterfaceType(Iface); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 869 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 870 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 871 | T = Function->getCallResultType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 872 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 873 | T = Method->getSendResultType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 874 | else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 875 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 876 | else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 877 | T = Property->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 878 | else if (const auto *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 879 | T = Value->getType(); |
Ilya Biryukov | c514ade | 2019-01-24 10:41:43 +0000 | [diff] [blame] | 880 | |
| 881 | if (T.isNull()) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 882 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 883 | |
| 884 | // Dig through references, function pointers, and block pointers to |
| 885 | // get down to the likely type of an expression when the entity is |
| 886 | // used. |
| 887 | do { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 888 | if (const auto *Ref = T->getAs<ReferenceType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 889 | T = Ref->getPointeeType(); |
| 890 | continue; |
| 891 | } |
| 892 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 893 | if (const auto *Pointer = T->getAs<PointerType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 894 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 895 | T = Pointer->getPointeeType(); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | break; |
| 900 | } |
| 901 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 902 | if (const auto *Block = T->getAs<BlockPointerType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 903 | T = Block->getPointeeType(); |
| 904 | continue; |
| 905 | } |
| 906 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 907 | if (const auto *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 908 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 909 | continue; |
| 910 | } |
| 911 | |
| 912 | break; |
| 913 | } while (true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 914 | |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 915 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 918 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 919 | if (!ND) |
| 920 | return CCP_Unlikely; |
| 921 | |
| 922 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 923 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 924 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 925 | // _cmd is relatively rare |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 926 | if (const auto *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 927 | if (ImplicitParam->getIdentifier() && |
| 928 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 929 | return CCP_ObjC_cmd; |
| 930 | |
| 931 | return CCP_LocalDeclaration; |
| 932 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 933 | |
| 934 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 935 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) { |
| 936 | // Explicit destructor calls are very rare. |
| 937 | if (isa<CXXDestructorDecl>(ND)) |
| 938 | return CCP_Unlikely; |
| 939 | // Explicit operator and conversion function calls are also very rare. |
| 940 | auto DeclNameKind = ND->getDeclName().getNameKind(); |
| 941 | if (DeclNameKind == DeclarationName::CXXOperatorName || |
| 942 | DeclNameKind == DeclarationName::CXXLiteralOperatorName || |
| 943 | DeclNameKind == DeclarationName::CXXConversionFunctionName) |
| 944 | return CCP_Unlikely; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 945 | return CCP_MemberDeclaration; |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 946 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 947 | |
| 948 | // Content-based decisions. |
| 949 | if (isa<EnumConstantDecl>(ND)) |
| 950 | return CCP_Constant; |
| 951 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 952 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 953 | // message receiver, or parenthesized expression context. There, it's as |
| 954 | // likely that the user will want to write a type as other declarations. |
| 955 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 956 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 957 | CompletionContext.getKind() == |
| 958 | CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 959 | CompletionContext.getKind() == |
| 960 | CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 961 | return CCP_Type; |
| 962 | |
| 963 | return CCP_Declaration; |
| 964 | } |
| 965 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 966 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 967 | // If this is an Objective-C method declaration whose selector matches our |
| 968 | // preferred selector, give it a priority boost. |
| 969 | if (!PreferredSelector.isNull()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 970 | if (const auto *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 971 | if (PreferredSelector == Method->getSelector()) |
| 972 | R.Priority += CCD_SelectorMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 974 | // If we have a preferred type, adjust the priority for results with exactly- |
| 975 | // matching or nearly-matching types. |
| 976 | if (!PreferredType.isNull()) { |
| 977 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 978 | if (!T.isNull()) { |
| 979 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 980 | // Check for exactly-matching types (modulo qualifiers). |
| 981 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 982 | R.Priority /= CCF_ExactTypeMatch; |
| 983 | // Check for nearly-matching types, based on classification of each. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 984 | else if ((getSimplifiedTypeClass(PreferredType) == |
| 985 | getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 986 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 987 | R.Priority /= CCF_SimilarTypeMatch; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 988 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 989 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Benjamin Kramer | 756ecb8 | 2019-02-11 14:52:15 +0000 | [diff] [blame] | 992 | static DeclContext::lookup_result getConstructors(ASTContext &Context, |
| 993 | const CXXRecordDecl *Record) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 994 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 995 | DeclarationName ConstructorName = |
| 996 | Context.DeclarationNames.getCXXConstructorName( |
| 997 | Context.getCanonicalType(RecordTy)); |
| 998 | return Record->lookup(ConstructorName); |
| 999 | } |
| 1000 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1001 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1002 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1003 | !CompletionContext.wantConstructorResults()) |
| 1004 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1005 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1006 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1007 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1008 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1009 | Record = ClassTemplate->getTemplatedDecl(); |
| 1010 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 1011 | // Skip specializations and partial specializations. |
| 1012 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 1013 | return; |
| 1014 | } else { |
| 1015 | // There are no constructors here. |
| 1016 | return; |
| 1017 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1019 | Record = Record->getDefinition(); |
| 1020 | if (!Record) |
| 1021 | return; |
| 1022 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1023 | for (NamedDecl *Ctor : getConstructors(SemaRef.Context, Record)) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 1024 | R.Declaration = Ctor; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1025 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 1026 | Results.push_back(R); |
| 1027 | } |
| 1028 | } |
| 1029 | |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1030 | static bool isConstructor(const Decl *ND) { |
| 1031 | if (const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1032 | ND = Tmpl->getTemplatedDecl(); |
| 1033 | return isa<CXXConstructorDecl>(ND); |
| 1034 | } |
| 1035 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1036 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 1037 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1038 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1039 | if (R.Kind != Result::RK_Declaration) { |
| 1040 | // For non-declaration results, just add the result. |
| 1041 | Results.push_back(R); |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | // Look through using declarations. |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1046 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 1047 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 1048 | getBasePriority(Using->getTargetDecl()), |
| 1049 | R.Qualifier); |
| 1050 | Result.ShadowDecl = Using; |
| 1051 | MaybeAddResult(Result, CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1054 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1055 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1056 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 1057 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1058 | bool AsNestedNameSpecifier = false; |
| 1059 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1060 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1061 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1062 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1063 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1064 | return; |
| 1065 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1066 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1067 | ShadowMapEntry::iterator I, IEnd; |
| 1068 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 1069 | if (NamePos != SMap.end()) { |
| 1070 | I = NamePos->second.begin(); |
| 1071 | IEnd = NamePos->second.end(); |
| 1072 | } |
| 1073 | |
| 1074 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1075 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1076 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1077 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 1078 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1079 | Results[Index].Declaration = R.Declaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1081 | // We're done. |
| 1082 | return; |
| 1083 | } |
| 1084 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1085 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1086 | // This is a new declaration in this scope. However, check whether this |
| 1087 | // declaration name is hidden by a similarly-named declaration in an outer |
| 1088 | // scope. |
| 1089 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 1090 | --SMEnd; |
| 1091 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1092 | ShadowMapEntry::iterator I, IEnd; |
| 1093 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 1094 | if (NamePos != SM->end()) { |
| 1095 | I = NamePos->second.begin(); |
| 1096 | IEnd = NamePos->second.end(); |
| 1097 | } |
| 1098 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1099 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1100 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1101 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 1102 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1103 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1105 | // Protocols are in distinct namespaces from everything else. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1106 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) || |
| 1107 | (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1108 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1109 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1111 | // The newly-added result is hidden by an entry in the shadow map. |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 1112 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1113 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1114 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1115 | break; |
| 1116 | } |
| 1117 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1119 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1120 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1121 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 1122 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1123 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1124 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1125 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1126 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1127 | R.Priority = CCP_NestedNameSpecifier; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1128 | } else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1129 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1131 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1132 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1133 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1134 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 1135 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1136 | R.Qualifier = |
| 1137 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1138 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1139 | R.Qualifier = NestedNameSpecifier::Create( |
| 1140 | SemaRef.Context, nullptr, false, |
| 1141 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1142 | else |
| 1143 | R.QualifierIsInformative = false; |
| 1144 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1146 | // Insert this result into the set of results and into the current shadow |
| 1147 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1148 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1149 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1151 | if (!AsNestedNameSpecifier) |
| 1152 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 1155 | static void setInBaseClass(ResultBuilder::Result &R) { |
| 1156 | R.Priority += CCD_InBaseClass; |
| 1157 | R.InBaseClass = true; |
| 1158 | } |
| 1159 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1160 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1161 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1162 | if (R.Kind != Result::RK_Declaration) { |
| 1163 | // For non-declaration results, just add the result. |
| 1164 | Results.push_back(R); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1168 | // Look through using declarations. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1169 | if (const auto *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1170 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 1171 | getBasePriority(Using->getTargetDecl()), |
| 1172 | R.Qualifier); |
| 1173 | Result.ShadowDecl = Using; |
| 1174 | AddResult(Result, CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1175 | return; |
| 1176 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1178 | bool AsNestedNameSpecifier = false; |
| 1179 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1180 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1182 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1183 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1186 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 1187 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1189 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1190 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1191 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1193 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1194 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1195 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1196 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1197 | R.Priority = CCP_NestedNameSpecifier; |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1198 | } else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && |
| 1199 | InBaseClass && |
| 1200 | isa<CXXRecordDecl>( |
| 1201 | R.Declaration->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1202 | R.QualifierIsInformative = true; |
| 1203 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1204 | // If this result is supposed to have an informative qualifier, add one. |
| 1205 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1206 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1207 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1208 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1209 | R.Qualifier = |
| 1210 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
| 1211 | else if (const auto *Tag = dyn_cast<TagDecl>(Ctx)) |
| 1212 | R.Qualifier = NestedNameSpecifier::Create( |
| 1213 | SemaRef.Context, nullptr, false, |
| 1214 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1215 | else |
| 1216 | R.QualifierIsInformative = false; |
| 1217 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1219 | // Adjust the priority if this result comes from a base class. |
| 1220 | if (InBaseClass) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 1221 | setInBaseClass(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1223 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1225 | if (HasObjectTypeQualifiers) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1226 | if (const auto *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1227 | if (Method->isInstance()) { |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 1228 | Qualifiers MethodQuals = Method->getMethodQualifiers(); |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1229 | if (ObjectTypeQualifiers == MethodQuals) |
| 1230 | R.Priority += CCD_ObjectQualifierMatch; |
| 1231 | else if (ObjectTypeQualifiers - MethodQuals) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1232 | // The method cannot be invoked, because doing so would drop |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1233 | // qualifiers. |
| 1234 | return; |
| 1235 | } |
| 1236 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1238 | // Insert this result into the set of results. |
| 1239 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1241 | if (!AsNestedNameSpecifier) |
| 1242 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1245 | void ResultBuilder::AddResult(Result R) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1246 | assert(R.Kind != Result::RK_Declaration && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1247 | "Declaration results need more context"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1248 | Results.push_back(R); |
| 1249 | } |
| 1250 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1251 | /// Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1252 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1253 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1254 | /// Exit from the current scope. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1255 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1256 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1257 | EEnd = ShadowMaps.back().end(); |
| 1258 | E != EEnd; ++E) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1259 | E->second.Destroy(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1260 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1261 | ShadowMaps.pop_back(); |
| 1262 | } |
| 1263 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1264 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1265 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1266 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1267 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1268 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1269 | // If name lookup finds a local extern declaration, then we are in a |
| 1270 | // context where it behaves like an ordinary name. |
| 1271 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1272 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1273 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1274 | else if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1275 | if (isa<ObjCIvarDecl>(ND)) |
| 1276 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1277 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1278 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1279 | return ND->getIdentifierNamespace() & IDNS; |
| 1280 | } |
| 1281 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1282 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1283 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1284 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1285 | ND = ND->getUnderlyingDecl(); |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1286 | if (isa<TypeDecl>(ND)) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1287 | return false; |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1288 | // Objective-C interfaces names are not filtered by this method because they |
| 1289 | // can be used in a class property expression. We can still filter out |
| 1290 | // @class declarations though. |
| 1291 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 1292 | if (!ID->getDefinition()) |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1296 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1297 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1298 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1299 | else if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1300 | if (isa<ObjCIvarDecl>(ND)) |
| 1301 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1302 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1304 | return ND->getIdentifierNamespace() & IDNS; |
| 1305 | } |
| 1306 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1307 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1308 | if (!IsOrdinaryNonTypeName(ND)) |
| 1309 | return 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1310 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1311 | if (const auto *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1312 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1313 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
| 1317 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1318 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1319 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1320 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1321 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1322 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1323 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1324 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1325 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1326 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1327 | return (ND->getIdentifierNamespace() & IDNS) && !isa<ValueDecl>(ND) && |
| 1328 | !isa<FunctionTemplateDecl>(ND) && !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1331 | /// Determines whether the given declaration is suitable as the |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1332 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1333 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1334 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1335 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1336 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1338 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1339 | } |
| 1340 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1341 | /// Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1342 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1343 | return isa<EnumDecl>(ND); |
| 1344 | } |
| 1345 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1346 | /// Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1347 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1348 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1349 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1350 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1351 | |
| 1352 | // For purposes of this check, interfaces match too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1353 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
| 1354 | return RD->getTagKind() == TTK_Class || RD->getTagKind() == TTK_Struct || |
| 1355 | RD->getTagKind() == TTK_Interface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1356 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1357 | return false; |
| 1358 | } |
| 1359 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1360 | /// Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1361 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1362 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1363 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1364 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1365 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1366 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1367 | return RD->getTagKind() == TTK_Union; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1369 | return false; |
| 1370 | } |
| 1371 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1372 | /// Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1373 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1374 | return isa<NamespaceDecl>(ND); |
| 1375 | } |
| 1376 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1377 | /// Determines whether the given declaration is a namespace or |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1378 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1379 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1380 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1383 | /// Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1384 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1385 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1386 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1389 | /// Determines which members of a class should be visible via |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1390 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1391 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1392 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1393 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1394 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1395 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1398 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1399 | T = C.getCanonicalType(T); |
| 1400 | switch (T->getTypeClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1401 | case Type::ObjCObject: |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1402 | case Type::ObjCInterface: |
| 1403 | case Type::ObjCObjectPointer: |
| 1404 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1406 | case Type::Builtin: |
| 1407 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1408 | case BuiltinType::ObjCId: |
| 1409 | case BuiltinType::ObjCClass: |
| 1410 | case BuiltinType::ObjCSel: |
| 1411 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1413 | default: |
| 1414 | break; |
| 1415 | } |
| 1416 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1418 | default: |
| 1419 | break; |
| 1420 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1421 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1422 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1423 | return false; |
| 1424 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1425 | // FIXME: We could perform more analysis here to determine whether a |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1426 | // particular class type has any conversions to Objective-C types. For now, |
| 1427 | // just accept all class types. |
| 1428 | return T->isDependentType() || T->isRecordType(); |
| 1429 | } |
| 1430 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1431 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1432 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1433 | if (T.isNull()) |
| 1434 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1435 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1436 | T = SemaRef.Context.getBaseElementType(T); |
| 1437 | return isObjCReceiverType(SemaRef.Context, T); |
| 1438 | } |
| 1439 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1440 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture( |
| 1441 | const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1442 | if (IsObjCMessageReceiver(ND)) |
| 1443 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1444 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1445 | const auto *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1446 | if (!Var) |
| 1447 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1449 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1450 | } |
| 1451 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1452 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1453 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1454 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1455 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1456 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1457 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1458 | if (T.isNull()) |
| 1459 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1460 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1461 | T = SemaRef.Context.getBaseElementType(T); |
| 1462 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1463 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1464 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1465 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1466 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1467 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1468 | return false; |
| 1469 | } |
| 1470 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1471 | /// Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1472 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1473 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1474 | return isa<ObjCIvarDecl>(ND); |
| 1475 | } |
| 1476 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1477 | namespace { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1478 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1479 | /// Visible declaration consumer that adds a code-completion result |
| 1480 | /// for each visible declaration. |
| 1481 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1482 | ResultBuilder &Results; |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1483 | DeclContext *InitialLookupCtx; |
| 1484 | // NamingClass and BaseType are used for access-checking. See |
| 1485 | // Sema::IsSimplyAccessible for details. |
| 1486 | CXXRecordDecl *NamingClass; |
| 1487 | QualType BaseType; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1488 | std::vector<FixItHint> FixIts; |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1489 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1490 | public: |
| 1491 | CodeCompletionDeclConsumer( |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1492 | ResultBuilder &Results, DeclContext *InitialLookupCtx, |
| 1493 | QualType BaseType = QualType(), |
| 1494 | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
| 1495 | : Results(Results), InitialLookupCtx(InitialLookupCtx), |
| 1496 | FixIts(std::move(FixIts)) { |
| 1497 | NamingClass = llvm::dyn_cast<CXXRecordDecl>(InitialLookupCtx); |
| 1498 | // If BaseType was not provided explicitly, emulate implicit 'this->'. |
| 1499 | if (BaseType.isNull()) { |
| 1500 | auto ThisType = Results.getSema().getCurrentThisType(); |
| 1501 | if (!ThisType.isNull()) { |
| 1502 | assert(ThisType->isPointerType()); |
| 1503 | BaseType = ThisType->getPointeeType(); |
| 1504 | if (!NamingClass) |
| 1505 | NamingClass = BaseType->getAsCXXRecordDecl(); |
| 1506 | } |
| 1507 | } |
| 1508 | this->BaseType = BaseType; |
| 1509 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1510 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1511 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1512 | bool InBaseClass) override { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1513 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
Ilya Biryukov | 9839755 | 2018-12-05 17:38:39 +0000 | [diff] [blame] | 1514 | false, IsAccessible(ND, Ctx), FixIts); |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1515 | Results.AddResult(Result, InitialLookupCtx, Hiding, InBaseClass); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1516 | } |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1517 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1518 | void EnteredContext(DeclContext *Ctx) override { |
| 1519 | Results.addVisitedContext(Ctx); |
| 1520 | } |
Ilya Biryukov | 9839755 | 2018-12-05 17:38:39 +0000 | [diff] [blame] | 1521 | |
| 1522 | private: |
| 1523 | bool IsAccessible(NamedDecl *ND, DeclContext *Ctx) { |
| 1524 | // Naming class to use for access check. In most cases it was provided |
| 1525 | // explicitly (e.g. member access (lhs.foo) or qualified lookup (X::)), |
| 1526 | // for unqualified lookup we fallback to the \p Ctx in which we found the |
| 1527 | // member. |
| 1528 | auto *NamingClass = this->NamingClass; |
| 1529 | QualType BaseType = this->BaseType; |
| 1530 | if (auto *Cls = llvm::dyn_cast_or_null<CXXRecordDecl>(Ctx)) { |
| 1531 | if (!NamingClass) |
| 1532 | NamingClass = Cls; |
| 1533 | // When we emulate implicit 'this->' in an unqualified lookup, we might |
| 1534 | // end up with an invalid naming class. In that case, we avoid emulating |
| 1535 | // 'this->' qualifier to satisfy preconditions of the access checking. |
| 1536 | if (NamingClass->getCanonicalDecl() != Cls->getCanonicalDecl() && |
| 1537 | !NamingClass->isDerivedFrom(Cls)) { |
| 1538 | NamingClass = Cls; |
| 1539 | BaseType = QualType(); |
| 1540 | } |
| 1541 | } else { |
| 1542 | // The decl was found outside the C++ class, so only ObjC access checks |
| 1543 | // apply. Those do not rely on NamingClass and BaseType, so we clear them |
| 1544 | // out. |
| 1545 | NamingClass = nullptr; |
| 1546 | BaseType = QualType(); |
| 1547 | } |
| 1548 | return Results.getSema().IsSimplyAccessible(ND, NamingClass, BaseType); |
| 1549 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1550 | }; |
| 1551 | } // namespace |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1552 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1553 | /// Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1554 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1555 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1556 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1557 | Results.AddResult(Result("short", CCP_Type)); |
| 1558 | Results.AddResult(Result("long", CCP_Type)); |
| 1559 | Results.AddResult(Result("signed", CCP_Type)); |
| 1560 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1561 | Results.AddResult(Result("void", CCP_Type)); |
| 1562 | Results.AddResult(Result("char", CCP_Type)); |
| 1563 | Results.AddResult(Result("int", CCP_Type)); |
| 1564 | Results.AddResult(Result("float", CCP_Type)); |
| 1565 | Results.AddResult(Result("double", CCP_Type)); |
| 1566 | Results.AddResult(Result("enum", CCP_Type)); |
| 1567 | Results.AddResult(Result("struct", CCP_Type)); |
| 1568 | Results.AddResult(Result("union", CCP_Type)); |
| 1569 | Results.AddResult(Result("const", CCP_Type)); |
| 1570 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1571 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1572 | if (LangOpts.C99) { |
| 1573 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1574 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1575 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1576 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1577 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1578 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1579 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1580 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1581 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1582 | if (LangOpts.CPlusPlus) { |
| 1583 | // C++-specific |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1584 | Results.AddResult( |
| 1585 | Result("bool", CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1586 | Results.AddResult(Result("class", CCP_Type)); |
| 1587 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1589 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1590 | Builder.AddTypedTextChunk("typename"); |
| 1591 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1592 | Builder.AddPlaceholderChunk("qualifier"); |
| 1593 | Builder.AddTextChunk("::"); |
| 1594 | Builder.AddPlaceholderChunk("name"); |
| 1595 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1596 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1597 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1598 | Results.AddResult(Result("auto", CCP_Type)); |
| 1599 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1600 | Results.AddResult(Result("char32_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1602 | Builder.AddTypedTextChunk("decltype"); |
| 1603 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1604 | Builder.AddPlaceholderChunk("expression"); |
| 1605 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1606 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1607 | } |
Alex Lorenz | 46eed9d | 2017-02-13 23:35:59 +0000 | [diff] [blame] | 1608 | } else |
| 1609 | Results.AddResult(Result("__auto_type", CCP_Type)); |
| 1610 | |
Richard Smith | 7b301e2 | 2018-05-24 21:51:52 +0000 | [diff] [blame] | 1611 | // GNU keywords |
| 1612 | if (LangOpts.GNUKeywords) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1613 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1614 | // Results.AddResult(Result("_Decimal32")); |
| 1615 | // Results.AddResult(Result("_Decimal64")); |
| 1616 | // Results.AddResult(Result("_Decimal128")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1618 | Builder.AddTypedTextChunk("typeof"); |
| 1619 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1620 | Builder.AddPlaceholderChunk("expression"); |
| 1621 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1623 | Builder.AddTypedTextChunk("typeof"); |
| 1624 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1625 | Builder.AddPlaceholderChunk("type"); |
| 1626 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1627 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1628 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1631 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1632 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1633 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1636 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1637 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1638 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1639 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1640 | // Note: we don't suggest either "auto" or "register", because both |
| 1641 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1642 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1643 | Results.AddResult(Result("extern")); |
| 1644 | Results.AddResult(Result("static")); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (LangOpts.CPlusPlus11) { |
| 1647 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 1648 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| 1649 | |
| 1650 | // alignas |
| 1651 | Builder.AddTypedTextChunk("alignas"); |
| 1652 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1653 | Builder.AddPlaceholderChunk("expression"); |
| 1654 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1655 | Results.AddResult(Result(Builder.TakeString())); |
| 1656 | |
| 1657 | Results.AddResult(Result("constexpr")); |
| 1658 | Results.AddResult(Result("thread_local")); |
| 1659 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1662 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1663 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1664 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1665 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1666 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1667 | case Sema::PCC_Class: |
| 1668 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1669 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1670 | Results.AddResult(Result("explicit")); |
| 1671 | Results.AddResult(Result("friend")); |
| 1672 | Results.AddResult(Result("mutable")); |
| 1673 | Results.AddResult(Result("virtual")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1674 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1675 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1676 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1677 | case Sema::PCC_ObjCInterface: |
| 1678 | case Sema::PCC_ObjCImplementation: |
| 1679 | case Sema::PCC_Namespace: |
| 1680 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1681 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1682 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1685 | case Sema::PCC_ObjCInstanceVariableList: |
| 1686 | case Sema::PCC_Expression: |
| 1687 | case Sema::PCC_Statement: |
| 1688 | case Sema::PCC_ForInit: |
| 1689 | case Sema::PCC_Condition: |
| 1690 | case Sema::PCC_RecoveryInFunction: |
| 1691 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1692 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1693 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1698 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1699 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1700 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1701 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1702 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1703 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1704 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1705 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1706 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1708 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1709 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1710 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1711 | Builder.AddTypedTextChunk("typedef"); |
| 1712 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1713 | Builder.AddPlaceholderChunk("type"); |
| 1714 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1715 | Builder.AddPlaceholderChunk("name"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1716 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1719 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1720 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1721 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1722 | case Sema::PCC_Namespace: |
| 1723 | case Sema::PCC_Class: |
| 1724 | case Sema::PCC_ObjCInstanceVariableList: |
| 1725 | case Sema::PCC_Template: |
| 1726 | case Sema::PCC_MemberTemplate: |
| 1727 | case Sema::PCC_Statement: |
| 1728 | case Sema::PCC_RecoveryInFunction: |
| 1729 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1730 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1731 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1732 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1733 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1734 | case Sema::PCC_Expression: |
| 1735 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1736 | return LangOpts.CPlusPlus; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1738 | case Sema::PCC_ObjCInterface: |
| 1739 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1740 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1741 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | case Sema::PCC_ForInit: |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1743 | return LangOpts.CPlusPlus || LangOpts.ObjC || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1744 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1745 | |
| 1746 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1749 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1750 | const Preprocessor &PP) { |
| 1751 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1752 | Policy.AnonymousTagLocations = false; |
| 1753 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1754 | Policy.SuppressUnwrittenScope = true; |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 1755 | Policy.SuppressScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1756 | return Policy; |
| 1757 | } |
| 1758 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1759 | /// Retrieve a printing policy suitable for code completion. |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1760 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1761 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1762 | } |
| 1763 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1764 | /// Retrieve the string representation of the given type as a string |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1765 | /// that has the appropriate lifetime for code completion. |
| 1766 | /// |
| 1767 | /// This routine provides a fast path where we provide constant strings for |
| 1768 | /// common type names. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1769 | static const char *GetCompletionTypeString(QualType T, ASTContext &Context, |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1770 | const PrintingPolicy &Policy, |
| 1771 | CodeCompletionAllocator &Allocator) { |
| 1772 | if (!T.getLocalQualifiers()) { |
| 1773 | // Built-in type names are constant strings. |
| 1774 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1775 | return BT->getNameAsCString(Policy); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1776 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1777 | // Anonymous tag types are constant strings. |
| 1778 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1779 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1780 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1781 | switch (Tag->getTagKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1782 | case TTK_Struct: |
| 1783 | return "struct <anonymous>"; |
| 1784 | case TTK_Interface: |
| 1785 | return "__interface <anonymous>"; |
| 1786 | case TTK_Class: |
| 1787 | return "class <anonymous>"; |
| 1788 | case TTK_Union: |
| 1789 | return "union <anonymous>"; |
| 1790 | case TTK_Enum: |
| 1791 | return "enum <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1796 | // Slow path: format the type as a string. |
| 1797 | std::string Result; |
| 1798 | T.getAsStringInternal(Result, Policy); |
| 1799 | return Allocator.CopyString(Result); |
| 1800 | } |
| 1801 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1802 | /// Add a completion for "this", if we're in a member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1803 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1804 | QualType ThisTy = S.getCurrentThisType(); |
| 1805 | if (ThisTy.isNull()) |
| 1806 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1808 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1809 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1810 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1811 | Builder.AddResultTypeChunk( |
| 1812 | GetCompletionTypeString(ThisTy, S.Context, Policy, Allocator)); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1813 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1814 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1817 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
| 1818 | ResultBuilder &Results, |
| 1819 | const LangOptions &LangOpts) { |
| 1820 | if (!LangOpts.CPlusPlus11) |
| 1821 | return; |
| 1822 | |
| 1823 | Builder.AddTypedTextChunk("static_assert"); |
| 1824 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1825 | Builder.AddPlaceholderChunk("expression"); |
| 1826 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 1827 | Builder.AddPlaceholderChunk("message"); |
| 1828 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1829 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 1830 | } |
| 1831 | |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 1832 | static void AddOverrideResults(ResultBuilder &Results, |
| 1833 | const CodeCompletionContext &CCContext, |
| 1834 | CodeCompletionBuilder &Builder) { |
| 1835 | Sema &S = Results.getSema(); |
| 1836 | const auto *CR = llvm::dyn_cast<CXXRecordDecl>(S.CurContext); |
| 1837 | // If not inside a class/struct/union return empty. |
| 1838 | if (!CR) |
| 1839 | return; |
| 1840 | // First store overrides within current class. |
| 1841 | // These are stored by name to make querying fast in the later step. |
| 1842 | llvm::StringMap<std::vector<FunctionDecl *>> Overrides; |
| 1843 | for (auto *Method : CR->methods()) { |
| 1844 | if (!Method->isVirtual() || !Method->getIdentifier()) |
| 1845 | continue; |
| 1846 | Overrides[Method->getName()].push_back(Method); |
| 1847 | } |
| 1848 | |
| 1849 | for (const auto &Base : CR->bases()) { |
| 1850 | const auto *BR = Base.getType().getTypePtr()->getAsCXXRecordDecl(); |
| 1851 | if (!BR) |
| 1852 | continue; |
| 1853 | for (auto *Method : BR->methods()) { |
| 1854 | if (!Method->isVirtual() || !Method->getIdentifier()) |
| 1855 | continue; |
| 1856 | const auto it = Overrides.find(Method->getName()); |
| 1857 | bool IsOverriden = false; |
| 1858 | if (it != Overrides.end()) { |
| 1859 | for (auto *MD : it->second) { |
| 1860 | // If the method in current body is not an overload of this virtual |
| 1861 | // function, then it overrides this one. |
| 1862 | if (!S.IsOverload(MD, Method, false)) { |
| 1863 | IsOverriden = true; |
| 1864 | break; |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | if (!IsOverriden) { |
| 1869 | // Generates a new CodeCompletionResult by taking this function and |
| 1870 | // converting it into an override declaration with only one chunk in the |
| 1871 | // final CodeCompletionString as a TypedTextChunk. |
| 1872 | std::string OverrideSignature; |
| 1873 | llvm::raw_string_ostream OS(OverrideSignature); |
| 1874 | CodeCompletionResult CCR(Method, 0); |
| 1875 | PrintingPolicy Policy = |
| 1876 | getCompletionPrintingPolicy(S.getASTContext(), S.getPreprocessor()); |
| 1877 | auto *CCS = CCR.createCodeCompletionStringForOverride( |
| 1878 | S.getPreprocessor(), S.getASTContext(), Builder, |
| 1879 | /*IncludeBriefComments=*/false, CCContext, Policy); |
| 1880 | Results.AddResult(CodeCompletionResult(CCS, Method, CCP_CodePattern)); |
| 1881 | } |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1886 | /// Add language constructs that show up for "ordinary" names. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1887 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, Scope *S, |
| 1888 | Sema &SemaRef, ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1889 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1890 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1891 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1892 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1893 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1894 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1895 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1896 | if (Results.includeCodePatterns()) { |
| 1897 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1898 | Builder.AddTypedTextChunk("namespace"); |
| 1899 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1900 | Builder.AddPlaceholderChunk("identifier"); |
| 1901 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1902 | Builder.AddPlaceholderChunk("declarations"); |
| 1903 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1904 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1905 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1906 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1907 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1908 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1909 | Builder.AddTypedTextChunk("namespace"); |
| 1910 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1911 | Builder.AddPlaceholderChunk("name"); |
| 1912 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1913 | Builder.AddPlaceholderChunk("namespace"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 1914 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1915 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1916 | |
| 1917 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1918 | Builder.AddTypedTextChunk("using"); |
| 1919 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1920 | Builder.AddTextChunk("namespace"); |
| 1921 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1922 | Builder.AddPlaceholderChunk("identifier"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 1923 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1924 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1925 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1926 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1927 | Builder.AddTypedTextChunk("asm"); |
| 1928 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1929 | Builder.AddPlaceholderChunk("string-literal"); |
| 1930 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1931 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1933 | if (Results.includeCodePatterns()) { |
| 1934 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1935 | Builder.AddTypedTextChunk("template"); |
| 1936 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1937 | Builder.AddPlaceholderChunk("declaration"); |
| 1938 | Results.AddResult(Result(Builder.TakeString())); |
Eric Liu | b87c6eb | 2018-10-15 12:37:23 +0000 | [diff] [blame] | 1939 | } else { |
| 1940 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1941 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1942 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1943 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1944 | if (SemaRef.getLangOpts().ObjC) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1945 | AddObjCTopLevelResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1947 | AddTypedefResult(Results); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1948 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1949 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1950 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1951 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1952 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1953 | Builder.AddTypedTextChunk("using"); |
| 1954 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1955 | Builder.AddPlaceholderChunk("qualifier"); |
| 1956 | Builder.AddTextChunk("::"); |
| 1957 | Builder.AddPlaceholderChunk("name"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 1958 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1959 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1961 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1962 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1963 | Builder.AddTypedTextChunk("using"); |
| 1964 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1965 | Builder.AddTextChunk("typename"); |
| 1966 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1967 | Builder.AddPlaceholderChunk("qualifier"); |
| 1968 | Builder.AddTextChunk("::"); |
| 1969 | Builder.AddPlaceholderChunk("name"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 1970 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1971 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1974 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| 1975 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1976 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1977 | AddTypedefResult(Results); |
| 1978 | |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1979 | bool IsNotInheritanceScope = |
| 1980 | !(S->getFlags() & Scope::ClassInheritanceScope); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1981 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1982 | Builder.AddTypedTextChunk("public"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1983 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1984 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1985 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1986 | |
| 1987 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1988 | Builder.AddTypedTextChunk("protected"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1989 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1990 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1991 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1992 | |
| 1993 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1994 | Builder.AddTypedTextChunk("private"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1995 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1996 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1997 | Results.AddResult(Result(Builder.TakeString())); |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 1998 | |
| 1999 | // FIXME: This adds override results only if we are at the first word of |
| 2000 | // the declaration/definition. Also call this from other sides to have |
| 2001 | // more use-cases. |
| 2002 | AddOverrideResults(Results, CodeCompletionContext::CCC_ClassStructUnion, |
| 2003 | Builder); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 2006 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2007 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2008 | case Sema::PCC_Template: |
| 2009 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2010 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2011 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2012 | Builder.AddTypedTextChunk("template"); |
| 2013 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2014 | Builder.AddPlaceholderChunk("parameters"); |
| 2015 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2016 | Results.AddResult(Result(Builder.TakeString())); |
Eric Liu | b87c6eb | 2018-10-15 12:37:23 +0000 | [diff] [blame] | 2017 | } else { |
| 2018 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2021 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2022 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2025 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2026 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 2027 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2028 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2029 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2030 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2031 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2032 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 2033 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2034 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2035 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2036 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2037 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2038 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2039 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2040 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2041 | case Sema::PCC_RecoveryInFunction: |
| 2042 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2043 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2044 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2045 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 2046 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2047 | Builder.AddTypedTextChunk("try"); |
| 2048 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2049 | Builder.AddPlaceholderChunk("statements"); |
| 2050 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2051 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2052 | Builder.AddTextChunk("catch"); |
| 2053 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2054 | Builder.AddPlaceholderChunk("declaration"); |
| 2055 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2056 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2057 | Builder.AddPlaceholderChunk("statements"); |
| 2058 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2059 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2060 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2061 | } |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2062 | if (SemaRef.getLangOpts().ObjC) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2063 | AddObjCStatementResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2064 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2065 | if (Results.includeCodePatterns()) { |
| 2066 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2067 | Builder.AddTypedTextChunk("if"); |
| 2068 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2069 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2070 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2071 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2072 | Builder.AddPlaceholderChunk("expression"); |
| 2073 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2074 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2075 | Builder.AddPlaceholderChunk("statements"); |
| 2076 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2077 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2078 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2080 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2081 | Builder.AddTypedTextChunk("switch"); |
| 2082 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2083 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2084 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2085 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2086 | Builder.AddPlaceholderChunk("expression"); |
| 2087 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2088 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2089 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2090 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2091 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2092 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2093 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2094 | // Switch-specific statements. |
Sam McCall | aeb4b3e | 2018-10-10 10:51:48 +0000 | [diff] [blame] | 2095 | if (SemaRef.getCurFunction() && |
| 2096 | !SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2097 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2098 | Builder.AddTypedTextChunk("case"); |
| 2099 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2100 | Builder.AddPlaceholderChunk("expression"); |
| 2101 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 2102 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2103 | |
| 2104 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2105 | Builder.AddTypedTextChunk("default"); |
| 2106 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 2107 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2110 | if (Results.includeCodePatterns()) { |
| 2111 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2112 | Builder.AddTypedTextChunk("while"); |
| 2113 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2114 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2115 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2116 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2117 | Builder.AddPlaceholderChunk("expression"); |
| 2118 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2119 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2120 | Builder.AddPlaceholderChunk("statements"); |
| 2121 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2122 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2123 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2124 | |
| 2125 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2126 | Builder.AddTypedTextChunk("do"); |
| 2127 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2128 | Builder.AddPlaceholderChunk("statements"); |
| 2129 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2130 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2131 | Builder.AddTextChunk("while"); |
| 2132 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2133 | Builder.AddPlaceholderChunk("expression"); |
| 2134 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2135 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2137 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2138 | Builder.AddTypedTextChunk("for"); |
| 2139 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2140 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2141 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2142 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2143 | Builder.AddPlaceholderChunk("init-expression"); |
| 2144 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2145 | Builder.AddPlaceholderChunk("condition"); |
| 2146 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2147 | Builder.AddPlaceholderChunk("inc-expression"); |
| 2148 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2149 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2150 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2151 | Builder.AddPlaceholderChunk("statements"); |
| 2152 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2153 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2154 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2155 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2157 | if (S->getContinueParent()) { |
| 2158 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2159 | Builder.AddTypedTextChunk("continue"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 2160 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2161 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | if (S->getBreakParent()) { |
| 2165 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2166 | Builder.AddTypedTextChunk("break"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 2167 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2168 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Ilya Biryukov | 32497f5 | 2019-05-27 09:52:09 +0000 | [diff] [blame^] | 2171 | // "return expression ;" or "return ;", depending on the return type. |
| 2172 | QualType ReturnType; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2173 | if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Ilya Biryukov | 32497f5 | 2019-05-27 09:52:09 +0000 | [diff] [blame^] | 2174 | ReturnType = Function->getReturnType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2175 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Ilya Biryukov | 32497f5 | 2019-05-27 09:52:09 +0000 | [diff] [blame^] | 2176 | ReturnType = Method->getReturnType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2177 | else if (SemaRef.getCurBlock() && |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 2178 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
Ilya Biryukov | 32497f5 | 2019-05-27 09:52:09 +0000 | [diff] [blame^] | 2179 | ReturnType = SemaRef.getCurBlock()->ReturnType;; |
| 2180 | if (ReturnType.isNull() || ReturnType->isVoidType()) { |
| 2181 | Builder.AddTypedTextChunk("return"); |
| 2182 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2183 | Results.AddResult(Result(Builder.TakeString())); |
| 2184 | } else { |
| 2185 | assert(!ReturnType.isNull()); |
| 2186 | // "return expression ;" |
| 2187 | Builder.AddTypedTextChunk("return"); |
| 2188 | Builder.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2189 | Builder.AddPlaceholderChunk("expression"); |
Ilya Biryukov | 32497f5 | 2019-05-27 09:52:09 +0000 | [diff] [blame^] | 2190 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2191 | Results.AddResult(Result(Builder.TakeString())); |
| 2192 | // When boolean, also add 'return true;' and 'return false;'. |
| 2193 | if (ReturnType->isBooleanType()) { |
| 2194 | Builder.AddTypedTextChunk("return true"); |
| 2195 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2196 | Results.AddResult(Result(Builder.TakeString())); |
| 2197 | |
| 2198 | Builder.AddTypedTextChunk("return false"); |
| 2199 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2200 | Results.AddResult(Result(Builder.TakeString())); |
| 2201 | } |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 2202 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2204 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2205 | Builder.AddTypedTextChunk("goto"); |
| 2206 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2207 | Builder.AddPlaceholderChunk("label"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 2208 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2209 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2211 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2212 | Builder.AddTypedTextChunk("using"); |
| 2213 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2214 | Builder.AddTextChunk("namespace"); |
| 2215 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2216 | Builder.AddPlaceholderChunk("identifier"); |
Ilya Biryukov | 15a37eb | 2019-05-06 13:18:00 +0000 | [diff] [blame] | 2217 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2218 | Results.AddResult(Result(Builder.TakeString())); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 2219 | |
| 2220 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2221 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2222 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2223 | |
| 2224 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2225 | case Sema::PCC_ForInit: |
| 2226 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2227 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2228 | // Fall through: conditions and statements can have expressions. |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 2229 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2230 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2231 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2232 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2233 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 2234 | // (__bridge <type>)<expression> |
| 2235 | Builder.AddTypedTextChunk("__bridge"); |
| 2236 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2237 | Builder.AddPlaceholderChunk("type"); |
| 2238 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2239 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2240 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2241 | |
| 2242 | // (__bridge_transfer <Objective-C type>)<expression> |
| 2243 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 2244 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2245 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 2246 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2247 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2248 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2249 | |
| 2250 | // (__bridge_retained <CF type>)<expression> |
| 2251 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 2252 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2253 | Builder.AddPlaceholderChunk("CF type"); |
| 2254 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2255 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2256 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2257 | } |
| 2258 | // Fall through |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 2259 | LLVM_FALLTHROUGH; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2260 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2261 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2262 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2263 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 2264 | addThisCompletion(SemaRef, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2266 | // true |
| 2267 | Builder.AddResultTypeChunk("bool"); |
| 2268 | Builder.AddTypedTextChunk("true"); |
| 2269 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2271 | // false |
| 2272 | Builder.AddResultTypeChunk("bool"); |
| 2273 | Builder.AddTypedTextChunk("false"); |
| 2274 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2275 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2276 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2277 | // dynamic_cast < type-id > ( expression ) |
| 2278 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 2279 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2280 | Builder.AddPlaceholderChunk("type"); |
| 2281 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2282 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2283 | Builder.AddPlaceholderChunk("expression"); |
| 2284 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2285 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2286 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2288 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2289 | Builder.AddTypedTextChunk("static_cast"); |
| 2290 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2291 | Builder.AddPlaceholderChunk("type"); |
| 2292 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2293 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2294 | Builder.AddPlaceholderChunk("expression"); |
| 2295 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2296 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2297 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2298 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2299 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 2300 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2301 | Builder.AddPlaceholderChunk("type"); |
| 2302 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2303 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2304 | Builder.AddPlaceholderChunk("expression"); |
| 2305 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2306 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2308 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2309 | Builder.AddTypedTextChunk("const_cast"); |
| 2310 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2311 | Builder.AddPlaceholderChunk("type"); |
| 2312 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2313 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2314 | Builder.AddPlaceholderChunk("expression"); |
| 2315 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2316 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2317 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2318 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2319 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2320 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2321 | Builder.AddTypedTextChunk("typeid"); |
| 2322 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2323 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2324 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2325 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2326 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2328 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2329 | Builder.AddTypedTextChunk("new"); |
| 2330 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2331 | Builder.AddPlaceholderChunk("type"); |
| 2332 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2333 | Builder.AddPlaceholderChunk("expressions"); |
| 2334 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2335 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2337 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2338 | Builder.AddTypedTextChunk("new"); |
| 2339 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2340 | Builder.AddPlaceholderChunk("type"); |
| 2341 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2342 | Builder.AddPlaceholderChunk("size"); |
| 2343 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2344 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2345 | Builder.AddPlaceholderChunk("expressions"); |
| 2346 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2347 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2349 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2350 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2351 | Builder.AddTypedTextChunk("delete"); |
| 2352 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2353 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2354 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2356 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2357 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2358 | Builder.AddTypedTextChunk("delete"); |
| 2359 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2360 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2361 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2362 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2363 | Builder.AddPlaceholderChunk("expression"); |
| 2364 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2365 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2366 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2367 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2368 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2369 | Builder.AddTypedTextChunk("throw"); |
| 2370 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2371 | Builder.AddPlaceholderChunk("expression"); |
| 2372 | Results.AddResult(Result(Builder.TakeString())); |
| 2373 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2374 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 2375 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2376 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2377 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2378 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2379 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2380 | Builder.AddTypedTextChunk("nullptr"); |
| 2381 | Results.AddResult(Result(Builder.TakeString())); |
| 2382 | |
| 2383 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2384 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2385 | Builder.AddTypedTextChunk("alignof"); |
| 2386 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2387 | Builder.AddPlaceholderChunk("type"); |
| 2388 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2389 | Results.AddResult(Result(Builder.TakeString())); |
| 2390 | |
| 2391 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2392 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2393 | Builder.AddTypedTextChunk("noexcept"); |
| 2394 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2395 | Builder.AddPlaceholderChunk("expression"); |
| 2396 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2397 | Results.AddResult(Result(Builder.TakeString())); |
| 2398 | |
| 2399 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2400 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2401 | Builder.AddTypedTextChunk("sizeof..."); |
| 2402 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2403 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2404 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2405 | Results.AddResult(Result(Builder.TakeString())); |
| 2406 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2409 | if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2410 | // Add "super", if we're in an Objective-C class with a superclass. |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2411 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2412 | // The interface can be NULL. |
| 2413 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2414 | if (ID->getSuperClass()) { |
| 2415 | std::string SuperType; |
| 2416 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2417 | if (Method->isInstanceMethod()) |
| 2418 | SuperType += " *"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2420 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2421 | Builder.AddTypedTextChunk("super"); |
| 2422 | Results.AddResult(Result(Builder.TakeString())); |
| 2423 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2426 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2429 | if (SemaRef.getLangOpts().C11) { |
| 2430 | // _Alignof |
| 2431 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2432 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2433 | Builder.AddTypedTextChunk("alignof"); |
| 2434 | else |
| 2435 | Builder.AddTypedTextChunk("_Alignof"); |
| 2436 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2437 | Builder.AddPlaceholderChunk("type"); |
| 2438 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2439 | Results.AddResult(Result(Builder.TakeString())); |
| 2440 | } |
| 2441 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2442 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2443 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2444 | Builder.AddTypedTextChunk("sizeof"); |
| 2445 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2446 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2447 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2448 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2451 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2452 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2453 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2454 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2457 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2458 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2459 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2460 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2461 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2464 | /// If the given declaration has an associated type, add it as a result |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2465 | /// type chunk. |
| 2466 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2467 | const PrintingPolicy &Policy, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2468 | const NamedDecl *ND, QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2469 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2470 | if (!ND) |
| 2471 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Skip constructors and conversion functions, which have their return types |
| 2474 | // built into their names. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 2475 | if (isConstructor(ND) || isa<CXXConversionDecl>(ND)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2478 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2479 | QualType T; |
| 2480 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2481 | T = Function->getReturnType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2482 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2483 | if (!BaseType.isNull()) |
| 2484 | T = Method->getSendResultType(BaseType); |
| 2485 | else |
| 2486 | T = Method->getReturnType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2487 | } else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2488 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2489 | T = clang::TypeName::getFullyQualifiedType(T, Context); |
| 2490 | } else if (isa<UnresolvedUsingValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2491 | /* Do nothing: ignore unresolved using declarations*/ |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2492 | } else if (const auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2493 | if (!BaseType.isNull()) |
| 2494 | T = Ivar->getUsageType(BaseType); |
| 2495 | else |
| 2496 | T = Ivar->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2497 | } else if (const auto *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2498 | T = Value->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2499 | } else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2500 | if (!BaseType.isNull()) |
| 2501 | T = Property->getUsageType(BaseType); |
| 2502 | else |
| 2503 | T = Property->getType(); |
| 2504 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2506 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2507 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2508 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2509 | Result.AddResultTypeChunk( |
| 2510 | GetCompletionTypeString(T, Context, Policy, Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2513 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2514 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2515 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2516 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2517 | if (Sentinel->getSentinel() == 0) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2518 | if (PP.getLangOpts().ObjC && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2519 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2520 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2521 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2522 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2523 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2527 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2528 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2529 | std::string Result; |
| 2530 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2531 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2532 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2533 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2534 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2535 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2536 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2537 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2538 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2539 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2540 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2541 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2542 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2543 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2544 | switch (*nullability) { |
| 2545 | case NullabilityKind::NonNull: |
| 2546 | Result += "nonnull "; |
| 2547 | break; |
| 2548 | |
| 2549 | case NullabilityKind::Nullable: |
| 2550 | Result += "nullable "; |
| 2551 | break; |
| 2552 | |
| 2553 | case NullabilityKind::Unspecified: |
| 2554 | Result += "null_unspecified "; |
| 2555 | break; |
| 2556 | } |
| 2557 | } |
| 2558 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2559 | return Result; |
| 2560 | } |
| 2561 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2562 | /// Tries to find the most appropriate type location for an Objective-C |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2563 | /// block placeholder. |
| 2564 | /// |
| 2565 | /// This function ignores things like typedefs and qualifiers in order to |
| 2566 | /// present the most relevant and accurate block placeholders in code completion |
| 2567 | /// results. |
| 2568 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
| 2569 | FunctionTypeLoc &Block, |
| 2570 | FunctionProtoTypeLoc &BlockProto, |
| 2571 | bool SuppressBlock = false) { |
| 2572 | if (!TSInfo) |
| 2573 | return; |
| 2574 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2575 | while (true) { |
| 2576 | // Look through typedefs. |
| 2577 | if (!SuppressBlock) { |
| 2578 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2579 | if (TypeSourceInfo *InnerTSInfo = |
| 2580 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
| 2581 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2582 | continue; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | // Look through qualified types |
| 2587 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2588 | TL = QualifiedTL.getUnqualifiedLoc(); |
| 2589 | continue; |
| 2590 | } |
| 2591 | |
| 2592 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2593 | TL = AttrTL.getModifiedLoc(); |
| 2594 | continue; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | // Try to get the function prototype behind the block pointer type, |
| 2599 | // then we're done. |
| 2600 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2601 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2602 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2603 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
| 2604 | } |
| 2605 | break; |
| 2606 | } |
| 2607 | } |
| 2608 | |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2609 | static std::string |
| 2610 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2611 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2612 | bool SuppressBlockName = false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2613 | bool SuppressBlock = false, |
| 2614 | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
| 2615 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2616 | static std::string |
| 2617 | FormatFunctionParameter(const PrintingPolicy &Policy, const ParmVarDecl *Param, |
| 2618 | bool SuppressName = false, bool SuppressBlock = false, |
| 2619 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Sam McCall | bc7ff89 | 2019-04-04 11:34:18 +0000 | [diff] [blame] | 2620 | // Params are unavailable in FunctionTypeLoc if the FunctionType is invalid. |
| 2621 | // It would be better to pass in the param Type, which is usually avaliable. |
| 2622 | // But this case is rare, so just pretend we fell back to int as elsewhere. |
| 2623 | if (!Param) |
| 2624 | return "int"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2625 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2626 | if (Param->getType()->isDependentType() || |
| 2627 | !Param->getType()->isBlockPointerType()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2628 | // The argument for a dependent or non-block parameter is a placeholder |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2629 | // containing that parameter's type. |
| 2630 | std::string Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2632 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2633 | Result = Param->getIdentifier()->getName(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2635 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2636 | if (ObjCSubsts) |
| 2637 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2638 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2639 | if (ObjCMethodParam) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2640 | Result = |
| 2641 | "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2642 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2643 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2644 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2645 | } else { |
| 2646 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2647 | } |
| 2648 | return Result; |
| 2649 | } |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2651 | // The argument for a block pointer parameter is a block literal with |
| 2652 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2653 | FunctionTypeLoc Block; |
| 2654 | FunctionProtoTypeLoc BlockProto; |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2655 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
| 2656 | SuppressBlock); |
Alex Lorenz | 6bf4a58 | 2017-03-13 15:43:42 +0000 | [diff] [blame] | 2657 | // Try to retrieve the block type information from the property if this is a |
| 2658 | // parameter in a setter. |
| 2659 | if (!Block && ObjCMethodParam && |
| 2660 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { |
| 2661 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
| 2662 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
| 2663 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
| 2664 | SuppressBlock); |
| 2665 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2666 | |
| 2667 | if (!Block) { |
| 2668 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2669 | // for the block; just use the parameter type as a placeholder. |
| 2670 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2671 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2672 | Result = Param->getIdentifier()->getName(); |
| 2673 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2674 | QualType Type = Param->getType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2676 | if (ObjCMethodParam) { |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2677 | Result = Type.getAsString(Policy); |
| 2678 | std::string Quals = |
| 2679 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
| 2680 | if (!Quals.empty()) |
| 2681 | Result = "(" + Quals + " " + Result + ")"; |
| 2682 | if (Result.back() != ')') |
| 2683 | Result += " "; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2684 | if (Param->getIdentifier()) |
| 2685 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2686 | } else { |
| 2687 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2688 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2689 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2690 | return Result; |
| 2691 | } |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2693 | // We have the function prototype behind the block pointer type, as it was |
| 2694 | // written in the source. |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2695 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
| 2696 | /*SuppressBlockName=*/false, SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2697 | ObjCSubsts); |
| 2698 | } |
| 2699 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2700 | /// Returns a placeholder string that corresponds to an Objective-C block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2701 | /// declaration. |
| 2702 | /// |
| 2703 | /// \param BlockDecl A declaration with an Objective-C block type. |
| 2704 | /// |
| 2705 | /// \param Block The most relevant type location for that block type. |
| 2706 | /// |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2707 | /// \param SuppressBlockName Determines whether or not the name of the block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2708 | /// declaration is included in the resulting string. |
| 2709 | static std::string |
| 2710 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2711 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2712 | bool SuppressBlockName, bool SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2713 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2714 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2715 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2716 | if (ObjCSubsts) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2717 | ResultType = |
| 2718 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
| 2719 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2720 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2721 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2722 | |
| 2723 | // Format the parameter list. |
| 2724 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2725 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2726 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2727 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2728 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2729 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2730 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2731 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2732 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2733 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2734 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2735 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2736 | /*SuppressName=*/false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2737 | /*SuppressBlock=*/true, ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2738 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2739 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2740 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2741 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2742 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2743 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2745 | if (SuppressBlock) { |
| 2746 | // Format as a parameter. |
| 2747 | Result = Result + " (^"; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2748 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2749 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2750 | Result += ")"; |
| 2751 | Result += Params; |
| 2752 | } else { |
| 2753 | // Format as a block literal argument. |
| 2754 | Result = '^' + Result; |
| 2755 | Result += Params; |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2756 | |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2757 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2758 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2759 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2761 | return Result; |
| 2762 | } |
| 2763 | |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2764 | static std::string GetDefaultValueString(const ParmVarDecl *Param, |
| 2765 | const SourceManager &SM, |
| 2766 | const LangOptions &LangOpts) { |
Ilya Biryukov | b6d1ec8 | 2017-07-21 09:24:00 +0000 | [diff] [blame] | 2767 | const SourceRange SrcRange = Param->getDefaultArgRange(); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2768 | CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange); |
| 2769 | bool Invalid = CharSrcRange.isInvalid(); |
| 2770 | if (Invalid) |
| 2771 | return ""; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2772 | StringRef srcText = |
| 2773 | Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2774 | if (Invalid) |
| 2775 | return ""; |
| 2776 | |
| 2777 | if (srcText.empty() || srcText == "=") { |
| 2778 | // Lexer can't determine the value. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2779 | // This happens if the code is incorrect (for example class is forward |
| 2780 | // declared). |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2781 | return ""; |
| 2782 | } |
Erik Verbruggen | 797980e | 2017-07-19 11:15:36 +0000 | [diff] [blame] | 2783 | std::string DefValue(srcText.str()); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2784 | // FIXME: remove this check if the Lexer::getSourceText value is fixed and |
| 2785 | // this value always has (or always does not have) '=' in front of it |
| 2786 | if (DefValue.at(0) != '=') { |
| 2787 | // If we don't have '=' in front of value. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2788 | // Lexer returns built-in types values without '=' and user-defined types |
| 2789 | // values with it. |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2790 | return " = " + DefValue; |
| 2791 | } |
| 2792 | return " " + DefValue; |
| 2793 | } |
| 2794 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2795 | /// Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2796 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2797 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2798 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2799 | CodeCompletionBuilder &Result, |
| 2800 | unsigned Start = 0, |
| 2801 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2802 | bool FirstParameter = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2804 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2805 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2807 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2808 | // When we see an optional default argument, put that argument and |
| 2809 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2810 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2811 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2812 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2813 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2814 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2815 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2816 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2817 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2819 | if (FirstParameter) |
| 2820 | FirstParameter = false; |
| 2821 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2822 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2824 | InOptional = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2826 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2827 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2828 | if (Param->hasDefaultArg()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2829 | PlaceholderStr += |
| 2830 | GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts()); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2832 | if (Function->isVariadic() && P == N - 1) |
| 2833 | PlaceholderStr += ", ..."; |
| 2834 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2835 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2836 | Result.AddPlaceholderChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2837 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2838 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2839 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2840 | if (const auto *Proto = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2841 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2842 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2843 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2844 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2845 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2846 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2849 | /// Add template parameter chunks to the given code completion string. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2850 | static void AddTemplateParameterChunks( |
| 2851 | ASTContext &Context, const PrintingPolicy &Policy, |
| 2852 | const TemplateDecl *Template, CodeCompletionBuilder &Result, |
| 2853 | unsigned MaxParameters = 0, unsigned Start = 0, bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2854 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Prefer to take the template parameter names from the first declaration of |
| 2857 | // the template. |
| 2858 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2859 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2860 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2861 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2862 | if (MaxParameters) |
| 2863 | PEnd = Params->begin() + MaxParameters; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2864 | for (TemplateParameterList::iterator P = Params->begin() + Start; P != PEnd; |
| 2865 | ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2866 | bool HasDefaultArg = false; |
| 2867 | std::string PlaceholderStr; |
| 2868 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2869 | if (TTP->wasDeclaredWithTypename()) |
| 2870 | PlaceholderStr = "typename"; |
| 2871 | else |
| 2872 | PlaceholderStr = "class"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2874 | if (TTP->getIdentifier()) { |
| 2875 | PlaceholderStr += ' '; |
| 2876 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2877 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2879 | HasDefaultArg = TTP->hasDefaultArgument(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2880 | } else if (NonTypeTemplateParmDecl *NTTP = |
| 2881 | dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2882 | if (NTTP->getIdentifier()) |
| 2883 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2884 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2885 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2886 | } else { |
| 2887 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2888 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2889 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2890 | // Since putting the template argument list into the placeholder would |
| 2891 | // be very, very long, we just use an abbreviation. |
| 2892 | PlaceholderStr = "template<...> class"; |
| 2893 | if (TTP->getIdentifier()) { |
| 2894 | PlaceholderStr += ' '; |
| 2895 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2896 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2898 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2899 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2901 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2902 | // When we see an optional default argument, put that argument and |
| 2903 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2904 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2905 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2906 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2907 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2908 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2909 | P - Params->begin(), true); |
| 2910 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2911 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2912 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2914 | InDefaultArg = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2915 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2916 | if (FirstParameter) |
| 2917 | FirstParameter = false; |
| 2918 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2919 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2921 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2922 | Result.AddPlaceholderChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2923 | Result.getAllocator().CopyString(PlaceholderStr)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2924 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2927 | /// Add a qualifier to the given code-completion string, if the |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2928 | /// provided nested-name-specifier is non-NULL. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2929 | static void AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
| 2930 | NestedNameSpecifier *Qualifier, |
| 2931 | bool QualifierIsInformative, |
| 2932 | ASTContext &Context, |
| 2933 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2934 | if (!Qualifier) |
| 2935 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2937 | std::string PrintedNNS; |
| 2938 | { |
| 2939 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2940 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2941 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2942 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2943 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2944 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2945 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2948 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2949 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2950 | const FunctionDecl *Function) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2951 | const auto *Proto = Function->getType()->getAs<FunctionProtoType>(); |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2952 | if (!Proto || !Proto->getMethodQuals()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2953 | return; |
| 2954 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2955 | // FIXME: Add ref-qualifier! |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2957 | // Handle single qualifiers without copying |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2958 | if (Proto->getMethodQuals().hasOnlyConst()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2959 | Result.AddInformativeChunk(" const"); |
| 2960 | return; |
| 2961 | } |
| 2962 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2963 | if (Proto->getMethodQuals().hasOnlyVolatile()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2964 | Result.AddInformativeChunk(" volatile"); |
| 2965 | return; |
| 2966 | } |
| 2967 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2968 | if (Proto->getMethodQuals().hasOnlyRestrict()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2969 | Result.AddInformativeChunk(" restrict"); |
| 2970 | return; |
| 2971 | } |
| 2972 | |
| 2973 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2974 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2975 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2976 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2977 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2978 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2979 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2980 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2981 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2984 | /// Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2985 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2986 | const NamedDecl *ND, |
| 2987 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2988 | DeclarationName Name = ND->getDeclName(); |
| 2989 | if (!Name) |
| 2990 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2992 | switch (Name.getNameKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2993 | case DeclarationName::CXXOperatorName: { |
| 2994 | const char *OperatorName = nullptr; |
| 2995 | switch (Name.getCXXOverloadedOperator()) { |
| 2996 | case OO_None: |
| 2997 | case OO_Conditional: |
| 2998 | case NUM_OVERLOADED_OPERATORS: |
| 2999 | OperatorName = "operator"; |
| 3000 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3001 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3002 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 3003 | case OO_##Name: \ |
| 3004 | OperatorName = "operator" Spelling; \ |
| 3005 | break; |
| 3006 | #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemberOnly) |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 3007 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3008 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3009 | case OO_New: |
| 3010 | OperatorName = "operator new"; |
| 3011 | break; |
| 3012 | case OO_Delete: |
| 3013 | OperatorName = "operator delete"; |
| 3014 | break; |
| 3015 | case OO_Array_New: |
| 3016 | OperatorName = "operator new[]"; |
| 3017 | break; |
| 3018 | case OO_Array_Delete: |
| 3019 | OperatorName = "operator delete[]"; |
| 3020 | break; |
| 3021 | case OO_Call: |
| 3022 | OperatorName = "operator()"; |
| 3023 | break; |
| 3024 | case OO_Subscript: |
| 3025 | OperatorName = "operator[]"; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3028 | Result.AddTypedTextChunk(OperatorName); |
| 3029 | break; |
| 3030 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3031 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3032 | case DeclarationName::Identifier: |
| 3033 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3034 | case DeclarationName::CXXDestructorName: |
| 3035 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3036 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3037 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3038 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3039 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3040 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3041 | case DeclarationName::CXXUsingDirective: |
| 3042 | case DeclarationName::ObjCZeroArgSelector: |
| 3043 | case DeclarationName::ObjCOneArgSelector: |
| 3044 | case DeclarationName::ObjCMultiArgSelector: |
| 3045 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3046 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3047 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3048 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3049 | QualType Ty = Name.getCXXNameType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3050 | if (const auto *RecordTy = Ty->getAs<RecordType>()) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3051 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3052 | else if (const auto *InjectedTy = Ty->getAs<InjectedClassNameType>()) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3053 | Record = InjectedTy->getDecl(); |
| 3054 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3055 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3056 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3057 | break; |
| 3058 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3060 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3061 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3062 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3063 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3064 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3065 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3066 | } |
| 3067 | break; |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3072 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
| 3073 | Sema &S, const CodeCompletionContext &CCContext, |
| 3074 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 3075 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3076 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 3077 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 3080 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro( |
| 3081 | Preprocessor &PP, CodeCompletionAllocator &Allocator, |
| 3082 | CodeCompletionTUInfo &CCTUInfo) { |
| 3083 | assert(Kind == RK_Macro); |
| 3084 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
| 3085 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
| 3086 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName())); |
| 3087 | |
| 3088 | if (!MI || !MI->isFunctionLike()) |
| 3089 | return Result.TakeString(); |
| 3090 | |
| 3091 | // Format a function-like macro with placeholders for the arguments. |
| 3092 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3093 | MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end(); |
| 3094 | |
| 3095 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 3096 | if (MI->isC99Varargs()) { |
| 3097 | --AEnd; |
| 3098 | |
| 3099 | if (A == AEnd) { |
| 3100 | Result.AddPlaceholderChunk("..."); |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) { |
| 3105 | if (A != MI->param_begin()) |
| 3106 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3107 | |
| 3108 | if (MI->isVariadic() && (A + 1) == AEnd) { |
| 3109 | SmallString<32> Arg = (*A)->getName(); |
| 3110 | if (MI->isC99Varargs()) |
| 3111 | Arg += ", ..."; |
| 3112 | else |
| 3113 | Arg += "..."; |
| 3114 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
| 3115 | break; |
| 3116 | } |
| 3117 | |
| 3118 | // Non-variadic macros are simple. |
| 3119 | Result.AddPlaceholderChunk( |
| 3120 | Result.getAllocator().CopyString((*A)->getName())); |
| 3121 | } |
| 3122 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| 3123 | return Result.TakeString(); |
| 3124 | } |
| 3125 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3126 | /// If possible, create a new code completion string for the given |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3127 | /// result. |
| 3128 | /// |
| 3129 | /// \returns Either a new, heap-allocated code completion string describing |
| 3130 | /// how to use this result, or NULL to indicate that the string or name of the |
| 3131 | /// result is all that is needed. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3132 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
| 3133 | ASTContext &Ctx, Preprocessor &PP, const CodeCompletionContext &CCContext, |
| 3134 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 3135 | bool IncludeBriefComments) { |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 3136 | if (Kind == RK_Macro) |
| 3137 | return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo); |
| 3138 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3139 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3140 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3141 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3142 | if (Kind == RK_Pattern) { |
| 3143 | Pattern->Priority = Priority; |
| 3144 | Pattern->Availability = Availability; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3146 | if (Declaration) { |
| 3147 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3148 | Pattern->ParentName = Result.getParentName(); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3149 | if (const RawComment *RC = |
| 3150 | getPatternCompletionComment(Ctx, Declaration)) { |
| 3151 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 3152 | Pattern->BriefComment = Result.getBriefComment(); |
| 3153 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3154 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3156 | return Pattern; |
| 3157 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3159 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3160 | Result.AddTypedTextChunk(Keyword); |
| 3161 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3162 | } |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 3163 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3164 | return createCodeCompletionStringForDecl( |
| 3165 | PP, Ctx, Result, IncludeBriefComments, CCContext, Policy); |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Ilya Biryukov | cabab29 | 2019-05-24 10:18:39 +0000 | [diff] [blame] | 3168 | static void printOverrideString(const CodeCompletionString &CCS, |
| 3169 | std::string &BeforeName, |
| 3170 | std::string &NameAndSignature) { |
| 3171 | bool SeenTypedChunk = false; |
| 3172 | for (auto &Chunk : CCS) { |
| 3173 | if (Chunk.Kind == CodeCompletionString::CK_Optional) { |
| 3174 | assert(SeenTypedChunk && "optional parameter before name"); |
| 3175 | // Note that we put all chunks inside into NameAndSignature. |
| 3176 | printOverrideString(*Chunk.Optional, NameAndSignature, NameAndSignature); |
| 3177 | continue; |
| 3178 | } |
| 3179 | SeenTypedChunk |= Chunk.Kind == CodeCompletionString::CK_TypedText; |
| 3180 | if (SeenTypedChunk) |
| 3181 | NameAndSignature += Chunk.Text; |
| 3182 | else |
| 3183 | BeforeName += Chunk.Text; |
| 3184 | } |
| 3185 | } |
| 3186 | |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3187 | CodeCompletionString * |
| 3188 | CodeCompletionResult::createCodeCompletionStringForOverride( |
| 3189 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
| 3190 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
| 3191 | PrintingPolicy &Policy) { |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3192 | auto *CCS = createCodeCompletionStringForDecl(PP, Ctx, Result, |
| 3193 | /*IncludeBriefComments=*/false, |
| 3194 | CCContext, Policy); |
Ilya Biryukov | cabab29 | 2019-05-24 10:18:39 +0000 | [diff] [blame] | 3195 | std::string BeforeName; |
| 3196 | std::string NameAndSignature; |
| 3197 | // For overrides all chunks go into the result, none are informative. |
| 3198 | printOverrideString(*CCS, BeforeName, NameAndSignature); |
| 3199 | NameAndSignature += " override"; |
| 3200 | |
| 3201 | Result.AddTextChunk(Result.getAllocator().CopyString(BeforeName)); |
| 3202 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3203 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(NameAndSignature)); |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3204 | return Result.TakeString(); |
| 3205 | } |
| 3206 | |
| 3207 | CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl( |
| 3208 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
| 3209 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
| 3210 | PrintingPolicy &Policy) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 3211 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3212 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3213 | |
| 3214 | if (IncludeBriefComments) { |
| 3215 | // Add documentation comment, if it exists. |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3216 | if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3217 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3218 | } |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3221 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3222 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3223 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3224 | Result.AddTextChunk("::"); |
| 3225 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3226 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 3227 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3228 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 3229 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3231 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3232 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3233 | if (const auto *Function = dyn_cast<FunctionDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3234 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3235 | Ctx, Policy); |
| 3236 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3237 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3238 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3239 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 3240 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3241 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3242 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3243 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3244 | if (const FunctionTemplateDecl *FunTmpl = |
| 3245 | dyn_cast<FunctionTemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3246 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3247 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3248 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3249 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3251 | // Figure out which template parameters are deduced (or have default |
| 3252 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 3253 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3254 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3255 | unsigned LastDeducibleArgument; |
| 3256 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 3257 | --LastDeducibleArgument) { |
| 3258 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 3259 | // C++0x: Figure out if the template argument has a default. If so, |
| 3260 | // the user doesn't need to type this argument. |
| 3261 | // FIXME: We need to abstract template parameters better! |
| 3262 | bool HasDefaultArg = false; |
| 3263 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3264 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3265 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3266 | HasDefaultArg = TTP->hasDefaultArgument(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3267 | else if (NonTypeTemplateParmDecl *NTTP = |
| 3268 | dyn_cast<NonTypeTemplateParmDecl>(Param)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3269 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 3270 | else { |
| 3271 | assert(isa<TemplateTemplateParmDecl>(Param)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3272 | HasDefaultArg = |
| 3273 | cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3274 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3276 | if (!HasDefaultArg) |
| 3277 | break; |
| 3278 | } |
| 3279 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3281 | if (LastDeducibleArgument) { |
| 3282 | // Some of the function template arguments cannot be deduced from a |
| 3283 | // function call, so we introduce an explicit template argument list |
| 3284 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3285 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3286 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3287 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3288 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3289 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3290 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3291 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3292 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3293 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3294 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 3295 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3296 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3297 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3298 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3299 | if (const auto *Template = dyn_cast<TemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3300 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3301 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3302 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3303 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3304 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3305 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3306 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3307 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3308 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3309 | if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3310 | Selector Sel = Method->getSelector(); |
| 3311 | if (Sel.isUnarySelector()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3312 | Result.AddTypedTextChunk( |
| 3313 | Result.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3314 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 3317 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3318 | SelName += ':'; |
| 3319 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3320 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3321 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3322 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3323 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3324 | // If there is only one parameter, and we're past it, add an empty |
| 3325 | // typed-text chunk since there is nothing to type. |
| 3326 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3327 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3328 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3329 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 3330 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3331 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3332 | P != PEnd; (void)++P, ++Idx) { |
| 3333 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3334 | std::string Keyword; |
| 3335 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3336 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3337 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 3338 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3339 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3340 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3341 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3342 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3343 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3344 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3346 | // If we're before the starting parameter, skip the placeholder. |
| 3347 | if (Idx < StartParameter) |
| 3348 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3349 | |
| 3350 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3351 | QualType ParamType = (*P)->getType(); |
| 3352 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 3353 | if (!CCContext.getBaseType().isNull()) |
| 3354 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 3355 | |
| 3356 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 3357 | Arg = FormatFunctionParameter(Policy, *P, true, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3358 | /*SuppressBlock=*/false, ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3359 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3360 | if (ObjCSubsts) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3361 | ParamType = ParamType.substObjCTypeArgs( |
| 3362 | Ctx, *ObjCSubsts, ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 3363 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3364 | ParamType); |
| 3365 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3366 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 3367 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 3368 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3369 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3370 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3371 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 3372 | Arg += ", ..."; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3374 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3375 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3376 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3377 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3378 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3379 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3382 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3383 | if (Method->param_size() == 0) { |
| 3384 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3385 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3386 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3387 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3388 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3389 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3390 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3391 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3392 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3393 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3394 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3395 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3398 | if (Qualifier) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3399 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3400 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3401 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3402 | Result.AddTypedTextChunk( |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3403 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3404 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3407 | const RawComment *clang::getCompletionComment(const ASTContext &Ctx, |
| 3408 | const NamedDecl *ND) { |
| 3409 | if (!ND) |
| 3410 | return nullptr; |
| 3411 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) |
| 3412 | return RC; |
| 3413 | |
| 3414 | // Try to find comment from a property for ObjC methods. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3415 | const auto *M = dyn_cast<ObjCMethodDecl>(ND); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3416 | if (!M) |
| 3417 | return nullptr; |
| 3418 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3419 | if (!PDecl) |
| 3420 | return nullptr; |
| 3421 | |
| 3422 | return Ctx.getRawCommentForAnyRedecl(PDecl); |
| 3423 | } |
| 3424 | |
| 3425 | const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, |
| 3426 | const NamedDecl *ND) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3427 | const auto *M = dyn_cast_or_null<ObjCMethodDecl>(ND); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3428 | if (!M || !M->isPropertyAccessor()) |
| 3429 | return nullptr; |
| 3430 | |
| 3431 | // Provide code completion comment for self.GetterName where |
| 3432 | // GetterName is the getter method for a property with name |
| 3433 | // different from the property name (declared via a property |
| 3434 | // getter attribute. |
| 3435 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3436 | if (!PDecl) |
| 3437 | return nullptr; |
| 3438 | if (PDecl->getGetterName() == M->getSelector() && |
| 3439 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 3440 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) |
| 3441 | return RC; |
| 3442 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 3443 | return RC; |
| 3444 | } |
| 3445 | return nullptr; |
| 3446 | } |
| 3447 | |
| 3448 | const RawComment *clang::getParameterComment( |
| 3449 | const ASTContext &Ctx, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3450 | const CodeCompleteConsumer::OverloadCandidate &Result, unsigned ArgIndex) { |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3451 | auto FDecl = Result.getFunction(); |
| 3452 | if (!FDecl) |
| 3453 | return nullptr; |
| 3454 | if (ArgIndex < FDecl->getNumParams()) |
| 3455 | return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); |
| 3456 | return nullptr; |
| 3457 | } |
| 3458 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3459 | /// Add function overload parameter chunks to the given code completion |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3460 | /// string. |
| 3461 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 3462 | const PrintingPolicy &Policy, |
| 3463 | const FunctionDecl *Function, |
| 3464 | const FunctionProtoType *Prototype, |
| 3465 | CodeCompletionBuilder &Result, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3466 | unsigned CurrentArg, unsigned Start = 0, |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3467 | bool InOptional = false) { |
| 3468 | bool FirstParameter = true; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3469 | unsigned NumParams = |
| 3470 | Function ? Function->getNumParams() : Prototype->getNumParams(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3471 | |
| 3472 | for (unsigned P = Start; P != NumParams; ++P) { |
| 3473 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 3474 | // When we see an optional default argument, put that argument and |
| 3475 | // the remaining default arguments into a new, optional string. |
| 3476 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3477 | Result.getCodeCompletionTUInfo()); |
| 3478 | if (!FirstParameter) |
| 3479 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3480 | // Optional sections are nested. |
| 3481 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 3482 | CurrentArg, P, /*InOptional=*/true); |
| 3483 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3484 | return; |
| 3485 | } |
| 3486 | |
| 3487 | if (FirstParameter) |
| 3488 | FirstParameter = false; |
| 3489 | else |
| 3490 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3491 | |
| 3492 | InOptional = false; |
| 3493 | |
| 3494 | // Format the placeholder string. |
| 3495 | std::string Placeholder; |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3496 | if (Function) { |
| 3497 | const ParmVarDecl *Param = Function->getParamDecl(P); |
| 3498 | Placeholder = FormatFunctionParameter(Policy, Param); |
| 3499 | if (Param->hasDefaultArg()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3500 | Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), |
| 3501 | Context.getLangOpts()); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3502 | } else { |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3503 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3504 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3505 | |
| 3506 | if (P == CurrentArg) |
| 3507 | Result.AddCurrentParameterChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3508 | Result.getAllocator().CopyString(Placeholder)); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3509 | else |
| 3510 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 3511 | } |
| 3512 | |
| 3513 | if (Prototype && Prototype->isVariadic()) { |
| 3514 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3515 | Result.getCodeCompletionTUInfo()); |
| 3516 | if (!FirstParameter) |
| 3517 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3518 | |
| 3519 | if (CurrentArg < NumParams) |
| 3520 | Opt.AddPlaceholderChunk("..."); |
| 3521 | else |
| 3522 | Opt.AddCurrentParameterChunk("..."); |
| 3523 | |
| 3524 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3525 | } |
| 3526 | } |
| 3527 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3528 | CodeCompletionString * |
| 3529 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3530 | unsigned CurrentArg, Sema &S, CodeCompletionAllocator &Allocator, |
| 3531 | CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3532 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3534 | // FIXME: Set priority, availability appropriately. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3535 | CodeCompletionBuilder Result(Allocator, CCTUInfo, 1, |
| 3536 | CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3537 | FunctionDecl *FDecl = getFunction(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3538 | const FunctionProtoType *Proto = |
| 3539 | dyn_cast<FunctionProtoType>(getFunctionType()); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3540 | if (!FDecl && !Proto) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3541 | // Function without a prototype. Just give the return type and a |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3542 | // highlighted ellipsis. |
| 3543 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3544 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3545 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3546 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3547 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 3548 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3549 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3550 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3551 | |
| 3552 | if (FDecl) { |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3553 | if (IncludeBriefComments) { |
| 3554 | if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg)) |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3555 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3556 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3557 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3558 | Result.AddTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3559 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3560 | } else { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3561 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3562 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3563 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3564 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3565 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3566 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 3567 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3568 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3570 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3573 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3574 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3575 | bool PreferredTypeIsPointer) { |
| 3576 | unsigned Priority = CCP_Macro; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3577 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3578 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3579 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3580 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3581 | Priority = CCP_Constant; |
| 3582 | if (PreferredTypeIsPointer) |
| 3583 | Priority = Priority / CCF_SimilarTypeMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3584 | } |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3585 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3586 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3587 | MacroName.equals("true") || MacroName.equals("false")) |
| 3588 | Priority = CCP_Constant; |
| 3589 | // Treat "bool" as a type. |
| 3590 | else if (MacroName.equals("bool")) |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3591 | Priority = CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC : 0); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3593 | return Priority; |
| 3594 | } |
| 3595 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3596 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3597 | if (!D) |
| 3598 | return CXCursor_UnexposedDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3600 | switch (D->getKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3601 | case Decl::Enum: |
| 3602 | return CXCursor_EnumDecl; |
| 3603 | case Decl::EnumConstant: |
| 3604 | return CXCursor_EnumConstantDecl; |
| 3605 | case Decl::Field: |
| 3606 | return CXCursor_FieldDecl; |
| 3607 | case Decl::Function: |
| 3608 | return CXCursor_FunctionDecl; |
| 3609 | case Decl::ObjCCategory: |
| 3610 | return CXCursor_ObjCCategoryDecl; |
| 3611 | case Decl::ObjCCategoryImpl: |
| 3612 | return CXCursor_ObjCCategoryImplDecl; |
| 3613 | case Decl::ObjCImplementation: |
| 3614 | return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3615 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3616 | case Decl::ObjCInterface: |
| 3617 | return CXCursor_ObjCInterfaceDecl; |
| 3618 | case Decl::ObjCIvar: |
| 3619 | return CXCursor_ObjCIvarDecl; |
| 3620 | case Decl::ObjCMethod: |
| 3621 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3622 | ? CXCursor_ObjCInstanceMethodDecl |
| 3623 | : CXCursor_ObjCClassMethodDecl; |
| 3624 | case Decl::CXXMethod: |
| 3625 | return CXCursor_CXXMethod; |
| 3626 | case Decl::CXXConstructor: |
| 3627 | return CXCursor_Constructor; |
| 3628 | case Decl::CXXDestructor: |
| 3629 | return CXCursor_Destructor; |
| 3630 | case Decl::CXXConversion: |
| 3631 | return CXCursor_ConversionFunction; |
| 3632 | case Decl::ObjCProperty: |
| 3633 | return CXCursor_ObjCPropertyDecl; |
| 3634 | case Decl::ObjCProtocol: |
| 3635 | return CXCursor_ObjCProtocolDecl; |
| 3636 | case Decl::ParmVar: |
| 3637 | return CXCursor_ParmDecl; |
| 3638 | case Decl::Typedef: |
| 3639 | return CXCursor_TypedefDecl; |
| 3640 | case Decl::TypeAlias: |
| 3641 | return CXCursor_TypeAliasDecl; |
| 3642 | case Decl::TypeAliasTemplate: |
| 3643 | return CXCursor_TypeAliasTemplateDecl; |
| 3644 | case Decl::Var: |
| 3645 | return CXCursor_VarDecl; |
| 3646 | case Decl::Namespace: |
| 3647 | return CXCursor_Namespace; |
| 3648 | case Decl::NamespaceAlias: |
| 3649 | return CXCursor_NamespaceAlias; |
| 3650 | case Decl::TemplateTypeParm: |
| 3651 | return CXCursor_TemplateTypeParameter; |
| 3652 | case Decl::NonTypeTemplateParm: |
| 3653 | return CXCursor_NonTypeTemplateParameter; |
| 3654 | case Decl::TemplateTemplateParm: |
| 3655 | return CXCursor_TemplateTemplateParameter; |
| 3656 | case Decl::FunctionTemplate: |
| 3657 | return CXCursor_FunctionTemplate; |
| 3658 | case Decl::ClassTemplate: |
| 3659 | return CXCursor_ClassTemplate; |
| 3660 | case Decl::AccessSpec: |
| 3661 | return CXCursor_CXXAccessSpecifier; |
| 3662 | case Decl::ClassTemplatePartialSpecialization: |
| 3663 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3664 | case Decl::UsingDirective: |
| 3665 | return CXCursor_UsingDirective; |
| 3666 | case Decl::StaticAssert: |
| 3667 | return CXCursor_StaticAssert; |
| 3668 | case Decl::Friend: |
| 3669 | return CXCursor_FriendDecl; |
| 3670 | case Decl::TranslationUnit: |
| 3671 | return CXCursor_TranslationUnit; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3672 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3673 | case Decl::Using: |
| 3674 | case Decl::UnresolvedUsingValue: |
| 3675 | case Decl::UnresolvedUsingTypename: |
| 3676 | return CXCursor_UsingDeclaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3677 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3678 | case Decl::ObjCPropertyImpl: |
| 3679 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3680 | case ObjCPropertyImplDecl::Dynamic: |
| 3681 | return CXCursor_ObjCDynamicDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3682 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3683 | case ObjCPropertyImplDecl::Synthesize: |
| 3684 | return CXCursor_ObjCSynthesizeDecl; |
| 3685 | } |
Bruno Ricci | e5bcf0b | 2018-12-21 17:52:13 +0000 | [diff] [blame] | 3686 | llvm_unreachable("Unexpected Kind!"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3687 | |
| 3688 | case Decl::Import: |
| 3689 | return CXCursor_ModuleImportDecl; |
| 3690 | |
| 3691 | case Decl::ObjCTypeParam: |
| 3692 | return CXCursor_TemplateTypeParameter; |
| 3693 | |
| 3694 | default: |
| 3695 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 3696 | switch (TD->getTagKind()) { |
| 3697 | case TTK_Interface: // fall through |
| 3698 | case TTK_Struct: |
| 3699 | return CXCursor_StructDecl; |
| 3700 | case TTK_Class: |
| 3701 | return CXCursor_ClassDecl; |
| 3702 | case TTK_Union: |
| 3703 | return CXCursor_UnionDecl; |
| 3704 | case TTK_Enum: |
| 3705 | return CXCursor_EnumDecl; |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3706 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3707 | } |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3708 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3710 | return CXCursor_UnexposedDecl; |
| 3711 | } |
| 3712 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3713 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3714 | bool LoadExternal, bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3715 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3716 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3718 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3719 | |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3720 | for (Preprocessor::macro_iterator M = PP.macro_begin(LoadExternal), |
| 3721 | MEnd = PP.macro_end(LoadExternal); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3722 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3723 | auto MD = PP.getMacroDefinition(M->first); |
| 3724 | if (IncludeUndefined || MD) { |
Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3725 | MacroInfo *MI = MD.getMacroInfo(); |
| 3726 | if (MI && MI->isUsedForHeaderGuard()) |
| 3727 | continue; |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3728 | |
Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3729 | Results.AddResult( |
| 3730 | Result(M->first, MI, |
| 3731 | getMacroUsagePriority(M->first->getName(), PP.getLangOpts(), |
| 3732 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3733 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3734 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3735 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3736 | Results.ExitScope(); |
| 3737 | } |
| 3738 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3739 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3740 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3741 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3742 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3743 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3744 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3745 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3746 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3747 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3748 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3749 | Results.ExitScope(); |
| 3750 | } |
| 3751 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3752 | static void HandleCodeCompleteResults(Sema *S, |
| 3753 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3754 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3755 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3756 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3757 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3758 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Ilya Biryukov | 4110967 | 2018-12-13 15:36:32 +0000 | [diff] [blame] | 3761 | static CodeCompletionContext |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3762 | mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3763 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3764 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3765 | return CodeCompletionContext::CCC_TopLevel; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3766 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3767 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3768 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3769 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3770 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3771 | return CodeCompletionContext::CCC_ObjCInterface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3772 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3773 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3774 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3775 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3776 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3777 | return CodeCompletionContext::CCC_ObjCIvarList; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3778 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3779 | case Sema::PCC_Template: |
| 3780 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3781 | if (S.CurContext->isFileContext()) |
| 3782 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3783 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3784 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3785 | return CodeCompletionContext::CCC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3786 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3787 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3788 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3789 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3790 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3791 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3792 | S.getLangOpts().ObjC) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3793 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3794 | else |
| 3795 | return CodeCompletionContext::CCC_Expression; |
| 3796 | |
| 3797 | case Sema::PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3798 | return CodeCompletionContext::CCC_Expression; |
Ilya Biryukov | 4110967 | 2018-12-13 15:36:32 +0000 | [diff] [blame] | 3799 | case Sema::PCC_Condition: |
| 3800 | return CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3801 | S.getASTContext().BoolTy); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3802 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3803 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3804 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3805 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3806 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3807 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3808 | |
| 3809 | case Sema::PCC_ParenthesizedExpression: |
| 3810 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3811 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3812 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3813 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3814 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3815 | |
| 3816 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3819 | /// If we're in a C++ virtual member function, add completion results |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3820 | /// that invoke the functions we override, since it's common to invoke the |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3821 | /// overridden function as well as adding new functionality. |
| 3822 | /// |
| 3823 | /// \param S The semantic analysis object for which we are generating results. |
| 3824 | /// |
| 3825 | /// \param InContext This context in which the nested-name-specifier preceding |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3826 | /// the code-completion point |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3827 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3828 | ResultBuilder &Results) { |
| 3829 | // Look through blocks. |
| 3830 | DeclContext *CurContext = S.CurContext; |
| 3831 | while (isa<BlockDecl>(CurContext)) |
| 3832 | CurContext = CurContext->getParent(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3834 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3835 | if (!Method || !Method->isVirtual()) |
| 3836 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3837 | |
| 3838 | // We need to have names for all of the parameters, if we're going to |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3839 | // generate a forwarding call. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3840 | for (auto P : Method->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3841 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3842 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3844 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 3845 | for (const CXXMethodDecl *Overridden : Method->overridden_methods()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3846 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3847 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3848 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3849 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3851 | // If we need a nested-name-specifier, add one now. |
| 3852 | if (!InContext) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3853 | NestedNameSpecifier *NNS = getRequiredQualification( |
| 3854 | S.Context, CurContext, Overridden->getDeclContext()); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3855 | if (NNS) { |
| 3856 | std::string Str; |
| 3857 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3858 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3859 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3860 | } |
| 3861 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3862 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3863 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3864 | Builder.AddTypedTextChunk( |
| 3865 | Results.getAllocator().CopyString(Overridden->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3866 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3867 | bool FirstParam = true; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3868 | for (auto P : Method->parameters()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3869 | if (FirstParam) |
| 3870 | FirstParam = false; |
| 3871 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3872 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3873 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3874 | Builder.AddPlaceholderChunk( |
| 3875 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3876 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3877 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3878 | Results.AddResult(CodeCompletionResult( |
| 3879 | Builder.TakeString(), CCP_SuperCompletion, CXCursor_CXXMethod, |
| 3880 | CXAvailability_Available, Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3881 | Results.Ignore(Overridden); |
| 3882 | } |
| 3883 | } |
| 3884 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3885 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3886 | ModuleIdPath Path) { |
| 3887 | typedef CodeCompletionResult Result; |
| 3888 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3889 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3890 | CodeCompletionContext::CCC_Other); |
| 3891 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3893 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3894 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3895 | typedef CodeCompletionResult Result; |
| 3896 | if (Path.empty()) { |
| 3897 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3898 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3899 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3900 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3901 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3902 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3903 | Results.AddResult(Result( |
| 3904 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
| 3905 | Modules[I]->isAvailable() ? CXAvailability_Available |
| 3906 | : CXAvailability_NotAvailable)); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3907 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3908 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3909 | // Load the named module. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3910 | Module *Mod = |
| 3911 | PP.getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
| 3912 | /*IsInclusionDirective=*/false); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3913 | // Enumerate submodules. |
| 3914 | if (Mod) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3915 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3916 | SubEnd = Mod->submodule_end(); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3917 | Sub != SubEnd; ++Sub) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3919 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3920 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3921 | Results.AddResult(Result( |
| 3922 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
| 3923 | (*Sub)->isAvailable() ? CXAvailability_Available |
| 3924 | : CXAvailability_NotAvailable)); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3928 | Results.ExitScope(); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3929 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3930 | Results.data(), Results.size()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3933 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3934 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3935 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3936 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3937 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3938 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3940 | // Determine how to filter results, e.g., so that the names of |
| 3941 | // values (functions, enumerators, function templates, etc.) are |
| 3942 | // only allowed where we can have an expression. |
| 3943 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3944 | case PCC_Namespace: |
| 3945 | case PCC_Class: |
| 3946 | case PCC_ObjCInterface: |
| 3947 | case PCC_ObjCImplementation: |
| 3948 | case PCC_ObjCInstanceVariableList: |
| 3949 | case PCC_Template: |
| 3950 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3951 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3952 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3953 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3954 | break; |
| 3955 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3956 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3957 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3958 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3959 | case PCC_ForInit: |
| 3960 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3961 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3962 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3963 | else |
| 3964 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3965 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3966 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3967 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3968 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3970 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3971 | // Unfiltered |
| 3972 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3975 | // If we are in a C++ non-static member function, check the qualifiers on |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 3976 | // the member function to filter/prioritize the results list. |
Ilya Biryukov | b45d851b | 2018-12-19 18:01:24 +0000 | [diff] [blame] | 3977 | auto ThisType = getCurrentThisType(); |
| 3978 | if (!ThisType.isNull()) |
| 3979 | Results.setObjectTypeQualifiers(ThisType->getPointeeType().getQualifiers()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3980 | |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 3981 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3982 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3983 | CodeCompleter->includeGlobals(), |
| 3984 | CodeCompleter->loadExternal()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3986 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3987 | Results.ExitScope(); |
| 3988 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3989 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3990 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3991 | case PCC_Expression: |
| 3992 | case PCC_Statement: |
| 3993 | case PCC_RecoveryInFunction: |
| 3994 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3995 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3996 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3997 | |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3998 | case PCC_Namespace: |
| 3999 | case PCC_Class: |
| 4000 | case PCC_ObjCInterface: |
| 4001 | case PCC_ObjCImplementation: |
| 4002 | case PCC_ObjCInstanceVariableList: |
| 4003 | case PCC_Template: |
| 4004 | case PCC_MemberTemplate: |
| 4005 | case PCC_ForInit: |
| 4006 | case PCC_Condition: |
| 4007 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 4008 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 4009 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 4010 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 4012 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4013 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4015 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4016 | Results.data(), Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4019 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4020 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4021 | ArrayRef<IdentifierInfo *> SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4022 | bool AtArgumentExpression, bool IsSuper, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4023 | ResultBuilder &Results); |
| 4024 | |
| 4025 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 4026 | bool AllowNonIdentifiers, |
| 4027 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4028 | typedef CodeCompletionResult Result; |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 4029 | ResultBuilder Results( |
| 4030 | *this, CodeCompleter->getAllocator(), |
| 4031 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4032 | AllowNestedNameSpecifiers |
| 4033 | // FIXME: Try to separate codepath leading here to deduce whether we |
| 4034 | // need an existing symbol or a new one. |
| 4035 | ? CodeCompletionContext::CCC_SymbolOrNewName |
| 4036 | : CodeCompletionContext::CCC_NewName); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4037 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4039 | // Type qualifiers can come after names. |
| 4040 | Results.AddResult(Result("const")); |
| 4041 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4042 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4043 | Results.AddResult(Result("restrict")); |
| 4044 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4045 | if (getLangOpts().CPlusPlus) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4046 | if (getLangOpts().CPlusPlus11 && |
| 4047 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 4048 | DS.getTypeSpecType() == DeclSpec::TST_struct)) |
| 4049 | Results.AddResult("final"); |
| 4050 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4051 | if (AllowNonIdentifiers) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4052 | Results.AddResult(Result("operator")); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4053 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4055 | // Add nested-name-specifiers. |
| 4056 | if (AllowNestedNameSpecifiers) { |
| 4057 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4058 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4059 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4060 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4061 | CodeCompleter->includeGlobals(), |
| 4062 | CodeCompleter->loadExternal()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4063 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4064 | } |
| 4065 | } |
| 4066 | Results.ExitScope(); |
| 4067 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4068 | // If we're in a context where we might have an expression (rather than a |
| 4069 | // declaration), and what we've seen so far is an Objective-C type that could |
| 4070 | // be a receiver of a class message, this may be a class message send with |
| 4071 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 4072 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4073 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4074 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4075 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 4076 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4077 | !DS.isTypeAltiVecVector() && S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4078 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 4079 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4080 | Scope::FunctionPrototypeScope | Scope::AtCatchScope)) == |
| 4081 | 0) { |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4082 | ParsedType T = DS.getRepAsType(); |
| 4083 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4084 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 4087 | // Note that we intentionally suppress macro results here, since we do not |
| 4088 | // encourage using macros to produce the names of entities. |
| 4089 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4090 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4091 | Results.data(), Results.size()); |
| 4092 | } |
| 4093 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4094 | struct Sema::CodeCompleteExpressionData { |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4095 | CodeCompleteExpressionData(QualType PreferredType = QualType(), |
| 4096 | bool IsParenthesized = false) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4097 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4098 | ObjCCollection(false), IsParenthesized(IsParenthesized) {} |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4099 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4100 | QualType PreferredType; |
| 4101 | bool IntegralConstantExpression; |
| 4102 | bool ObjCCollection; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4103 | bool IsParenthesized; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4104 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4105 | }; |
| 4106 | |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4107 | namespace { |
| 4108 | /// Information that allows to avoid completing redundant enumerators. |
| 4109 | struct CoveredEnumerators { |
| 4110 | llvm::SmallPtrSet<EnumConstantDecl *, 8> Seen; |
| 4111 | NestedNameSpecifier *SuggestedQualifier = nullptr; |
| 4112 | }; |
| 4113 | } // namespace |
| 4114 | |
| 4115 | static void AddEnumerators(ResultBuilder &Results, ASTContext &Context, |
| 4116 | EnumDecl *Enum, DeclContext *CurContext, |
| 4117 | const CoveredEnumerators &Enumerators) { |
| 4118 | NestedNameSpecifier *Qualifier = Enumerators.SuggestedQualifier; |
| 4119 | if (Context.getLangOpts().CPlusPlus && !Qualifier && Enumerators.Seen.empty()) { |
| 4120 | // If there are no prior enumerators in C++, check whether we have to |
| 4121 | // qualify the names of the enumerators that we suggest, because they |
| 4122 | // may not be visible in this scope. |
| 4123 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
| 4124 | } |
| 4125 | |
| 4126 | Results.EnterNewScope(); |
| 4127 | for (auto *E : Enum->enumerators()) { |
| 4128 | if (Enumerators.Seen.count(E)) |
| 4129 | continue; |
| 4130 | |
| 4131 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
| 4132 | Results.AddResult(R, CurContext, nullptr, false); |
| 4133 | } |
| 4134 | Results.ExitScope(); |
| 4135 | } |
| 4136 | |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4137 | /// Try to find a corresponding FunctionProtoType for function-like types (e.g. |
| 4138 | /// function pointers, std::function, etc). |
| 4139 | static const FunctionProtoType *TryDeconstructFunctionLike(QualType T) { |
| 4140 | assert(!T.isNull()); |
| 4141 | // Try to extract first template argument from std::function<> and similar. |
| 4142 | // Note we only handle the sugared types, they closely match what users wrote. |
| 4143 | // We explicitly choose to not handle ClassTemplateSpecializationDecl. |
| 4144 | if (auto *Specialization = T->getAs<TemplateSpecializationType>()) { |
| 4145 | if (Specialization->getNumArgs() != 1) |
| 4146 | return nullptr; |
| 4147 | const TemplateArgument &Argument = Specialization->getArg(0); |
| 4148 | if (Argument.getKind() != TemplateArgument::Type) |
| 4149 | return nullptr; |
| 4150 | return Argument.getAsType()->getAs<FunctionProtoType>(); |
| 4151 | } |
| 4152 | // Handle other cases. |
| 4153 | if (T->isPointerType()) |
| 4154 | T = T->getPointeeType(); |
| 4155 | return T->getAs<FunctionProtoType>(); |
| 4156 | } |
| 4157 | |
| 4158 | /// Adds a pattern completion for a lambda expression with the specified |
| 4159 | /// parameter types and placeholders for parameter names. |
| 4160 | static void AddLambdaCompletion(ResultBuilder &Results, |
| 4161 | llvm::ArrayRef<QualType> Parameters, |
| 4162 | const LangOptions &LangOpts) { |
Ilya Biryukov | fd11a5f | 2019-05-23 16:39:26 +0000 | [diff] [blame] | 4163 | if (!Results.includeCodePatterns()) |
| 4164 | return; |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4165 | CodeCompletionBuilder Completion(Results.getAllocator(), |
| 4166 | Results.getCodeCompletionTUInfo()); |
| 4167 | // [](<parameters>) {} |
| 4168 | Completion.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 4169 | Completion.AddPlaceholderChunk("="); |
| 4170 | Completion.AddChunk(CodeCompletionString::CK_RightBracket); |
| 4171 | if (!Parameters.empty()) { |
| 4172 | Completion.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4173 | bool First = true; |
| 4174 | for (auto Parameter : Parameters) { |
| 4175 | if (!First) |
| 4176 | Completion.AddChunk(CodeCompletionString::ChunkKind::CK_Comma); |
| 4177 | else |
| 4178 | First = false; |
| 4179 | |
| 4180 | constexpr llvm::StringLiteral NamePlaceholder = "!#!NAME_GOES_HERE!#!"; |
| 4181 | std::string Type = NamePlaceholder; |
| 4182 | Parameter.getAsStringInternal(Type, PrintingPolicy(LangOpts)); |
| 4183 | llvm::StringRef Prefix, Suffix; |
| 4184 | std::tie(Prefix, Suffix) = llvm::StringRef(Type).split(NamePlaceholder); |
| 4185 | Prefix = Prefix.rtrim(); |
| 4186 | Suffix = Suffix.ltrim(); |
| 4187 | |
| 4188 | Completion.AddTextChunk(Completion.getAllocator().CopyString(Prefix)); |
| 4189 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4190 | Completion.AddPlaceholderChunk("parameter"); |
| 4191 | Completion.AddTextChunk(Completion.getAllocator().CopyString(Suffix)); |
| 4192 | }; |
| 4193 | Completion.AddChunk(CodeCompletionString::CK_RightParen); |
| 4194 | } |
Ilya Biryukov | 47fd4f0 | 2019-05-24 16:16:15 +0000 | [diff] [blame] | 4195 | Completion.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace); |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4196 | Completion.AddChunk(CodeCompletionString::CK_LeftBrace); |
Ilya Biryukov | 47fd4f0 | 2019-05-24 16:16:15 +0000 | [diff] [blame] | 4197 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4198 | Completion.AddPlaceholderChunk("body"); |
Ilya Biryukov | 47fd4f0 | 2019-05-24 16:16:15 +0000 | [diff] [blame] | 4199 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4200 | Completion.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4201 | |
| 4202 | Results.AddResult(Completion.TakeString()); |
| 4203 | } |
| 4204 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4205 | /// Perform code-completion in an expression context when we know what |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4206 | /// type we're looking for. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4207 | void Sema::CodeCompleteExpression(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4208 | const CodeCompleteExpressionData &Data) { |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4209 | ResultBuilder Results( |
| 4210 | *this, CodeCompleter->getAllocator(), |
| 4211 | CodeCompleter->getCodeCompletionTUInfo(), |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4212 | CodeCompletionContext( |
| 4213 | Data.IsParenthesized |
| 4214 | ? CodeCompletionContext::CCC_ParenthesizedExpression |
| 4215 | : CodeCompletionContext::CCC_Expression, |
| 4216 | Data.PreferredType)); |
| 4217 | auto PCC = |
| 4218 | Data.IsParenthesized ? PCC_ParenthesizedExpression : PCC_Expression; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4219 | if (Data.ObjCCollection) |
| 4220 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 4221 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4222 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4223 | else if (WantTypesInContext(PCC, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4224 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4225 | else |
| 4226 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4227 | |
| 4228 | if (!Data.PreferredType.isNull()) |
| 4229 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4231 | // Ignore any declarations that we were told that we don't care about. |
| 4232 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 4233 | Results.Ignore(Data.IgnoreDecls[I]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4234 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4235 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4236 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4237 | CodeCompleter->includeGlobals(), |
| 4238 | CodeCompleter->loadExternal()); |
| 4239 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4240 | Results.EnterNewScope(); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4241 | AddOrdinaryNameResults(PCC, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4242 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4243 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 4244 | bool PreferredTypeIsPointer = false; |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4245 | if (!Data.PreferredType.isNull()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4246 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() || |
| 4247 | Data.PreferredType->isMemberPointerType() || |
| 4248 | Data.PreferredType->isBlockPointerType(); |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4249 | if (Data.PreferredType->isEnumeralType()) { |
| 4250 | EnumDecl *Enum = Data.PreferredType->castAs<EnumType>()->getDecl(); |
| 4251 | if (auto *Def = Enum->getDefinition()) |
| 4252 | Enum = Def; |
| 4253 | // FIXME: collect covered enumerators in cases like: |
| 4254 | // if (x == my_enum::one) { ... } else if (x == ^) {} |
| 4255 | AddEnumerators(Results, Context, Enum, CurContext, CoveredEnumerators()); |
| 4256 | } |
| 4257 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4258 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4259 | if (S->getFnParent() && !Data.ObjCCollection && |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 4260 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4261 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4263 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4264 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false, |
| 4265 | PreferredTypeIsPointer); |
Ilya Biryukov | 3a2f0e4 | 2019-05-23 07:45:35 +0000 | [diff] [blame] | 4266 | |
| 4267 | // Complete a lambda expression when preferred type is a function. |
| 4268 | if (!Data.PreferredType.isNull() && getLangOpts().CPlusPlus11) { |
| 4269 | if (const FunctionProtoType *F = |
| 4270 | TryDeconstructFunctionLike(Data.PreferredType)) |
| 4271 | AddLambdaCompletion(Results, F->getParamTypes(), getLangOpts()); |
| 4272 | } |
| 4273 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4274 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4275 | Results.data(), Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4278 | void Sema::CodeCompleteExpression(Scope *S, QualType PreferredType, |
| 4279 | bool IsParenthesized) { |
| 4280 | return CodeCompleteExpression( |
| 4281 | S, CodeCompleteExpressionData(PreferredType, IsParenthesized)); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4284 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E, |
| 4285 | QualType PreferredType) { |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 4286 | if (E.isInvalid()) |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4287 | CodeCompleteExpression(S, PreferredType); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4288 | else if (getLangOpts().ObjC) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4289 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4290 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4291 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4292 | /// The set of properties that have already been added, referenced by |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 4293 | /// property name. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4294 | typedef llvm::SmallPtrSet<IdentifierInfo *, 16> AddedPropertiesSet; |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 4295 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4296 | /// Retrieve the container definition, if any? |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4297 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 4298 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 4299 | if (Interface->hasDefinition()) |
| 4300 | return Interface->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4301 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4302 | return Interface; |
| 4303 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4304 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4305 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4306 | if (Protocol->hasDefinition()) |
| 4307 | return Protocol->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4309 | return Protocol; |
| 4310 | } |
| 4311 | return Container; |
| 4312 | } |
| 4313 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4314 | /// Adds a block invocation code completion result for the given block |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4315 | /// declaration \p BD. |
| 4316 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
| 4317 | CodeCompletionBuilder &Builder, |
| 4318 | const NamedDecl *BD, |
| 4319 | const FunctionTypeLoc &BlockLoc, |
| 4320 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
| 4321 | Builder.AddResultTypeChunk( |
| 4322 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
| 4323 | Policy, Builder.getAllocator())); |
| 4324 | |
| 4325 | AddTypedNameChunk(Context, Policy, BD, Builder); |
| 4326 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4327 | |
| 4328 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { |
| 4329 | Builder.AddPlaceholderChunk("..."); |
| 4330 | } else { |
| 4331 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { |
| 4332 | if (I) |
| 4333 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 4334 | |
| 4335 | // Format the placeholder string. |
| 4336 | std::string PlaceholderStr = |
| 4337 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
| 4338 | |
| 4339 | if (I == N - 1 && BlockProtoLoc && |
| 4340 | BlockProtoLoc.getTypePtr()->isVariadic()) |
| 4341 | PlaceholderStr += ", ..."; |
| 4342 | |
| 4343 | // Add the placeholder string. |
| 4344 | Builder.AddPlaceholderChunk( |
| 4345 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4350 | } |
| 4351 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4352 | static void |
| 4353 | AddObjCProperties(const CodeCompletionContext &CCContext, |
| 4354 | ObjCContainerDecl *Container, bool AllowCategories, |
| 4355 | bool AllowNullaryMethods, DeclContext *CurContext, |
| 4356 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
| 4357 | bool IsBaseExprStatement = false, |
| 4358 | bool IsClassProperty = false, bool InOriginalClass = true) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4359 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4360 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4361 | // Retrieve the definition. |
| 4362 | Container = getContainerDef(Container); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4364 | // Add properties in this container. |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4365 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4366 | if (!AddedProperties.insert(P->getIdentifier()).second) |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4367 | return; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4368 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4369 | // FIXME: Provide block invocation completion for non-statement |
| 4370 | // expressions. |
| 4371 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
| 4372 | !IsBaseExprStatement) { |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4373 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
| 4374 | if (!InOriginalClass) |
| 4375 | setInBaseClass(R); |
| 4376 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4377 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | // Block setter and invocation completion is provided only when we are able |
| 4381 | // to find the FunctionProtoTypeLoc with parameter names for the block. |
| 4382 | FunctionTypeLoc BlockLoc; |
| 4383 | FunctionProtoTypeLoc BlockProtoLoc; |
| 4384 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
| 4385 | BlockProtoLoc); |
| 4386 | if (!BlockLoc) { |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4387 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
| 4388 | if (!InOriginalClass) |
| 4389 | setInBaseClass(R); |
| 4390 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4391 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | // The default completion result for block properties should be the block |
| 4395 | // invocation completion when the base expression is a statement. |
| 4396 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4397 | Results.getCodeCompletionTUInfo()); |
| 4398 | AddObjCBlockCall(Container->getASTContext(), |
| 4399 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
| 4400 | BlockLoc, BlockProtoLoc); |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4401 | Result R = Result(Builder.TakeString(), P, Results.getBasePriority(P)); |
| 4402 | if (!InOriginalClass) |
| 4403 | setInBaseClass(R); |
| 4404 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4405 | |
| 4406 | // Provide additional block setter completion iff the base expression is a |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4407 | // statement and the block property is mutable. |
| 4408 | if (!P->isReadOnly()) { |
| 4409 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4410 | Results.getCodeCompletionTUInfo()); |
| 4411 | AddResultTypeChunk(Container->getASTContext(), |
| 4412 | getCompletionPrintingPolicy(Results.getSema()), P, |
| 4413 | CCContext.getBaseType(), Builder); |
| 4414 | Builder.AddTypedTextChunk( |
| 4415 | Results.getAllocator().CopyString(P->getName())); |
| 4416 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4417 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4418 | std::string PlaceholderStr = formatBlockPlaceholder( |
| 4419 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
| 4420 | BlockProtoLoc, /*SuppressBlockName=*/true); |
| 4421 | // Add the placeholder string. |
| 4422 | Builder.AddPlaceholderChunk( |
| 4423 | Builder.getAllocator().CopyString(PlaceholderStr)); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4424 | |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 4425 | // When completing blocks properties that return void the default |
| 4426 | // property completion result should show up before the setter, |
| 4427 | // otherwise the setter completion should show up before the default |
| 4428 | // property completion, as we normally want to use the result of the |
| 4429 | // call. |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4430 | Result R = |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4431 | Result(Builder.TakeString(), P, |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 4432 | Results.getBasePriority(P) + |
| 4433 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
| 4434 | ? CCD_BlockPropertySetter |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4435 | : -CCD_BlockPropertySetter)); |
| 4436 | if (!InOriginalClass) |
| 4437 | setInBaseClass(R); |
| 4438 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4439 | } |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4440 | }; |
| 4441 | |
| 4442 | if (IsClassProperty) { |
| 4443 | for (const auto *P : Container->class_properties()) |
| 4444 | AddProperty(P); |
| 4445 | } else { |
| 4446 | for (const auto *P : Container->instance_properties()) |
| 4447 | AddProperty(P); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4448 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4449 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4450 | // Add nullary methods or implicit class properties |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 4451 | if (AllowNullaryMethods) { |
| 4452 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4453 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4454 | // Adds a method result |
| 4455 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
| 4456 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
| 4457 | if (!Name) |
| 4458 | return; |
| 4459 | if (!AddedProperties.insert(Name).second) |
| 4460 | return; |
| 4461 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4462 | Results.getCodeCompletionTUInfo()); |
| 4463 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
| 4464 | Builder.AddTypedTextChunk( |
| 4465 | Results.getAllocator().CopyString(Name->getName())); |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4466 | Result R = Result(Builder.TakeString(), M, |
| 4467 | CCP_MemberDeclaration + CCD_MethodAsProperty); |
| 4468 | if (!InOriginalClass) |
| 4469 | setInBaseClass(R); |
| 4470 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4471 | }; |
| 4472 | |
| 4473 | if (IsClassProperty) { |
| 4474 | for (const auto *M : Container->methods()) { |
| 4475 | // Gather the class method that can be used as implicit property |
| 4476 | // getters. Methods with arguments or methods that return void aren't |
| 4477 | // added to the results as they can't be used as a getter. |
| 4478 | if (!M->getSelector().isUnarySelector() || |
| 4479 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) |
| 4480 | continue; |
| 4481 | AddMethod(M); |
| 4482 | } |
| 4483 | } else { |
| 4484 | for (auto *M : Container->methods()) { |
| 4485 | if (M->getSelector().isUnarySelector()) |
| 4486 | AddMethod(M); |
| 4487 | } |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4490 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4491 | // Add properties in referenced protocols. |
| 4492 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 4493 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4494 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4495 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4496 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4497 | /*InOriginalClass*/ false); |
| 4498 | } else if (ObjCInterfaceDecl *IFace = |
| 4499 | dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4500 | if (AllowCategories) { |
| 4501 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4502 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4503 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4504 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4505 | IsBaseExprStatement, IsClassProperty, |
| 4506 | InOriginalClass); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4507 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4509 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 4510 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4511 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4512 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4513 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4514 | /*InOriginalClass*/ false); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4516 | // Look in the superclass. |
| 4517 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4518 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4519 | AllowNullaryMethods, CurContext, AddedProperties, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4520 | Results, IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4521 | /*InOriginalClass*/ false); |
| 4522 | } else if (const auto *Category = |
| 4523 | dyn_cast<ObjCCategoryDecl>(Container)) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4524 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 4525 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4526 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4527 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4528 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4529 | /*InOriginalClass*/ false); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4530 | } |
| 4531 | } |
| 4532 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4533 | static void |
| 4534 | AddRecordMembersCompletionResults(Sema &SemaRef, ResultBuilder &Results, |
| 4535 | Scope *S, QualType BaseType, RecordDecl *RD, |
| 4536 | Optional<FixItHint> AccessOpFixIt) { |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4537 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 4538 | // for the base object type. |
| 4539 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 4540 | |
| 4541 | // Access to a C/C++ class, struct, or union. |
| 4542 | Results.allowNestedNameSpecifiers(); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4543 | std::vector<FixItHint> FixIts; |
| 4544 | if (AccessOpFixIt) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4545 | FixIts.emplace_back(AccessOpFixIt.getValue()); |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 4546 | CodeCompletionDeclConsumer Consumer(Results, RD, BaseType, std::move(FixIts)); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4547 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
Alex Lorenz | e6afa39 | 2017-05-11 13:48:57 +0000 | [diff] [blame] | 4548 | SemaRef.CodeCompleter->includeGlobals(), |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4549 | /*IncludeDependentBases=*/true, |
| 4550 | SemaRef.CodeCompleter->loadExternal()); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4551 | |
| 4552 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 4553 | if (!Results.empty()) { |
| 4554 | // The "template" keyword can follow "->" or "." in the grammar. |
| 4555 | // However, we only want to suggest the template keyword if something |
| 4556 | // is dependent. |
| 4557 | bool IsDependent = BaseType->isDependentType(); |
| 4558 | if (!IsDependent) { |
| 4559 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 4560 | if (DeclContext *Ctx = DepScope->getEntity()) { |
| 4561 | IsDependent = Ctx->isDependentContext(); |
| 4562 | break; |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | if (IsDependent) |
| 4567 | Results.AddResult(CodeCompletionResult("template")); |
| 4568 | } |
| 4569 | } |
| 4570 | } |
| 4571 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4572 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4573 | Expr *OtherOpBase, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4574 | SourceLocation OpLoc, bool IsArrow, |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4575 | bool IsBaseExprStatement, |
| 4576 | QualType PreferredType) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4577 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4578 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4580 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4581 | if (ConvertedBase.isInvalid()) |
| 4582 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4583 | QualType ConvertedBaseType = ConvertedBase.get()->getType(); |
| 4584 | |
| 4585 | enum CodeCompletionContext::Kind contextKind; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4586 | |
| 4587 | if (IsArrow) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4588 | if (const auto *Ptr = ConvertedBaseType->getAs<PointerType>()) |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4589 | ConvertedBaseType = Ptr->getPointeeType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4590 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4592 | if (IsArrow) { |
| 4593 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4594 | } else { |
| 4595 | if (ConvertedBaseType->isObjCObjectPointerType() || |
| 4596 | ConvertedBaseType->isObjCObjectOrInterfaceType()) { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4597 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4598 | } else { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4599 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 4600 | } |
| 4601 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4602 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4603 | CodeCompletionContext CCContext(contextKind, ConvertedBaseType); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4604 | CCContext.setPreferredType(PreferredType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4605 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4606 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4607 | &ResultBuilder::IsMember); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4608 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4609 | auto DoCompletion = [&](Expr *Base, bool IsArrow, |
| 4610 | Optional<FixItHint> AccessOpFixIt) -> bool { |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4611 | if (!Base) |
| 4612 | return false; |
| 4613 | |
| 4614 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4615 | if (ConvertedBase.isInvalid()) |
| 4616 | return false; |
| 4617 | Base = ConvertedBase.get(); |
| 4618 | |
| 4619 | QualType BaseType = Base->getType(); |
| 4620 | |
| 4621 | if (IsArrow) { |
| 4622 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4623 | BaseType = Ptr->getPointeeType(); |
| 4624 | else if (BaseType->isObjCObjectPointerType()) |
| 4625 | /*Do nothing*/; |
| 4626 | else |
| 4627 | return false; |
| 4628 | } |
| 4629 | |
| 4630 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 4631 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, |
| 4632 | Record->getDecl(), |
| 4633 | std::move(AccessOpFixIt)); |
| 4634 | } else if (const auto *TST = |
| 4635 | BaseType->getAs<TemplateSpecializationType>()) { |
| 4636 | TemplateName TN = TST->getTemplateName(); |
| 4637 | if (const auto *TD = |
| 4638 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) { |
| 4639 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 4640 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4641 | std::move(AccessOpFixIt)); |
| 4642 | } |
| 4643 | } else if (const auto *ICNT = BaseType->getAs<InjectedClassNameType>()) { |
| 4644 | if (auto *RD = ICNT->getDecl()) |
| 4645 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4646 | std::move(AccessOpFixIt)); |
| 4647 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { |
| 4648 | // Objective-C property reference. |
| 4649 | AddedPropertiesSet AddedProperties; |
| 4650 | |
| 4651 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4652 | BaseType->getAsObjCInterfacePointerType()) { |
| 4653 | // Add property results based on our interface. |
| 4654 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
| 4655 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
| 4656 | /*AllowNullaryMethods=*/true, CurContext, |
| 4657 | AddedProperties, Results, IsBaseExprStatement); |
| 4658 | } |
| 4659 | |
| 4660 | // Add properties from the protocols in a qualified interface. |
| 4661 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) |
| 4662 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 4663 | CurContext, AddedProperties, Results, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4664 | IsBaseExprStatement, /*IsClassProperty*/ false, |
| 4665 | /*InOriginalClass*/ false); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4666 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 4667 | (!IsArrow && BaseType->isObjCObjectType())) { |
| 4668 | // Objective-C instance variable access. |
| 4669 | ObjCInterfaceDecl *Class = nullptr; |
| 4670 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4671 | BaseType->getAs<ObjCObjectPointerType>()) |
| 4672 | Class = ObjCPtr->getInterfaceDecl(); |
| 4673 | else |
| 4674 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
| 4675 | |
| 4676 | // Add all ivars from this class and its superclasses. |
| 4677 | if (Class) { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 4678 | CodeCompletionDeclConsumer Consumer(Results, Class, BaseType); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4679 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
| 4680 | LookupVisibleDecls( |
| 4681 | Class, LookupMemberName, Consumer, CodeCompleter->includeGlobals(), |
| 4682 | /*IncludeDependentBases=*/false, CodeCompleter->loadExternal()); |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | // FIXME: How do we cope with isa? |
| 4687 | return true; |
| 4688 | }; |
| 4689 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4690 | Results.EnterNewScope(); |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 4691 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4692 | bool CompletionSucceded = DoCompletion(Base, IsArrow, None); |
| 4693 | if (CodeCompleter->includeFixIts()) { |
| 4694 | const CharSourceRange OpRange = |
| 4695 | CharSourceRange::getTokenRange(OpLoc, OpLoc); |
| 4696 | CompletionSucceded |= DoCompletion( |
| 4697 | OtherOpBase, !IsArrow, |
| 4698 | FixItHint::CreateReplacement(OpRange, IsArrow ? "." : "->")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4699 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4701 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4702 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4703 | if (!CompletionSucceded) |
| 4704 | return; |
| 4705 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4706 | // Hand off the results found for code completion. |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4707 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4708 | Results.data(), Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4711 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, |
| 4712 | IdentifierInfo &ClassName, |
| 4713 | SourceLocation ClassNameLoc, |
| 4714 | bool IsBaseExprStatement) { |
| 4715 | IdentifierInfo *ClassNamePtr = &ClassName; |
| 4716 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); |
| 4717 | if (!IFace) |
| 4718 | return; |
| 4719 | CodeCompletionContext CCContext( |
| 4720 | CodeCompletionContext::CCC_ObjCPropertyAccess); |
| 4721 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4722 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| 4723 | &ResultBuilder::IsMember); |
| 4724 | Results.EnterNewScope(); |
| 4725 | AddedPropertiesSet AddedProperties; |
| 4726 | AddObjCProperties(CCContext, IFace, true, |
| 4727 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, |
| 4728 | Results, IsBaseExprStatement, |
| 4729 | /*IsClassProperty=*/true); |
| 4730 | Results.ExitScope(); |
| 4731 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4732 | Results.data(), Results.size()); |
| 4733 | } |
| 4734 | |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 4735 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4736 | if (!CodeCompleter) |
| 4737 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4738 | |
| 4739 | ResultBuilder::LookupFilter Filter = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4740 | enum CodeCompletionContext::Kind ContextKind = |
| 4741 | CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4742 | switch ((DeclSpec::TST)TagSpec) { |
| 4743 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4744 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4745 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4746 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4747 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4748 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4749 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4750 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4751 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4753 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4754 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4755 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4756 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4757 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4758 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4760 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4761 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4762 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4763 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4764 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4765 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4766 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4767 | |
| 4768 | // First pass: look for tags. |
| 4769 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4770 | LookupVisibleDecls(S, LookupTagName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4771 | CodeCompleter->includeGlobals(), |
| 4772 | CodeCompleter->loadExternal()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4774 | if (CodeCompleter->includeGlobals()) { |
| 4775 | // Second pass: look for nested name specifiers. |
| 4776 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4777 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 4778 | CodeCompleter->includeGlobals(), |
| 4779 | CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4780 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4782 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4783 | Results.data(), Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4786 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, |
| 4787 | const LangOptions &LangOpts) { |
| 4788 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 4789 | Results.AddResult("const"); |
| 4790 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 4791 | Results.AddResult("volatile"); |
| 4792 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 4793 | Results.AddResult("restrict"); |
| 4794 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 4795 | Results.AddResult("_Atomic"); |
| 4796 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 4797 | Results.AddResult("__unaligned"); |
| 4798 | } |
| 4799 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4800 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4801 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4802 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4803 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4804 | Results.EnterNewScope(); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4805 | AddTypeQualifierResults(DS, Results, LangOpts); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4806 | Results.ExitScope(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4807 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4808 | Results.data(), Results.size()); |
| 4809 | } |
| 4810 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4811 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, |
| 4812 | const VirtSpecifiers *VS) { |
| 4813 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4814 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4815 | CodeCompletionContext::CCC_TypeQualifiers); |
| 4816 | Results.EnterNewScope(); |
| 4817 | AddTypeQualifierResults(DS, Results, LangOpts); |
| 4818 | if (LangOpts.CPlusPlus11) { |
| 4819 | Results.AddResult("noexcept"); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 4820 | if (D.getContext() == DeclaratorContext::MemberContext && |
| 4821 | !D.isCtorOrDtor() && !D.isStaticMember()) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4822 | if (!VS || !VS->isFinalSpecified()) |
| 4823 | Results.AddResult("final"); |
| 4824 | if (!VS || !VS->isOverrideSpecified()) |
| 4825 | Results.AddResult("override"); |
| 4826 | } |
| 4827 | } |
| 4828 | Results.ExitScope(); |
| 4829 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4830 | Results.data(), Results.size()); |
| 4831 | } |
| 4832 | |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 4833 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 4834 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 4835 | } |
| 4836 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4837 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4838 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4839 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4840 | |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 4841 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back().getPointer(); |
Kadir Cetinkaya | 6d57266 | 2018-10-23 13:49:37 +0000 | [diff] [blame] | 4842 | // Condition expression might be invalid, do not continue in this case. |
| 4843 | if (!Switch->getCond()) |
| 4844 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4845 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 4846 | if (!type->isEnumeralType()) { |
| 4847 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4848 | Data.IntegralConstantExpression = true; |
| 4849 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4850 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4851 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4852 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4853 | // Code-complete the cases of a switch statement over an enumeration type |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4854 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4855 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4856 | if (EnumDecl *Def = Enum->getDefinition()) |
| 4857 | Enum = Def; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4859 | // Determine which enumerators we have already seen in the switch statement. |
| 4860 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 4861 | // token, in case we are code-completing in the middle of the switch and not |
| 4862 | // at the end. However, we aren't able to do so at the moment. |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4863 | CoveredEnumerators Enumerators; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4864 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4865 | SC = SC->getNextSwitchCase()) { |
| 4866 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 4867 | if (!Case) |
| 4868 | continue; |
| 4869 | |
| 4870 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4871 | if (auto *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 4872 | if (auto *Enumerator = |
| 4873 | dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4874 | // We look into the AST of the case statement to determine which |
| 4875 | // enumerator was named. Alternatively, we could compute the value of |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4876 | // the integral constant expression, then compare it against the |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4877 | // values of each enumerator. However, value-based approach would not |
| 4878 | // work as well with C++ templates where enumerators declared within a |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4879 | // template are type- and value-dependent. |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4880 | Enumerators.Seen.insert(Enumerator); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4881 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4882 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 4883 | // so that we can reproduce it as part of code completion, e.g., |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4884 | // |
| 4885 | // switch (TagD.getKind()) { |
| 4886 | // case TagDecl::TK_enum: |
| 4887 | // break; |
| 4888 | // case XXX |
| 4889 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4890 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4891 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 4892 | // TK_struct, and TK_class. |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4893 | Enumerators.SuggestedQualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4894 | } |
| 4895 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4896 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4897 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4898 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4899 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4900 | CodeCompletionContext::CCC_Expression); |
Ilya Biryukov | 600ec01b | 2019-05-16 16:06:57 +0000 | [diff] [blame] | 4901 | AddEnumerators(Results, Context, Enum, CurContext, Enumerators); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4903 | if (CodeCompleter->includeMacros()) { |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4904 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4905 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4906 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4907 | Results.data(), Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 4910 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4911 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4912 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4913 | |
| 4914 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4915 | if (!Args[I]) |
| 4916 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4917 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4918 | return false; |
| 4919 | } |
| 4920 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4921 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 4922 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4923 | static void mergeCandidatesWithResults( |
| 4924 | Sema &SemaRef, SmallVectorImpl<ResultCandidate> &Results, |
| 4925 | OverloadCandidateSet &CandidateSet, SourceLocation Loc) { |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 4926 | // Sort the overload candidate set by placing the best overloads first. |
| 4927 | llvm::stable_sort(CandidateSet, [&](const OverloadCandidate &X, |
| 4928 | const OverloadCandidate &Y) { |
| 4929 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc, |
| 4930 | CandidateSet.getKind()); |
| 4931 | }); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4932 | |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 4933 | // Add the remaining viable overload candidates as code-completion results. |
| 4934 | for (OverloadCandidate &Candidate : CandidateSet) { |
| 4935 | if (Candidate.Function && Candidate.Function->isDeleted()) |
| 4936 | continue; |
| 4937 | if (Candidate.Viable) |
| 4938 | Results.push_back(ResultCandidate(Candidate.Function)); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4939 | } |
| 4940 | } |
| 4941 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4942 | /// Get the type of the Nth parameter from a given set of overload |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4943 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4944 | static QualType getParamType(Sema &SemaRef, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4945 | ArrayRef<ResultCandidate> Candidates, unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 4948 | // up to N, return the type of the Nth parameter if it is the same for all |
| 4949 | // overload candidates. |
| 4950 | QualType ParamType; |
| 4951 | for (auto &Candidate : Candidates) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4952 | if (const auto *FType = Candidate.getFunctionType()) |
| 4953 | if (const auto *Proto = dyn_cast<FunctionProtoType>(FType)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4954 | if (N < Proto->getNumParams()) { |
| 4955 | if (ParamType.isNull()) |
| 4956 | ParamType = Proto->getParamType(N); |
| 4957 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4958 | ParamType.getNonReferenceType(), |
| 4959 | Proto->getParamType(N).getNonReferenceType())) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4960 | // Otherwise return a default-constructed QualType. |
| 4961 | return QualType(); |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | return ParamType; |
| 4966 | } |
| 4967 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4968 | static QualType |
| 4969 | ProduceSignatureHelp(Sema &SemaRef, Scope *S, |
| 4970 | MutableArrayRef<ResultCandidate> Candidates, |
| 4971 | unsigned CurrentArg, SourceLocation OpenParLoc) { |
| 4972 | if (Candidates.empty()) |
| 4973 | return QualType(); |
| 4974 | SemaRef.CodeCompleter->ProcessOverloadCandidates( |
| 4975 | SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc); |
| 4976 | return getParamType(SemaRef, Candidates, CurrentArg); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4979 | QualType Sema::ProduceCallSignatureHelp(Scope *S, Expr *Fn, |
| 4980 | ArrayRef<Expr *> Args, |
| 4981 | SourceLocation OpenParLoc) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4982 | if (!CodeCompleter) |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4983 | return QualType(); |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4984 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4985 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4986 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4987 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4988 | Expr::hasAnyTypeDependentArguments(Args)) { |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4989 | return QualType(); |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4990 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4991 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4992 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4993 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4994 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4995 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4996 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4997 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4998 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4999 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5000 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5001 | /*PartialOverloading=*/true); |
| 5002 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 5003 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 5004 | if (UME->hasExplicitTemplateArgs()) { |
| 5005 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 5006 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 5007 | } |
Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 5008 | |
| 5009 | // Add the base as first argument (use a nullptr if the base is implicit). |
| 5010 | SmallVector<Expr *, 12> ArgExprs( |
| 5011 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5012 | ArgExprs.append(Args.begin(), Args.end()); |
| 5013 | UnresolvedSet<8> Decls; |
| 5014 | Decls.append(UME->decls_begin(), UME->decls_end()); |
Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 5015 | const bool FirstArgumentIsBase = !UME->isImplicitAccess() && UME->getBase(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5016 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 5017 | /*SuppressUsedConversions=*/false, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5018 | /*PartialOverloading=*/true, FirstArgumentIsBase); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5019 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5020 | FunctionDecl *FD = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5021 | if (auto *MCE = dyn_cast<MemberExpr>(NakedFn)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5022 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5023 | else if (auto *DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5024 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 5025 | if (FD) { // We check whether it's a resolved function declaration. |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 5026 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5027 | !FD->getType()->getAs<FunctionProtoType>()) |
| 5028 | Results.push_back(ResultCandidate(FD)); |
| 5029 | else |
| 5030 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 5031 | Args, CandidateSet, |
| 5032 | /*SuppressUsedConversions=*/false, |
| 5033 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 5034 | |
| 5035 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 5036 | // If expression's type is CXXRecordDecl, it may overload the function |
| 5037 | // call operator, so we check if it does and add them as candidates. |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 5038 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5039 | if (isCompleteType(Loc, NakedFn->getType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5040 | DeclarationName OpName = |
| 5041 | Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 5042 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 5043 | LookupQualifiedName(R, DC); |
| 5044 | R.suppressDiagnostics(); |
| 5045 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 5046 | ArgExprs.append(Args.begin(), Args.end()); |
| 5047 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 5048 | /*ExplicitArgs=*/nullptr, |
| 5049 | /*SuppressUsedConversions=*/false, |
| 5050 | /*PartialOverloading=*/true); |
| 5051 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 5052 | } else { |
| 5053 | // Lastly we check whether expression's type is function pointer or |
| 5054 | // function. |
| 5055 | QualType T = NakedFn->getType(); |
| 5056 | if (!T->getPointeeType().isNull()) |
| 5057 | T = T->getPointeeType(); |
| 5058 | |
| 5059 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 5060 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5061 | /*PartialOverloading=*/true) || |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 5062 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 5063 | Results.push_back(ResultCandidate(FP)); |
| 5064 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 5065 | // No prototype and declaration, it may be a K & R style function. |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 5066 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5068 | } |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5069 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 5070 | QualType ParamType = |
| 5071 | ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
| 5072 | return !CandidateSet.empty() ? ParamType : QualType(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 5075 | QualType Sema::ProduceConstructorSignatureHelp(Scope *S, QualType Type, |
| 5076 | SourceLocation Loc, |
| 5077 | ArrayRef<Expr *> Args, |
| 5078 | SourceLocation OpenParLoc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5079 | if (!CodeCompleter) |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 5080 | return QualType(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5081 | |
| 5082 | // A complete type is needed to lookup for constructors. |
Ilya Biryukov | fb9dde7 | 2018-05-14 13:50:36 +0000 | [diff] [blame] | 5083 | CXXRecordDecl *RD = |
| 5084 | isCompleteType(Loc, Type) ? Type->getAsCXXRecordDecl() : nullptr; |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 5085 | if (!RD) |
| 5086 | return Type; |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 5087 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5088 | // FIXME: Provide support for member initializers. |
| 5089 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5090 | |
| 5091 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 5092 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5093 | for (NamedDecl *C : LookupConstructors(RD)) { |
| 5094 | if (auto *FD = dyn_cast<FunctionDecl>(C)) { |
| 5095 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), Args, |
| 5096 | CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5097 | /*SuppressUsedConversions=*/false, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 5098 | /*PartialOverloading=*/true, |
| 5099 | /*AllowExplicit*/ true); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5100 | } else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 5101 | AddTemplateOverloadCandidate( |
| 5102 | FTD, DeclAccessPair::make(FTD, C->getAccess()), |
| 5103 | /*ExplicitTemplateArgs=*/nullptr, Args, CandidateSet, |
| 5104 | /*SuppressUsedConversions=*/false, |
Hans Wennborg | d2b9fc8 | 2019-05-06 09:51:10 +0000 | [diff] [blame] | 5105 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | SmallVector<ResultCandidate, 8> Results; |
| 5110 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 5111 | return ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 5114 | QualType Sema::ProduceCtorInitMemberSignatureHelp( |
| 5115 | Scope *S, Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy, |
| 5116 | ArrayRef<Expr *> ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc) { |
| 5117 | if (!CodeCompleter) |
| 5118 | return QualType(); |
| 5119 | |
| 5120 | CXXConstructorDecl *Constructor = |
| 5121 | dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
| 5122 | if (!Constructor) |
| 5123 | return QualType(); |
| 5124 | // FIXME: Add support for Base class constructors as well. |
| 5125 | if (ValueDecl *MemberDecl = tryLookupCtorInitMemberDecl( |
| 5126 | Constructor->getParent(), SS, TemplateTypeTy, II)) |
| 5127 | return ProduceConstructorSignatureHelp(getCurScope(), MemberDecl->getType(), |
| 5128 | MemberDecl->getLocation(), ArgExprs, |
| 5129 | OpenParLoc); |
| 5130 | return QualType(); |
| 5131 | } |
| 5132 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5133 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 5134 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5135 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5136 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5137 | return; |
| 5138 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5139 | |
Ilya Biryukov | ebf0a6d | 2018-11-07 10:02:31 +0000 | [diff] [blame] | 5140 | CodeCompleteExpressionData Data; |
| 5141 | Data.PreferredType = VD->getType(); |
| 5142 | // Ignore VD to avoid completing the variable itself, e.g. in 'int foo = ^'. |
| 5143 | Data.IgnoreDecls.push_back(VD); |
| 5144 | |
| 5145 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5148 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5149 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5150 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5151 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 5152 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 5153 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5154 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5155 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5156 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5157 | CodeCompleter->includeGlobals(), |
| 5158 | CodeCompleter->loadExternal()); |
| 5159 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5160 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5162 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5163 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5164 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5165 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 5166 | if (Results.includeCodePatterns()) { |
| 5167 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5168 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5169 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5170 | Builder.AddPlaceholderChunk("statements"); |
| 5171 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5172 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5173 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5174 | Results.AddResult(Builder.TakeString()); |
| 5175 | |
| 5176 | // "else if" block |
| 5177 | Builder.AddTypedTextChunk("else"); |
| 5178 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5179 | Builder.AddTextChunk("if"); |
| 5180 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5181 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5182 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5183 | Builder.AddPlaceholderChunk("condition"); |
| 5184 | else |
| 5185 | Builder.AddPlaceholderChunk("expression"); |
| 5186 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 5187 | if (Results.includeCodePatterns()) { |
| 5188 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5189 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5190 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5191 | Builder.AddPlaceholderChunk("statements"); |
| 5192 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5193 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5194 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5195 | Results.AddResult(Builder.TakeString()); |
| 5196 | |
| 5197 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5198 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5199 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 5200 | AddPrettyFunctionResults(getLangOpts(), Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5202 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 5203 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5204 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5205 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5206 | Results.data(), Results.size()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5209 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 5210 | bool EnteringContext, QualType BaseType) { |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5211 | if (SS.isEmpty() || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 5212 | return; |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 5213 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5214 | // We want to keep the scope specifier even if it's invalid (e.g. the scope |
| 5215 | // "a::b::" is not corresponding to any context/namespace in the AST), since |
| 5216 | // it can be useful for global code completion which have information about |
| 5217 | // contexts/symbols that are not in the AST. |
| 5218 | if (SS.isInvalid()) { |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5219 | CodeCompletionContext CC(CodeCompletionContext::CCC_Symbol); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5220 | CC.setCXXScopeSpecifier(SS); |
Eric Liu | 206740e | 2019-02-21 11:22:58 +0000 | [diff] [blame] | 5221 | // As SS is invalid, we try to collect accessible contexts from the current |
| 5222 | // scope with a dummy lookup so that the completion consumer can try to |
| 5223 | // guess what the specified scope is. |
| 5224 | ResultBuilder DummyResults(*this, CodeCompleter->getAllocator(), |
| 5225 | CodeCompleter->getCodeCompletionTUInfo(), CC); |
| 5226 | if (S->getEntity()) { |
| 5227 | CodeCompletionDeclConsumer Consumer(DummyResults, S->getEntity(), |
| 5228 | BaseType); |
| 5229 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5230 | /*IncludeGlobalScope=*/false, |
| 5231 | /*LoadExternal=*/false); |
| 5232 | } |
| 5233 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5234 | DummyResults.getCompletionContext(), nullptr, 0); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5235 | return; |
| 5236 | } |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 5237 | // Always pretend to enter a context to ensure that a dependent type |
| 5238 | // resolves to a dependent record. |
| 5239 | DeclContext *Ctx = computeDeclContext(SS, /*EnteringContext=*/true); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5240 | if (!Ctx) |
| 5241 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 5242 | |
| 5243 | // Try to instantiate any non-dependent declaration contexts before |
| 5244 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5245 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 5246 | return; |
| 5247 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5248 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5249 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5250 | CodeCompletionContext::CCC_Symbol); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5251 | Results.EnterNewScope(); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5252 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5253 | // The "template" keyword can follow "::" in the grammar, but only |
| 5254 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 5255 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5256 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 5257 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5258 | |
| 5259 | // Add calls to overridden virtual functions, if there are any. |
| 5260 | // |
| 5261 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 5262 | // in a context that permits expressions. This is a general issue with |
| 5263 | // qualified-id completions. |
| 5264 | if (!EnteringContext) |
| 5265 | MaybeAddOverrideCalls(*this, Ctx, Results); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5266 | Results.ExitScope(); |
| 5267 | |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5268 | if (CodeCompleter->includeNamespaceLevelDecls() || |
| 5269 | (!Ctx->isNamespace() && !Ctx->isTranslationUnit())) { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 5270 | CodeCompletionDeclConsumer Consumer(Results, Ctx, BaseType); |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5271 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer, |
| 5272 | /*IncludeGlobalScope=*/true, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5273 | /*IncludeDependentBases=*/true, |
| 5274 | CodeCompleter->loadExternal()); |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5275 | } |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5276 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5277 | auto CC = Results.getCompletionContext(); |
| 5278 | CC.setCXXScopeSpecifier(SS); |
| 5279 | |
| 5280 | HandleCodeCompleteResults(this, CodeCompleter, CC, Results.data(), |
| 5281 | Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 5282 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5283 | |
| 5284 | void Sema::CodeCompleteUsing(Scope *S) { |
| 5285 | if (!CodeCompleter) |
| 5286 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5287 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5288 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5289 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5290 | // This can be both a using alias or using |
| 5291 | // declaration, in the former we expect a new name and a |
| 5292 | // symbol in the latter case. |
| 5293 | CodeCompletionContext::CCC_SymbolOrNewName, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5294 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5295 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5297 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 5298 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5299 | Results.AddResult(CodeCompletionResult("namespace")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5300 | |
| 5301 | // After "using", we can see anything that would start a |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5302 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5303 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5304 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5305 | CodeCompleter->includeGlobals(), |
| 5306 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5307 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5308 | |
| 5309 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5310 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 5314 | if (!CodeCompleter) |
| 5315 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5317 | // After "using namespace", we expect to see a namespace name or namespace |
| 5318 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5319 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5320 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5321 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5322 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5323 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5324 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5325 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5326 | CodeCompleter->includeGlobals(), |
| 5327 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5328 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5329 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5330 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5333 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5334 | if (!CodeCompleter) |
| 5335 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5336 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5337 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5338 | if (!S->getParent()) |
| 5339 | Ctx = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5340 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5341 | bool SuppressedGlobalResults = |
| 5342 | Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5343 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5344 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5345 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5346 | SuppressedGlobalResults |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5347 | ? CodeCompletionContext::CCC_Namespace |
| 5348 | : CodeCompletionContext::CCC_Other, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5349 | &ResultBuilder::IsNamespace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5350 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5351 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5352 | // We only want to see those namespaces that have already been defined |
| 5353 | // within this scope, because its likely that the user is creating an |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5354 | // extended namespace declaration. Keep track of the most recent |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5355 | // definition of each namespace. |
| 5356 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5357 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5358 | NS(Ctx->decls_begin()), |
| 5359 | NSEnd(Ctx->decls_end()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5360 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5361 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5362 | |
| 5363 | // Add the most recent definition (or extended definition) of each |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5364 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5365 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5366 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5367 | NS = OrigToLatest.begin(), |
| 5368 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5369 | NS != NSEnd; ++NS) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5370 | Results.AddResult( |
| 5371 | CodeCompletionResult(NS->second, Results.getBasePriority(NS->second), |
| 5372 | nullptr), |
| 5373 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5374 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5375 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5376 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5377 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5378 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5381 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5382 | if (!CodeCompleter) |
| 5383 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5384 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5385 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5386 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5387 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5388 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5389 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5390 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5391 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5392 | CodeCompleter->includeGlobals(), |
| 5393 | CodeCompleter->loadExternal()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5394 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5395 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 5398 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 5399 | if (!CodeCompleter) |
| 5400 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5401 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5402 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5403 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5404 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5405 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5406 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5407 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5408 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5409 | // Add the names of overloadable operators. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5410 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 5411 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 5412 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5413 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5414 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5415 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 5416 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5417 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5418 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5419 | CodeCompleter->includeGlobals(), |
| 5420 | CodeCompleter->loadExternal()); |
| 5421 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5422 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5423 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5424 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5425 | |
| 5426 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5427 | Results.data(), Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 5428 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5429 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5430 | void Sema::CodeCompleteConstructorInitializer( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5431 | Decl *ConstructorD, ArrayRef<CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 5432 | if (!ConstructorD) |
| 5433 | return; |
| 5434 | |
| 5435 | AdjustDeclIfTemplate(ConstructorD); |
| 5436 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5437 | auto *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5438 | if (!Constructor) |
| 5439 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5441 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5442 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5443 | CodeCompletionContext::CCC_Symbol); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5444 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5446 | // Fill in any already-initialized fields or base classes. |
| 5447 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 5448 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5449 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5450 | if (Initializers[I]->isBaseInitializer()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5451 | InitializedBases.insert(Context.getCanonicalType( |
| 5452 | QualType(Initializers[I]->getBaseClass(), 0))); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5453 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5454 | InitializedFields.insert( |
| 5455 | cast<FieldDecl>(Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5456 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5458 | // Add completions for base classes. |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 5459 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5460 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5461 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5462 | |
| 5463 | auto GenerateCCS = [&](const NamedDecl *ND, const char *Name) { |
| 5464 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5465 | Results.getCodeCompletionTUInfo()); |
| 5466 | Builder.AddTypedTextChunk(Name); |
| 5467 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5468 | if (const auto *Function = dyn_cast<FunctionDecl>(ND)) |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5469 | AddFunctionParameterChunks(PP, Policy, Function, Builder); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5470 | else if (const auto *FunTemplDecl = dyn_cast<FunctionTemplateDecl>(ND)) |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5471 | AddFunctionParameterChunks(PP, Policy, FunTemplDecl->getTemplatedDecl(), |
| 5472 | Builder); |
| 5473 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5474 | return Builder.TakeString(); |
| 5475 | }; |
| 5476 | auto AddDefaultCtorInit = [&](const char *Name, const char *Type, |
| 5477 | const NamedDecl *ND) { |
| 5478 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5479 | Results.getCodeCompletionTUInfo()); |
| 5480 | Builder.AddTypedTextChunk(Name); |
| 5481 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5482 | Builder.AddPlaceholderChunk(Type); |
| 5483 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5484 | if (ND) { |
| 5485 | auto CCR = CodeCompletionResult( |
| 5486 | Builder.TakeString(), ND, |
| 5487 | SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration); |
| 5488 | if (isa<FieldDecl>(ND)) |
| 5489 | CCR.CursorKind = CXCursor_MemberRef; |
| 5490 | return Results.AddResult(CCR); |
| 5491 | } |
| 5492 | return Results.AddResult(CodeCompletionResult( |
| 5493 | Builder.TakeString(), |
| 5494 | SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration)); |
| 5495 | }; |
| 5496 | auto AddCtorsWithName = [&](const CXXRecordDecl *RD, unsigned int Priority, |
| 5497 | const char *Name, const FieldDecl *FD) { |
| 5498 | if (!RD) |
| 5499 | return AddDefaultCtorInit(Name, |
| 5500 | FD ? Results.getAllocator().CopyString( |
| 5501 | FD->getType().getAsString(Policy)) |
| 5502 | : Name, |
| 5503 | FD); |
| 5504 | auto Ctors = getConstructors(Context, RD); |
| 5505 | if (Ctors.begin() == Ctors.end()) |
| 5506 | return AddDefaultCtorInit(Name, Name, RD); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5507 | for (const NamedDecl *Ctor : Ctors) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5508 | auto CCR = CodeCompletionResult(GenerateCCS(Ctor, Name), RD, Priority); |
| 5509 | CCR.CursorKind = getCursorKindForDecl(Ctor); |
| 5510 | Results.AddResult(CCR); |
| 5511 | } |
| 5512 | }; |
| 5513 | auto AddBase = [&](const CXXBaseSpecifier &Base) { |
| 5514 | const char *BaseName = |
| 5515 | Results.getAllocator().CopyString(Base.getType().getAsString(Policy)); |
| 5516 | const auto *RD = Base.getType()->getAsCXXRecordDecl(); |
| 5517 | AddCtorsWithName( |
| 5518 | RD, SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration, |
| 5519 | BaseName, nullptr); |
| 5520 | }; |
| 5521 | auto AddField = [&](const FieldDecl *FD) { |
| 5522 | const char *FieldName = |
| 5523 | Results.getAllocator().CopyString(FD->getIdentifier()->getName()); |
| 5524 | const CXXRecordDecl *RD = FD->getType()->getAsCXXRecordDecl(); |
| 5525 | AddCtorsWithName( |
| 5526 | RD, SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration, |
| 5527 | FieldName, FD); |
| 5528 | }; |
| 5529 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5530 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5531 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 5532 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5533 | SawLastInitializer = |
| 5534 | !Initializers.empty() && Initializers.back()->isBaseInitializer() && |
| 5535 | Context.hasSameUnqualifiedType( |
| 5536 | Base.getType(), QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5537 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5538 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5539 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5540 | AddBase(Base); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5541 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5542 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5543 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5544 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 5545 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5546 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 5547 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5548 | SawLastInitializer = |
| 5549 | !Initializers.empty() && Initializers.back()->isBaseInitializer() && |
| 5550 | Context.hasSameUnqualifiedType( |
| 5551 | Base.getType(), QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5552 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5553 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5554 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5555 | AddBase(Base); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5556 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5557 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5558 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5559 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5560 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5561 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 5562 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5563 | SawLastInitializer = !Initializers.empty() && |
| 5564 | Initializers.back()->isAnyMemberInitializer() && |
| 5565 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5566 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5567 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5568 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5569 | if (!Field->getDeclName()) |
| 5570 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5571 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5572 | AddField(Field); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5573 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5574 | } |
| 5575 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5576 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5577 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5578 | Results.data(), Results.size()); |
| 5579 | } |
| 5580 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5581 | /// Determine whether this scope denotes a namespace. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5582 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5583 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5584 | if (!DC) |
| 5585 | return false; |
| 5586 | |
| 5587 | return DC->isFileContext(); |
| 5588 | } |
| 5589 | |
| 5590 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 5591 | bool AfterAmpersand) { |
| 5592 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5593 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5594 | CodeCompletionContext::CCC_Other); |
| 5595 | Results.EnterNewScope(); |
| 5596 | |
| 5597 | // Note what has already been captured. |
| 5598 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 5599 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5600 | for (const auto &C : Intro.Captures) { |
| 5601 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5602 | IncludedThis = true; |
| 5603 | continue; |
| 5604 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5605 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5606 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5607 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5609 | // Look for other capturable variables. |
| 5610 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 5611 | for (const auto *D : S->decls()) { |
| 5612 | const auto *Var = dyn_cast<VarDecl>(D); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5613 | if (!Var || !Var->hasLocalStorage() || Var->hasAttr<BlocksAttr>()) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5614 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5615 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5616 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 5617 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5618 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5619 | } |
| 5620 | } |
| 5621 | |
| 5622 | // Add 'this', if it would be valid. |
| 5623 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 5624 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5625 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5626 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5628 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5629 | Results.data(), Results.size()); |
| 5630 | } |
| 5631 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5632 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 5633 | /// Keyword, depending on whether NeedAt is true or false. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5634 | #define OBJC_AT_KEYWORD_NAME(NeedAt, Keyword) ((NeedAt) ? "@" Keyword : Keyword) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5636 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5637 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5638 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5639 | // Since we have an implementation, we can end it. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5640 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5641 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5642 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5643 | Results.getCodeCompletionTUInfo()); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5644 | if (LangOpts.ObjC) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5645 | // @dynamic |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5646 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5647 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5648 | Builder.AddPlaceholderChunk("property"); |
| 5649 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5650 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5651 | // @synthesize |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5652 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5653 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5654 | Builder.AddPlaceholderChunk("property"); |
| 5655 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5656 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5659 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5660 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5661 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5662 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5663 | // Since we have an interface or protocol, we can end it. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5664 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5665 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5666 | if (LangOpts.ObjC) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5667 | // @property |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5668 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "property"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5669 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5670 | // @required |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5671 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "required"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5672 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5673 | // @optional |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5674 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5675 | } |
| 5676 | } |
| 5677 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5678 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5679 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5680 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5681 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5682 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5683 | // @class name ; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5684 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5685 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5686 | Builder.AddPlaceholderChunk("name"); |
| 5687 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5689 | if (Results.includeCodePatterns()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5690 | // @interface name |
| 5691 | // FIXME: Could introduce the whole pattern, including superclasses and |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5692 | // such. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5693 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5694 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5695 | Builder.AddPlaceholderChunk("class"); |
| 5696 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5697 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5698 | // @protocol name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5699 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5700 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5701 | Builder.AddPlaceholderChunk("protocol"); |
| 5702 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5703 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5704 | // @implementation name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5705 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5706 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5707 | Builder.AddPlaceholderChunk("class"); |
| 5708 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5709 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5710 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5711 | // @compatibility_alias name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5712 | Builder.AddTypedTextChunk( |
| 5713 | OBJC_AT_KEYWORD_NAME(NeedAt, "compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5714 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5715 | Builder.AddPlaceholderChunk("alias"); |
| 5716 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5717 | Builder.AddPlaceholderChunk("class"); |
| 5718 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 5719 | |
| 5720 | if (Results.getSema().getLangOpts().Modules) { |
| 5721 | // @import name |
| 5722 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 5723 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5724 | Builder.AddPlaceholderChunk("module"); |
| 5725 | Results.AddResult(Result(Builder.TakeString())); |
| 5726 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5729 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5730 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5731 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5732 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5733 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5734 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5735 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5736 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5737 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5738 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5739 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5740 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5741 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5742 | Results.data(), Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5745 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5746 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5747 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5748 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5749 | |
| 5750 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5751 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5752 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 5753 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5754 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5755 | Builder.AddResultTypeChunk(EncodeType); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5756 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5757 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5758 | Builder.AddPlaceholderChunk("type-name"); |
| 5759 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5760 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5761 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5762 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5763 | Builder.AddResultTypeChunk("Protocol *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5764 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5765 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5766 | Builder.AddPlaceholderChunk("protocol-name"); |
| 5767 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5768 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5769 | |
| 5770 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5771 | Builder.AddResultTypeChunk("SEL"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5772 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5773 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5774 | Builder.AddPlaceholderChunk("selector"); |
| 5775 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5776 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5777 | |
| 5778 | // @"string" |
| 5779 | Builder.AddResultTypeChunk("NSString *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5780 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "\"")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5781 | Builder.AddPlaceholderChunk("string"); |
| 5782 | Builder.AddTextChunk("\""); |
| 5783 | Results.AddResult(Result(Builder.TakeString())); |
| 5784 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5785 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5786 | Builder.AddResultTypeChunk("NSArray *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5787 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5788 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5789 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 5790 | Results.AddResult(Result(Builder.TakeString())); |
| 5791 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5792 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5793 | Builder.AddResultTypeChunk("NSDictionary *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5794 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5795 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5796 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5797 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5798 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5799 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5800 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5802 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5803 | Builder.AddResultTypeChunk("id"); |
| 5804 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5805 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5806 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5807 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5810 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5811 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5812 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5813 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5814 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5815 | if (Results.includeCodePatterns()) { |
| 5816 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 5817 | // { statements } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5818 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5819 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5820 | Builder.AddPlaceholderChunk("statements"); |
| 5821 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5822 | Builder.AddTextChunk("@catch"); |
| 5823 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5824 | Builder.AddPlaceholderChunk("parameter"); |
| 5825 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5826 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5827 | Builder.AddPlaceholderChunk("statements"); |
| 5828 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5829 | Builder.AddTextChunk("@finally"); |
| 5830 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5831 | Builder.AddPlaceholderChunk("statements"); |
| 5832 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5833 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5834 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5835 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5836 | // @throw |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5837 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5838 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5839 | Builder.AddPlaceholderChunk("expression"); |
| 5840 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5841 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5842 | if (Results.includeCodePatterns()) { |
| 5843 | // @synchronized ( expression ) { statements } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5844 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5845 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5846 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5847 | Builder.AddPlaceholderChunk("expression"); |
| 5848 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5849 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5850 | Builder.AddPlaceholderChunk("statements"); |
| 5851 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5852 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5853 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5854 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5855 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5856 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5857 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5858 | typedef CodeCompletionResult Result; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5859 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "private"))); |
| 5860 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "protected"))); |
| 5861 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "public"))); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5862 | if (LangOpts.ObjC) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5863 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5867 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5868 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5869 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5870 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5871 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5872 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5873 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5874 | Results.data(), Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
| 5877 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5878 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5879 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5880 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5881 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5882 | AddObjCStatementResults(Results, false); |
| 5883 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5884 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5885 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5886 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5890 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5891 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5892 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5893 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5894 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5895 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5896 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5897 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5900 | /// Determine whether the addition of the given flag to an Objective-C |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5901 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5902 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5903 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5904 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5905 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5906 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5907 | Attributes |= NewFlag; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5908 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5909 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5910 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 5911 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5912 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5913 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5914 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5915 | unsigned AssignCopyRetMask = |
| 5916 | Attributes & |
| 5917 | (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 5918 | ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain | |
| 5919 | ObjCDeclSpec::DQ_PR_strong | ObjCDeclSpec::DQ_PR_weak); |
| 5920 | if (AssignCopyRetMask && AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5921 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5922 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5923 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5924 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 5925 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5926 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5928 | return false; |
| 5929 | } |
| 5930 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5931 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5932 | if (!CodeCompleter) |
| 5933 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5934 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5935 | unsigned Attributes = ODS.getPropertyAttributes(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5936 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5937 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5938 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5939 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5940 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5941 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5942 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5943 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5944 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5945 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5946 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 5947 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5948 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5949 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5950 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5951 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5952 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5953 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5954 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5955 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5956 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5957 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5958 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 5959 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5960 | |
| 5961 | // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5962 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5963 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5964 | Results.AddResult(CodeCompletionResult("weak")); |
| 5965 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5966 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5967 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 5968 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5969 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5970 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5971 | Setter.AddPlaceholderChunk("method"); |
| 5972 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5973 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5974 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5975 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 5976 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5977 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5978 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5979 | Getter.AddPlaceholderChunk("method"); |
| 5980 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5981 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5982 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 5983 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 5984 | Results.AddResult(CodeCompletionResult("nullable")); |
| 5985 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 5986 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 5987 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5988 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5989 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5990 | Results.data(), Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5991 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5992 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5993 | /// Describes the kind of Objective-C method that we want to find |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5994 | /// via code completion. |
| 5995 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 5996 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 5997 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5998 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5999 | }; |
| 6000 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6001 | static bool isAcceptableObjCSelector(Selector Sel, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6002 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6003 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6004 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6005 | if (NumSelIdents > Sel.getNumArgs()) |
| 6006 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6007 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6008 | switch (WantKind) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6009 | case MK_Any: |
| 6010 | break; |
| 6011 | case MK_ZeroArgSelector: |
| 6012 | return Sel.isUnarySelector(); |
| 6013 | case MK_OneArgSelector: |
| 6014 | return Sel.getNumArgs() == 1; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6015 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6016 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6017 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 6018 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6019 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6020 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 6021 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 6022 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6023 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6024 | return true; |
| 6025 | } |
| 6026 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6027 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 6028 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6029 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6030 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6031 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6032 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6033 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6034 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6035 | /// A set of selectors, which is used to avoid introducing multiple |
| 6036 | /// completions with the same selector into the result set. |
| 6037 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6038 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6039 | /// Add all of the Objective-C methods in the given Objective-C |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6040 | /// container to the set of results. |
| 6041 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6042 | /// The container will be a class, protocol, category, or implementation of |
| 6043 | /// any of the above. This mether will recurse to include methods from |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6044 | /// the superclasses of classes along with their categories, protocols, and |
| 6045 | /// implementations. |
| 6046 | /// |
| 6047 | /// \param Container the container in which we'll look to find methods. |
| 6048 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 6049 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 6050 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6051 | /// |
| 6052 | /// \param CurContext the context in which we're performing the lookup that |
| 6053 | /// finds methods. |
| 6054 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6055 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 6056 | /// when it has the same number of parameters as we have selector identifiers. |
| 6057 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6058 | /// \param Results the structure into which we'll add results. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6059 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 6060 | bool WantInstanceMethods, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6061 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6062 | DeclContext *CurContext, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6063 | VisitedSelectorSet &Selectors, bool AllowSameLength, |
| 6064 | ResultBuilder &Results, bool InOriginalClass = true, |
| 6065 | bool IsRootClass = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6066 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6067 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 6068 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6069 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6070 | for (ObjCMethodDecl *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 6071 | // The instance methods on the root class can be messaged via the |
| 6072 | // metaclass. |
| 6073 | if (M->isInstanceMethod() == WantInstanceMethods || |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6074 | (IsRootClass && !WantInstanceMethods)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6075 | // Check whether the selector identifiers we've been given are a |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 6076 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6077 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 6078 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6079 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6080 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6081 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6082 | |
| 6083 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6084 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6085 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6086 | if (!InOriginalClass) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 6087 | setInBaseClass(R); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 6088 | Results.MaybeAddResult(R, CurContext); |
| 6089 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6090 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6091 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 6092 | // Visit the protocols of protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6093 | if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6094 | if (Protocol->hasDefinition()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6095 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 6096 | Protocol->getReferencedProtocols(); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6097 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6098 | E = Protocols.end(); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6099 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6100 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6101 | Selectors, AllowSameLength, Results, false, IsRootClass); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6102 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 6103 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6104 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6105 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6106 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6107 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6108 | // Add methods in protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6109 | for (ObjCProtocolDecl *I : IFace->protocols()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6110 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6111 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 6112 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6113 | // Add methods in categories. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6114 | for (ObjCCategoryDecl *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6115 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6116 | CurContext, Selectors, AllowSameLength, Results, |
| 6117 | InOriginalClass, IsRootClass); |
| 6118 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6119 | // Add a categories protocol methods. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6120 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 6121 | CatDecl->getReferencedProtocols(); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6122 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6123 | E = Protocols.end(); |
| 6124 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6125 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6126 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 6127 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6128 | // Add methods in category implementations. |
| 6129 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6130 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6131 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 6132 | IsRootClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6133 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6134 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6135 | // Add methods in superclass. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6136 | // Avoid passing in IsRootClass since root classes won't have super classes. |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6137 | if (IFace->getSuperClass()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6138 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 6139 | SelIdents, CurContext, Selectors, AllowSameLength, Results, |
| 6140 | /*IsRootClass=*/false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6141 | |
| 6142 | // Add methods in our implementation, if any. |
| 6143 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6144 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6145 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 6146 | IsRootClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6149 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6150 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6151 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6152 | if (!Class) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6153 | if (ObjCCategoryDecl *Category = |
| 6154 | dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6155 | Class = Category->getClassInterface(); |
| 6156 | |
| 6157 | if (!Class) |
| 6158 | return; |
| 6159 | } |
| 6160 | |
| 6161 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6162 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6163 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6164 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6165 | Results.EnterNewScope(); |
| 6166 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6167 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6168 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6169 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6170 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6171 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6172 | Results.data(), Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6175 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6176 | // Try to find the interface where setters might live. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6177 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6178 | if (!Class) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6179 | if (ObjCCategoryDecl *Category = |
| 6180 | dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6181 | Class = Category->getClassInterface(); |
| 6182 | |
| 6183 | if (!Class) |
| 6184 | return; |
| 6185 | } |
| 6186 | |
| 6187 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6188 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6189 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6190 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6191 | Results.EnterNewScope(); |
| 6192 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6193 | VisitedSelectorSet Selectors; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6194 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, Selectors, |
| 6195 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6196 | |
| 6197 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6198 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6199 | Results.data(), Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6202 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 6203 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6204 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6205 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6206 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6207 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6209 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 6210 | bool AddedInOut = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6211 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6212 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 6213 | Results.AddResult("in"); |
| 6214 | Results.AddResult("inout"); |
| 6215 | AddedInOut = true; |
| 6216 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6217 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6218 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 6219 | Results.AddResult("out"); |
| 6220 | if (!AddedInOut) |
| 6221 | Results.AddResult("inout"); |
| 6222 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6223 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6224 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 6225 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6226 | Results.AddResult("bycopy"); |
| 6227 | Results.AddResult("byref"); |
| 6228 | Results.AddResult("oneway"); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6229 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6230 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 6231 | Results.AddResult("nonnull"); |
| 6232 | Results.AddResult("nullable"); |
| 6233 | Results.AddResult("null_unspecified"); |
| 6234 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6235 | |
| 6236 | // If we're completing the return type of an Objective-C method and the |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6237 | // identifier IBAction refers to a macro, provide a completion item for |
| 6238 | // an action, e.g., |
| 6239 | // IBAction)<#selector#>:(id)sender |
| 6240 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 6241 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6242 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6243 | Results.getCodeCompletionTUInfo(), |
| 6244 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6245 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6246 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6247 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6248 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 6249 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6250 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6251 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6252 | Builder.AddTextChunk("sender"); |
| 6253 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 6254 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 6255 | |
| 6256 | // If we're completing the return type, provide 'instancetype'. |
| 6257 | if (!IsParameter) { |
| 6258 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 6259 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6260 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6261 | // Add various builtin type names and specifiers. |
| 6262 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 6263 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6265 | // Add the various type names |
| 6266 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 6267 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 6268 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 6269 | CodeCompleter->includeGlobals(), |
| 6270 | CodeCompleter->loadExternal()); |
| 6271 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6272 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 6273 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6274 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6275 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6276 | Results.data(), Results.size()); |
| 6277 | } |
| 6278 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6279 | /// When we have an expression with type "id", we may assume |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6280 | /// that it has some more-specific class type based on knowledge of |
| 6281 | /// common uses of Objective-C. This routine returns that class type, |
| 6282 | /// or NULL if no better result could be determined. |
| 6283 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6284 | auto *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6285 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6286 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6287 | |
| 6288 | Selector Sel = Msg->getSelector(); |
| 6289 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6290 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6291 | |
| 6292 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 6293 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6294 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6295 | |
| 6296 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 6297 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6298 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6299 | |
| 6300 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6301 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 6302 | switch (Msg->getReceiverKind()) { |
| 6303 | case ObjCMessageExpr::Class: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6304 | if (const ObjCObjectType *ObjType = |
| 6305 | Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6306 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 6307 | break; |
| 6308 | |
| 6309 | case ObjCMessageExpr::Instance: { |
| 6310 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 6311 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 6312 | IFace = Ptr->getInterfaceDecl(); |
| 6313 | break; |
| 6314 | } |
| 6315 | |
| 6316 | case ObjCMessageExpr::SuperInstance: |
| 6317 | case ObjCMessageExpr::SuperClass: |
| 6318 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6322 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6323 | |
| 6324 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 6325 | if (Method->isInstanceMethod()) |
| 6326 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6327 | .Case("retain", IFace) |
| 6328 | .Case("strong", IFace) |
| 6329 | .Case("autorelease", IFace) |
| 6330 | .Case("copy", IFace) |
| 6331 | .Case("copyWithZone", IFace) |
| 6332 | .Case("mutableCopy", IFace) |
| 6333 | .Case("mutableCopyWithZone", IFace) |
| 6334 | .Case("awakeFromCoder", IFace) |
| 6335 | .Case("replacementObjectFromCoder", IFace) |
| 6336 | .Case("class", IFace) |
| 6337 | .Case("classForCoder", IFace) |
| 6338 | .Case("superclass", Super) |
| 6339 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6340 | |
| 6341 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6342 | .Case("new", IFace) |
| 6343 | .Case("alloc", IFace) |
| 6344 | .Case("allocWithZone", IFace) |
| 6345 | .Case("class", IFace) |
| 6346 | .Case("superclass", Super) |
| 6347 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6350 | // Add a special completion for a message send to "super", which fills in the |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6351 | // most likely case of forwarding all of our arguments to the superclass |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6352 | // function. |
| 6353 | /// |
| 6354 | /// \param S The semantic analysis object. |
| 6355 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 6356 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6357 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 6358 | /// |
| 6359 | /// \param SelIdents The identifiers in the selector that have already been |
| 6360 | /// provided as arguments for a send to "super". |
| 6361 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6362 | /// \param Results The set of results to augment. |
| 6363 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6364 | /// \returns the Objective-C method declaration that would be invoked by |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6365 | /// this "super" completion. If NULL, no completion was added. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6366 | static ObjCMethodDecl * |
| 6367 | AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 6368 | ArrayRef<IdentifierInfo *> SelIdents, |
| 6369 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6370 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 6371 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6372 | return nullptr; |
| 6373 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6374 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 6375 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6376 | return nullptr; |
| 6377 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6378 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6379 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6380 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 6381 | // Check in the class |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6382 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6383 | CurMethod->isInstanceMethod()); |
| 6384 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6385 | // Check in categories or class extensions. |
| 6386 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 6387 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6388 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6389 | CurMethod->isInstanceMethod()))) |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6390 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6391 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6392 | } |
| 6393 | } |
| 6394 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6395 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6396 | return nullptr; |
| 6397 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6398 | // Check whether the superclass method has the same signature. |
| 6399 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 6400 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6401 | return nullptr; |
| 6402 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6403 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6404 | CurPEnd = CurMethod->param_end(), |
| 6405 | SuperP = SuperMethod->param_begin(); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6406 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 6407 | // Make sure the parameter types are compatible. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6408 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6409 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6410 | return nullptr; |
| 6411 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6412 | // Make sure we have a parameter name to forward! |
| 6413 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6414 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6415 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6416 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6417 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6418 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6419 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6420 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6421 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6422 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6423 | Results.getCompletionContext().getBaseType(), Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6424 | |
| 6425 | // If we need the "super" keyword, add it (plus some spacing). |
| 6426 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6427 | Builder.AddTypedTextChunk("super"); |
| 6428 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6429 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6430 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6431 | Selector Sel = CurMethod->getSelector(); |
| 6432 | if (Sel.isUnarySelector()) { |
| 6433 | if (NeedSuperKeyword) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6434 | Builder.AddTextChunk( |
| 6435 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6436 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6437 | Builder.AddTypedTextChunk( |
| 6438 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6439 | } else { |
| 6440 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 6441 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6442 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6443 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6444 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6445 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6446 | Builder.AddInformativeChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6447 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6448 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6449 | Builder.AddTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6450 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6451 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6452 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6453 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6454 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6455 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6456 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6457 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6458 | } |
| 6459 | } |
| 6460 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6461 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 6462 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 6463 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6464 | return SuperMethod; |
| 6465 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6466 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6467 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6468 | typedef CodeCompletionResult Result; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6469 | ResultBuilder Results( |
| 6470 | *this, CodeCompleter->getAllocator(), |
| 6471 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6472 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
| 6473 | getLangOpts().CPlusPlus11 |
| 6474 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 6475 | : &ResultBuilder::IsObjCMessageReceiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6476 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6477 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 6478 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 6479 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 6480 | CodeCompleter->includeGlobals(), |
| 6481 | CodeCompleter->loadExternal()); |
| 6482 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6483 | // If we are in an Objective-C method inside a class that has a superclass, |
| 6484 | // add "super" as an option. |
| 6485 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 6486 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6487 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6488 | Results.AddResult(Result("super")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6489 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6490 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6491 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6492 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6493 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 6494 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6495 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6496 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6498 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 6499 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 6500 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6501 | Results.data(), Results.size()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6504 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6505 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6506 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6507 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6508 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6509 | // Figure out which interface we're in. |
| 6510 | CDecl = CurMethod->getClassInterface(); |
| 6511 | if (!CDecl) |
| 6512 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6513 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6514 | // Find the superclass of this class. |
| 6515 | CDecl = CDecl->getSuperClass(); |
| 6516 | if (!CDecl) |
| 6517 | return; |
| 6518 | |
| 6519 | if (CurMethod->isInstanceMethod()) { |
| 6520 | // We are inside an instance method, which means that the message |
| 6521 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6522 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6523 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6524 | AtArgumentExpression, CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | // Fall through to send to the superclass in CDecl. |
| 6528 | } else { |
| 6529 | // "super" may be the name of a type or variable. Figure out which |
| 6530 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 6531 | IdentifierInfo *Super = getSuperIdentifier(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6532 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, LookupOrdinaryName); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6533 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 6534 | // "super" names an interface. Use it. |
| 6535 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6536 | if (const ObjCObjectType *Iface = |
| 6537 | Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6538 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6539 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 6540 | // "super" names an unresolved type; we can't be more specific. |
| 6541 | } else { |
| 6542 | // Assume that "super" names some kind of value and parse that way. |
| 6543 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 6544 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6545 | UnqualifiedId id; |
| 6546 | id.setIdentifier(Super, SuperLoc); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 6547 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 6548 | /*HasTrailingLParen=*/false, |
| 6549 | /*IsAddressOfOperand=*/false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6550 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6551 | SelIdents, AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | // Fall through |
| 6555 | } |
| 6556 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6557 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6558 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6559 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6560 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6561 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6562 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6565 | /// Given a set of code-completion results for the argument of a message |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6566 | /// send, determine the preferred type (if any) for that argument expression. |
| 6567 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 6568 | unsigned NumSelIdents) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6569 | typedef CodeCompletionResult Result; |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6570 | ASTContext &Context = Results.getSema().Context; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6571 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6572 | QualType PreferredType; |
| 6573 | unsigned BestPriority = CCP_Unlikely * 2; |
| 6574 | Result *ResultsData = Results.data(); |
| 6575 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 6576 | Result &R = ResultsData[I]; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6577 | if (R.Kind == Result::RK_Declaration && |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6578 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 6579 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 6580 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6581 | if (NumSelIdents <= Method->param_size()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6582 | QualType MyPreferredType = |
| 6583 | Method->parameters()[NumSelIdents - 1]->getType(); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6584 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 6585 | BestPriority = R.Priority; |
| 6586 | PreferredType = MyPreferredType; |
| 6587 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 6588 | MyPreferredType)) { |
| 6589 | PreferredType = QualType(); |
| 6590 | } |
| 6591 | } |
| 6592 | } |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | return PreferredType; |
| 6597 | } |
| 6598 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6599 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6600 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6601 | ArrayRef<IdentifierInfo *> SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6602 | bool AtArgumentExpression, bool IsSuper, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6603 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6604 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6605 | ObjCInterfaceDecl *CDecl = nullptr; |
| 6606 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6607 | // If the given name refers to an interface type, retrieve the |
| 6608 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6609 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6610 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6611 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6612 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 6613 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6614 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6615 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6616 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 6617 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6618 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6619 | |
| 6620 | // If this is a send-to-super, try to add the special "super" send |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6621 | // completion. |
| 6622 | if (IsSuper) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6623 | if (ObjCMethodDecl *SuperMethod = |
| 6624 | AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6625 | Results.Ignore(SuperMethod); |
| 6626 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6627 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6628 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6629 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6630 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6631 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6633 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6634 | if (CDecl) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6635 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, SemaRef.CurContext, |
| 6636 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6637 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6638 | // We're messaging "id" as a type; provide all class/factory methods. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6639 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6640 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6641 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6642 | if (SemaRef.getExternalSource()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6643 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6644 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6645 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6646 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6647 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6648 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6649 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6650 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6651 | } |
| 6652 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6654 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6655 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6656 | M != MEnd; ++M) { |
| 6657 | for (ObjCMethodList *MethList = &M->second.second; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6658 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6659 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6660 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6661 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6662 | Result R(MethList->getMethod(), |
| 6663 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6664 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6665 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6666 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6667 | } |
| 6668 | } |
| 6669 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6670 | |
| 6671 | Results.ExitScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6672 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6673 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6674 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6675 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6676 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6677 | bool IsSuper) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6678 | |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6679 | QualType T = this->GetTypeFromParser(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6680 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6681 | ResultBuilder Results( |
| 6682 | *this, CodeCompleter->getAllocator(), |
| 6683 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6684 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, T, |
| 6685 | SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6686 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6687 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6688 | AtArgumentExpression, IsSuper, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6689 | |
| 6690 | // If we're actually at the argument expression (rather than prior to the |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6691 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6692 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6693 | // code-complete the expression using the corresponding parameter type as |
| 6694 | // our preferred type, improving completion results. |
| 6695 | if (AtArgumentExpression) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6696 | QualType PreferredType = |
| 6697 | getPreferredArgumentTypeForMessageSend(Results, SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6698 | if (PreferredType.isNull()) |
| 6699 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6700 | else |
| 6701 | CodeCompleteExpression(S, PreferredType); |
| 6702 | return; |
| 6703 | } |
| 6704 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6705 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6706 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 6709 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6710 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6711 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6712 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6713 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6714 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6715 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6716 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6717 | // If necessary, apply function/array conversion to the receiver. |
| 6718 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6719 | if (RecExpr) { |
| 6720 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 6721 | if (Conv.isInvalid()) // conversion failed. bail. |
| 6722 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6723 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6724 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6725 | QualType ReceiverType = RecExpr |
| 6726 | ? RecExpr->getType() |
| 6727 | : Super ? Context.getObjCObjectPointerType( |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6728 | Context.getObjCInterfaceType(Super)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6729 | : Context.getObjCIdType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6730 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6731 | // If we're messaging an expression with type "id" or "Class", check |
| 6732 | // whether we know something special about the receiver that allows |
| 6733 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6734 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6735 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 6736 | if (ReceiverType->isObjCClassType()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6737 | return CodeCompleteObjCClassMessage( |
| 6738 | S, ParsedType::make(Context.getObjCInterfaceType(IFace)), SelIdents, |
| 6739 | AtArgumentExpression, Super); |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6740 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6741 | ReceiverType = |
| 6742 | Context.getObjCObjectPointerType(Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6743 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6744 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 6745 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 6746 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6747 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6748 | ReceiverType = RecExpr->getType(); |
| 6749 | } |
| 6750 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6752 | // Build the set of methods we can see. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6753 | ResultBuilder Results( |
| 6754 | *this, CodeCompleter->getAllocator(), |
| 6755 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6756 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
| 6757 | ReceiverType, SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6758 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6759 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6760 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6761 | // If this is a send-to-super, try to add the special "super" send |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6762 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6763 | if (Super) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6764 | if (ObjCMethodDecl *SuperMethod = |
| 6765 | AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6766 | Results.Ignore(SuperMethod); |
| 6767 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6768 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6769 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6770 | // others. |
| 6771 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 6772 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6773 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6774 | // Keep track of the selectors we've already added. |
| 6775 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6777 | // Handle messages to Class. This really isn't a message to an instance |
| 6778 | // method, so we treat it the same way we would treat a message send to a |
| 6779 | // class method. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6780 | if (ReceiverType->isObjCClassType() || |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6781 | ReceiverType->isObjCQualifiedClassType()) { |
| 6782 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6783 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6784 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, CurContext, |
| 6785 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6786 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6787 | } |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6788 | // Handle messages to a qualified ID ("id<foo>"). |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6789 | else if (const ObjCObjectPointerType *QualID = |
| 6790 | ReceiverType->getAsObjCQualifiedIdType()) { |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6791 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6792 | for (auto *I : QualID->quals()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6793 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, Selectors, |
| 6794 | AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6795 | } |
| 6796 | // Handle messages to a pointer to interface type. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6797 | else if (const ObjCObjectPointerType *IFacePtr = |
| 6798 | ReceiverType->getAsObjCInterfacePointerType()) { |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6799 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6800 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6801 | CurContext, Selectors, AtArgumentExpression, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6802 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6803 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6804 | for (auto *I : IFacePtr->quals()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6805 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, Selectors, |
| 6806 | AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6807 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6808 | // Handle messages to "id". |
| 6809 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6810 | // We're messaging "id", so provide all instance methods we know |
| 6811 | // about as code-completion results. |
| 6812 | |
| 6813 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6814 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6815 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6816 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6817 | I != N; ++I) { |
| 6818 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6819 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6820 | continue; |
| 6821 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6822 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6823 | } |
| 6824 | } |
| 6825 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6826 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6827 | MEnd = MethodPool.end(); |
| 6828 | M != MEnd; ++M) { |
| 6829 | for (ObjCMethodList *MethList = &M->second.first; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6830 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6831 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6832 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6833 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6834 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6835 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6836 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6837 | Result R(MethList->getMethod(), |
| 6838 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6839 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6840 | R.AllParametersAreInformative = false; |
| 6841 | Results.MaybeAddResult(R, CurContext); |
| 6842 | } |
| 6843 | } |
| 6844 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6845 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6846 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6847 | // If we're actually at the argument expression (rather than prior to the |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6848 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6849 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6850 | // code-complete the expression using the corresponding parameter type as |
| 6851 | // our preferred type, improving completion results. |
| 6852 | if (AtArgumentExpression) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6853 | QualType PreferredType = |
| 6854 | getPreferredArgumentTypeForMessageSend(Results, SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6855 | if (PreferredType.isNull()) |
| 6856 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6857 | else |
| 6858 | CodeCompleteExpression(S, PreferredType); |
| 6859 | return; |
| 6860 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6861 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6862 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6863 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6864 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6865 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6866 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6867 | DeclGroupPtrTy IterationVar) { |
| 6868 | CodeCompleteExpressionData Data; |
| 6869 | Data.ObjCCollection = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6870 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6871 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 6872 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6873 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 6874 | if (*I) |
| 6875 | Data.IgnoreDecls.push_back(*I); |
| 6876 | } |
| 6877 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6879 | CodeCompleteExpression(S, Data); |
| 6880 | } |
| 6881 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6882 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 6883 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6884 | // If we have an external source, load the entire class method |
| 6885 | // pool from the AST file. |
| 6886 | if (ExternalSource) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6887 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); I != N; |
| 6888 | ++I) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6889 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 6890 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 6891 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6892 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6893 | ReadMethodPool(Sel); |
| 6894 | } |
| 6895 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6896 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6897 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6898 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6899 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6900 | Results.EnterNewScope(); |
| 6901 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6902 | MEnd = MethodPool.end(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6903 | M != MEnd; ++M) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6904 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6905 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6906 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6907 | continue; |
| 6908 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6909 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6910 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6911 | if (Sel.isUnarySelector()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6912 | Builder.AddTypedTextChunk( |
| 6913 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6914 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6915 | continue; |
| 6916 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6917 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6918 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6919 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6920 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6921 | if (!Accumulator.empty()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6922 | Builder.AddInformativeChunk( |
| 6923 | Builder.getAllocator().CopyString(Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6924 | Accumulator.clear(); |
| 6925 | } |
| 6926 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6927 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6928 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6929 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6930 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6931 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6932 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6933 | } |
| 6934 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6935 | |
| 6936 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6937 | Results.data(), Results.size()); |
| 6938 | } |
| 6939 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6940 | /// Add all of the protocol declarations that we find in the given |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6941 | /// (translation unit) context. |
| 6942 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6943 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6944 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6945 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6946 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6947 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6948 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6949 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6950 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6951 | Results.AddResult( |
| 6952 | Result(Proto, Results.getBasePriority(Proto), nullptr), CurContext, |
| 6953 | nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6954 | } |
| 6955 | } |
| 6956 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6957 | void Sema::CodeCompleteObjCProtocolReferences( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6958 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6959 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6960 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6961 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6962 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6963 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6964 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6965 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6966 | // Tell the result set to ignore all of the protocols we have |
| 6967 | // already seen. |
| 6968 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6969 | for (const IdentifierLocPair &Pair : Protocols) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6970 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6971 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6972 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6973 | // Add all protocols. |
| 6974 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6975 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6976 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6977 | Results.ExitScope(); |
| 6978 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6979 | |
| 6980 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6981 | Results.data(), Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6985 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6986 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6987 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6988 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6989 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6990 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6991 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6992 | // Add all protocols. |
| 6993 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 6994 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6995 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6996 | Results.ExitScope(); |
| 6997 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6998 | |
| 6999 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7000 | Results.data(), Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 7001 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7002 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7003 | /// Add all of the Objective-C interface declarations that we find in |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7004 | /// the given (translation unit) context. |
| 7005 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 7006 | bool OnlyForwardDeclarations, |
| 7007 | bool OnlyUnimplemented, |
| 7008 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7009 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7010 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 7011 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 7012 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 7013 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 7014 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 7015 | (!OnlyUnimplemented || !Class->getImplementation())) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7016 | Results.AddResult( |
| 7017 | Result(Class, Results.getBasePriority(Class), nullptr), CurContext, |
| 7018 | nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7019 | } |
| 7020 | } |
| 7021 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7022 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7023 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7024 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7025 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7026 | Results.EnterNewScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7027 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7028 | if (CodeCompleter->includeGlobals()) { |
| 7029 | // Add all classes. |
| 7030 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 7031 | false, Results); |
| 7032 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7033 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7034 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7035 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7036 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7037 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 7040 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7041 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7042 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7043 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7044 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7045 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7046 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7047 | // Make sure that we ignore the class we're currently defining. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7048 | NamedDecl *CurClass = |
| 7049 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7050 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7051 | Results.Ignore(CurClass); |
| 7052 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7053 | if (CodeCompleter->includeGlobals()) { |
| 7054 | // Add all classes. |
| 7055 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 7056 | false, Results); |
| 7057 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7058 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7059 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7060 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7061 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7062 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7065 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7066 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7067 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7068 | CodeCompletionContext::CCC_ObjCImplementation); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7069 | Results.EnterNewScope(); |
| 7070 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7071 | if (CodeCompleter->includeGlobals()) { |
| 7072 | // Add all unimplemented classes. |
| 7073 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 7074 | true, Results); |
| 7075 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7076 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7077 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 7078 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7079 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7080 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 7081 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7082 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7083 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 7084 | IdentifierInfo *ClassName, |
| 7085 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7086 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7087 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7088 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7089 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 7090 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7091 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7092 | // Ignore any categories we find that have already been implemented by this |
| 7093 | // interface. |
| 7094 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7095 | NamedDecl *CurClass = |
| 7096 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
| 7097 | if (ObjCInterfaceDecl *Class = |
| 7098 | dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7099 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7100 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7101 | } |
| 7102 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7103 | // Add all of the categories we know about. |
| 7104 | Results.EnterNewScope(); |
| 7105 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7106 | for (const auto *D : TU->decls()) |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 7107 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7108 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7109 | Results.AddResult( |
| 7110 | Result(Category, Results.getBasePriority(Category), nullptr), |
| 7111 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7112 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7113 | |
| 7114 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7115 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7116 | } |
| 7117 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7118 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 7119 | IdentifierInfo *ClassName, |
| 7120 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7121 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7122 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7123 | // Find the corresponding interface. If we couldn't find the interface, the |
| 7124 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 7125 | // providing the list of all of the categories we know about. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7126 | NamedDecl *CurClass = |
| 7127 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7128 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 7129 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 7130 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7131 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7132 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7133 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 7134 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7135 | |
| 7136 | // Add all of the categories that have have corresponding interface |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7137 | // declarations in this class and any of its superclasses, except for |
| 7138 | // already-implemented categories in the class itself. |
| 7139 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 7140 | Results.EnterNewScope(); |
| 7141 | bool IgnoreImplemented = true; |
| 7142 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7143 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7144 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7145 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7146 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 7147 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7148 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7149 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7150 | Class = Class->getSuperClass(); |
| 7151 | IgnoreImplemented = false; |
| 7152 | } |
| 7153 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7154 | |
| 7155 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7156 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7157 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7158 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7159 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7160 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7161 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7162 | CodeCompleter->getCodeCompletionTUInfo(), CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7163 | |
| 7164 | // Figure out where this @synthesize lives. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7165 | ObjCContainerDecl *Container = |
| 7166 | dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| 7167 | if (!Container || (!isa<ObjCImplementationDecl>(Container) && |
| 7168 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7169 | return; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7170 | |
| 7171 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7172 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 7173 | for (const auto *D : Container->decls()) |
| 7174 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7175 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7176 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7177 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 7178 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7179 | Results.EnterNewScope(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7180 | if (ObjCImplementationDecl *ClassImpl = |
| 7181 | dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7182 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7183 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 7184 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7185 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7186 | AddObjCProperties(CCContext, |
| 7187 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7188 | false, /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 7189 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7190 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7191 | |
| 7192 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7193 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7194 | } |
| 7195 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7196 | void Sema::CodeCompleteObjCPropertySynthesizeIvar( |
| 7197 | Scope *S, IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7198 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7199 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7200 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7201 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7202 | |
| 7203 | // Figure out where this @synthesize lives. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7204 | ObjCContainerDecl *Container = |
| 7205 | dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| 7206 | if (!Container || (!isa<ObjCImplementationDecl>(Container) && |
| 7207 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7208 | return; |
| 7209 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7210 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7211 | ObjCInterfaceDecl *Class = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7212 | if (ObjCImplementationDecl *ClassImpl = |
| 7213 | dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7214 | Class = ClassImpl->getClassInterface(); |
| 7215 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7216 | Class = cast<ObjCCategoryImplDecl>(Container) |
| 7217 | ->getCategoryDecl() |
| 7218 | ->getClassInterface(); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7219 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7220 | // Determine the type of the property we're synthesizing. |
| 7221 | QualType PropertyType = Context.getObjCIdType(); |
| 7222 | if (Class) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 7223 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 7224 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7225 | PropertyType = |
| 7226 | Property->getType().getNonReferenceType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7227 | |
| 7228 | // Give preference to ivars |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7229 | Results.setPreferredType(PropertyType); |
| 7230 | } |
| 7231 | } |
| 7232 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7233 | // Add all of the instance variables in this class and its superclasses. |
| 7234 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7235 | bool SawSimilarlyNamedIvar = false; |
| 7236 | std::string NameWithPrefix; |
| 7237 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 7238 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7239 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 7240 | NameWithSuffix += '_'; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7241 | for (; Class; Class = Class->getSuperClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7242 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7243 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7244 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 7245 | CurContext, nullptr, false); |
| 7246 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7247 | // Determine whether we've seen an ivar with a name similar to the |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7248 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7249 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7250 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7251 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7252 | SawSimilarlyNamedIvar = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7253 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7254 | // Reduce the priority of this result by one, to give it a slight |
| 7255 | // advantage over other results whose names don't match so closely. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7256 | if (Results.size() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7257 | Results.data()[Results.size() - 1].Kind == |
| 7258 | CodeCompletionResult::RK_Declaration && |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7259 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 7260 | Results.data()[Results.size() - 1].Priority--; |
| 7261 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7262 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7263 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7264 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7265 | if (!SawSimilarlyNamedIvar) { |
| 7266 | // Create ivar result _propName, that the user can use to synthesize |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7267 | // an ivar of the appropriate type. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7268 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7269 | typedef CodeCompletionResult Result; |
| 7270 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7271 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7272 | Priority, CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7273 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7274 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7275 | Builder.AddResultTypeChunk( |
| 7276 | GetCompletionTypeString(PropertyType, Context, Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7277 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7278 | Results.AddResult( |
| 7279 | Result(Builder.TakeString(), Priority, CXCursor_ObjCIvarDecl)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7280 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7281 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7282 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7283 | |
| 7284 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7285 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7286 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7287 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7288 | // Mapping from selectors to the methods that implement that selector, along |
| 7289 | // with the "in original class" flag. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7290 | typedef llvm::DenseMap<Selector, |
| 7291 | llvm::PointerIntPair<ObjCMethodDecl *, 1, bool>> |
| 7292 | KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7293 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7294 | /// Find all of the methods that reside in the given container |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7295 | /// (and its superclasses, protocols, etc.) that meet the given |
| 7296 | /// criteria. Insert those methods into the map of known methods, |
| 7297 | /// indexed by selector so they can be easily found. |
| 7298 | static void FindImplementableMethods(ASTContext &Context, |
| 7299 | ObjCContainerDecl *Container, |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7300 | Optional<bool> WantInstanceMethods, |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7301 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7302 | KnownMethodsMap &KnownMethods, |
| 7303 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7304 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7305 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 7306 | if (!IFace->hasDefinition()) |
| 7307 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7308 | |
| 7309 | IFace = IFace->getDefinition(); |
| 7310 | Container = IFace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7311 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7312 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7313 | IFace->getReferencedProtocols(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7314 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7315 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7316 | I != E; ++I) |
| 7317 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7318 | KnownMethods, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7319 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7320 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7321 | for (auto *Cat : IFace->visible_categories()) { |
| 7322 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7323 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7326 | // Visit the superclass. |
| 7327 | if (IFace->getSuperClass()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7328 | FindImplementableMethods(Context, IFace->getSuperClass(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7329 | WantInstanceMethods, ReturnType, KnownMethods, |
| 7330 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7331 | } |
| 7332 | |
| 7333 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 7334 | // Recurse into protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7335 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7336 | Category->getReferencedProtocols(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7337 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7338 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7339 | I != E; ++I) |
| 7340 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7341 | KnownMethods, InOriginalClass); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7342 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7343 | // If this category is the original class, jump to the interface. |
| 7344 | if (InOriginalClass && Category->getClassInterface()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7345 | FindImplementableMethods(Context, Category->getClassInterface(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7346 | WantInstanceMethods, ReturnType, KnownMethods, |
| 7347 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7351 | // Make sure we have a definition; that's what we'll walk. |
| 7352 | if (!Protocol->hasDefinition()) |
| 7353 | return; |
| 7354 | Protocol = Protocol->getDefinition(); |
| 7355 | Container = Protocol; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7356 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7357 | // Recurse into protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7358 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7359 | Protocol->getReferencedProtocols(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7360 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7361 | E = Protocols.end(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7362 | I != E; ++I) |
| 7363 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 7364 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | // Add methods in this container. This operation occurs last because |
| 7368 | // we want the methods from this container to override any methods |
| 7369 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 7370 | for (auto *M : Container->methods()) { |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7371 | if (!WantInstanceMethods || M->isInstanceMethod() == *WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7372 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7373 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7374 | continue; |
| 7375 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7376 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 7377 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7378 | } |
| 7379 | } |
| 7380 | } |
| 7381 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7382 | /// Add the parenthesized return or parameter type chunk to a code |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7383 | /// completion string. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7384 | static void AddObjCPassingTypeChunk(QualType Type, unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7385 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7386 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7387 | CodeCompletionBuilder &Builder) { |
| 7388 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7389 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7390 | if (!Quals.empty()) |
| 7391 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7392 | Builder.AddTextChunk( |
| 7393 | GetCompletionTypeString(Type, Context, Policy, Builder.getAllocator())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7394 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7395 | } |
| 7396 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7397 | /// Determine whether the given class is or inherits from a class by |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7398 | /// the given name. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7399 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7400 | if (!Class) |
| 7401 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7402 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7403 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 7404 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7405 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7406 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 7407 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7408 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7409 | /// Add code completions for Objective-C Key-Value Coding (KVC) and |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7410 | /// Key-Value Observing (KVO). |
| 7411 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 7412 | bool IsInstanceMethod, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7413 | QualType ReturnType, ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7414 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7415 | ResultBuilder &Results) { |
| 7416 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 7417 | if (!PropName || PropName->getLength() == 0) |
| 7418 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7419 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7420 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 7421 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7422 | // Builder that will create each code completion. |
| 7423 | typedef CodeCompletionResult Result; |
| 7424 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7425 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7426 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7427 | // The selector table. |
| 7428 | SelectorTable &Selectors = Context.Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7429 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7430 | // The property name, copied into the code completion allocation region |
| 7431 | // on demand. |
| 7432 | struct KeyHolder { |
| 7433 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7434 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7435 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7436 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7437 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7438 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7439 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7440 | operator const char *() { |
| 7441 | if (CopiedKey) |
| 7442 | return CopiedKey; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7443 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7444 | return CopiedKey = Allocator.CopyString(Key); |
| 7445 | } |
| 7446 | } Key(Allocator, PropName->getName()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7447 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7448 | // The uppercased name of the property name. |
| 7449 | std::string UpperKey = PropName->getName(); |
| 7450 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 7451 | UpperKey[0] = toUppercase(UpperKey[0]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7452 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7453 | bool ReturnTypeMatchesProperty = |
| 7454 | ReturnType.isNull() || |
| 7455 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 7456 | Property->getType()); |
| 7457 | bool ReturnTypeMatchesVoid = ReturnType.isNull() || ReturnType->isVoidType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7458 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7459 | // Add the normal accessor -(type)key. |
| 7460 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7461 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7462 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 7463 | if (ReturnType.isNull()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7464 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, Context, Policy, |
| 7465 | Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7466 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7467 | Builder.AddTypedTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7468 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7469 | CXCursor_ObjCInstanceMethodDecl)); |
| 7470 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7471 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7472 | // If we have an integral or boolean property (or the user has provided |
| 7473 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 7474 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7475 | ((!ReturnType.isNull() && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7476 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7477 | (ReturnType.isNull() && (Property->getType()->isIntegerType() || |
| 7478 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7479 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7480 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7481 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7482 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7483 | if (ReturnType.isNull()) { |
| 7484 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7485 | Builder.AddTextChunk("BOOL"); |
| 7486 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7487 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7488 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7489 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7490 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7491 | CXCursor_ObjCInstanceMethodDecl)); |
| 7492 | } |
| 7493 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7494 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7495 | // Add the normal mutator. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7496 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7497 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7498 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7499 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7500 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7501 | if (ReturnType.isNull()) { |
| 7502 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7503 | Builder.AddTextChunk("void"); |
| 7504 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7505 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7506 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7507 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7508 | Builder.AddTypedTextChunk(":"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7509 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, Context, Policy, |
| 7510 | Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7511 | Builder.AddTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7512 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7513 | CXCursor_ObjCInstanceMethodDecl)); |
| 7514 | } |
| 7515 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7516 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7517 | // Indexed and unordered accessors |
| 7518 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 7519 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 7520 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 7521 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7522 | if (const auto *ObjCPointer = |
| 7523 | Property->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7524 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 7525 | // If this interface type is not provably derived from a known |
| 7526 | // collection, penalize the corresponding completions. |
| 7527 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7528 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7529 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 7530 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7531 | } |
| 7532 | |
| 7533 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7534 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7535 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 7536 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7537 | } |
| 7538 | } |
| 7539 | } else { |
| 7540 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7541 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7542 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7543 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7544 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7545 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7546 | // Add -(NSUInteger)countOf<key> |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7547 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7548 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7549 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7550 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7551 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7552 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7553 | if (ReturnType.isNull()) { |
| 7554 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7555 | Builder.AddTextChunk("NSUInteger"); |
| 7556 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7557 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7558 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7559 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
| 7560 | Results.AddResult( |
| 7561 | Result(Builder.TakeString(), |
| 7562 | std::min(IndexedGetterPriority, UnorderedGetterPriority), |
| 7563 | CXCursor_ObjCInstanceMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7564 | } |
| 7565 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7566 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7567 | // Indexed getters |
| 7568 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 7569 | if (IsInstanceMethod && |
| 7570 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7571 | std::string SelectorName = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7572 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7573 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7574 | if (ReturnType.isNull()) { |
| 7575 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7576 | Builder.AddTextChunk("id"); |
| 7577 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7578 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7579 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7580 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7581 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7582 | Builder.AddTextChunk("NSUInteger"); |
| 7583 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7584 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7585 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7586 | CXCursor_ObjCInstanceMethodDecl)); |
| 7587 | } |
| 7588 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7589 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7590 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 7591 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7592 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7593 | (ReturnType->isObjCObjectPointerType() && |
| 7594 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7595 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7596 | ->getInterfaceDecl() |
| 7597 | ->getName() == "NSArray"))) { |
| 7598 | std::string SelectorName = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7599 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7600 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7601 | if (ReturnType.isNull()) { |
| 7602 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7603 | Builder.AddTextChunk("NSArray *"); |
| 7604 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7605 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7606 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7607 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7608 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7609 | Builder.AddTextChunk("NSIndexSet *"); |
| 7610 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7611 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7612 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7613 | CXCursor_ObjCInstanceMethodDecl)); |
| 7614 | } |
| 7615 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7616 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7617 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 7618 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7619 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7620 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7621 | &Context.Idents.get("range")}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7622 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7623 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7624 | if (ReturnType.isNull()) { |
| 7625 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7626 | Builder.AddTextChunk("void"); |
| 7627 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7628 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7629 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7630 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7631 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7632 | Builder.AddPlaceholderChunk("object-type"); |
| 7633 | Builder.AddTextChunk(" **"); |
| 7634 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7635 | Builder.AddTextChunk("buffer"); |
| 7636 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7637 | Builder.AddTypedTextChunk("range:"); |
| 7638 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7639 | Builder.AddTextChunk("NSRange"); |
| 7640 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7641 | Builder.AddTextChunk("inRange"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7642 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7643 | CXCursor_ObjCInstanceMethodDecl)); |
| 7644 | } |
| 7645 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7646 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7647 | // Mutable indexed accessors |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7648 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7649 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 7650 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7651 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7652 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get("insertObject"), |
| 7653 | &Context.Idents.get(SelectorName)}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7654 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7655 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7656 | if (ReturnType.isNull()) { |
| 7657 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7658 | Builder.AddTextChunk("void"); |
| 7659 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7660 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7661 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7662 | Builder.AddTypedTextChunk("insertObject:"); |
| 7663 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7664 | Builder.AddPlaceholderChunk("object-type"); |
| 7665 | Builder.AddTextChunk(" *"); |
| 7666 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7667 | Builder.AddTextChunk("object"); |
| 7668 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7669 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7670 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7671 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7672 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7673 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7674 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7675 | CXCursor_ObjCInstanceMethodDecl)); |
| 7676 | } |
| 7677 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7678 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7679 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 7680 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7681 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7682 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7683 | &Context.Idents.get("atIndexes")}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7684 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7685 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7686 | if (ReturnType.isNull()) { |
| 7687 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7688 | Builder.AddTextChunk("void"); |
| 7689 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7690 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7692 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7693 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7694 | Builder.AddTextChunk("NSArray *"); |
| 7695 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7696 | Builder.AddTextChunk("array"); |
| 7697 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7698 | Builder.AddTypedTextChunk("atIndexes:"); |
| 7699 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7700 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7701 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7702 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7703 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7704 | CXCursor_ObjCInstanceMethodDecl)); |
| 7705 | } |
| 7706 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7707 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7708 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 7709 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7710 | std::string SelectorName = |
| 7711 | (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7712 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7713 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7714 | if (ReturnType.isNull()) { |
| 7715 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7716 | Builder.AddTextChunk("void"); |
| 7717 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7718 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7719 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7720 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7721 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7722 | Builder.AddTextChunk("NSUInteger"); |
| 7723 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7724 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7725 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7726 | CXCursor_ObjCInstanceMethodDecl)); |
| 7727 | } |
| 7728 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7729 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7730 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 7731 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7732 | std::string SelectorName = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7733 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7734 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7735 | if (ReturnType.isNull()) { |
| 7736 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7737 | Builder.AddTextChunk("void"); |
| 7738 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7739 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7740 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7741 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7742 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7743 | Builder.AddTextChunk("NSIndexSet *"); |
| 7744 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7745 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7746 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7747 | CXCursor_ObjCInstanceMethodDecl)); |
| 7748 | } |
| 7749 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7750 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7751 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 7752 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7753 | std::string SelectorName = |
| 7754 | (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
| 7755 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7756 | &Context.Idents.get("withObject")}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7757 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7758 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7759 | if (ReturnType.isNull()) { |
| 7760 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7761 | Builder.AddTextChunk("void"); |
| 7762 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7763 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7764 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7765 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7766 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7767 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7768 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7769 | Builder.AddTextChunk("index"); |
| 7770 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7771 | Builder.AddTypedTextChunk("withObject:"); |
| 7772 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7773 | Builder.AddTextChunk("id"); |
| 7774 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7775 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7776 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7777 | CXCursor_ObjCInstanceMethodDecl)); |
| 7778 | } |
| 7779 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7780 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7781 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 7782 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7783 | std::string SelectorName1 = |
| 7784 | (Twine("replace") + UpperKey + "AtIndexes").str(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7785 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7786 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName1), |
| 7787 | &Context.Idents.get(SelectorName2)}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7788 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7789 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7790 | if (ReturnType.isNull()) { |
| 7791 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7792 | Builder.AddTextChunk("void"); |
| 7793 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7794 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7795 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7796 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 7797 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7798 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7799 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7800 | Builder.AddTextChunk("indexes"); |
| 7801 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7802 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 7803 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7804 | Builder.AddTextChunk("NSArray *"); |
| 7805 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7806 | Builder.AddTextChunk("array"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7807 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7808 | CXCursor_ObjCInstanceMethodDecl)); |
| 7809 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7812 | // Unordered getters |
| 7813 | // - (NSEnumerator *)enumeratorOfKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7814 | if (IsInstanceMethod && |
| 7815 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7816 | (ReturnType->isObjCObjectPointerType() && |
| 7817 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7818 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7819 | ->getInterfaceDecl() |
| 7820 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7821 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7822 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7823 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7824 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7825 | if (ReturnType.isNull()) { |
| 7826 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7827 | Builder.AddTextChunk("NSEnumerator *"); |
| 7828 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7829 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7830 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7831 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7832 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7833 | CXCursor_ObjCInstanceMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7834 | } |
| 7835 | } |
| 7836 | |
| 7837 | // - (type *)memberOfKey:(type *)object |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7838 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7839 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7840 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7841 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7842 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7843 | if (ReturnType.isNull()) { |
| 7844 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7845 | Builder.AddPlaceholderChunk("object-type"); |
| 7846 | Builder.AddTextChunk(" *"); |
| 7847 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7848 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7849 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7850 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7851 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7852 | if (ReturnType.isNull()) { |
| 7853 | Builder.AddPlaceholderChunk("object-type"); |
| 7854 | Builder.AddTextChunk(" *"); |
| 7855 | } else { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7856 | Builder.AddTextChunk(GetCompletionTypeString( |
| 7857 | ReturnType, Context, Policy, Builder.getAllocator())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7858 | } |
| 7859 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7860 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7861 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7862 | CXCursor_ObjCInstanceMethodDecl)); |
| 7863 | } |
| 7864 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7865 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7866 | // Mutable unordered accessors |
| 7867 | // - (void)addKeyObject:(type *)object |
| 7868 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7869 | std::string SelectorName = |
| 7870 | (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7871 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7872 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7873 | if (ReturnType.isNull()) { |
| 7874 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7875 | Builder.AddTextChunk("void"); |
| 7876 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7877 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7878 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7879 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7880 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7881 | Builder.AddPlaceholderChunk("object-type"); |
| 7882 | Builder.AddTextChunk(" *"); |
| 7883 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7884 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7885 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7886 | CXCursor_ObjCInstanceMethodDecl)); |
| 7887 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7888 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7889 | |
| 7890 | // - (void)addKey:(NSSet *)objects |
| 7891 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7892 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7893 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7894 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7895 | if (ReturnType.isNull()) { |
| 7896 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7897 | Builder.AddTextChunk("void"); |
| 7898 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7899 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7900 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7901 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7902 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7903 | Builder.AddTextChunk("NSSet *"); |
| 7904 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7905 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7906 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7907 | CXCursor_ObjCInstanceMethodDecl)); |
| 7908 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7911 | // - (void)removeKeyObject:(type *)object |
| 7912 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7913 | std::string SelectorName = |
| 7914 | (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7915 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7916 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7917 | if (ReturnType.isNull()) { |
| 7918 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7919 | Builder.AddTextChunk("void"); |
| 7920 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7921 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7922 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7923 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7924 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7925 | Builder.AddPlaceholderChunk("object-type"); |
| 7926 | Builder.AddTextChunk(" *"); |
| 7927 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7928 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7929 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7930 | CXCursor_ObjCInstanceMethodDecl)); |
| 7931 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7932 | } |
| 7933 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7934 | // - (void)removeKey:(NSSet *)objects |
| 7935 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7936 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7937 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7938 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7939 | if (ReturnType.isNull()) { |
| 7940 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7941 | Builder.AddTextChunk("void"); |
| 7942 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7943 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7944 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7945 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7946 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7947 | Builder.AddTextChunk("NSSet *"); |
| 7948 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7949 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7950 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7951 | CXCursor_ObjCInstanceMethodDecl)); |
| 7952 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7953 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7954 | |
| 7955 | // - (void)intersectKey:(NSSet *)objects |
| 7956 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7957 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7958 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7959 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7960 | if (ReturnType.isNull()) { |
| 7961 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7962 | Builder.AddTextChunk("void"); |
| 7963 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7964 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7965 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7966 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7967 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7968 | Builder.AddTextChunk("NSSet *"); |
| 7969 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7970 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7971 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7972 | CXCursor_ObjCInstanceMethodDecl)); |
| 7973 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7974 | } |
| 7975 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7976 | // Key-Value Observing |
| 7977 | // + (NSSet *)keyPathsForValuesAffectingKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7978 | if (!IsInstanceMethod && |
| 7979 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7980 | (ReturnType->isObjCObjectPointerType() && |
| 7981 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7982 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7983 | ->getInterfaceDecl() |
| 7984 | ->getName() == "NSSet"))) { |
| 7985 | std::string SelectorName = |
| 7986 | (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7987 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7988 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7989 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7990 | if (ReturnType.isNull()) { |
| 7991 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Alex Lorenz | 71ecb07 | 2016-12-08 16:49:05 +0000 | [diff] [blame] | 7992 | Builder.AddTextChunk("NSSet<NSString *> *"); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7993 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7994 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7995 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7996 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7997 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7998 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7999 | } |
| 8000 | } |
| 8001 | |
| 8002 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 8003 | if (!IsInstanceMethod && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8004 | (ReturnType.isNull() || ReturnType->isIntegerType() || |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 8005 | ReturnType->isBooleanType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8006 | std::string SelectorName = |
| 8007 | (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 8008 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8009 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 8010 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 8011 | if (ReturnType.isNull()) { |
| 8012 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 8013 | Builder.AddTextChunk("BOOL"); |
| 8014 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 8015 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8016 | |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 8017 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8018 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8019 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8020 | } |
| 8021 | } |
| 8022 | } |
| 8023 | |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 8024 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, Optional<bool> IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 8025 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8026 | // Determine the return type of the method we're declaring, if |
| 8027 | // provided. |
| 8028 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8029 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 8030 | if (CurContext->isObjCContainer()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8031 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 8032 | IDecl = OCD; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 8033 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 8034 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8035 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8036 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8037 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8038 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 8039 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8040 | IsInImplementation = true; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8041 | } else if (ObjCCategoryImplDecl *CatImpl = |
| 8042 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8043 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8044 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 8045 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8046 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8047 | } |
| 8048 | |
| 8049 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 8050 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8051 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8052 | } |
| 8053 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 8054 | if (!SearchDecl) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8055 | HandleCodeCompleteResults(this, CodeCompleter, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8056 | CodeCompletionContext::CCC_Other, nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8057 | return; |
| 8058 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8059 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8060 | // Find all of the methods that we could declare/implement here. |
| 8061 | KnownMethodsMap KnownMethods; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8062 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, ReturnType, |
| 8063 | KnownMethods); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8064 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8065 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 8066 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8067 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8068 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8069 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8070 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8071 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8072 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8073 | MEnd = KnownMethods.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8074 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 8075 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8076 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8077 | Results.getCodeCompletionTUInfo()); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 8078 | |
| 8079 | // Add the '-'/'+' prefix if it wasn't provided yet. |
| 8080 | if (!IsInstanceMethod) { |
| 8081 | Builder.AddTextChunk(Method->isInstanceMethod() ? "-" : "+"); |
| 8082 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8083 | } |
| 8084 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8085 | // If the result type was not already provided, add it to the |
| 8086 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 8087 | if (ReturnType.isNull()) { |
| 8088 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 8089 | AttributedType::stripOuterNullability(ResTy); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8090 | AddObjCPassingTypeChunk(ResTy, Method->getObjCDeclQualifier(), Context, |
| 8091 | Policy, Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 8092 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8093 | |
| 8094 | Selector Sel = Method->getSelector(); |
| 8095 | |
| 8096 | // Add the first part of the selector to the pattern. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8097 | Builder.AddTypedTextChunk( |
| 8098 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8099 | |
| 8100 | // Add parameters to the pattern. |
| 8101 | unsigned I = 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8102 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8103 | PEnd = Method->param_end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8104 | P != PEnd; (void)++P, ++I) { |
| 8105 | // Add the part of the selector name. |
| 8106 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8107 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8108 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8109 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8110 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8111 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8112 | } else |
| 8113 | break; |
| 8114 | |
| 8115 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 8116 | QualType ParamType; |
| 8117 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 8118 | ParamType = (*P)->getType(); |
| 8119 | else |
| 8120 | ParamType = (*P)->getOriginalType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8121 | ParamType = ParamType.substObjCTypeArgs( |
| 8122 | Context, {}, ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 8123 | AttributedType::stripOuterNullability(ParamType); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8124 | AddObjCPassingTypeChunk(ParamType, (*P)->getObjCDeclQualifier(), Context, |
| 8125 | Policy, Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8126 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8127 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8128 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
| 8131 | if (Method->isVariadic()) { |
| 8132 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8133 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 8134 | Builder.AddTextChunk("..."); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8135 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8136 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 8137 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8138 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8139 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8140 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 8141 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8142 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8143 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8144 | Builder.AddTextChunk("return"); |
| 8145 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8146 | Builder.AddPlaceholderChunk("expression"); |
| 8147 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8148 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8149 | Builder.AddPlaceholderChunk("statements"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8150 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8151 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 8152 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 8155 | unsigned Priority = CCP_CodePattern; |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 8156 | auto R = Result(Builder.TakeString(), Method, Priority); |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 8157 | if (!M->second.getInt()) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 8158 | setInBaseClass(R); |
| 8159 | Results.AddResult(std::move(R)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8160 | } |
| 8161 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8162 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8163 | // the properties in this class and its categories. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8164 | if (Context.getLangOpts().ObjC) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8165 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8166 | Containers.push_back(SearchDecl); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8167 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 8168 | VisitedSelectorSet KnownSelectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8169 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8170 | MEnd = KnownMethods.end(); |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 8171 | M != MEnd; ++M) |
| 8172 | KnownSelectors.insert(M->first); |
| 8173 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8174 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 8175 | if (!IFace) |
| 8176 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 8177 | IFace = Category->getClassInterface(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8178 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 8179 | if (IFace) |
| 8180 | for (auto *Cat : IFace->visible_categories()) |
| 8181 | Containers.push_back(Cat); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 8182 | |
| 8183 | if (IsInstanceMethod) { |
| 8184 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
| 8185 | for (auto *P : Containers[I]->instance_properties()) |
| 8186 | AddObjCKeyValueCompletions(P, *IsInstanceMethod, ReturnType, Context, |
| 8187 | KnownSelectors, Results); |
| 8188 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8189 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8190 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8191 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8192 | |
| 8193 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8194 | Results.data(), Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8195 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8196 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8197 | void Sema::CodeCompleteObjCMethodDeclSelector( |
| 8198 | Scope *S, bool IsInstanceMethod, bool AtParameterName, ParsedType ReturnTy, |
| 8199 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8200 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 8201 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8202 | if (ExternalSource) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8203 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); I != N; |
| 8204 | ++I) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8205 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8206 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8207 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8208 | |
| 8209 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8210 | } |
| 8211 | } |
| 8212 | |
| 8213 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 8214 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8215 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8216 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8217 | CodeCompletionContext::CCC_Other); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8218 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8219 | if (ReturnTy) |
| 8220 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8221 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8222 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8223 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 8224 | MEnd = MethodPool.end(); |
| 8225 | M != MEnd; ++M) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8226 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first |
| 8227 | : &M->second.second; |
| 8228 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8229 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8230 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8231 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8232 | if (AtParameterName) { |
| 8233 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 8234 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8235 | if (NumSelIdents && |
| 8236 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 8237 | ParmVarDecl *Param = |
| 8238 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8239 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8240 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8241 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 8242 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8243 | Param->getIdentifier()->getName())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8244 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8245 | } |
| 8246 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8247 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8248 | continue; |
| 8249 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8250 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8251 | Result R(MethList->getMethod(), |
| 8252 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 8253 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8254 | R.AllParametersAreInformative = false; |
| 8255 | R.DeclaringEntity = true; |
| 8256 | Results.MaybeAddResult(R, CurContext); |
| 8257 | } |
| 8258 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8259 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8260 | Results.ExitScope(); |
Alex Lorenz | 847fda1 | 2017-01-03 11:56:40 +0000 | [diff] [blame] | 8261 | |
| 8262 | if (!AtParameterName && !SelIdents.empty() && |
| 8263 | SelIdents.front()->getName().startswith("init")) { |
| 8264 | for (const auto &M : PP.macros()) { |
| 8265 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") |
| 8266 | continue; |
| 8267 | Results.EnterNewScope(); |
| 8268 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8269 | Results.getCodeCompletionTUInfo()); |
| 8270 | Builder.AddTypedTextChunk( |
| 8271 | Builder.getAllocator().CopyString(M.first->getName())); |
| 8272 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, |
| 8273 | CXCursor_MacroDefinition)); |
| 8274 | Results.ExitScope(); |
| 8275 | } |
| 8276 | } |
| 8277 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8278 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8279 | Results.data(), Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8280 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8281 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8282 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8283 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8284 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8285 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8286 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8287 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8288 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8289 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8290 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8291 | Builder.AddTypedTextChunk("if"); |
| 8292 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8293 | Builder.AddPlaceholderChunk("condition"); |
| 8294 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8295 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8296 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8297 | Builder.AddTypedTextChunk("ifdef"); |
| 8298 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8299 | Builder.AddPlaceholderChunk("macro"); |
| 8300 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8301 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8302 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8303 | Builder.AddTypedTextChunk("ifndef"); |
| 8304 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8305 | Builder.AddPlaceholderChunk("macro"); |
| 8306 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8307 | |
| 8308 | if (InConditional) { |
| 8309 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8310 | Builder.AddTypedTextChunk("elif"); |
| 8311 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8312 | Builder.AddPlaceholderChunk("condition"); |
| 8313 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8314 | |
| 8315 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8316 | Builder.AddTypedTextChunk("else"); |
| 8317 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8318 | |
| 8319 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8320 | Builder.AddTypedTextChunk("endif"); |
| 8321 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8322 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8323 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8324 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8325 | Builder.AddTypedTextChunk("include"); |
| 8326 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8327 | Builder.AddTextChunk("\""); |
| 8328 | Builder.AddPlaceholderChunk("header"); |
| 8329 | Builder.AddTextChunk("\""); |
| 8330 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8331 | |
| 8332 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8333 | Builder.AddTypedTextChunk("include"); |
| 8334 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8335 | Builder.AddTextChunk("<"); |
| 8336 | Builder.AddPlaceholderChunk("header"); |
| 8337 | Builder.AddTextChunk(">"); |
| 8338 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8339 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8340 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8341 | Builder.AddTypedTextChunk("define"); |
| 8342 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8343 | Builder.AddPlaceholderChunk("macro"); |
| 8344 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8345 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8346 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8347 | Builder.AddTypedTextChunk("define"); |
| 8348 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8349 | Builder.AddPlaceholderChunk("macro"); |
| 8350 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 8351 | Builder.AddPlaceholderChunk("args"); |
| 8352 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 8353 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8354 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8355 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8356 | Builder.AddTypedTextChunk("undef"); |
| 8357 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8358 | Builder.AddPlaceholderChunk("macro"); |
| 8359 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8360 | |
| 8361 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8362 | Builder.AddTypedTextChunk("line"); |
| 8363 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8364 | Builder.AddPlaceholderChunk("number"); |
| 8365 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8366 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8367 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8368 | Builder.AddTypedTextChunk("line"); |
| 8369 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8370 | Builder.AddPlaceholderChunk("number"); |
| 8371 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8372 | Builder.AddTextChunk("\""); |
| 8373 | Builder.AddPlaceholderChunk("filename"); |
| 8374 | Builder.AddTextChunk("\""); |
| 8375 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8376 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8377 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8378 | Builder.AddTypedTextChunk("error"); |
| 8379 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8380 | Builder.AddPlaceholderChunk("message"); |
| 8381 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8382 | |
| 8383 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8384 | Builder.AddTypedTextChunk("pragma"); |
| 8385 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8386 | Builder.AddPlaceholderChunk("arguments"); |
| 8387 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8388 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8389 | if (getLangOpts().ObjC) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8390 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8391 | Builder.AddTypedTextChunk("import"); |
| 8392 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8393 | Builder.AddTextChunk("\""); |
| 8394 | Builder.AddPlaceholderChunk("header"); |
| 8395 | Builder.AddTextChunk("\""); |
| 8396 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8397 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8398 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8399 | Builder.AddTypedTextChunk("import"); |
| 8400 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8401 | Builder.AddTextChunk("<"); |
| 8402 | Builder.AddPlaceholderChunk("header"); |
| 8403 | Builder.AddTextChunk(">"); |
| 8404 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8405 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8406 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8407 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8408 | Builder.AddTypedTextChunk("include_next"); |
| 8409 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8410 | Builder.AddTextChunk("\""); |
| 8411 | Builder.AddPlaceholderChunk("header"); |
| 8412 | Builder.AddTextChunk("\""); |
| 8413 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8414 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8415 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8416 | Builder.AddTypedTextChunk("include_next"); |
| 8417 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8418 | Builder.AddTextChunk("<"); |
| 8419 | Builder.AddPlaceholderChunk("header"); |
| 8420 | Builder.AddTextChunk(">"); |
| 8421 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8422 | |
| 8423 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8424 | Builder.AddTypedTextChunk("warning"); |
| 8425 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8426 | Builder.AddPlaceholderChunk("message"); |
| 8427 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8428 | |
| 8429 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 8430 | // completions for them. And __include_macros is a Clang-internal extension |
| 8431 | // that we don't want to encourage anyone to use. |
| 8432 | |
| 8433 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 8434 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8435 | |
| 8436 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8437 | Results.data(), Results.size()); |
| 8438 | } |
| 8439 | |
| 8440 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8441 | CodeCompleteOrdinaryName(S, S->getFnParent() ? Sema::PCC_RecoveryInFunction |
| 8442 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8443 | } |
| 8444 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8445 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8446 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8447 | CodeCompleter->getCodeCompletionTUInfo(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8448 | IsDefinition ? CodeCompletionContext::CCC_MacroName |
| 8449 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8450 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8451 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8452 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8453 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8454 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8455 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8456 | MEnd = PP.macro_end(); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8457 | M != MEnd; ++M) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8458 | Builder.AddTypedTextChunk( |
| 8459 | Builder.getAllocator().CopyString(M->first->getName())); |
| 8460 | Results.AddResult(CodeCompletionResult( |
| 8461 | Builder.TakeString(), CCP_CodePattern, CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8462 | } |
| 8463 | Results.ExitScope(); |
| 8464 | } else if (IsDefinition) { |
| 8465 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 8466 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8467 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8468 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8469 | Results.data(), Results.size()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8470 | } |
| 8471 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8472 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8473 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8474 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8475 | CodeCompletionContext::CCC_PreprocessorExpression); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8476 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8477 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 8478 | AddMacroResults(PP, Results, |
| 8479 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 8480 | true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8481 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8482 | // defined (<macro>) |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8483 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8484 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8485 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8486 | Builder.AddTypedTextChunk("defined"); |
| 8487 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8488 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 8489 | Builder.AddPlaceholderChunk("macro"); |
| 8490 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 8491 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8492 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8493 | |
| 8494 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8495 | Results.data(), Results.size()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8496 | } |
| 8497 | |
| 8498 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 8499 | IdentifierInfo *Macro, |
| 8500 | MacroInfo *MacroInfo, |
| 8501 | unsigned Argument) { |
| 8502 | // FIXME: In the future, we could provide "overload" results, much like we |
| 8503 | // do for function calls. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8504 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 8505 | // Now just ignore this. There will be another code-completion callback |
| 8506 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8507 | } |
| 8508 | |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8509 | // This handles completion inside an #include filename, e.g. #include <foo/ba |
| 8510 | // We look for the directory "foo" under each directory on the include path, |
| 8511 | // list its files, and reassemble the appropriate #include. |
| 8512 | void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) { |
| 8513 | // RelDir should use /, but unescaped \ is possible on windows! |
| 8514 | // Our completions will normalize to / for simplicity, this case is rare. |
| 8515 | std::string RelDir = llvm::sys::path::convert_to_slash(Dir); |
| 8516 | // We need the native slashes for the actual file system interactions. |
| 8517 | SmallString<128> NativeRelDir = StringRef(RelDir); |
| 8518 | llvm::sys::path::native(NativeRelDir); |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 8519 | llvm::vfs::FileSystem &FS = |
| 8520 | getSourceManager().getFileManager().getVirtualFileSystem(); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8521 | |
| 8522 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8523 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8524 | CodeCompletionContext::CCC_IncludedFile); |
| 8525 | llvm::DenseSet<StringRef> SeenResults; // To deduplicate results. |
| 8526 | |
| 8527 | // Helper: adds one file or directory completion result. |
| 8528 | auto AddCompletion = [&](StringRef Filename, bool IsDirectory) { |
| 8529 | SmallString<64> TypedChunk = Filename; |
| 8530 | // Directory completion is up to the slash, e.g. <sys/ |
| 8531 | TypedChunk.push_back(IsDirectory ? '/' : Angled ? '>' : '"'); |
| 8532 | auto R = SeenResults.insert(TypedChunk); |
| 8533 | if (R.second) { // New completion |
| 8534 | const char *InternedTyped = Results.getAllocator().CopyString(TypedChunk); |
| 8535 | *R.first = InternedTyped; // Avoid dangling StringRef. |
| 8536 | CodeCompletionBuilder Builder(CodeCompleter->getAllocator(), |
| 8537 | CodeCompleter->getCodeCompletionTUInfo()); |
| 8538 | Builder.AddTypedTextChunk(InternedTyped); |
| 8539 | // The result is a "Pattern", which is pretty opaque. |
| 8540 | // We may want to include the real filename to allow smart ranking. |
| 8541 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 8542 | } |
| 8543 | }; |
| 8544 | |
| 8545 | // Helper: scans IncludeDir for nice files, and adds results for each. |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8546 | auto AddFilesFromIncludeDir = [&](StringRef IncludeDir, |
| 8547 | bool IsSystem, |
| 8548 | DirectoryLookup::LookupType_t LookupType) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8549 | llvm::SmallString<128> Dir = IncludeDir; |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8550 | if (!NativeRelDir.empty()) { |
| 8551 | if (LookupType == DirectoryLookup::LT_Framework) { |
| 8552 | // For a framework dir, #include <Foo/Bar/> actually maps to |
| 8553 | // a path of Foo.framework/Headers/Bar/. |
| 8554 | auto Begin = llvm::sys::path::begin(NativeRelDir); |
| 8555 | auto End = llvm::sys::path::end(NativeRelDir); |
| 8556 | |
| 8557 | llvm::sys::path::append(Dir, *Begin + ".framework", "Headers"); |
| 8558 | llvm::sys::path::append(Dir, ++Begin, End); |
| 8559 | } else { |
| 8560 | llvm::sys::path::append(Dir, NativeRelDir); |
| 8561 | } |
| 8562 | } |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8563 | |
| 8564 | std::error_code EC; |
| 8565 | unsigned Count = 0; |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 8566 | for (auto It = FS.dir_begin(Dir, EC); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 8567 | !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8568 | if (++Count == 2500) // If we happen to hit a huge directory, |
| 8569 | break; // bail out early so we're not too slow. |
| 8570 | StringRef Filename = llvm::sys::path::filename(It->path()); |
| 8571 | switch (It->type()) { |
| 8572 | case llvm::sys::fs::file_type::directory_file: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8573 | // All entries in a framework directory must have a ".framework" suffix, |
| 8574 | // but the suffix does not appear in the source code's include/import. |
| 8575 | if (LookupType == DirectoryLookup::LT_Framework && |
| 8576 | NativeRelDir.empty() && !Filename.consume_back(".framework")) |
| 8577 | break; |
| 8578 | |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8579 | AddCompletion(Filename, /*IsDirectory=*/true); |
| 8580 | break; |
| 8581 | case llvm::sys::fs::file_type::regular_file: |
| 8582 | // Only files that really look like headers. (Except in system dirs). |
| 8583 | if (!IsSystem) { |
| 8584 | // Header extensions from Types.def, which we can't depend on here. |
| 8585 | if (!(Filename.endswith_lower(".h") || |
| 8586 | Filename.endswith_lower(".hh") || |
| 8587 | Filename.endswith_lower(".hpp") || |
| 8588 | Filename.endswith_lower(".inc"))) |
| 8589 | break; |
| 8590 | } |
| 8591 | AddCompletion(Filename, /*IsDirectory=*/false); |
| 8592 | break; |
| 8593 | default: |
| 8594 | break; |
| 8595 | } |
| 8596 | } |
| 8597 | }; |
| 8598 | |
| 8599 | // Helper: adds results relative to IncludeDir, if possible. |
| 8600 | auto AddFilesFromDirLookup = [&](const DirectoryLookup &IncludeDir, |
| 8601 | bool IsSystem) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8602 | switch (IncludeDir.getLookupType()) { |
| 8603 | case DirectoryLookup::LT_HeaderMap: |
| 8604 | // header maps are not (currently) enumerable. |
| 8605 | break; |
| 8606 | case DirectoryLookup::LT_NormalDir: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8607 | AddFilesFromIncludeDir(IncludeDir.getDir()->getName(), IsSystem, |
| 8608 | DirectoryLookup::LT_NormalDir); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8609 | break; |
| 8610 | case DirectoryLookup::LT_Framework: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8611 | AddFilesFromIncludeDir(IncludeDir.getFrameworkDir()->getName(), IsSystem, |
| 8612 | DirectoryLookup::LT_Framework); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8613 | break; |
| 8614 | } |
| 8615 | }; |
| 8616 | |
| 8617 | // Finally with all our helpers, we can scan the include path. |
| 8618 | // Do this in standard order so deduplication keeps the right file. |
| 8619 | // (In case we decide to add more details to the results later). |
| 8620 | const auto &S = PP.getHeaderSearchInfo(); |
| 8621 | using llvm::make_range; |
| 8622 | if (!Angled) { |
| 8623 | // The current directory is on the include path for "quoted" includes. |
| 8624 | auto *CurFile = PP.getCurrentFileLexer()->getFileEntry(); |
| 8625 | if (CurFile && CurFile->getDir()) |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8626 | AddFilesFromIncludeDir(CurFile->getDir()->getName(), false, |
| 8627 | DirectoryLookup::LT_NormalDir); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8628 | for (const auto &D : make_range(S.quoted_dir_begin(), S.quoted_dir_end())) |
| 8629 | AddFilesFromDirLookup(D, false); |
| 8630 | } |
| 8631 | for (const auto &D : make_range(S.angled_dir_begin(), S.angled_dir_end())) |
Sam McCall | 3e4f5eb | 2018-10-01 11:56:42 +0000 | [diff] [blame] | 8632 | AddFilesFromDirLookup(D, false); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8633 | for (const auto &D : make_range(S.system_dir_begin(), S.system_dir_end())) |
| 8634 | AddFilesFromDirLookup(D, true); |
| 8635 | |
| 8636 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8637 | Results.data(), Results.size()); |
| 8638 | } |
| 8639 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8640 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8641 | HandleCodeCompleteResults(this, CodeCompleter, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8642 | CodeCompletionContext::CCC_NaturalLanguage, nullptr, |
| 8643 | 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8646 | void Sema::CodeCompleteAvailabilityPlatformName() { |
| 8647 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8648 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8649 | CodeCompletionContext::CCC_Other); |
| 8650 | Results.EnterNewScope(); |
| 8651 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; |
| 8652 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { |
| 8653 | Results.AddResult(CodeCompletionResult(Platform)); |
| 8654 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( |
| 8655 | Twine(Platform) + "ApplicationExtension"))); |
| 8656 | } |
| 8657 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8658 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8659 | Results.data(), Results.size()); |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8660 | } |
| 8661 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8662 | void Sema::GatherGlobalCodeCompletions( |
| 8663 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 8664 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8665 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 8666 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8667 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8668 | CodeCompletionDeclConsumer Consumer(Builder, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8669 | Context.getTranslationUnitDecl()); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 8670 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 8671 | Consumer, |
| 8672 | !CodeCompleter || CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8673 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8674 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8675 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 8676 | AddMacroResults(PP, Builder, |
| 8677 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 8678 | true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8679 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8680 | Results.clear(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8681 | Results.insert(Results.end(), Builder.data(), |
| 8682 | Builder.data() + Builder.size()); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8683 | } |