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> |
| 40 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 43 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 45 | namespace { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 46 | /// A container of code-completion results. |
| 47 | class ResultBuilder { |
| 48 | public: |
| 49 | /// The type of a name-lookup filter, which can be provided to the |
| 50 | /// name-lookup routines to specify which declarations should be included in |
| 51 | /// the result set (when it returns true) and which declarations should be |
| 52 | /// filtered out (returns false). |
| 53 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Ivan Donchevskii | 13d9054 | 2017-10-27 11:05:40 +0000 | [diff] [blame] | 54 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 55 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 56 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 57 | private: |
| 58 | /// The actual results we have found. |
| 59 | std::vector<Result> Results; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 60 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 61 | /// A record of all of the declarations we have found and placed |
| 62 | /// into the result set, used to ensure that no declaration ever gets into |
| 63 | /// the result set twice. |
| 64 | llvm::SmallPtrSet<const Decl *, 16> AllDeclsFound; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 65 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 66 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 67 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 68 | /// An entry in the shadow map, which is optimized to store |
| 69 | /// a single (declaration, index) mapping (the common case) but |
| 70 | /// can also store a list of (declaration, index) mappings. |
| 71 | class ShadowMapEntry { |
| 72 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 73 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 74 | /// Contains either the solitary NamedDecl * or a vector |
| 75 | /// of (declaration, index) pairs. |
| 76 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector *> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 77 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 78 | /// When the entry contains a single declaration, this is |
| 79 | /// the index associated with that entry. |
| 80 | unsigned SingleDeclIndex; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 82 | public: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 83 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) {} |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 84 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 85 | void Add(const NamedDecl *ND, unsigned Index) { |
| 86 | if (DeclOrVector.isNull()) { |
| 87 | // 0 - > 1 elements: just set the single element information. |
| 88 | DeclOrVector = ND; |
| 89 | SingleDeclIndex = Index; |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (const NamedDecl *PrevND = |
| 94 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
| 95 | // 1 -> 2 elements: create the vector of results and push in the |
| 96 | // existing declaration. |
| 97 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 98 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 99 | DeclOrVector = Vec; |
| 100 | } |
| 101 | |
| 102 | // Add the new element to the end of the vector. |
| 103 | DeclOrVector.get<DeclIndexPairVector *>()->push_back( |
| 104 | DeclIndexPair(ND, Index)); |
| 105 | } |
| 106 | |
| 107 | void Destroy() { |
| 108 | if (DeclIndexPairVector *Vec = |
| 109 | DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 110 | delete Vec; |
| 111 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 114 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 115 | // Iteration. |
| 116 | class iterator; |
| 117 | iterator begin() const; |
| 118 | iterator end() const; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 119 | }; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 120 | |
| 121 | /// A mapping from declaration names to the declarations that have |
| 122 | /// this name within a particular scope and their index within the list of |
| 123 | /// results. |
| 124 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| 125 | |
| 126 | /// The semantic analysis object for which results are being |
| 127 | /// produced. |
| 128 | Sema &SemaRef; |
| 129 | |
| 130 | /// The allocator used to allocate new code-completion strings. |
| 131 | CodeCompletionAllocator &Allocator; |
| 132 | |
| 133 | CodeCompletionTUInfo &CCTUInfo; |
| 134 | |
| 135 | /// If non-NULL, a filter function used to remove any code-completion |
| 136 | /// results that are not desirable. |
| 137 | LookupFilter Filter; |
| 138 | |
| 139 | /// Whether we should allow declarations as |
| 140 | /// nested-name-specifiers that would otherwise be filtered out. |
| 141 | bool AllowNestedNameSpecifiers; |
| 142 | |
| 143 | /// If set, the type that we would prefer our resulting value |
| 144 | /// declarations to have. |
| 145 | /// |
| 146 | /// Closely matching the preferred type gives a boost to a result's |
| 147 | /// priority. |
| 148 | CanQualType PreferredType; |
| 149 | |
| 150 | /// A list of shadow maps, which is used to model name hiding at |
| 151 | /// different levels of, e.g., the inheritance hierarchy. |
| 152 | std::list<ShadowMap> ShadowMaps; |
| 153 | |
| 154 | /// If we're potentially referring to a C++ member function, the set |
| 155 | /// of qualifiers applied to the object type. |
| 156 | Qualifiers ObjectTypeQualifiers; |
| 157 | |
| 158 | /// Whether the \p ObjectTypeQualifiers field is active. |
| 159 | bool HasObjectTypeQualifiers; |
| 160 | |
| 161 | /// The selector that we prefer. |
| 162 | Selector PreferredSelector; |
| 163 | |
| 164 | /// The completion context in which we are gathering results. |
| 165 | CodeCompletionContext CompletionContext; |
| 166 | |
| 167 | /// If we are in an instance method definition, the \@implementation |
| 168 | /// object. |
| 169 | ObjCImplementationDecl *ObjCImplementation; |
| 170 | |
| 171 | void AdjustResultPriorityForDecl(Result &R); |
| 172 | |
| 173 | void MaybeAddConstructorResults(Result R); |
| 174 | |
| 175 | public: |
| 176 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
| 177 | CodeCompletionTUInfo &CCTUInfo, |
| 178 | const CodeCompletionContext &CompletionContext, |
| 179 | LookupFilter Filter = nullptr) |
| 180 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 181 | Filter(Filter), AllowNestedNameSpecifiers(false), |
| 182 | HasObjectTypeQualifiers(false), CompletionContext(CompletionContext), |
| 183 | ObjCImplementation(nullptr) { |
| 184 | // If this is an Objective-C instance method definition, dig out the |
| 185 | // corresponding implementation. |
| 186 | switch (CompletionContext.getKind()) { |
| 187 | case CodeCompletionContext::CCC_Expression: |
| 188 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 189 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 190 | case CodeCompletionContext::CCC_Statement: |
| 191 | case CodeCompletionContext::CCC_Recovery: |
| 192 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 193 | if (Method->isInstanceMethod()) |
| 194 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 195 | ObjCImplementation = Interface->getImplementation(); |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// Determine the priority for a reference to the given declaration. |
| 204 | unsigned getBasePriority(const NamedDecl *D); |
| 205 | |
| 206 | /// Whether we should include code patterns in the completion |
| 207 | /// results. |
| 208 | bool includeCodePatterns() const { |
| 209 | return SemaRef.CodeCompleter && |
| 210 | SemaRef.CodeCompleter->includeCodePatterns(); |
| 211 | } |
| 212 | |
| 213 | /// Set the filter used for code-completion results. |
| 214 | void setFilter(LookupFilter Filter) { this->Filter = Filter; } |
| 215 | |
| 216 | Result *data() { return Results.empty() ? nullptr : &Results.front(); } |
| 217 | unsigned size() const { return Results.size(); } |
| 218 | bool empty() const { return Results.empty(); } |
| 219 | |
| 220 | /// Specify the preferred type. |
| 221 | void setPreferredType(QualType T) { |
| 222 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 223 | } |
| 224 | |
| 225 | /// Set the cv-qualifiers on the object type, for us in filtering |
| 226 | /// calls to member functions. |
| 227 | /// |
| 228 | /// When there are qualifiers in this set, they will be used to filter |
| 229 | /// out member functions that aren't available (because there will be a |
| 230 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 231 | /// match. |
| 232 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 233 | ObjectTypeQualifiers = Quals; |
| 234 | HasObjectTypeQualifiers = true; |
| 235 | } |
| 236 | |
| 237 | /// Set the preferred selector. |
| 238 | /// |
| 239 | /// When an Objective-C method declaration result is added, and that |
| 240 | /// method's selector matches this preferred selector, we give that method |
| 241 | /// a slight priority boost. |
| 242 | void setPreferredSelector(Selector Sel) { PreferredSelector = Sel; } |
| 243 | |
| 244 | /// Retrieve the code-completion context for which results are |
| 245 | /// being collected. |
| 246 | const CodeCompletionContext &getCompletionContext() const { |
| 247 | return CompletionContext; |
| 248 | } |
| 249 | |
| 250 | /// Specify whether nested-name-specifiers are allowed. |
| 251 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 252 | AllowNestedNameSpecifiers = Allow; |
| 253 | } |
| 254 | |
| 255 | /// Return the semantic analysis object for which we are collecting |
| 256 | /// code completion results. |
| 257 | Sema &getSema() const { return SemaRef; } |
| 258 | |
| 259 | /// Retrieve the allocator used to allocate code completion strings. |
| 260 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
| 261 | |
| 262 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
| 263 | |
| 264 | /// Determine whether the given declaration is at all interesting |
| 265 | /// as a code-completion result. |
| 266 | /// |
| 267 | /// \param ND the declaration that we are inspecting. |
| 268 | /// |
| 269 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 270 | /// only interesting when it is a nested-name-specifier. |
| 271 | bool isInterestingDecl(const NamedDecl *ND, |
| 272 | bool &AsNestedNameSpecifier) const; |
| 273 | |
| 274 | /// Check whether the result is hidden by the Hiding declaration. |
| 275 | /// |
| 276 | /// \returns true if the result is hidden and cannot be found, false if |
| 277 | /// the hidden result could still be found. When false, \p R may be |
| 278 | /// modified to describe how the result can be found (e.g., via extra |
| 279 | /// qualification). |
| 280 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 281 | const NamedDecl *Hiding); |
| 282 | |
| 283 | /// Add a new result to this result set (if it isn't already in one |
| 284 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 285 | /// redeclaration). |
| 286 | /// |
| 287 | /// \param R the result to add (if it is unique). |
| 288 | /// |
| 289 | /// \param CurContext the context in which this result will be named. |
| 290 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 291 | |
| 292 | /// Add a new result to this result set, where we already know |
| 293 | /// the hiding declaration (if any). |
| 294 | /// |
| 295 | /// \param R the result to add (if it is unique). |
| 296 | /// |
| 297 | /// \param CurContext the context in which this result will be named. |
| 298 | /// |
| 299 | /// \param Hiding the declaration that hides the result. |
| 300 | /// |
| 301 | /// \param InBaseClass whether the result was found in a base |
| 302 | /// class of the searched context. |
| 303 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 304 | bool InBaseClass); |
| 305 | |
| 306 | /// Add a new non-declaration result to this result set. |
| 307 | void AddResult(Result R); |
| 308 | |
| 309 | /// Enter into a new scope. |
| 310 | void EnterNewScope(); |
| 311 | |
| 312 | /// Exit from the current scope. |
| 313 | void ExitScope(); |
| 314 | |
| 315 | /// Ignore this declaration, if it is seen again. |
| 316 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 317 | |
| 318 | /// Add a visited context. |
| 319 | void addVisitedContext(DeclContext *Ctx) { |
| 320 | CompletionContext.addVisitedContext(Ctx); |
| 321 | } |
| 322 | |
| 323 | /// \name Name lookup predicates |
| 324 | /// |
| 325 | /// These predicates can be passed to the name lookup functions to filter the |
| 326 | /// results of name lookup. All of the predicates have the same type, so that |
| 327 | /// |
| 328 | //@{ |
| 329 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 330 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 331 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 332 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 333 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 334 | bool IsEnum(const NamedDecl *ND) const; |
| 335 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 336 | bool IsUnion(const NamedDecl *ND) const; |
| 337 | bool IsNamespace(const NamedDecl *ND) const; |
| 338 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 339 | bool IsType(const NamedDecl *ND) const; |
| 340 | bool IsMember(const NamedDecl *ND) const; |
| 341 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 342 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 343 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 344 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 345 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
| 346 | //@} |
| 347 | }; |
| 348 | } // namespace |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 349 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 350 | void PreferredTypeBuilder::enterReturn(Sema &S, SourceLocation Tok) { |
| 351 | if (isa<BlockDecl>(S.CurContext)) { |
| 352 | if (sema::BlockScopeInfo *BSI = S.getCurBlock()) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 353 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 354 | Type = BSI->ReturnType; |
| 355 | ExpectedLoc = Tok; |
| 356 | } |
| 357 | } else if (const auto *Function = dyn_cast<FunctionDecl>(S.CurContext)) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 358 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 359 | Type = Function->getReturnType(); |
| 360 | ExpectedLoc = Tok; |
| 361 | } else if (const auto *Method = dyn_cast<ObjCMethodDecl>(S.CurContext)) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 362 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 363 | Type = Method->getReturnType(); |
| 364 | ExpectedLoc = Tok; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void PreferredTypeBuilder::enterVariableInit(SourceLocation Tok, Decl *D) { |
| 369 | auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D); |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 370 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 371 | Type = VD ? VD->getType() : QualType(); |
| 372 | ExpectedLoc = Tok; |
| 373 | } |
| 374 | |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 375 | void PreferredTypeBuilder::enterFunctionArgument( |
| 376 | SourceLocation Tok, llvm::function_ref<QualType()> ComputeType) { |
| 377 | this->ComputeType = ComputeType; |
| 378 | Type = QualType(); |
| 379 | ExpectedLoc = Tok; |
| 380 | } |
| 381 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 382 | void PreferredTypeBuilder::enterParenExpr(SourceLocation Tok, |
| 383 | SourceLocation LParLoc) { |
| 384 | // expected type for parenthesized expression does not change. |
| 385 | if (ExpectedLoc == LParLoc) |
| 386 | ExpectedLoc = Tok; |
| 387 | } |
| 388 | |
| 389 | static QualType getPreferredTypeOfBinaryRHS(Sema &S, Expr *LHS, |
| 390 | tok::TokenKind Op) { |
| 391 | if (!LHS) |
| 392 | return QualType(); |
| 393 | |
| 394 | QualType LHSType = LHS->getType(); |
| 395 | if (LHSType->isPointerType()) { |
| 396 | if (Op == tok::plus || Op == tok::plusequal || Op == tok::minusequal) |
| 397 | return S.getASTContext().getPointerDiffType(); |
| 398 | // Pointer difference is more common than subtracting an int from a pointer. |
| 399 | if (Op == tok::minus) |
| 400 | return LHSType; |
| 401 | } |
| 402 | |
| 403 | switch (Op) { |
| 404 | // No way to infer the type of RHS from LHS. |
| 405 | case tok::comma: |
| 406 | return QualType(); |
| 407 | // Prefer the type of the left operand for all of these. |
| 408 | // Arithmetic operations. |
| 409 | case tok::plus: |
| 410 | case tok::plusequal: |
| 411 | case tok::minus: |
| 412 | case tok::minusequal: |
| 413 | case tok::percent: |
| 414 | case tok::percentequal: |
| 415 | case tok::slash: |
| 416 | case tok::slashequal: |
| 417 | case tok::star: |
| 418 | case tok::starequal: |
| 419 | // Assignment. |
| 420 | case tok::equal: |
| 421 | // Comparison operators. |
| 422 | case tok::equalequal: |
| 423 | case tok::exclaimequal: |
| 424 | case tok::less: |
| 425 | case tok::lessequal: |
| 426 | case tok::greater: |
| 427 | case tok::greaterequal: |
| 428 | case tok::spaceship: |
| 429 | return LHS->getType(); |
| 430 | // Binary shifts are often overloaded, so don't try to guess those. |
| 431 | case tok::greatergreater: |
| 432 | case tok::greatergreaterequal: |
| 433 | case tok::lessless: |
| 434 | case tok::lesslessequal: |
| 435 | if (LHSType->isIntegralOrEnumerationType()) |
| 436 | return S.getASTContext().IntTy; |
| 437 | return QualType(); |
| 438 | // Logical operators, assume we want bool. |
| 439 | case tok::ampamp: |
| 440 | case tok::pipepipe: |
| 441 | case tok::caretcaret: |
| 442 | return S.getASTContext().BoolTy; |
| 443 | // Operators often used for bit manipulation are typically used with the type |
| 444 | // of the left argument. |
| 445 | case tok::pipe: |
| 446 | case tok::pipeequal: |
| 447 | case tok::caret: |
| 448 | case tok::caretequal: |
| 449 | case tok::amp: |
| 450 | case tok::ampequal: |
| 451 | if (LHSType->isIntegralOrEnumerationType()) |
| 452 | return LHSType; |
| 453 | return QualType(); |
| 454 | // RHS should be a pointer to a member of the 'LHS' type, but we can't give |
| 455 | // any particular type here. |
| 456 | case tok::periodstar: |
| 457 | case tok::arrowstar: |
| 458 | return QualType(); |
| 459 | default: |
| 460 | // FIXME(ibiryukov): handle the missing op, re-add the assertion. |
| 461 | // assert(false && "unhandled binary op"); |
| 462 | return QualType(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /// Get preferred type for an argument of an unary expression. \p ContextType is |
| 467 | /// preferred type of the whole unary expression. |
| 468 | static QualType getPreferredTypeOfUnaryArg(Sema &S, QualType ContextType, |
| 469 | tok::TokenKind Op) { |
| 470 | switch (Op) { |
| 471 | case tok::exclaim: |
| 472 | return S.getASTContext().BoolTy; |
| 473 | case tok::amp: |
| 474 | if (!ContextType.isNull() && ContextType->isPointerType()) |
| 475 | return ContextType->getPointeeType(); |
| 476 | return QualType(); |
| 477 | case tok::star: |
| 478 | if (ContextType.isNull()) |
| 479 | return QualType(); |
| 480 | return S.getASTContext().getPointerType(ContextType.getNonReferenceType()); |
| 481 | case tok::plus: |
| 482 | case tok::minus: |
| 483 | case tok::tilde: |
| 484 | case tok::minusminus: |
| 485 | case tok::plusplus: |
| 486 | if (ContextType.isNull()) |
| 487 | return S.getASTContext().IntTy; |
| 488 | // leave as is, these operators typically return the same type. |
| 489 | return ContextType; |
| 490 | case tok::kw___real: |
| 491 | case tok::kw___imag: |
| 492 | return QualType(); |
| 493 | default: |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 494 | assert(false && "unhandled unary op"); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 495 | return QualType(); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void PreferredTypeBuilder::enterBinary(Sema &S, SourceLocation Tok, Expr *LHS, |
| 500 | tok::TokenKind Op) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 501 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 502 | Type = getPreferredTypeOfBinaryRHS(S, LHS, Op); |
| 503 | ExpectedLoc = Tok; |
| 504 | } |
| 505 | |
| 506 | void PreferredTypeBuilder::enterMemAccess(Sema &S, SourceLocation Tok, |
| 507 | Expr *Base) { |
| 508 | if (!Base) |
| 509 | return; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 510 | // Do we have expected type for Base? |
| 511 | if (ExpectedLoc != Base->getBeginLoc()) |
| 512 | return; |
| 513 | // Keep the expected type, only update the location. |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 514 | ExpectedLoc = Tok; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 515 | return; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void PreferredTypeBuilder::enterUnary(Sema &S, SourceLocation Tok, |
| 519 | tok::TokenKind OpKind, |
| 520 | SourceLocation OpLoc) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 521 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 522 | Type = getPreferredTypeOfUnaryArg(S, this->get(OpLoc), OpKind); |
| 523 | ExpectedLoc = Tok; |
| 524 | } |
| 525 | |
| 526 | void PreferredTypeBuilder::enterSubscript(Sema &S, SourceLocation Tok, |
| 527 | Expr *LHS) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 528 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 529 | Type = S.getASTContext().IntTy; |
| 530 | ExpectedLoc = Tok; |
| 531 | } |
| 532 | |
| 533 | void PreferredTypeBuilder::enterTypeCast(SourceLocation Tok, |
| 534 | QualType CastType) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 535 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 536 | Type = !CastType.isNull() ? CastType.getCanonicalType() : QualType(); |
| 537 | ExpectedLoc = Tok; |
| 538 | } |
| 539 | |
| 540 | void PreferredTypeBuilder::enterCondition(Sema &S, SourceLocation Tok) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 541 | ComputeType = nullptr; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 542 | Type = S.getASTContext().BoolTy; |
| 543 | ExpectedLoc = Tok; |
| 544 | } |
| 545 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 546 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 547 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 548 | unsigned SingleDeclIndex; |
| 549 | |
| 550 | public: |
| 551 | typedef DeclIndexPair value_type; |
| 552 | typedef value_type reference; |
| 553 | typedef std::ptrdiff_t difference_type; |
| 554 | typedef std::input_iterator_tag iterator_category; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 556 | class pointer { |
| 557 | DeclIndexPair Value; |
| 558 | |
| 559 | public: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 560 | pointer(const DeclIndexPair &Value) : Value(Value) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 561 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 562 | const DeclIndexPair *operator->() const { return &Value; } |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 563 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 564 | |
| 565 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 566 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 567 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 568 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 569 | |
| 570 | iterator(const DeclIndexPair *Iterator) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 571 | : DeclOrIterator(Iterator), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 572 | |
| 573 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 574 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 575 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 576 | SingleDeclIndex = 0; |
| 577 | return *this; |
| 578 | } |
| 579 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 580 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair *>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 581 | ++I; |
| 582 | DeclOrIterator = I; |
| 583 | return *this; |
| 584 | } |
| 585 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 586 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 587 | iterator tmp(*this); |
| 588 | ++(*this); |
| 589 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 590 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 591 | |
| 592 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 593 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 594 | return reference(ND, SingleDeclIndex); |
| 595 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 596 | return *DeclOrIterator.get<const DeclIndexPair *>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 599 | pointer operator->() const { return pointer(**this); } |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 600 | |
| 601 | friend bool operator==(const iterator &X, const iterator &Y) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 602 | return X.DeclOrIterator.getOpaqueValue() == |
| 603 | Y.DeclOrIterator.getOpaqueValue() && |
| 604 | X.SingleDeclIndex == Y.SingleDeclIndex; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 608 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 609 | } |
| 610 | }; |
| 611 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 612 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 613 | ResultBuilder::ShadowMapEntry::begin() const { |
| 614 | if (DeclOrVector.isNull()) |
| 615 | return iterator(); |
| 616 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 617 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 618 | return iterator(ND, SingleDeclIndex); |
| 619 | |
| 620 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 621 | } |
| 622 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 623 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 624 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 625 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 626 | return iterator(); |
| 627 | |
| 628 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 629 | } |
| 630 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 631 | /// Compute the qualification required to get from the current context |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 632 | /// (\p CurContext) to the target context (\p TargetContext). |
| 633 | /// |
| 634 | /// \param Context the AST context in which the qualification will be used. |
| 635 | /// |
| 636 | /// \param CurContext the context where an entity is being named, which is |
| 637 | /// typically based on the current scope. |
| 638 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 639 | /// \param TargetContext the context in which the named entity actually |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 640 | /// resides. |
| 641 | /// |
| 642 | /// \returns a nested name specifier that refers into the target context, or |
| 643 | /// NULL if no qualification is needed. |
| 644 | static NestedNameSpecifier * |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 645 | getRequiredQualification(ASTContext &Context, const DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 646 | const DeclContext *TargetContext) { |
| 647 | SmallVector<const DeclContext *, 4> TargetParents; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 648 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 649 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 650 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 651 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 652 | if (CommonAncestor->isTransparentContext() || |
| 653 | CommonAncestor->isFunctionOrMethod()) |
| 654 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 656 | TargetParents.push_back(CommonAncestor); |
| 657 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 658 | |
| 659 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 660 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 661 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 662 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 663 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 664 | if (!Namespace->getIdentifier()) |
| 665 | continue; |
| 666 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 667 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 668 | } else if (const auto *TD = dyn_cast<TagDecl>(Parent)) |
| 669 | Result = NestedNameSpecifier::Create( |
| 670 | Context, Result, false, Context.getTypeDeclType(TD).getTypePtr()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 671 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 672 | return Result; |
| 673 | } |
| 674 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 675 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 676 | /// 7.1.3, C++ [lib.global.names]). |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 677 | static bool isReservedName(const IdentifierInfo *Id, |
| 678 | bool doubleUnderscoreOnly = false) { |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 679 | if (Id->getLength() < 2) |
| 680 | return false; |
| 681 | const char *Name = Id->getNameStart(); |
| 682 | return Name[0] == '_' && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 683 | (Name[1] == '_' || |
| 684 | (Name[1] >= 'A' && Name[1] <= 'Z' && !doubleUnderscoreOnly)); |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // Some declarations have reserved names that we don't want to ever show. |
| 688 | // Filter out names reserved for the implementation if they come from a |
| 689 | // system header. |
| 690 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 691 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 692 | if (!Id) |
| 693 | return false; |
| 694 | |
| 695 | // Ignore reserved names for compiler provided decls. |
| 696 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 697 | return true; |
| 698 | |
| 699 | // For system headers ignore only double-underscore names. |
| 700 | // This allows for system headers providing private symbols with a single |
| 701 | // underscore. |
| 702 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 703 | SemaRef.SourceMgr.isInSystemHeader( |
| 704 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 705 | return true; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 706 | |
| 707 | return false; |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 710 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 711 | bool &AsNestedNameSpecifier) const { |
| 712 | AsNestedNameSpecifier = false; |
| 713 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 714 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 715 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 716 | |
| 717 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 718 | if (!ND->getDeclName()) |
| 719 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 720 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 721 | // Friend declarations and declarations introduced due to friends are never |
| 722 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 723 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 724 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 725 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 726 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 727 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 728 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 729 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 730 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 731 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 732 | if (isa<UsingDecl>(ND)) |
| 733 | return false; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 734 | |
| 735 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 736 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 738 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 739 | (isa<NamespaceDecl>(ND) && Filter != &ResultBuilder::IsNamespace && |
| 740 | Filter != &ResultBuilder::IsNamespaceOrAlias && Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 741 | AsNestedNameSpecifier = true; |
| 742 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 743 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 744 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 745 | // Check whether it is interesting as a nested-name-specifier. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 746 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 747 | IsNestedNameSpecifier(ND) && |
| 748 | (Filter != &ResultBuilder::IsMember || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 749 | (isa<CXXRecordDecl>(ND) && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 750 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 751 | AsNestedNameSpecifier = true; |
| 752 | return true; |
| 753 | } |
| 754 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 755 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 757 | // ... then it must be interesting! |
| 758 | return true; |
| 759 | } |
| 760 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 761 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 762 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 763 | // In C, there is no way to refer to a hidden name. |
| 764 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 765 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 766 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 767 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 768 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 769 | const DeclContext *HiddenCtx = |
| 770 | R.Declaration->getDeclContext()->getRedeclContext(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 772 | // There is no way to qualify a name declared in a function or method. |
| 773 | if (HiddenCtx->isFunctionOrMethod()) |
| 774 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 775 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 776 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 777 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 779 | // We can refer to the result with the appropriate qualification. Do it. |
| 780 | R.Hidden = true; |
| 781 | R.QualifierIsInformative = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 783 | if (!R.Qualifier) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 784 | R.Qualifier = getRequiredQualification(SemaRef.Context, CurContext, |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 785 | R.Declaration->getDeclContext()); |
| 786 | return false; |
| 787 | } |
| 788 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 789 | /// A simplified classification of types used to determine whether two |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 790 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 791 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 792 | switch (T->getTypeClass()) { |
| 793 | case Type::Builtin: |
| 794 | switch (cast<BuiltinType>(T)->getKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 795 | case BuiltinType::Void: |
| 796 | return STC_Void; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 797 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 798 | case BuiltinType::NullPtr: |
| 799 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 800 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 801 | case BuiltinType::Overload: |
| 802 | case BuiltinType::Dependent: |
| 803 | return STC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 804 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 805 | case BuiltinType::ObjCId: |
| 806 | case BuiltinType::ObjCClass: |
| 807 | case BuiltinType::ObjCSel: |
| 808 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 809 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 810 | default: |
| 811 | return STC_Arithmetic; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 812 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 814 | case Type::Complex: |
| 815 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 817 | case Type::Pointer: |
| 818 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 820 | case Type::BlockPointer: |
| 821 | return STC_Block; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 822 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 823 | case Type::LValueReference: |
| 824 | case Type::RValueReference: |
| 825 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 827 | case Type::ConstantArray: |
| 828 | case Type::IncompleteArray: |
| 829 | case Type::VariableArray: |
| 830 | case Type::DependentSizedArray: |
| 831 | return STC_Array; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 832 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 833 | case Type::DependentSizedExtVector: |
| 834 | case Type::Vector: |
| 835 | case Type::ExtVector: |
| 836 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 838 | case Type::FunctionProto: |
| 839 | case Type::FunctionNoProto: |
| 840 | return STC_Function; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 842 | case Type::Record: |
| 843 | return STC_Record; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 845 | case Type::Enum: |
| 846 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 848 | case Type::ObjCObject: |
| 849 | case Type::ObjCInterface: |
| 850 | case Type::ObjCObjectPointer: |
| 851 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 853 | default: |
| 854 | return STC_Other; |
| 855 | } |
| 856 | } |
| 857 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 858 | /// Get the type that a given expression will have if this declaration |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 859 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 860 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 861 | ND = ND->getUnderlyingDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 862 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 863 | if (const auto *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 864 | return C.getTypeDeclType(Type); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 865 | if (const auto *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 866 | return C.getObjCInterfaceType(Iface); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 868 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 869 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 870 | T = Function->getCallResultType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 871 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 872 | T = Method->getSendResultType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 873 | else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 874 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 875 | else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 876 | T = Property->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 877 | else if (const auto *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 878 | T = Value->getType(); |
Ilya Biryukov | c514ade | 2019-01-24 10:41:43 +0000 | [diff] [blame] | 879 | |
| 880 | if (T.isNull()) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 881 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 882 | |
| 883 | // Dig through references, function pointers, and block pointers to |
| 884 | // get down to the likely type of an expression when the entity is |
| 885 | // used. |
| 886 | do { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 887 | if (const auto *Ref = T->getAs<ReferenceType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 888 | T = Ref->getPointeeType(); |
| 889 | continue; |
| 890 | } |
| 891 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 892 | if (const auto *Pointer = T->getAs<PointerType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 893 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 894 | T = Pointer->getPointeeType(); |
| 895 | continue; |
| 896 | } |
| 897 | |
| 898 | break; |
| 899 | } |
| 900 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 901 | if (const auto *Block = T->getAs<BlockPointerType>()) { |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 902 | T = Block->getPointeeType(); |
| 903 | continue; |
| 904 | } |
| 905 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 906 | if (const auto *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 907 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 908 | continue; |
| 909 | } |
| 910 | |
| 911 | break; |
| 912 | } while (true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 913 | |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 914 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 917 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 918 | if (!ND) |
| 919 | return CCP_Unlikely; |
| 920 | |
| 921 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 922 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 923 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 924 | // _cmd is relatively rare |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 925 | if (const auto *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 926 | if (ImplicitParam->getIdentifier() && |
| 927 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 928 | return CCP_ObjC_cmd; |
| 929 | |
| 930 | return CCP_LocalDeclaration; |
| 931 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 932 | |
| 933 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 934 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) { |
| 935 | // Explicit destructor calls are very rare. |
| 936 | if (isa<CXXDestructorDecl>(ND)) |
| 937 | return CCP_Unlikely; |
| 938 | // Explicit operator and conversion function calls are also very rare. |
| 939 | auto DeclNameKind = ND->getDeclName().getNameKind(); |
| 940 | if (DeclNameKind == DeclarationName::CXXOperatorName || |
| 941 | DeclNameKind == DeclarationName::CXXLiteralOperatorName || |
| 942 | DeclNameKind == DeclarationName::CXXConversionFunctionName) |
| 943 | return CCP_Unlikely; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 944 | return CCP_MemberDeclaration; |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 945 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 946 | |
| 947 | // Content-based decisions. |
| 948 | if (isa<EnumConstantDecl>(ND)) |
| 949 | return CCP_Constant; |
| 950 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 951 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 952 | // message receiver, or parenthesized expression context. There, it's as |
| 953 | // likely that the user will want to write a type as other declarations. |
| 954 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 955 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 956 | CompletionContext.getKind() == |
| 957 | CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 958 | CompletionContext.getKind() == |
| 959 | CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 960 | return CCP_Type; |
| 961 | |
| 962 | return CCP_Declaration; |
| 963 | } |
| 964 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 965 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 966 | // If this is an Objective-C method declaration whose selector matches our |
| 967 | // preferred selector, give it a priority boost. |
| 968 | if (!PreferredSelector.isNull()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 969 | if (const auto *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 970 | if (PreferredSelector == Method->getSelector()) |
| 971 | R.Priority += CCD_SelectorMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 973 | // If we have a preferred type, adjust the priority for results with exactly- |
| 974 | // matching or nearly-matching types. |
| 975 | if (!PreferredType.isNull()) { |
| 976 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 977 | if (!T.isNull()) { |
| 978 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 979 | // Check for exactly-matching types (modulo qualifiers). |
| 980 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 981 | R.Priority /= CCF_ExactTypeMatch; |
| 982 | // Check for nearly-matching types, based on classification of each. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 983 | else if ((getSimplifiedTypeClass(PreferredType) == |
| 984 | getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 985 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 986 | R.Priority /= CCF_SimilarTypeMatch; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 987 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 988 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Benjamin Kramer | 756ecb8 | 2019-02-11 14:52:15 +0000 | [diff] [blame] | 991 | static DeclContext::lookup_result getConstructors(ASTContext &Context, |
| 992 | const CXXRecordDecl *Record) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 993 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 994 | DeclarationName ConstructorName = |
| 995 | Context.DeclarationNames.getCXXConstructorName( |
| 996 | Context.getCanonicalType(RecordTy)); |
| 997 | return Record->lookup(ConstructorName); |
| 998 | } |
| 999 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1000 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1001 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1002 | !CompletionContext.wantConstructorResults()) |
| 1003 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1004 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1005 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1006 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1007 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1008 | Record = ClassTemplate->getTemplatedDecl(); |
| 1009 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 1010 | // Skip specializations and partial specializations. |
| 1011 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 1012 | return; |
| 1013 | } else { |
| 1014 | // There are no constructors here. |
| 1015 | return; |
| 1016 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1017 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1018 | Record = Record->getDefinition(); |
| 1019 | if (!Record) |
| 1020 | return; |
| 1021 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1022 | for (NamedDecl *Ctor : getConstructors(SemaRef.Context, Record)) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 1023 | R.Declaration = Ctor; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1024 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 1025 | Results.push_back(R); |
| 1026 | } |
| 1027 | } |
| 1028 | |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1029 | static bool isConstructor(const Decl *ND) { |
| 1030 | if (const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1031 | ND = Tmpl->getTemplatedDecl(); |
| 1032 | return isa<CXXConstructorDecl>(ND); |
| 1033 | } |
| 1034 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1035 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 1036 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1038 | if (R.Kind != Result::RK_Declaration) { |
| 1039 | // For non-declaration results, just add the result. |
| 1040 | Results.push_back(R); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | // Look through using declarations. |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1045 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 1046 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 1047 | getBasePriority(Using->getTargetDecl()), |
| 1048 | R.Qualifier); |
| 1049 | Result.ShadowDecl = Using; |
| 1050 | MaybeAddResult(Result, CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1053 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1054 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1055 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 1056 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1057 | bool AsNestedNameSpecifier = false; |
| 1058 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 1059 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1061 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1062 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1065 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1066 | ShadowMapEntry::iterator I, IEnd; |
| 1067 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 1068 | if (NamePos != SMap.end()) { |
| 1069 | I = NamePos->second.begin(); |
| 1070 | IEnd = NamePos->second.end(); |
| 1071 | } |
| 1072 | |
| 1073 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1074 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1075 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1076 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 1077 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1078 | Results[Index].Declaration = R.Declaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1079 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1080 | // We're done. |
| 1081 | return; |
| 1082 | } |
| 1083 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1085 | // This is a new declaration in this scope. However, check whether this |
| 1086 | // declaration name is hidden by a similarly-named declaration in an outer |
| 1087 | // scope. |
| 1088 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 1089 | --SMEnd; |
| 1090 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1091 | ShadowMapEntry::iterator I, IEnd; |
| 1092 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 1093 | if (NamePos != SM->end()) { |
| 1094 | I = NamePos->second.begin(); |
| 1095 | IEnd = NamePos->second.end(); |
| 1096 | } |
| 1097 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1098 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1099 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1100 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 1101 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1102 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1104 | // Protocols are in distinct namespaces from everything else. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1105 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) || |
| 1106 | (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1107 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1108 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1109 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1110 | // 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] | 1111 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1112 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1114 | break; |
| 1115 | } |
| 1116 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1118 | // 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] | 1119 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1120 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1122 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1123 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1124 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1125 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1126 | R.Priority = CCP_NestedNameSpecifier; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1127 | } else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1128 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1130 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1131 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1132 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1133 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 1134 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1135 | R.Qualifier = |
| 1136 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1137 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1138 | R.Qualifier = NestedNameSpecifier::Create( |
| 1139 | SemaRef.Context, nullptr, false, |
| 1140 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1141 | else |
| 1142 | R.QualifierIsInformative = false; |
| 1143 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1145 | // Insert this result into the set of results and into the current shadow |
| 1146 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1147 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1148 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1150 | if (!AsNestedNameSpecifier) |
| 1151 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 1154 | static void setInBaseClass(ResultBuilder::Result &R) { |
| 1155 | R.Priority += CCD_InBaseClass; |
| 1156 | R.InBaseClass = true; |
| 1157 | } |
| 1158 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1159 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1160 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1161 | if (R.Kind != Result::RK_Declaration) { |
| 1162 | // For non-declaration results, just add the result. |
| 1163 | Results.push_back(R); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1167 | // Look through using declarations. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1168 | if (const auto *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1169 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 1170 | getBasePriority(Using->getTargetDecl()), |
| 1171 | R.Qualifier); |
| 1172 | Result.ShadowDecl = Using; |
| 1173 | AddResult(Result, CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1176 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1177 | bool AsNestedNameSpecifier = false; |
| 1178 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1179 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1181 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 1182 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1183 | return; |
| 1184 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1185 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 1186 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 1187 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1188 | // 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] | 1189 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1190 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1192 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1193 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1194 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1195 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1196 | R.Priority = CCP_NestedNameSpecifier; |
Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1197 | } else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && |
| 1198 | InBaseClass && |
| 1199 | isa<CXXRecordDecl>( |
| 1200 | R.Declaration->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1201 | R.QualifierIsInformative = true; |
| 1202 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1203 | // If this result is supposed to have an informative qualifier, add one. |
| 1204 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1205 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1206 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1207 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1208 | R.Qualifier = |
| 1209 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
| 1210 | else if (const auto *Tag = dyn_cast<TagDecl>(Ctx)) |
| 1211 | R.Qualifier = NestedNameSpecifier::Create( |
| 1212 | SemaRef.Context, nullptr, false, |
| 1213 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1214 | else |
| 1215 | R.QualifierIsInformative = false; |
| 1216 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1218 | // Adjust the priority if this result comes from a base class. |
| 1219 | if (InBaseClass) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 1220 | setInBaseClass(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1222 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1224 | if (HasObjectTypeQualifiers) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1225 | if (const auto *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1226 | if (Method->isInstance()) { |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 1227 | Qualifiers MethodQuals = Method->getMethodQualifiers(); |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1228 | if (ObjectTypeQualifiers == MethodQuals) |
| 1229 | R.Priority += CCD_ObjectQualifierMatch; |
| 1230 | else if (ObjectTypeQualifiers - MethodQuals) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1231 | // The method cannot be invoked, because doing so would drop |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1232 | // qualifiers. |
| 1233 | return; |
| 1234 | } |
| 1235 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1237 | // Insert this result into the set of results. |
| 1238 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1240 | if (!AsNestedNameSpecifier) |
| 1241 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1244 | void ResultBuilder::AddResult(Result R) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1245 | assert(R.Kind != Result::RK_Declaration && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1246 | "Declaration results need more context"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1247 | Results.push_back(R); |
| 1248 | } |
| 1249 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1250 | /// Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1251 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1252 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1253 | /// Exit from the current scope. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1254 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1255 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1256 | EEnd = ShadowMaps.back().end(); |
| 1257 | E != EEnd; ++E) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1258 | E->second.Destroy(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1260 | ShadowMaps.pop_back(); |
| 1261 | } |
| 1262 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1263 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1264 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1265 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1266 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1267 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1268 | // If name lookup finds a local extern declaration, then we are in a |
| 1269 | // context where it behaves like an ordinary name. |
| 1270 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1271 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1272 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1273 | else if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1274 | if (isa<ObjCIvarDecl>(ND)) |
| 1275 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1276 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1278 | return ND->getIdentifierNamespace() & IDNS; |
| 1279 | } |
| 1280 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1281 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1282 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1283 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1284 | ND = ND->getUnderlyingDecl(); |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1285 | if (isa<TypeDecl>(ND)) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1286 | return false; |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1287 | // Objective-C interfaces names are not filtered by this method because they |
| 1288 | // can be used in a class property expression. We can still filter out |
| 1289 | // @class declarations though. |
| 1290 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 1291 | if (!ID->getDefinition()) |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1295 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1296 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1297 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1298 | else if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1299 | if (isa<ObjCIvarDecl>(ND)) |
| 1300 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1301 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1303 | return ND->getIdentifierNamespace() & IDNS; |
| 1304 | } |
| 1305 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1306 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1307 | if (!IsOrdinaryNonTypeName(ND)) |
| 1308 | return 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1309 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1310 | if (const auto *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1311 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1312 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1314 | return false; |
| 1315 | } |
| 1316 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1317 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1318 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1319 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1320 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1321 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1322 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1323 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1324 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1325 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1326 | return (ND->getIdentifierNamespace() & IDNS) && !isa<ValueDecl>(ND) && |
| 1327 | !isa<FunctionTemplateDecl>(ND) && !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1330 | /// Determines whether the given declaration is suitable as the |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1331 | /// 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] | 1332 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1333 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1334 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1335 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1337 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1338 | } |
| 1339 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1340 | /// Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1341 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1342 | return isa<EnumDecl>(ND); |
| 1343 | } |
| 1344 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1345 | /// Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1346 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1347 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1348 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1349 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1350 | |
| 1351 | // For purposes of this check, interfaces match too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1352 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
| 1353 | return RD->getTagKind() == TTK_Class || RD->getTagKind() == TTK_Struct || |
| 1354 | RD->getTagKind() == TTK_Interface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1359 | /// Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1360 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1361 | // Allow us to find class templates, too. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1362 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1363 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1364 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1365 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1366 | return RD->getTagKind() == TTK_Union; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1367 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
| 1370 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1371 | /// Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1372 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1373 | return isa<NamespaceDecl>(ND); |
| 1374 | } |
| 1375 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1376 | /// Determines whether the given declaration is a namespace or |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1377 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1378 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1379 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1382 | /// Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1383 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1384 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1385 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1388 | /// Determines which members of a class should be visible via |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1389 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1390 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1391 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1392 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1393 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1394 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1397 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1398 | T = C.getCanonicalType(T); |
| 1399 | switch (T->getTypeClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1400 | case Type::ObjCObject: |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1401 | case Type::ObjCInterface: |
| 1402 | case Type::ObjCObjectPointer: |
| 1403 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1405 | case Type::Builtin: |
| 1406 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1407 | case BuiltinType::ObjCId: |
| 1408 | case BuiltinType::ObjCClass: |
| 1409 | case BuiltinType::ObjCSel: |
| 1410 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1412 | default: |
| 1413 | break; |
| 1414 | } |
| 1415 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1417 | default: |
| 1418 | break; |
| 1419 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1420 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1421 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1422 | return false; |
| 1423 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1424 | // FIXME: We could perform more analysis here to determine whether a |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1425 | // particular class type has any conversions to Objective-C types. For now, |
| 1426 | // just accept all class types. |
| 1427 | return T->isDependentType() || T->isRecordType(); |
| 1428 | } |
| 1429 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1430 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1431 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1432 | if (T.isNull()) |
| 1433 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1435 | T = SemaRef.Context.getBaseElementType(T); |
| 1436 | return isObjCReceiverType(SemaRef.Context, T); |
| 1437 | } |
| 1438 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1439 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture( |
| 1440 | const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1441 | if (IsObjCMessageReceiver(ND)) |
| 1442 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1443 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1444 | const auto *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1445 | if (!Var) |
| 1446 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1448 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1449 | } |
| 1450 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1451 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1452 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1453 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1454 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1456 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1457 | if (T.isNull()) |
| 1458 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1459 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1460 | T = SemaRef.Context.getBaseElementType(T); |
| 1461 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1462 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1463 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1464 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1465 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1466 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | } |
| 1469 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1470 | /// Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1471 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1472 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1473 | return isa<ObjCIvarDecl>(ND); |
| 1474 | } |
| 1475 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1476 | namespace { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1477 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1478 | /// Visible declaration consumer that adds a code-completion result |
| 1479 | /// for each visible declaration. |
| 1480 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1481 | ResultBuilder &Results; |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1482 | DeclContext *InitialLookupCtx; |
| 1483 | // NamingClass and BaseType are used for access-checking. See |
| 1484 | // Sema::IsSimplyAccessible for details. |
| 1485 | CXXRecordDecl *NamingClass; |
| 1486 | QualType BaseType; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1487 | std::vector<FixItHint> FixIts; |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1488 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1489 | public: |
| 1490 | CodeCompletionDeclConsumer( |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1491 | ResultBuilder &Results, DeclContext *InitialLookupCtx, |
| 1492 | QualType BaseType = QualType(), |
| 1493 | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
| 1494 | : Results(Results), InitialLookupCtx(InitialLookupCtx), |
| 1495 | FixIts(std::move(FixIts)) { |
| 1496 | NamingClass = llvm::dyn_cast<CXXRecordDecl>(InitialLookupCtx); |
| 1497 | // If BaseType was not provided explicitly, emulate implicit 'this->'. |
| 1498 | if (BaseType.isNull()) { |
| 1499 | auto ThisType = Results.getSema().getCurrentThisType(); |
| 1500 | if (!ThisType.isNull()) { |
| 1501 | assert(ThisType->isPointerType()); |
| 1502 | BaseType = ThisType->getPointeeType(); |
| 1503 | if (!NamingClass) |
| 1504 | NamingClass = BaseType->getAsCXXRecordDecl(); |
| 1505 | } |
| 1506 | } |
| 1507 | this->BaseType = BaseType; |
| 1508 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1509 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1510 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1511 | bool InBaseClass) override { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1512 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
Ilya Biryukov | 9839755 | 2018-12-05 17:38:39 +0000 | [diff] [blame] | 1513 | false, IsAccessible(ND, Ctx), FixIts); |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 1514 | Results.AddResult(Result, InitialLookupCtx, Hiding, InBaseClass); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1515 | } |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1516 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1517 | void EnteredContext(DeclContext *Ctx) override { |
| 1518 | Results.addVisitedContext(Ctx); |
| 1519 | } |
Ilya Biryukov | 9839755 | 2018-12-05 17:38:39 +0000 | [diff] [blame] | 1520 | |
| 1521 | private: |
| 1522 | bool IsAccessible(NamedDecl *ND, DeclContext *Ctx) { |
| 1523 | // Naming class to use for access check. In most cases it was provided |
| 1524 | // explicitly (e.g. member access (lhs.foo) or qualified lookup (X::)), |
| 1525 | // for unqualified lookup we fallback to the \p Ctx in which we found the |
| 1526 | // member. |
| 1527 | auto *NamingClass = this->NamingClass; |
| 1528 | QualType BaseType = this->BaseType; |
| 1529 | if (auto *Cls = llvm::dyn_cast_or_null<CXXRecordDecl>(Ctx)) { |
| 1530 | if (!NamingClass) |
| 1531 | NamingClass = Cls; |
| 1532 | // When we emulate implicit 'this->' in an unqualified lookup, we might |
| 1533 | // end up with an invalid naming class. In that case, we avoid emulating |
| 1534 | // 'this->' qualifier to satisfy preconditions of the access checking. |
| 1535 | if (NamingClass->getCanonicalDecl() != Cls->getCanonicalDecl() && |
| 1536 | !NamingClass->isDerivedFrom(Cls)) { |
| 1537 | NamingClass = Cls; |
| 1538 | BaseType = QualType(); |
| 1539 | } |
| 1540 | } else { |
| 1541 | // The decl was found outside the C++ class, so only ObjC access checks |
| 1542 | // apply. Those do not rely on NamingClass and BaseType, so we clear them |
| 1543 | // out. |
| 1544 | NamingClass = nullptr; |
| 1545 | BaseType = QualType(); |
| 1546 | } |
| 1547 | return Results.getSema().IsSimplyAccessible(ND, NamingClass, BaseType); |
| 1548 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1549 | }; |
| 1550 | } // namespace |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1551 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1552 | /// Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1553 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1554 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1555 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1556 | Results.AddResult(Result("short", CCP_Type)); |
| 1557 | Results.AddResult(Result("long", CCP_Type)); |
| 1558 | Results.AddResult(Result("signed", CCP_Type)); |
| 1559 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1560 | Results.AddResult(Result("void", CCP_Type)); |
| 1561 | Results.AddResult(Result("char", CCP_Type)); |
| 1562 | Results.AddResult(Result("int", CCP_Type)); |
| 1563 | Results.AddResult(Result("float", CCP_Type)); |
| 1564 | Results.AddResult(Result("double", CCP_Type)); |
| 1565 | Results.AddResult(Result("enum", CCP_Type)); |
| 1566 | Results.AddResult(Result("struct", CCP_Type)); |
| 1567 | Results.AddResult(Result("union", CCP_Type)); |
| 1568 | Results.AddResult(Result("const", CCP_Type)); |
| 1569 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1571 | if (LangOpts.C99) { |
| 1572 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1573 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1574 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1575 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1576 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1577 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1578 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1579 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1580 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1581 | if (LangOpts.CPlusPlus) { |
| 1582 | // C++-specific |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1583 | Results.AddResult( |
| 1584 | Result("bool", CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1585 | Results.AddResult(Result("class", CCP_Type)); |
| 1586 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1588 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1589 | Builder.AddTypedTextChunk("typename"); |
| 1590 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1591 | Builder.AddPlaceholderChunk("qualifier"); |
| 1592 | Builder.AddTextChunk("::"); |
| 1593 | Builder.AddPlaceholderChunk("name"); |
| 1594 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1595 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1596 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1597 | Results.AddResult(Result("auto", CCP_Type)); |
| 1598 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1599 | Results.AddResult(Result("char32_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1601 | Builder.AddTypedTextChunk("decltype"); |
| 1602 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1603 | Builder.AddPlaceholderChunk("expression"); |
| 1604 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1605 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1606 | } |
Alex Lorenz | 46eed9d | 2017-02-13 23:35:59 +0000 | [diff] [blame] | 1607 | } else |
| 1608 | Results.AddResult(Result("__auto_type", CCP_Type)); |
| 1609 | |
Richard Smith | 7b301e2 | 2018-05-24 21:51:52 +0000 | [diff] [blame] | 1610 | // GNU keywords |
| 1611 | if (LangOpts.GNUKeywords) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1612 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1613 | // Results.AddResult(Result("_Decimal32")); |
| 1614 | // Results.AddResult(Result("_Decimal64")); |
| 1615 | // Results.AddResult(Result("_Decimal128")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1616 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1617 | Builder.AddTypedTextChunk("typeof"); |
| 1618 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1619 | Builder.AddPlaceholderChunk("expression"); |
| 1620 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1622 | Builder.AddTypedTextChunk("typeof"); |
| 1623 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1624 | Builder.AddPlaceholderChunk("type"); |
| 1625 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1626 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1627 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1630 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1631 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1632 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1635 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1636 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1637 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1638 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1639 | // Note: we don't suggest either "auto" or "register", because both |
| 1640 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1641 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1642 | Results.AddResult(Result("extern")); |
| 1643 | Results.AddResult(Result("static")); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1644 | |
| 1645 | if (LangOpts.CPlusPlus11) { |
| 1646 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 1647 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| 1648 | |
| 1649 | // alignas |
| 1650 | Builder.AddTypedTextChunk("alignas"); |
| 1651 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1652 | Builder.AddPlaceholderChunk("expression"); |
| 1653 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1654 | Results.AddResult(Result(Builder.TakeString())); |
| 1655 | |
| 1656 | Results.AddResult(Result("constexpr")); |
| 1657 | Results.AddResult(Result("thread_local")); |
| 1658 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1661 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1662 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1663 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1664 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1665 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1666 | case Sema::PCC_Class: |
| 1667 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1668 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1669 | Results.AddResult(Result("explicit")); |
| 1670 | Results.AddResult(Result("friend")); |
| 1671 | Results.AddResult(Result("mutable")); |
| 1672 | Results.AddResult(Result("virtual")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1673 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1674 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1675 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1676 | case Sema::PCC_ObjCInterface: |
| 1677 | case Sema::PCC_ObjCImplementation: |
| 1678 | case Sema::PCC_Namespace: |
| 1679 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1680 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1681 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1684 | case Sema::PCC_ObjCInstanceVariableList: |
| 1685 | case Sema::PCC_Expression: |
| 1686 | case Sema::PCC_Statement: |
| 1687 | case Sema::PCC_ForInit: |
| 1688 | case Sema::PCC_Condition: |
| 1689 | case Sema::PCC_RecoveryInFunction: |
| 1690 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1691 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1692 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1697 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1698 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1699 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1700 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1701 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1702 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1703 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1704 | ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1705 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1707 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1708 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1709 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1710 | Builder.AddTypedTextChunk("typedef"); |
| 1711 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1712 | Builder.AddPlaceholderChunk("type"); |
| 1713 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1714 | Builder.AddPlaceholderChunk("name"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1715 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1718 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1719 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1720 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1721 | case Sema::PCC_Namespace: |
| 1722 | case Sema::PCC_Class: |
| 1723 | case Sema::PCC_ObjCInstanceVariableList: |
| 1724 | case Sema::PCC_Template: |
| 1725 | case Sema::PCC_MemberTemplate: |
| 1726 | case Sema::PCC_Statement: |
| 1727 | case Sema::PCC_RecoveryInFunction: |
| 1728 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1729 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1730 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1731 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1732 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1733 | case Sema::PCC_Expression: |
| 1734 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1735 | return LangOpts.CPlusPlus; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1737 | case Sema::PCC_ObjCInterface: |
| 1738 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1739 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1740 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1741 | case Sema::PCC_ForInit: |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1742 | return LangOpts.CPlusPlus || LangOpts.ObjC || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1743 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1744 | |
| 1745 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1748 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1749 | const Preprocessor &PP) { |
| 1750 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1751 | Policy.AnonymousTagLocations = false; |
| 1752 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1753 | Policy.SuppressUnwrittenScope = true; |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 1754 | Policy.SuppressScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1755 | return Policy; |
| 1756 | } |
| 1757 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1758 | /// Retrieve a printing policy suitable for code completion. |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1759 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1760 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1761 | } |
| 1762 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1763 | /// Retrieve the string representation of the given type as a string |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1764 | /// that has the appropriate lifetime for code completion. |
| 1765 | /// |
| 1766 | /// This routine provides a fast path where we provide constant strings for |
| 1767 | /// common type names. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1768 | static const char *GetCompletionTypeString(QualType T, ASTContext &Context, |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1769 | const PrintingPolicy &Policy, |
| 1770 | CodeCompletionAllocator &Allocator) { |
| 1771 | if (!T.getLocalQualifiers()) { |
| 1772 | // Built-in type names are constant strings. |
| 1773 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1774 | return BT->getNameAsCString(Policy); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1776 | // Anonymous tag types are constant strings. |
| 1777 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1778 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1779 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1780 | switch (Tag->getTagKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1781 | case TTK_Struct: |
| 1782 | return "struct <anonymous>"; |
| 1783 | case TTK_Interface: |
| 1784 | return "__interface <anonymous>"; |
| 1785 | case TTK_Class: |
| 1786 | return "class <anonymous>"; |
| 1787 | case TTK_Union: |
| 1788 | return "union <anonymous>"; |
| 1789 | case TTK_Enum: |
| 1790 | return "enum <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1795 | // Slow path: format the type as a string. |
| 1796 | std::string Result; |
| 1797 | T.getAsStringInternal(Result, Policy); |
| 1798 | return Allocator.CopyString(Result); |
| 1799 | } |
| 1800 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1801 | /// Add a completion for "this", if we're in a member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1802 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1803 | QualType ThisTy = S.getCurrentThisType(); |
| 1804 | if (ThisTy.isNull()) |
| 1805 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1807 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1808 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1809 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1810 | Builder.AddResultTypeChunk( |
| 1811 | GetCompletionTypeString(ThisTy, S.Context, Policy, Allocator)); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1812 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1813 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1816 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
| 1817 | ResultBuilder &Results, |
| 1818 | const LangOptions &LangOpts) { |
| 1819 | if (!LangOpts.CPlusPlus11) |
| 1820 | return; |
| 1821 | |
| 1822 | Builder.AddTypedTextChunk("static_assert"); |
| 1823 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1824 | Builder.AddPlaceholderChunk("expression"); |
| 1825 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 1826 | Builder.AddPlaceholderChunk("message"); |
| 1827 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1828 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 1829 | } |
| 1830 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1831 | static void printOverrideString(llvm::raw_ostream &OS, |
| 1832 | CodeCompletionString *CCS) { |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 1833 | for (const auto &C : *CCS) { |
| 1834 | if (C.Kind == CodeCompletionString::CK_Optional) |
| 1835 | printOverrideString(OS, C.Optional); |
| 1836 | else |
| 1837 | OS << C.Text; |
| 1838 | // Add a space after return type. |
| 1839 | if (C.Kind == CodeCompletionString::CK_ResultType) |
| 1840 | OS << ' '; |
| 1841 | } |
| 1842 | } |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 1843 | |
| 1844 | static void AddOverrideResults(ResultBuilder &Results, |
| 1845 | const CodeCompletionContext &CCContext, |
| 1846 | CodeCompletionBuilder &Builder) { |
| 1847 | Sema &S = Results.getSema(); |
| 1848 | const auto *CR = llvm::dyn_cast<CXXRecordDecl>(S.CurContext); |
| 1849 | // If not inside a class/struct/union return empty. |
| 1850 | if (!CR) |
| 1851 | return; |
| 1852 | // First store overrides within current class. |
| 1853 | // These are stored by name to make querying fast in the later step. |
| 1854 | llvm::StringMap<std::vector<FunctionDecl *>> Overrides; |
| 1855 | for (auto *Method : CR->methods()) { |
| 1856 | if (!Method->isVirtual() || !Method->getIdentifier()) |
| 1857 | continue; |
| 1858 | Overrides[Method->getName()].push_back(Method); |
| 1859 | } |
| 1860 | |
| 1861 | for (const auto &Base : CR->bases()) { |
| 1862 | const auto *BR = Base.getType().getTypePtr()->getAsCXXRecordDecl(); |
| 1863 | if (!BR) |
| 1864 | continue; |
| 1865 | for (auto *Method : BR->methods()) { |
| 1866 | if (!Method->isVirtual() || !Method->getIdentifier()) |
| 1867 | continue; |
| 1868 | const auto it = Overrides.find(Method->getName()); |
| 1869 | bool IsOverriden = false; |
| 1870 | if (it != Overrides.end()) { |
| 1871 | for (auto *MD : it->second) { |
| 1872 | // If the method in current body is not an overload of this virtual |
| 1873 | // function, then it overrides this one. |
| 1874 | if (!S.IsOverload(MD, Method, false)) { |
| 1875 | IsOverriden = true; |
| 1876 | break; |
| 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | if (!IsOverriden) { |
| 1881 | // Generates a new CodeCompletionResult by taking this function and |
| 1882 | // converting it into an override declaration with only one chunk in the |
| 1883 | // final CodeCompletionString as a TypedTextChunk. |
| 1884 | std::string OverrideSignature; |
| 1885 | llvm::raw_string_ostream OS(OverrideSignature); |
| 1886 | CodeCompletionResult CCR(Method, 0); |
| 1887 | PrintingPolicy Policy = |
| 1888 | getCompletionPrintingPolicy(S.getASTContext(), S.getPreprocessor()); |
| 1889 | auto *CCS = CCR.createCodeCompletionStringForOverride( |
| 1890 | S.getPreprocessor(), S.getASTContext(), Builder, |
| 1891 | /*IncludeBriefComments=*/false, CCContext, Policy); |
| 1892 | Results.AddResult(CodeCompletionResult(CCS, Method, CCP_CodePattern)); |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1898 | /// Add language constructs that show up for "ordinary" names. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 1899 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, Scope *S, |
| 1900 | Sema &SemaRef, ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1901 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1902 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1903 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1904 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1905 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1906 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1907 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1908 | if (Results.includeCodePatterns()) { |
| 1909 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1910 | Builder.AddTypedTextChunk("namespace"); |
| 1911 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1912 | Builder.AddPlaceholderChunk("identifier"); |
| 1913 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1914 | Builder.AddPlaceholderChunk("declarations"); |
| 1915 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1916 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1917 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1918 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1920 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1921 | Builder.AddTypedTextChunk("namespace"); |
| 1922 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1923 | Builder.AddPlaceholderChunk("name"); |
| 1924 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1925 | Builder.AddPlaceholderChunk("namespace"); |
| 1926 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1929 | Builder.AddTypedTextChunk("using"); |
| 1930 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1931 | Builder.AddTextChunk("namespace"); |
| 1932 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1933 | Builder.AddPlaceholderChunk("identifier"); |
| 1934 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1935 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1936 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1937 | Builder.AddTypedTextChunk("asm"); |
| 1938 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1939 | Builder.AddPlaceholderChunk("string-literal"); |
| 1940 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1941 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1943 | if (Results.includeCodePatterns()) { |
| 1944 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1945 | Builder.AddTypedTextChunk("template"); |
| 1946 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1947 | Builder.AddPlaceholderChunk("declaration"); |
| 1948 | Results.AddResult(Result(Builder.TakeString())); |
Eric Liu | b87c6eb | 2018-10-15 12:37:23 +0000 | [diff] [blame] | 1949 | } else { |
| 1950 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1951 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1952 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1953 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1954 | if (SemaRef.getLangOpts().ObjC) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1955 | AddObjCTopLevelResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1957 | AddTypedefResult(Results); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1958 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1959 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1960 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1961 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1962 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1963 | Builder.AddTypedTextChunk("using"); |
| 1964 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1965 | Builder.AddPlaceholderChunk("qualifier"); |
| 1966 | Builder.AddTextChunk("::"); |
| 1967 | Builder.AddPlaceholderChunk("name"); |
| 1968 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1970 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1971 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1972 | Builder.AddTypedTextChunk("using"); |
| 1973 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1974 | Builder.AddTextChunk("typename"); |
| 1975 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1976 | Builder.AddPlaceholderChunk("qualifier"); |
| 1977 | Builder.AddTextChunk("::"); |
| 1978 | Builder.AddPlaceholderChunk("name"); |
| 1979 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1982 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| 1983 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1984 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1985 | AddTypedefResult(Results); |
| 1986 | |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1987 | bool IsNotInheritanceScope = |
| 1988 | !(S->getFlags() & Scope::ClassInheritanceScope); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1989 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1990 | Builder.AddTypedTextChunk("public"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1991 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1992 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1993 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1994 | |
| 1995 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1996 | Builder.AddTypedTextChunk("protected"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1997 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1998 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1999 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2000 | |
| 2001 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2002 | Builder.AddTypedTextChunk("private"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 2003 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 2004 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2005 | Results.AddResult(Result(Builder.TakeString())); |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 2006 | |
| 2007 | // FIXME: This adds override results only if we are at the first word of |
| 2008 | // the declaration/definition. Also call this from other sides to have |
| 2009 | // more use-cases. |
| 2010 | AddOverrideResults(Results, CodeCompletionContext::CCC_ClassStructUnion, |
| 2011 | Builder); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2012 | } |
| 2013 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 2014 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2015 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2016 | case Sema::PCC_Template: |
| 2017 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2018 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2019 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2020 | Builder.AddTypedTextChunk("template"); |
| 2021 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2022 | Builder.AddPlaceholderChunk("parameters"); |
| 2023 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2024 | Results.AddResult(Result(Builder.TakeString())); |
Eric Liu | b87c6eb | 2018-10-15 12:37:23 +0000 | [diff] [blame] | 2025 | } else { |
| 2026 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2029 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2030 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2031 | break; |
| 2032 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2033 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2034 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 2035 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2036 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2037 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2038 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2039 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2040 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 2041 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 2042 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2043 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2044 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2045 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2046 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2047 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2048 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2049 | case Sema::PCC_RecoveryInFunction: |
| 2050 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2051 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2052 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2053 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 2054 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2055 | Builder.AddTypedTextChunk("try"); |
| 2056 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2057 | Builder.AddPlaceholderChunk("statements"); |
| 2058 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2059 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2060 | Builder.AddTextChunk("catch"); |
| 2061 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2062 | Builder.AddPlaceholderChunk("declaration"); |
| 2063 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2064 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2065 | Builder.AddPlaceholderChunk("statements"); |
| 2066 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2067 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2068 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2069 | } |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2070 | if (SemaRef.getLangOpts().ObjC) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2071 | AddObjCStatementResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2073 | if (Results.includeCodePatterns()) { |
| 2074 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2075 | Builder.AddTypedTextChunk("if"); |
| 2076 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2077 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2078 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2079 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2080 | Builder.AddPlaceholderChunk("expression"); |
| 2081 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2082 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2083 | Builder.AddPlaceholderChunk("statements"); |
| 2084 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2085 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2086 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2088 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2089 | Builder.AddTypedTextChunk("switch"); |
| 2090 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2091 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2092 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2093 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2094 | Builder.AddPlaceholderChunk("expression"); |
| 2095 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2096 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2097 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2098 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2099 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2100 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2102 | // Switch-specific statements. |
Sam McCall | aeb4b3e | 2018-10-10 10:51:48 +0000 | [diff] [blame] | 2103 | if (SemaRef.getCurFunction() && |
| 2104 | !SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2105 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2106 | Builder.AddTypedTextChunk("case"); |
| 2107 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2108 | Builder.AddPlaceholderChunk("expression"); |
| 2109 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 2110 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2111 | |
| 2112 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2113 | Builder.AddTypedTextChunk("default"); |
| 2114 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 2115 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2118 | if (Results.includeCodePatterns()) { |
| 2119 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2120 | Builder.AddTypedTextChunk("while"); |
| 2121 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2122 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2123 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2124 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2125 | Builder.AddPlaceholderChunk("expression"); |
| 2126 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2127 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2128 | Builder.AddPlaceholderChunk("statements"); |
| 2129 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2130 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2131 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2132 | |
| 2133 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2134 | Builder.AddTypedTextChunk("do"); |
| 2135 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2136 | Builder.AddPlaceholderChunk("statements"); |
| 2137 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2138 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2139 | Builder.AddTextChunk("while"); |
| 2140 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2141 | Builder.AddPlaceholderChunk("expression"); |
| 2142 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2143 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2145 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2146 | Builder.AddTypedTextChunk("for"); |
| 2147 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2148 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2149 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2150 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2151 | Builder.AddPlaceholderChunk("init-expression"); |
| 2152 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2153 | Builder.AddPlaceholderChunk("condition"); |
| 2154 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 2155 | Builder.AddPlaceholderChunk("inc-expression"); |
| 2156 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2157 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2158 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2159 | Builder.AddPlaceholderChunk("statements"); |
| 2160 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 2161 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 2162 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2163 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2165 | if (S->getContinueParent()) { |
| 2166 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2167 | Builder.AddTypedTextChunk("continue"); |
| 2168 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | if (S->getBreakParent()) { |
| 2172 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2173 | Builder.AddTypedTextChunk("break"); |
| 2174 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | // "return expression ;" or "return ;", depending on whether we |
| 2178 | // know the function is void or not. |
| 2179 | bool isVoid = false; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2180 | if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2181 | isVoid = Function->getReturnType()->isVoidType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2182 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2183 | isVoid = Method->getReturnType()->isVoidType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2184 | else if (SemaRef.getCurBlock() && |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 2185 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 2186 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2187 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 2188 | if (!isVoid) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2189 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2190 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 2191 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2192 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2194 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2195 | Builder.AddTypedTextChunk("goto"); |
| 2196 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2197 | Builder.AddPlaceholderChunk("label"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2198 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2200 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2201 | Builder.AddTypedTextChunk("using"); |
| 2202 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2203 | Builder.AddTextChunk("namespace"); |
| 2204 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2205 | Builder.AddPlaceholderChunk("identifier"); |
| 2206 | Results.AddResult(Result(Builder.TakeString())); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 2207 | |
| 2208 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2209 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2210 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2211 | |
| 2212 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2213 | case Sema::PCC_ForInit: |
| 2214 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2215 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2216 | // Fall through: conditions and statements can have expressions. |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 2217 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2219 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2220 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2221 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 2222 | // (__bridge <type>)<expression> |
| 2223 | Builder.AddTypedTextChunk("__bridge"); |
| 2224 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2225 | Builder.AddPlaceholderChunk("type"); |
| 2226 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2227 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2228 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2229 | |
| 2230 | // (__bridge_transfer <Objective-C type>)<expression> |
| 2231 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 2232 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2233 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 2234 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2235 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2236 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2237 | |
| 2238 | // (__bridge_retained <CF type>)<expression> |
| 2239 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 2240 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2241 | Builder.AddPlaceholderChunk("CF type"); |
| 2242 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2243 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2244 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2245 | } |
| 2246 | // Fall through |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 2247 | LLVM_FALLTHROUGH; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2248 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2249 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2250 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2251 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 2252 | addThisCompletion(SemaRef, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2254 | // true |
| 2255 | Builder.AddResultTypeChunk("bool"); |
| 2256 | Builder.AddTypedTextChunk("true"); |
| 2257 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2259 | // false |
| 2260 | Builder.AddResultTypeChunk("bool"); |
| 2261 | Builder.AddTypedTextChunk("false"); |
| 2262 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2263 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2264 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2265 | // dynamic_cast < type-id > ( expression ) |
| 2266 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 2267 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2268 | Builder.AddPlaceholderChunk("type"); |
| 2269 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2270 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2271 | Builder.AddPlaceholderChunk("expression"); |
| 2272 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2273 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2274 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2276 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2277 | Builder.AddTypedTextChunk("static_cast"); |
| 2278 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2279 | Builder.AddPlaceholderChunk("type"); |
| 2280 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2281 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2282 | Builder.AddPlaceholderChunk("expression"); |
| 2283 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2284 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2285 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2286 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2287 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 2288 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2289 | Builder.AddPlaceholderChunk("type"); |
| 2290 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2291 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2292 | Builder.AddPlaceholderChunk("expression"); |
| 2293 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2294 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2295 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2296 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2297 | Builder.AddTypedTextChunk("const_cast"); |
| 2298 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 2299 | Builder.AddPlaceholderChunk("type"); |
| 2300 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 2301 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2302 | Builder.AddPlaceholderChunk("expression"); |
| 2303 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2304 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2305 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2306 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2307 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2308 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2309 | Builder.AddTypedTextChunk("typeid"); |
| 2310 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2311 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2312 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2313 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2314 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2316 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2317 | Builder.AddTypedTextChunk("new"); |
| 2318 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2319 | Builder.AddPlaceholderChunk("type"); |
| 2320 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2321 | Builder.AddPlaceholderChunk("expressions"); |
| 2322 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2323 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2325 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2326 | Builder.AddTypedTextChunk("new"); |
| 2327 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2328 | Builder.AddPlaceholderChunk("type"); |
| 2329 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2330 | Builder.AddPlaceholderChunk("size"); |
| 2331 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 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 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2338 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2339 | Builder.AddTypedTextChunk("delete"); |
| 2340 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2341 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2342 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2343 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2344 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2345 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2346 | Builder.AddTypedTextChunk("delete"); |
| 2347 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2348 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2349 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2350 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2351 | Builder.AddPlaceholderChunk("expression"); |
| 2352 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2353 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2354 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2355 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2356 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2357 | Builder.AddTypedTextChunk("throw"); |
| 2358 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2359 | Builder.AddPlaceholderChunk("expression"); |
| 2360 | Results.AddResult(Result(Builder.TakeString())); |
| 2361 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 2363 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2364 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2365 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2366 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2367 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2368 | Builder.AddTypedTextChunk("nullptr"); |
| 2369 | Results.AddResult(Result(Builder.TakeString())); |
| 2370 | |
| 2371 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2372 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2373 | Builder.AddTypedTextChunk("alignof"); |
| 2374 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2375 | Builder.AddPlaceholderChunk("type"); |
| 2376 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2377 | Results.AddResult(Result(Builder.TakeString())); |
| 2378 | |
| 2379 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2380 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2381 | Builder.AddTypedTextChunk("noexcept"); |
| 2382 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2383 | Builder.AddPlaceholderChunk("expression"); |
| 2384 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2385 | Results.AddResult(Result(Builder.TakeString())); |
| 2386 | |
| 2387 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2388 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2389 | Builder.AddTypedTextChunk("sizeof..."); |
| 2390 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2391 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2392 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2393 | Results.AddResult(Result(Builder.TakeString())); |
| 2394 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2397 | if (SemaRef.getLangOpts().ObjC) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2398 | // 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] | 2399 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2400 | // The interface can be NULL. |
| 2401 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2402 | if (ID->getSuperClass()) { |
| 2403 | std::string SuperType; |
| 2404 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2405 | if (Method->isInstanceMethod()) |
| 2406 | SuperType += " *"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2407 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2408 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2409 | Builder.AddTypedTextChunk("super"); |
| 2410 | Results.AddResult(Result(Builder.TakeString())); |
| 2411 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2414 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2417 | if (SemaRef.getLangOpts().C11) { |
| 2418 | // _Alignof |
| 2419 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2420 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2421 | Builder.AddTypedTextChunk("alignof"); |
| 2422 | else |
| 2423 | Builder.AddTypedTextChunk("_Alignof"); |
| 2424 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2425 | Builder.AddPlaceholderChunk("type"); |
| 2426 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2427 | Results.AddResult(Result(Builder.TakeString())); |
| 2428 | } |
| 2429 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2430 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2431 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2432 | Builder.AddTypedTextChunk("sizeof"); |
| 2433 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2434 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2435 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2436 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2437 | break; |
| 2438 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2439 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2440 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2441 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2442 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2445 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2446 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2447 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2448 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2449 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2452 | /// 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] | 2453 | /// type chunk. |
| 2454 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2455 | const PrintingPolicy &Policy, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2456 | const NamedDecl *ND, QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2457 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2458 | if (!ND) |
| 2459 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Skip constructors and conversion functions, which have their return types |
| 2462 | // built into their names. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 2463 | if (isConstructor(ND) || isa<CXXConversionDecl>(ND)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2464 | return; |
| 2465 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2466 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2467 | QualType T; |
| 2468 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2469 | T = Function->getReturnType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2470 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2471 | if (!BaseType.isNull()) |
| 2472 | T = Method->getSendResultType(BaseType); |
| 2473 | else |
| 2474 | T = Method->getReturnType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2475 | } else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2476 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2477 | T = clang::TypeName::getFullyQualifiedType(T, Context); |
| 2478 | } else if (isa<UnresolvedUsingValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2479 | /* Do nothing: ignore unresolved using declarations*/ |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2480 | } else if (const auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2481 | if (!BaseType.isNull()) |
| 2482 | T = Ivar->getUsageType(BaseType); |
| 2483 | else |
| 2484 | T = Ivar->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2485 | } else if (const auto *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2486 | T = Value->getType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2487 | } else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2488 | if (!BaseType.isNull()) |
| 2489 | T = Property->getUsageType(BaseType); |
| 2490 | else |
| 2491 | T = Property->getType(); |
| 2492 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2494 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2495 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2496 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2497 | Result.AddResultTypeChunk( |
| 2498 | GetCompletionTypeString(T, Context, Policy, Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2501 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2502 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2503 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2504 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2505 | if (Sentinel->getSentinel() == 0) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2506 | if (PP.getLangOpts().ObjC && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2507 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2508 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2509 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2510 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2511 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2515 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2516 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2517 | std::string Result; |
| 2518 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2519 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2520 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2521 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2522 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2523 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2524 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2525 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2526 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2527 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2528 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2529 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2530 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2531 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2532 | switch (*nullability) { |
| 2533 | case NullabilityKind::NonNull: |
| 2534 | Result += "nonnull "; |
| 2535 | break; |
| 2536 | |
| 2537 | case NullabilityKind::Nullable: |
| 2538 | Result += "nullable "; |
| 2539 | break; |
| 2540 | |
| 2541 | case NullabilityKind::Unspecified: |
| 2542 | Result += "null_unspecified "; |
| 2543 | break; |
| 2544 | } |
| 2545 | } |
| 2546 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2547 | return Result; |
| 2548 | } |
| 2549 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2550 | /// Tries to find the most appropriate type location for an Objective-C |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2551 | /// block placeholder. |
| 2552 | /// |
| 2553 | /// This function ignores things like typedefs and qualifiers in order to |
| 2554 | /// present the most relevant and accurate block placeholders in code completion |
| 2555 | /// results. |
| 2556 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
| 2557 | FunctionTypeLoc &Block, |
| 2558 | FunctionProtoTypeLoc &BlockProto, |
| 2559 | bool SuppressBlock = false) { |
| 2560 | if (!TSInfo) |
| 2561 | return; |
| 2562 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2563 | while (true) { |
| 2564 | // Look through typedefs. |
| 2565 | if (!SuppressBlock) { |
| 2566 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2567 | if (TypeSourceInfo *InnerTSInfo = |
| 2568 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
| 2569 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2570 | continue; |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | // Look through qualified types |
| 2575 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2576 | TL = QualifiedTL.getUnqualifiedLoc(); |
| 2577 | continue; |
| 2578 | } |
| 2579 | |
| 2580 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2581 | TL = AttrTL.getModifiedLoc(); |
| 2582 | continue; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | // Try to get the function prototype behind the block pointer type, |
| 2587 | // then we're done. |
| 2588 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2589 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2590 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2591 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
| 2592 | } |
| 2593 | break; |
| 2594 | } |
| 2595 | } |
| 2596 | |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2597 | static std::string |
| 2598 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2599 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2600 | bool SuppressBlockName = false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2601 | bool SuppressBlock = false, |
| 2602 | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
| 2603 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2604 | static std::string |
| 2605 | FormatFunctionParameter(const PrintingPolicy &Policy, const ParmVarDecl *Param, |
| 2606 | bool SuppressName = false, bool SuppressBlock = false, |
| 2607 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Sam McCall | bc7ff89 | 2019-04-04 11:34:18 +0000 | [diff] [blame] | 2608 | // Params are unavailable in FunctionTypeLoc if the FunctionType is invalid. |
| 2609 | // It would be better to pass in the param Type, which is usually avaliable. |
| 2610 | // But this case is rare, so just pretend we fell back to int as elsewhere. |
| 2611 | if (!Param) |
| 2612 | return "int"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2613 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2614 | if (Param->getType()->isDependentType() || |
| 2615 | !Param->getType()->isBlockPointerType()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2616 | // The argument for a dependent or non-block parameter is a placeholder |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2617 | // containing that parameter's type. |
| 2618 | std::string Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2619 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2620 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2621 | Result = Param->getIdentifier()->getName(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2622 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2623 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2624 | if (ObjCSubsts) |
| 2625 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2626 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2627 | if (ObjCMethodParam) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2628 | Result = |
| 2629 | "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2630 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2631 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2632 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2633 | } else { |
| 2634 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2635 | } |
| 2636 | return Result; |
| 2637 | } |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2639 | // The argument for a block pointer parameter is a block literal with |
| 2640 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2641 | FunctionTypeLoc Block; |
| 2642 | FunctionProtoTypeLoc BlockProto; |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2643 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
| 2644 | SuppressBlock); |
Alex Lorenz | 6bf4a58 | 2017-03-13 15:43:42 +0000 | [diff] [blame] | 2645 | // Try to retrieve the block type information from the property if this is a |
| 2646 | // parameter in a setter. |
| 2647 | if (!Block && ObjCMethodParam && |
| 2648 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { |
| 2649 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
| 2650 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
| 2651 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
| 2652 | SuppressBlock); |
| 2653 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (!Block) { |
| 2656 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2657 | // for the block; just use the parameter type as a placeholder. |
| 2658 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2659 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2660 | Result = Param->getIdentifier()->getName(); |
| 2661 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2662 | QualType Type = Param->getType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2664 | if (ObjCMethodParam) { |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2665 | Result = Type.getAsString(Policy); |
| 2666 | std::string Quals = |
| 2667 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
| 2668 | if (!Quals.empty()) |
| 2669 | Result = "(" + Quals + " " + Result + ")"; |
| 2670 | if (Result.back() != ')') |
| 2671 | Result += " "; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2672 | if (Param->getIdentifier()) |
| 2673 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2674 | } else { |
| 2675 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2676 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2678 | return Result; |
| 2679 | } |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2681 | // We have the function prototype behind the block pointer type, as it was |
| 2682 | // written in the source. |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2683 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
| 2684 | /*SuppressBlockName=*/false, SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2685 | ObjCSubsts); |
| 2686 | } |
| 2687 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2688 | /// Returns a placeholder string that corresponds to an Objective-C block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2689 | /// declaration. |
| 2690 | /// |
| 2691 | /// \param BlockDecl A declaration with an Objective-C block type. |
| 2692 | /// |
| 2693 | /// \param Block The most relevant type location for that block type. |
| 2694 | /// |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2695 | /// \param SuppressBlockName Determines whether or not the name of the block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2696 | /// declaration is included in the resulting string. |
| 2697 | static std::string |
| 2698 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2699 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2700 | bool SuppressBlockName, bool SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2701 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2702 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2703 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2704 | if (ObjCSubsts) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2705 | ResultType = |
| 2706 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
| 2707 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2708 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2709 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2710 | |
| 2711 | // Format the parameter list. |
| 2712 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2713 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2714 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2715 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2716 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2717 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2718 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2719 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2720 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2721 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2722 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2723 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2724 | /*SuppressName=*/false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2725 | /*SuppressBlock=*/true, ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2726 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2727 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2728 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2729 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2730 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2731 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2733 | if (SuppressBlock) { |
| 2734 | // Format as a parameter. |
| 2735 | Result = Result + " (^"; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2736 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2737 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2738 | Result += ")"; |
| 2739 | Result += Params; |
| 2740 | } else { |
| 2741 | // Format as a block literal argument. |
| 2742 | Result = '^' + Result; |
| 2743 | Result += Params; |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2744 | |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2745 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2746 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2747 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2749 | return Result; |
| 2750 | } |
| 2751 | |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2752 | static std::string GetDefaultValueString(const ParmVarDecl *Param, |
| 2753 | const SourceManager &SM, |
| 2754 | const LangOptions &LangOpts) { |
Ilya Biryukov | b6d1ec8 | 2017-07-21 09:24:00 +0000 | [diff] [blame] | 2755 | const SourceRange SrcRange = Param->getDefaultArgRange(); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2756 | CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange); |
| 2757 | bool Invalid = CharSrcRange.isInvalid(); |
| 2758 | if (Invalid) |
| 2759 | return ""; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2760 | StringRef srcText = |
| 2761 | Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2762 | if (Invalid) |
| 2763 | return ""; |
| 2764 | |
| 2765 | if (srcText.empty() || srcText == "=") { |
| 2766 | // Lexer can't determine the value. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2767 | // This happens if the code is incorrect (for example class is forward |
| 2768 | // declared). |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2769 | return ""; |
| 2770 | } |
Erik Verbruggen | 797980e | 2017-07-19 11:15:36 +0000 | [diff] [blame] | 2771 | std::string DefValue(srcText.str()); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2772 | // FIXME: remove this check if the Lexer::getSourceText value is fixed and |
| 2773 | // this value always has (or always does not have) '=' in front of it |
| 2774 | if (DefValue.at(0) != '=') { |
| 2775 | // If we don't have '=' in front of value. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2776 | // Lexer returns built-in types values without '=' and user-defined types |
| 2777 | // values with it. |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2778 | return " = " + DefValue; |
| 2779 | } |
| 2780 | return " " + DefValue; |
| 2781 | } |
| 2782 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2783 | /// Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2784 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2785 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2786 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2787 | CodeCompletionBuilder &Result, |
| 2788 | unsigned Start = 0, |
| 2789 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2790 | bool FirstParameter = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2792 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2793 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2795 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2796 | // When we see an optional default argument, put that argument and |
| 2797 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2798 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2799 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2800 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2801 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2802 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2803 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2804 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2805 | } |
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 (FirstParameter) |
| 2808 | FirstParameter = false; |
| 2809 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2810 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2811 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2812 | InOptional = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2814 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2815 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2816 | if (Param->hasDefaultArg()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2817 | PlaceholderStr += |
| 2818 | GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts()); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2820 | if (Function->isVariadic() && P == N - 1) |
| 2821 | PlaceholderStr += ", ..."; |
| 2822 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2823 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2824 | Result.AddPlaceholderChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2825 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2826 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2827 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2828 | if (const auto *Proto = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2829 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2830 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2831 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2832 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2833 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2834 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2837 | /// Add template parameter chunks to the given code completion string. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2838 | static void AddTemplateParameterChunks( |
| 2839 | ASTContext &Context, const PrintingPolicy &Policy, |
| 2840 | const TemplateDecl *Template, CodeCompletionBuilder &Result, |
| 2841 | unsigned MaxParameters = 0, unsigned Start = 0, bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2842 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2843 | |
| 2844 | // Prefer to take the template parameter names from the first declaration of |
| 2845 | // the template. |
| 2846 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2847 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2848 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2849 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2850 | if (MaxParameters) |
| 2851 | PEnd = Params->begin() + MaxParameters; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2852 | for (TemplateParameterList::iterator P = Params->begin() + Start; P != PEnd; |
| 2853 | ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2854 | bool HasDefaultArg = false; |
| 2855 | std::string PlaceholderStr; |
| 2856 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2857 | if (TTP->wasDeclaredWithTypename()) |
| 2858 | PlaceholderStr = "typename"; |
| 2859 | else |
| 2860 | PlaceholderStr = "class"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2862 | if (TTP->getIdentifier()) { |
| 2863 | PlaceholderStr += ' '; |
| 2864 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2865 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2867 | HasDefaultArg = TTP->hasDefaultArgument(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2868 | } else if (NonTypeTemplateParmDecl *NTTP = |
| 2869 | dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2870 | if (NTTP->getIdentifier()) |
| 2871 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2872 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2873 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2874 | } else { |
| 2875 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2876 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2877 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2878 | // Since putting the template argument list into the placeholder would |
| 2879 | // be very, very long, we just use an abbreviation. |
| 2880 | PlaceholderStr = "template<...> class"; |
| 2881 | if (TTP->getIdentifier()) { |
| 2882 | PlaceholderStr += ' '; |
| 2883 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2884 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2886 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2887 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2889 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2890 | // When we see an optional default argument, put that argument and |
| 2891 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2892 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2893 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2894 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2895 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2896 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2897 | P - Params->begin(), true); |
| 2898 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2899 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2900 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2902 | InDefaultArg = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2904 | if (FirstParameter) |
| 2905 | FirstParameter = false; |
| 2906 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2907 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2909 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2910 | Result.AddPlaceholderChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2911 | Result.getAllocator().CopyString(PlaceholderStr)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2912 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2915 | /// Add a qualifier to the given code-completion string, if the |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2916 | /// provided nested-name-specifier is non-NULL. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2917 | static void AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
| 2918 | NestedNameSpecifier *Qualifier, |
| 2919 | bool QualifierIsInformative, |
| 2920 | ASTContext &Context, |
| 2921 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2922 | if (!Qualifier) |
| 2923 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2925 | std::string PrintedNNS; |
| 2926 | { |
| 2927 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2928 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2929 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2930 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2931 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2932 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2933 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2936 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2937 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2938 | const FunctionDecl *Function) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2939 | const auto *Proto = Function->getType()->getAs<FunctionProtoType>(); |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2940 | if (!Proto || !Proto->getMethodQuals()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2941 | return; |
| 2942 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2943 | // FIXME: Add ref-qualifier! |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2945 | // Handle single qualifiers without copying |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2946 | if (Proto->getMethodQuals().hasOnlyConst()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2947 | Result.AddInformativeChunk(" const"); |
| 2948 | return; |
| 2949 | } |
| 2950 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2951 | if (Proto->getMethodQuals().hasOnlyVolatile()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2952 | Result.AddInformativeChunk(" volatile"); |
| 2953 | return; |
| 2954 | } |
| 2955 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 2956 | if (Proto->getMethodQuals().hasOnlyRestrict()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2957 | Result.AddInformativeChunk(" restrict"); |
| 2958 | return; |
| 2959 | } |
| 2960 | |
| 2961 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2962 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2963 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2964 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2965 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2966 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2967 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2968 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2969 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2972 | /// Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2973 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2974 | const NamedDecl *ND, |
| 2975 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2976 | DeclarationName Name = ND->getDeclName(); |
| 2977 | if (!Name) |
| 2978 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2980 | switch (Name.getNameKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2981 | case DeclarationName::CXXOperatorName: { |
| 2982 | const char *OperatorName = nullptr; |
| 2983 | switch (Name.getCXXOverloadedOperator()) { |
| 2984 | case OO_None: |
| 2985 | case OO_Conditional: |
| 2986 | case NUM_OVERLOADED_OPERATORS: |
| 2987 | OperatorName = "operator"; |
| 2988 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2989 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2990 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 2991 | case OO_##Name: \ |
| 2992 | OperatorName = "operator" Spelling; \ |
| 2993 | break; |
| 2994 | #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemberOnly) |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2995 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2996 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 2997 | case OO_New: |
| 2998 | OperatorName = "operator new"; |
| 2999 | break; |
| 3000 | case OO_Delete: |
| 3001 | OperatorName = "operator delete"; |
| 3002 | break; |
| 3003 | case OO_Array_New: |
| 3004 | OperatorName = "operator new[]"; |
| 3005 | break; |
| 3006 | case OO_Array_Delete: |
| 3007 | OperatorName = "operator delete[]"; |
| 3008 | break; |
| 3009 | case OO_Call: |
| 3010 | OperatorName = "operator()"; |
| 3011 | break; |
| 3012 | case OO_Subscript: |
| 3013 | OperatorName = "operator[]"; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 3014 | break; |
| 3015 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3016 | Result.AddTypedTextChunk(OperatorName); |
| 3017 | break; |
| 3018 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3019 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3020 | case DeclarationName::Identifier: |
| 3021 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3022 | case DeclarationName::CXXDestructorName: |
| 3023 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3024 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3025 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3026 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3027 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3028 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3029 | case DeclarationName::CXXUsingDirective: |
| 3030 | case DeclarationName::ObjCZeroArgSelector: |
| 3031 | case DeclarationName::ObjCOneArgSelector: |
| 3032 | case DeclarationName::ObjCMultiArgSelector: |
| 3033 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3035 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3036 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3037 | QualType Ty = Name.getCXXNameType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3038 | if (const auto *RecordTy = Ty->getAs<RecordType>()) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3039 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3040 | else if (const auto *InjectedTy = Ty->getAs<InjectedClassNameType>()) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3041 | Record = InjectedTy->getDecl(); |
| 3042 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3043 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3044 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3045 | break; |
| 3046 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3048 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3049 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3050 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3051 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3052 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3053 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3054 | } |
| 3055 | break; |
| 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3060 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
| 3061 | Sema &S, const CodeCompletionContext &CCContext, |
| 3062 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 3063 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3064 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 3065 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 3068 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro( |
| 3069 | Preprocessor &PP, CodeCompletionAllocator &Allocator, |
| 3070 | CodeCompletionTUInfo &CCTUInfo) { |
| 3071 | assert(Kind == RK_Macro); |
| 3072 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
| 3073 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
| 3074 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName())); |
| 3075 | |
| 3076 | if (!MI || !MI->isFunctionLike()) |
| 3077 | return Result.TakeString(); |
| 3078 | |
| 3079 | // Format a function-like macro with placeholders for the arguments. |
| 3080 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3081 | MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end(); |
| 3082 | |
| 3083 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 3084 | if (MI->isC99Varargs()) { |
| 3085 | --AEnd; |
| 3086 | |
| 3087 | if (A == AEnd) { |
| 3088 | Result.AddPlaceholderChunk("..."); |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) { |
| 3093 | if (A != MI->param_begin()) |
| 3094 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3095 | |
| 3096 | if (MI->isVariadic() && (A + 1) == AEnd) { |
| 3097 | SmallString<32> Arg = (*A)->getName(); |
| 3098 | if (MI->isC99Varargs()) |
| 3099 | Arg += ", ..."; |
| 3100 | else |
| 3101 | Arg += "..."; |
| 3102 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
| 3103 | break; |
| 3104 | } |
| 3105 | |
| 3106 | // Non-variadic macros are simple. |
| 3107 | Result.AddPlaceholderChunk( |
| 3108 | Result.getAllocator().CopyString((*A)->getName())); |
| 3109 | } |
| 3110 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| 3111 | return Result.TakeString(); |
| 3112 | } |
| 3113 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3114 | /// If possible, create a new code completion string for the given |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3115 | /// result. |
| 3116 | /// |
| 3117 | /// \returns Either a new, heap-allocated code completion string describing |
| 3118 | /// how to use this result, or NULL to indicate that the string or name of the |
| 3119 | /// result is all that is needed. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3120 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
| 3121 | ASTContext &Ctx, Preprocessor &PP, const CodeCompletionContext &CCContext, |
| 3122 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 3123 | bool IncludeBriefComments) { |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 3124 | if (Kind == RK_Macro) |
| 3125 | return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo); |
| 3126 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3127 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3128 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3129 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3130 | if (Kind == RK_Pattern) { |
| 3131 | Pattern->Priority = Priority; |
| 3132 | Pattern->Availability = Availability; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3134 | if (Declaration) { |
| 3135 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3136 | Pattern->ParentName = Result.getParentName(); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3137 | if (const RawComment *RC = |
| 3138 | getPatternCompletionComment(Ctx, Declaration)) { |
| 3139 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 3140 | Pattern->BriefComment = Result.getBriefComment(); |
| 3141 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3142 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3143 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3144 | return Pattern; |
| 3145 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3146 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3147 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3148 | Result.AddTypedTextChunk(Keyword); |
| 3149 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3150 | } |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 3151 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3152 | return createCodeCompletionStringForDecl( |
| 3153 | PP, Ctx, Result, IncludeBriefComments, CCContext, Policy); |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | CodeCompletionString * |
| 3157 | CodeCompletionResult::createCodeCompletionStringForOverride( |
| 3158 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
| 3159 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
| 3160 | PrintingPolicy &Policy) { |
| 3161 | std::string OverrideSignature; |
| 3162 | llvm::raw_string_ostream OS(OverrideSignature); |
| 3163 | auto *CCS = createCodeCompletionStringForDecl(PP, Ctx, Result, |
| 3164 | /*IncludeBriefComments=*/false, |
| 3165 | CCContext, Policy); |
| 3166 | printOverrideString(OS, CCS); |
| 3167 | OS << " override"; |
| 3168 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(OS.str())); |
| 3169 | return Result.TakeString(); |
| 3170 | } |
| 3171 | |
| 3172 | CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl( |
| 3173 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
| 3174 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
| 3175 | PrintingPolicy &Policy) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 3176 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3177 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3178 | |
| 3179 | if (IncludeBriefComments) { |
| 3180 | // Add documentation comment, if it exists. |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3181 | if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3182 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3183 | } |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3186 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3187 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3188 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3189 | Result.AddTextChunk("::"); |
| 3190 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3191 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 3192 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3193 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 3194 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3196 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3197 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3198 | if (const auto *Function = dyn_cast<FunctionDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3199 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3200 | Ctx, Policy); |
| 3201 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3202 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3203 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3204 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 3205 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3206 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3207 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3208 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3209 | if (const FunctionTemplateDecl *FunTmpl = |
| 3210 | dyn_cast<FunctionTemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3211 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3212 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3213 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3214 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3216 | // Figure out which template parameters are deduced (or have default |
| 3217 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 3218 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3219 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3220 | unsigned LastDeducibleArgument; |
| 3221 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 3222 | --LastDeducibleArgument) { |
| 3223 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 3224 | // C++0x: Figure out if the template argument has a default. If so, |
| 3225 | // the user doesn't need to type this argument. |
| 3226 | // FIXME: We need to abstract template parameters better! |
| 3227 | bool HasDefaultArg = false; |
| 3228 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3229 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3230 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3231 | HasDefaultArg = TTP->hasDefaultArgument(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3232 | else if (NonTypeTemplateParmDecl *NTTP = |
| 3233 | dyn_cast<NonTypeTemplateParmDecl>(Param)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3234 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 3235 | else { |
| 3236 | assert(isa<TemplateTemplateParmDecl>(Param)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3237 | HasDefaultArg = |
| 3238 | cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3239 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3240 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3241 | if (!HasDefaultArg) |
| 3242 | break; |
| 3243 | } |
| 3244 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3246 | if (LastDeducibleArgument) { |
| 3247 | // Some of the function template arguments cannot be deduced from a |
| 3248 | // function call, so we introduce an explicit template argument list |
| 3249 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3250 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3251 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3252 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3253 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3254 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3255 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3256 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3257 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3258 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3259 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 3260 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3261 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3262 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3263 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3264 | if (const auto *Template = dyn_cast<TemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3265 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3266 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3267 | Result.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3268 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3269 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3270 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3271 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3272 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3273 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3274 | if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3275 | Selector Sel = Method->getSelector(); |
| 3276 | if (Sel.isUnarySelector()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3277 | Result.AddTypedTextChunk( |
| 3278 | Result.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3279 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 3282 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3283 | SelName += ':'; |
| 3284 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3285 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3286 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3287 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3289 | // If there is only one parameter, and we're past it, add an empty |
| 3290 | // typed-text chunk since there is nothing to type. |
| 3291 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3292 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3293 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3294 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 3295 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3296 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3297 | P != PEnd; (void)++P, ++Idx) { |
| 3298 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3299 | std::string Keyword; |
| 3300 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3301 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3302 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 3303 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3304 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3305 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3306 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3307 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3308 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3309 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3311 | // If we're before the starting parameter, skip the placeholder. |
| 3312 | if (Idx < StartParameter) |
| 3313 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3314 | |
| 3315 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3316 | QualType ParamType = (*P)->getType(); |
| 3317 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 3318 | if (!CCContext.getBaseType().isNull()) |
| 3319 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 3320 | |
| 3321 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 3322 | Arg = FormatFunctionParameter(Policy, *P, true, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3323 | /*SuppressBlock=*/false, ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3324 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3325 | if (ObjCSubsts) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3326 | ParamType = ParamType.substObjCTypeArgs( |
| 3327 | Ctx, *ObjCSubsts, ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 3328 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3329 | ParamType); |
| 3330 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3331 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 3332 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 3333 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 3334 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3336 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 3337 | Arg += ", ..."; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3338 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3339 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3340 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3341 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3342 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3343 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3344 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3347 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3348 | if (Method->param_size() == 0) { |
| 3349 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3350 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3351 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3352 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3353 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3354 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3355 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3356 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3357 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3358 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3359 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3360 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3363 | if (Qualifier) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3364 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3365 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3367 | Result.AddTypedTextChunk( |
Kadir Cetinkaya | ae45d0a4 | 2018-10-02 09:42:31 +0000 | [diff] [blame] | 3368 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3369 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3372 | const RawComment *clang::getCompletionComment(const ASTContext &Ctx, |
| 3373 | const NamedDecl *ND) { |
| 3374 | if (!ND) |
| 3375 | return nullptr; |
| 3376 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) |
| 3377 | return RC; |
| 3378 | |
| 3379 | // Try to find comment from a property for ObjC methods. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3380 | const auto *M = dyn_cast<ObjCMethodDecl>(ND); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3381 | if (!M) |
| 3382 | return nullptr; |
| 3383 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3384 | if (!PDecl) |
| 3385 | return nullptr; |
| 3386 | |
| 3387 | return Ctx.getRawCommentForAnyRedecl(PDecl); |
| 3388 | } |
| 3389 | |
| 3390 | const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, |
| 3391 | const NamedDecl *ND) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3392 | const auto *M = dyn_cast_or_null<ObjCMethodDecl>(ND); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3393 | if (!M || !M->isPropertyAccessor()) |
| 3394 | return nullptr; |
| 3395 | |
| 3396 | // Provide code completion comment for self.GetterName where |
| 3397 | // GetterName is the getter method for a property with name |
| 3398 | // different from the property name (declared via a property |
| 3399 | // getter attribute. |
| 3400 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3401 | if (!PDecl) |
| 3402 | return nullptr; |
| 3403 | if (PDecl->getGetterName() == M->getSelector() && |
| 3404 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 3405 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) |
| 3406 | return RC; |
| 3407 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 3408 | return RC; |
| 3409 | } |
| 3410 | return nullptr; |
| 3411 | } |
| 3412 | |
| 3413 | const RawComment *clang::getParameterComment( |
| 3414 | const ASTContext &Ctx, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3415 | const CodeCompleteConsumer::OverloadCandidate &Result, unsigned ArgIndex) { |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3416 | auto FDecl = Result.getFunction(); |
| 3417 | if (!FDecl) |
| 3418 | return nullptr; |
| 3419 | if (ArgIndex < FDecl->getNumParams()) |
| 3420 | return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); |
| 3421 | return nullptr; |
| 3422 | } |
| 3423 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3424 | /// Add function overload parameter chunks to the given code completion |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3425 | /// string. |
| 3426 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 3427 | const PrintingPolicy &Policy, |
| 3428 | const FunctionDecl *Function, |
| 3429 | const FunctionProtoType *Prototype, |
| 3430 | CodeCompletionBuilder &Result, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3431 | unsigned CurrentArg, unsigned Start = 0, |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3432 | bool InOptional = false) { |
| 3433 | bool FirstParameter = true; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3434 | unsigned NumParams = |
| 3435 | Function ? Function->getNumParams() : Prototype->getNumParams(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3436 | |
| 3437 | for (unsigned P = Start; P != NumParams; ++P) { |
| 3438 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 3439 | // When we see an optional default argument, put that argument and |
| 3440 | // the remaining default arguments into a new, optional string. |
| 3441 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3442 | Result.getCodeCompletionTUInfo()); |
| 3443 | if (!FirstParameter) |
| 3444 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3445 | // Optional sections are nested. |
| 3446 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 3447 | CurrentArg, P, /*InOptional=*/true); |
| 3448 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3449 | return; |
| 3450 | } |
| 3451 | |
| 3452 | if (FirstParameter) |
| 3453 | FirstParameter = false; |
| 3454 | else |
| 3455 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3456 | |
| 3457 | InOptional = false; |
| 3458 | |
| 3459 | // Format the placeholder string. |
| 3460 | std::string Placeholder; |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3461 | if (Function) { |
| 3462 | const ParmVarDecl *Param = Function->getParamDecl(P); |
| 3463 | Placeholder = FormatFunctionParameter(Policy, Param); |
| 3464 | if (Param->hasDefaultArg()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3465 | Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), |
| 3466 | Context.getLangOpts()); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3467 | } else { |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3468 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3469 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3470 | |
| 3471 | if (P == CurrentArg) |
| 3472 | Result.AddCurrentParameterChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3473 | Result.getAllocator().CopyString(Placeholder)); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3474 | else |
| 3475 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 3476 | } |
| 3477 | |
| 3478 | if (Prototype && Prototype->isVariadic()) { |
| 3479 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3480 | Result.getCodeCompletionTUInfo()); |
| 3481 | if (!FirstParameter) |
| 3482 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3483 | |
| 3484 | if (CurrentArg < NumParams) |
| 3485 | Opt.AddPlaceholderChunk("..."); |
| 3486 | else |
| 3487 | Opt.AddCurrentParameterChunk("..."); |
| 3488 | |
| 3489 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3490 | } |
| 3491 | } |
| 3492 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3493 | CodeCompletionString * |
| 3494 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3495 | unsigned CurrentArg, Sema &S, CodeCompletionAllocator &Allocator, |
| 3496 | CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3497 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3499 | // FIXME: Set priority, availability appropriately. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3500 | CodeCompletionBuilder Result(Allocator, CCTUInfo, 1, |
| 3501 | CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3502 | FunctionDecl *FDecl = getFunction(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3503 | const FunctionProtoType *Proto = |
| 3504 | dyn_cast<FunctionProtoType>(getFunctionType()); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3505 | if (!FDecl && !Proto) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3506 | // Function without a prototype. Just give the return type and a |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3507 | // highlighted ellipsis. |
| 3508 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3509 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3510 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3511 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3512 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 3513 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3514 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3515 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3516 | |
| 3517 | if (FDecl) { |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3518 | if (IncludeBriefComments) { |
| 3519 | if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg)) |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3520 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3521 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3522 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3523 | Result.AddTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3524 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3525 | } else { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3526 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3527 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3528 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3529 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3530 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3531 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 3532 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3533 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3534 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3535 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3538 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3539 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3540 | bool PreferredTypeIsPointer) { |
| 3541 | unsigned Priority = CCP_Macro; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3542 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3543 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3544 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3545 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3546 | Priority = CCP_Constant; |
| 3547 | if (PreferredTypeIsPointer) |
| 3548 | Priority = Priority / CCF_SimilarTypeMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3549 | } |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3550 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3551 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3552 | MacroName.equals("true") || MacroName.equals("false")) |
| 3553 | Priority = CCP_Constant; |
| 3554 | // Treat "bool" as a type. |
| 3555 | else if (MacroName.equals("bool")) |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3556 | Priority = CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC : 0); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3558 | return Priority; |
| 3559 | } |
| 3560 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3561 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3562 | if (!D) |
| 3563 | return CXCursor_UnexposedDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3565 | switch (D->getKind()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3566 | case Decl::Enum: |
| 3567 | return CXCursor_EnumDecl; |
| 3568 | case Decl::EnumConstant: |
| 3569 | return CXCursor_EnumConstantDecl; |
| 3570 | case Decl::Field: |
| 3571 | return CXCursor_FieldDecl; |
| 3572 | case Decl::Function: |
| 3573 | return CXCursor_FunctionDecl; |
| 3574 | case Decl::ObjCCategory: |
| 3575 | return CXCursor_ObjCCategoryDecl; |
| 3576 | case Decl::ObjCCategoryImpl: |
| 3577 | return CXCursor_ObjCCategoryImplDecl; |
| 3578 | case Decl::ObjCImplementation: |
| 3579 | return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3580 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3581 | case Decl::ObjCInterface: |
| 3582 | return CXCursor_ObjCInterfaceDecl; |
| 3583 | case Decl::ObjCIvar: |
| 3584 | return CXCursor_ObjCIvarDecl; |
| 3585 | case Decl::ObjCMethod: |
| 3586 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3587 | ? CXCursor_ObjCInstanceMethodDecl |
| 3588 | : CXCursor_ObjCClassMethodDecl; |
| 3589 | case Decl::CXXMethod: |
| 3590 | return CXCursor_CXXMethod; |
| 3591 | case Decl::CXXConstructor: |
| 3592 | return CXCursor_Constructor; |
| 3593 | case Decl::CXXDestructor: |
| 3594 | return CXCursor_Destructor; |
| 3595 | case Decl::CXXConversion: |
| 3596 | return CXCursor_ConversionFunction; |
| 3597 | case Decl::ObjCProperty: |
| 3598 | return CXCursor_ObjCPropertyDecl; |
| 3599 | case Decl::ObjCProtocol: |
| 3600 | return CXCursor_ObjCProtocolDecl; |
| 3601 | case Decl::ParmVar: |
| 3602 | return CXCursor_ParmDecl; |
| 3603 | case Decl::Typedef: |
| 3604 | return CXCursor_TypedefDecl; |
| 3605 | case Decl::TypeAlias: |
| 3606 | return CXCursor_TypeAliasDecl; |
| 3607 | case Decl::TypeAliasTemplate: |
| 3608 | return CXCursor_TypeAliasTemplateDecl; |
| 3609 | case Decl::Var: |
| 3610 | return CXCursor_VarDecl; |
| 3611 | case Decl::Namespace: |
| 3612 | return CXCursor_Namespace; |
| 3613 | case Decl::NamespaceAlias: |
| 3614 | return CXCursor_NamespaceAlias; |
| 3615 | case Decl::TemplateTypeParm: |
| 3616 | return CXCursor_TemplateTypeParameter; |
| 3617 | case Decl::NonTypeTemplateParm: |
| 3618 | return CXCursor_NonTypeTemplateParameter; |
| 3619 | case Decl::TemplateTemplateParm: |
| 3620 | return CXCursor_TemplateTemplateParameter; |
| 3621 | case Decl::FunctionTemplate: |
| 3622 | return CXCursor_FunctionTemplate; |
| 3623 | case Decl::ClassTemplate: |
| 3624 | return CXCursor_ClassTemplate; |
| 3625 | case Decl::AccessSpec: |
| 3626 | return CXCursor_CXXAccessSpecifier; |
| 3627 | case Decl::ClassTemplatePartialSpecialization: |
| 3628 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3629 | case Decl::UsingDirective: |
| 3630 | return CXCursor_UsingDirective; |
| 3631 | case Decl::StaticAssert: |
| 3632 | return CXCursor_StaticAssert; |
| 3633 | case Decl::Friend: |
| 3634 | return CXCursor_FriendDecl; |
| 3635 | case Decl::TranslationUnit: |
| 3636 | return CXCursor_TranslationUnit; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3637 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3638 | case Decl::Using: |
| 3639 | case Decl::UnresolvedUsingValue: |
| 3640 | case Decl::UnresolvedUsingTypename: |
| 3641 | return CXCursor_UsingDeclaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3642 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3643 | case Decl::ObjCPropertyImpl: |
| 3644 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3645 | case ObjCPropertyImplDecl::Dynamic: |
| 3646 | return CXCursor_ObjCDynamicDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3647 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3648 | case ObjCPropertyImplDecl::Synthesize: |
| 3649 | return CXCursor_ObjCSynthesizeDecl; |
| 3650 | } |
Bruno Ricci | e5bcf0b | 2018-12-21 17:52:13 +0000 | [diff] [blame] | 3651 | llvm_unreachable("Unexpected Kind!"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3652 | |
| 3653 | case Decl::Import: |
| 3654 | return CXCursor_ModuleImportDecl; |
| 3655 | |
| 3656 | case Decl::ObjCTypeParam: |
| 3657 | return CXCursor_TemplateTypeParameter; |
| 3658 | |
| 3659 | default: |
| 3660 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 3661 | switch (TD->getTagKind()) { |
| 3662 | case TTK_Interface: // fall through |
| 3663 | case TTK_Struct: |
| 3664 | return CXCursor_StructDecl; |
| 3665 | case TTK_Class: |
| 3666 | return CXCursor_ClassDecl; |
| 3667 | case TTK_Union: |
| 3668 | return CXCursor_UnionDecl; |
| 3669 | case TTK_Enum: |
| 3670 | return CXCursor_EnumDecl; |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3671 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3672 | } |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3673 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3674 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3675 | return CXCursor_UnexposedDecl; |
| 3676 | } |
| 3677 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3678 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3679 | bool LoadExternal, bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3680 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3681 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3682 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3683 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3684 | |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3685 | for (Preprocessor::macro_iterator M = PP.macro_begin(LoadExternal), |
| 3686 | MEnd = PP.macro_end(LoadExternal); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3687 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3688 | auto MD = PP.getMacroDefinition(M->first); |
| 3689 | if (IncludeUndefined || MD) { |
Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3690 | MacroInfo *MI = MD.getMacroInfo(); |
| 3691 | if (MI && MI->isUsedForHeaderGuard()) |
| 3692 | continue; |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3693 | |
Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3694 | Results.AddResult( |
| 3695 | Result(M->first, MI, |
| 3696 | getMacroUsagePriority(M->first->getName(), PP.getLangOpts(), |
| 3697 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3698 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3699 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3701 | Results.ExitScope(); |
| 3702 | } |
| 3703 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3704 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3705 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3706 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3707 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3708 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3710 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3711 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3712 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3713 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3714 | Results.ExitScope(); |
| 3715 | } |
| 3716 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3717 | static void HandleCodeCompleteResults(Sema *S, |
| 3718 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3719 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3720 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3721 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3722 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3723 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Ilya Biryukov | 4110967 | 2018-12-13 15:36:32 +0000 | [diff] [blame] | 3726 | static CodeCompletionContext |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3727 | mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3728 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3729 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3730 | return CodeCompletionContext::CCC_TopLevel; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3731 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3732 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3733 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3734 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3735 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3736 | return CodeCompletionContext::CCC_ObjCInterface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3737 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3738 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3739 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3740 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3741 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3742 | return CodeCompletionContext::CCC_ObjCIvarList; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3743 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3744 | case Sema::PCC_Template: |
| 3745 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3746 | if (S.CurContext->isFileContext()) |
| 3747 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3748 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3749 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3750 | return CodeCompletionContext::CCC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3751 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3752 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3753 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3754 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3755 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3756 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3757 | S.getLangOpts().ObjC) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3758 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3759 | else |
| 3760 | return CodeCompletionContext::CCC_Expression; |
| 3761 | |
| 3762 | case Sema::PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3763 | return CodeCompletionContext::CCC_Expression; |
Ilya Biryukov | 4110967 | 2018-12-13 15:36:32 +0000 | [diff] [blame] | 3764 | case Sema::PCC_Condition: |
| 3765 | return CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3766 | S.getASTContext().BoolTy); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3767 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3768 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3769 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3770 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3771 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3772 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3773 | |
| 3774 | case Sema::PCC_ParenthesizedExpression: |
| 3775 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3777 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3778 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3779 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3780 | |
| 3781 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3784 | /// If we're in a C++ virtual member function, add completion results |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3785 | /// 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] | 3786 | /// overridden function as well as adding new functionality. |
| 3787 | /// |
| 3788 | /// \param S The semantic analysis object for which we are generating results. |
| 3789 | /// |
| 3790 | /// \param InContext This context in which the nested-name-specifier preceding |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3791 | /// the code-completion point |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3792 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3793 | ResultBuilder &Results) { |
| 3794 | // Look through blocks. |
| 3795 | DeclContext *CurContext = S.CurContext; |
| 3796 | while (isa<BlockDecl>(CurContext)) |
| 3797 | CurContext = CurContext->getParent(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3799 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3800 | if (!Method || !Method->isVirtual()) |
| 3801 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3802 | |
| 3803 | // 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] | 3804 | // generate a forwarding call. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3805 | for (auto P : Method->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3806 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3807 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3809 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 3810 | for (const CXXMethodDecl *Overridden : Method->overridden_methods()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3811 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3812 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3813 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3814 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3815 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3816 | // If we need a nested-name-specifier, add one now. |
| 3817 | if (!InContext) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3818 | NestedNameSpecifier *NNS = getRequiredQualification( |
| 3819 | S.Context, CurContext, Overridden->getDeclContext()); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3820 | if (NNS) { |
| 3821 | std::string Str; |
| 3822 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3823 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3824 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3825 | } |
| 3826 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3827 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3828 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3829 | Builder.AddTypedTextChunk( |
| 3830 | Results.getAllocator().CopyString(Overridden->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3831 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3832 | bool FirstParam = true; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3833 | for (auto P : Method->parameters()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3834 | if (FirstParam) |
| 3835 | FirstParam = false; |
| 3836 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3837 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3838 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3839 | Builder.AddPlaceholderChunk( |
| 3840 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3841 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3842 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3843 | Results.AddResult(CodeCompletionResult( |
| 3844 | Builder.TakeString(), CCP_SuperCompletion, CXCursor_CXXMethod, |
| 3845 | CXAvailability_Available, Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3846 | Results.Ignore(Overridden); |
| 3847 | } |
| 3848 | } |
| 3849 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3850 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3851 | ModuleIdPath Path) { |
| 3852 | typedef CodeCompletionResult Result; |
| 3853 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3854 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3855 | CodeCompletionContext::CCC_Other); |
| 3856 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3858 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3859 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3860 | typedef CodeCompletionResult Result; |
| 3861 | if (Path.empty()) { |
| 3862 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3863 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3864 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3865 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3866 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3867 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3868 | Results.AddResult(Result( |
| 3869 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
| 3870 | Modules[I]->isAvailable() ? CXAvailability_Available |
| 3871 | : CXAvailability_NotAvailable)); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3872 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3873 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3874 | // Load the named module. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3875 | Module *Mod = |
| 3876 | PP.getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
| 3877 | /*IsInclusionDirective=*/false); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3878 | // Enumerate submodules. |
| 3879 | if (Mod) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3880 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3881 | SubEnd = Mod->submodule_end(); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3882 | Sub != SubEnd; ++Sub) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3883 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3884 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3885 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3886 | Results.AddResult(Result( |
| 3887 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
| 3888 | (*Sub)->isAvailable() ? CXAvailability_Available |
| 3889 | : CXAvailability_NotAvailable)); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3893 | Results.ExitScope(); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3894 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3895 | Results.data(), Results.size()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3898 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3899 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3900 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3901 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3902 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3903 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3905 | // Determine how to filter results, e.g., so that the names of |
| 3906 | // values (functions, enumerators, function templates, etc.) are |
| 3907 | // only allowed where we can have an expression. |
| 3908 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3909 | case PCC_Namespace: |
| 3910 | case PCC_Class: |
| 3911 | case PCC_ObjCInterface: |
| 3912 | case PCC_ObjCImplementation: |
| 3913 | case PCC_ObjCInstanceVariableList: |
| 3914 | case PCC_Template: |
| 3915 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3916 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3917 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3918 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3919 | break; |
| 3920 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3921 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3922 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3923 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3924 | case PCC_ForInit: |
| 3925 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3926 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3927 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3928 | else |
| 3929 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3930 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3931 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3932 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3933 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3935 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3936 | // Unfiltered |
| 3937 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3940 | // 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] | 3941 | // the member function to filter/prioritize the results list. |
Ilya Biryukov | b45d851b | 2018-12-19 18:01:24 +0000 | [diff] [blame] | 3942 | auto ThisType = getCurrentThisType(); |
| 3943 | if (!ThisType.isNull()) |
| 3944 | Results.setObjectTypeQualifiers(ThisType->getPointeeType().getQualifiers()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3945 | |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 3946 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3947 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3948 | CodeCompleter->includeGlobals(), |
| 3949 | CodeCompleter->loadExternal()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3951 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3952 | Results.ExitScope(); |
| 3953 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3954 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3955 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3956 | case PCC_Expression: |
| 3957 | case PCC_Statement: |
| 3958 | case PCC_RecoveryInFunction: |
| 3959 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3960 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3961 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3963 | case PCC_Namespace: |
| 3964 | case PCC_Class: |
| 3965 | case PCC_ObjCInterface: |
| 3966 | case PCC_ObjCImplementation: |
| 3967 | case PCC_ObjCInstanceVariableList: |
| 3968 | case PCC_Template: |
| 3969 | case PCC_MemberTemplate: |
| 3970 | case PCC_ForInit: |
| 3971 | case PCC_Condition: |
| 3972 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3973 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3974 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3975 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3977 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3978 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3980 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3981 | Results.data(), Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3984 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3985 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3986 | ArrayRef<IdentifierInfo *> SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 3987 | bool AtArgumentExpression, bool IsSuper, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3988 | ResultBuilder &Results); |
| 3989 | |
| 3990 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3991 | bool AllowNonIdentifiers, |
| 3992 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3993 | typedef CodeCompletionResult Result; |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 3994 | ResultBuilder Results( |
| 3995 | *this, CodeCompleter->getAllocator(), |
| 3996 | CodeCompleter->getCodeCompletionTUInfo(), |
| 3997 | AllowNestedNameSpecifiers |
| 3998 | // FIXME: Try to separate codepath leading here to deduce whether we |
| 3999 | // need an existing symbol or a new one. |
| 4000 | ? CodeCompletionContext::CCC_SymbolOrNewName |
| 4001 | : CodeCompletionContext::CCC_NewName); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4002 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4003 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4004 | // Type qualifiers can come after names. |
| 4005 | Results.AddResult(Result("const")); |
| 4006 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4007 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4008 | Results.AddResult(Result("restrict")); |
| 4009 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4010 | if (getLangOpts().CPlusPlus) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4011 | if (getLangOpts().CPlusPlus11 && |
| 4012 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 4013 | DS.getTypeSpecType() == DeclSpec::TST_struct)) |
| 4014 | Results.AddResult("final"); |
| 4015 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4016 | if (AllowNonIdentifiers) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4017 | Results.AddResult(Result("operator")); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4018 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4020 | // Add nested-name-specifiers. |
| 4021 | if (AllowNestedNameSpecifiers) { |
| 4022 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4023 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4024 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4025 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4026 | CodeCompleter->includeGlobals(), |
| 4027 | CodeCompleter->loadExternal()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4028 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | Results.ExitScope(); |
| 4032 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4033 | // If we're in a context where we might have an expression (rather than a |
| 4034 | // declaration), and what we've seen so far is an Objective-C type that could |
| 4035 | // be a receiver of a class message, this may be a class message send with |
| 4036 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 4037 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4038 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4039 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4040 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 4041 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4042 | !DS.isTypeAltiVecVector() && S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4043 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 4044 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4045 | Scope::FunctionPrototypeScope | Scope::AtCatchScope)) == |
| 4046 | 0) { |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4047 | ParsedType T = DS.getRepAsType(); |
| 4048 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4049 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 4052 | // Note that we intentionally suppress macro results here, since we do not |
| 4053 | // encourage using macros to produce the names of entities. |
| 4054 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4055 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 4056 | Results.data(), Results.size()); |
| 4057 | } |
| 4058 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4059 | struct Sema::CodeCompleteExpressionData { |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4060 | CodeCompleteExpressionData(QualType PreferredType = QualType(), |
| 4061 | bool IsParenthesized = false) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4062 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4063 | ObjCCollection(false), IsParenthesized(IsParenthesized) {} |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4065 | QualType PreferredType; |
| 4066 | bool IntegralConstantExpression; |
| 4067 | bool ObjCCollection; |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4068 | bool IsParenthesized; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4069 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4070 | }; |
| 4071 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4072 | /// Perform code-completion in an expression context when we know what |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4073 | /// type we're looking for. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4074 | void Sema::CodeCompleteExpression(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4075 | const CodeCompleteExpressionData &Data) { |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4076 | ResultBuilder Results( |
| 4077 | *this, CodeCompleter->getAllocator(), |
| 4078 | CodeCompleter->getCodeCompletionTUInfo(), |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4079 | CodeCompletionContext( |
| 4080 | Data.IsParenthesized |
| 4081 | ? CodeCompletionContext::CCC_ParenthesizedExpression |
| 4082 | : CodeCompletionContext::CCC_Expression, |
| 4083 | Data.PreferredType)); |
| 4084 | auto PCC = |
| 4085 | Data.IsParenthesized ? PCC_ParenthesizedExpression : PCC_Expression; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4086 | if (Data.ObjCCollection) |
| 4087 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 4088 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4089 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4090 | else if (WantTypesInContext(PCC, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4091 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4092 | else |
| 4093 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4094 | |
| 4095 | if (!Data.PreferredType.isNull()) |
| 4096 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4098 | // Ignore any declarations that we were told that we don't care about. |
| 4099 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 4100 | Results.Ignore(Data.IgnoreDecls[I]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4102 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4103 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4104 | CodeCompleter->includeGlobals(), |
| 4105 | CodeCompleter->loadExternal()); |
| 4106 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4107 | Results.EnterNewScope(); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4108 | AddOrdinaryNameResults(PCC, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4109 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4110 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 4111 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4112 | if (!Data.PreferredType.isNull()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4113 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() || |
| 4114 | Data.PreferredType->isMemberPointerType() || |
| 4115 | Data.PreferredType->isBlockPointerType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4116 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4117 | if (S->getFnParent() && !Data.ObjCCollection && |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 4118 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4119 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 4120 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4121 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4122 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false, |
| 4123 | PreferredTypeIsPointer); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4124 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4125 | Results.data(), Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4128 | void Sema::CodeCompleteExpression(Scope *S, QualType PreferredType, |
| 4129 | bool IsParenthesized) { |
| 4130 | return CodeCompleteExpression( |
| 4131 | S, CodeCompleteExpressionData(PreferredType, IsParenthesized)); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4134 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E, |
| 4135 | QualType PreferredType) { |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 4136 | if (E.isInvalid()) |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4137 | CodeCompleteExpression(S, PreferredType); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4138 | else if (getLangOpts().ObjC) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4139 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4140 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4141 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4142 | /// The set of properties that have already been added, referenced by |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 4143 | /// property name. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4144 | typedef llvm::SmallPtrSet<IdentifierInfo *, 16> AddedPropertiesSet; |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 4145 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4146 | /// Retrieve the container definition, if any? |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4147 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 4148 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 4149 | if (Interface->hasDefinition()) |
| 4150 | return Interface->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4152 | return Interface; |
| 4153 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4155 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4156 | if (Protocol->hasDefinition()) |
| 4157 | return Protocol->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4158 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4159 | return Protocol; |
| 4160 | } |
| 4161 | return Container; |
| 4162 | } |
| 4163 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4164 | /// Adds a block invocation code completion result for the given block |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4165 | /// declaration \p BD. |
| 4166 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
| 4167 | CodeCompletionBuilder &Builder, |
| 4168 | const NamedDecl *BD, |
| 4169 | const FunctionTypeLoc &BlockLoc, |
| 4170 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
| 4171 | Builder.AddResultTypeChunk( |
| 4172 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
| 4173 | Policy, Builder.getAllocator())); |
| 4174 | |
| 4175 | AddTypedNameChunk(Context, Policy, BD, Builder); |
| 4176 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4177 | |
| 4178 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { |
| 4179 | Builder.AddPlaceholderChunk("..."); |
| 4180 | } else { |
| 4181 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { |
| 4182 | if (I) |
| 4183 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 4184 | |
| 4185 | // Format the placeholder string. |
| 4186 | std::string PlaceholderStr = |
| 4187 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
| 4188 | |
| 4189 | if (I == N - 1 && BlockProtoLoc && |
| 4190 | BlockProtoLoc.getTypePtr()->isVariadic()) |
| 4191 | PlaceholderStr += ", ..."; |
| 4192 | |
| 4193 | // Add the placeholder string. |
| 4194 | Builder.AddPlaceholderChunk( |
| 4195 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4200 | } |
| 4201 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4202 | static void |
| 4203 | AddObjCProperties(const CodeCompletionContext &CCContext, |
| 4204 | ObjCContainerDecl *Container, bool AllowCategories, |
| 4205 | bool AllowNullaryMethods, DeclContext *CurContext, |
| 4206 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
| 4207 | bool IsBaseExprStatement = false, |
| 4208 | bool IsClassProperty = false, bool InOriginalClass = true) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4209 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4211 | // Retrieve the definition. |
| 4212 | Container = getContainerDef(Container); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4213 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4214 | // Add properties in this container. |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4215 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4216 | if (!AddedProperties.insert(P->getIdentifier()).second) |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4217 | return; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4218 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4219 | // FIXME: Provide block invocation completion for non-statement |
| 4220 | // expressions. |
| 4221 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
| 4222 | !IsBaseExprStatement) { |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4223 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
| 4224 | if (!InOriginalClass) |
| 4225 | setInBaseClass(R); |
| 4226 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4227 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | // Block setter and invocation completion is provided only when we are able |
| 4231 | // to find the FunctionProtoTypeLoc with parameter names for the block. |
| 4232 | FunctionTypeLoc BlockLoc; |
| 4233 | FunctionProtoTypeLoc BlockProtoLoc; |
| 4234 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
| 4235 | BlockProtoLoc); |
| 4236 | if (!BlockLoc) { |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4237 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
| 4238 | if (!InOriginalClass) |
| 4239 | setInBaseClass(R); |
| 4240 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4241 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | // The default completion result for block properties should be the block |
| 4245 | // invocation completion when the base expression is a statement. |
| 4246 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4247 | Results.getCodeCompletionTUInfo()); |
| 4248 | AddObjCBlockCall(Container->getASTContext(), |
| 4249 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
| 4250 | BlockLoc, BlockProtoLoc); |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4251 | Result R = Result(Builder.TakeString(), P, Results.getBasePriority(P)); |
| 4252 | if (!InOriginalClass) |
| 4253 | setInBaseClass(R); |
| 4254 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4255 | |
| 4256 | // Provide additional block setter completion iff the base expression is a |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4257 | // statement and the block property is mutable. |
| 4258 | if (!P->isReadOnly()) { |
| 4259 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4260 | Results.getCodeCompletionTUInfo()); |
| 4261 | AddResultTypeChunk(Container->getASTContext(), |
| 4262 | getCompletionPrintingPolicy(Results.getSema()), P, |
| 4263 | CCContext.getBaseType(), Builder); |
| 4264 | Builder.AddTypedTextChunk( |
| 4265 | Results.getAllocator().CopyString(P->getName())); |
| 4266 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4267 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4268 | std::string PlaceholderStr = formatBlockPlaceholder( |
| 4269 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
| 4270 | BlockProtoLoc, /*SuppressBlockName=*/true); |
| 4271 | // Add the placeholder string. |
| 4272 | Builder.AddPlaceholderChunk( |
| 4273 | Builder.getAllocator().CopyString(PlaceholderStr)); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4274 | |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 4275 | // When completing blocks properties that return void the default |
| 4276 | // property completion result should show up before the setter, |
| 4277 | // otherwise the setter completion should show up before the default |
| 4278 | // property completion, as we normally want to use the result of the |
| 4279 | // call. |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4280 | Result R = |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 4281 | Result(Builder.TakeString(), P, |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 4282 | Results.getBasePriority(P) + |
| 4283 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
| 4284 | ? CCD_BlockPropertySetter |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4285 | : -CCD_BlockPropertySetter)); |
| 4286 | if (!InOriginalClass) |
| 4287 | setInBaseClass(R); |
| 4288 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4289 | } |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4290 | }; |
| 4291 | |
| 4292 | if (IsClassProperty) { |
| 4293 | for (const auto *P : Container->class_properties()) |
| 4294 | AddProperty(P); |
| 4295 | } else { |
| 4296 | for (const auto *P : Container->instance_properties()) |
| 4297 | AddProperty(P); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4298 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4299 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4300 | // Add nullary methods or implicit class properties |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 4301 | if (AllowNullaryMethods) { |
| 4302 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4303 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4304 | // Adds a method result |
| 4305 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
| 4306 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
| 4307 | if (!Name) |
| 4308 | return; |
| 4309 | if (!AddedProperties.insert(Name).second) |
| 4310 | return; |
| 4311 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4312 | Results.getCodeCompletionTUInfo()); |
| 4313 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
| 4314 | Builder.AddTypedTextChunk( |
| 4315 | Results.getAllocator().CopyString(Name->getName())); |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4316 | Result R = Result(Builder.TakeString(), M, |
| 4317 | CCP_MemberDeclaration + CCD_MethodAsProperty); |
| 4318 | if (!InOriginalClass) |
| 4319 | setInBaseClass(R); |
| 4320 | Results.MaybeAddResult(R, CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4321 | }; |
| 4322 | |
| 4323 | if (IsClassProperty) { |
| 4324 | for (const auto *M : Container->methods()) { |
| 4325 | // Gather the class method that can be used as implicit property |
| 4326 | // getters. Methods with arguments or methods that return void aren't |
| 4327 | // added to the results as they can't be used as a getter. |
| 4328 | if (!M->getSelector().isUnarySelector() || |
| 4329 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) |
| 4330 | continue; |
| 4331 | AddMethod(M); |
| 4332 | } |
| 4333 | } else { |
| 4334 | for (auto *M : Container->methods()) { |
| 4335 | if (M->getSelector().isUnarySelector()) |
| 4336 | AddMethod(M); |
| 4337 | } |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 4338 | } |
| 4339 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4341 | // Add properties in referenced protocols. |
| 4342 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 4343 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4344 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4345 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4346 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4347 | /*InOriginalClass*/ false); |
| 4348 | } else if (ObjCInterfaceDecl *IFace = |
| 4349 | dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4350 | if (AllowCategories) { |
| 4351 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4352 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4353 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4354 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4355 | IsBaseExprStatement, IsClassProperty, |
| 4356 | InOriginalClass); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4357 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4359 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 4360 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4361 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4362 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4363 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4364 | /*InOriginalClass*/ false); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4366 | // Look in the superclass. |
| 4367 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4368 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4369 | AllowNullaryMethods, CurContext, AddedProperties, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4370 | Results, IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4371 | /*InOriginalClass*/ false); |
| 4372 | } else if (const auto *Category = |
| 4373 | dyn_cast<ObjCCategoryDecl>(Container)) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4374 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 4375 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4376 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4377 | CurContext, AddedProperties, Results, |
Sam McCall | 8e9baa3 | 2018-11-20 22:06:54 +0000 | [diff] [blame] | 4378 | IsBaseExprStatement, IsClassProperty, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4379 | /*InOriginalClass*/ false); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4383 | static void |
| 4384 | AddRecordMembersCompletionResults(Sema &SemaRef, ResultBuilder &Results, |
| 4385 | Scope *S, QualType BaseType, RecordDecl *RD, |
| 4386 | Optional<FixItHint> AccessOpFixIt) { |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4387 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 4388 | // for the base object type. |
| 4389 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 4390 | |
| 4391 | // Access to a C/C++ class, struct, or union. |
| 4392 | Results.allowNestedNameSpecifiers(); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4393 | std::vector<FixItHint> FixIts; |
| 4394 | if (AccessOpFixIt) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4395 | FixIts.emplace_back(AccessOpFixIt.getValue()); |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 4396 | CodeCompletionDeclConsumer Consumer(Results, RD, BaseType, std::move(FixIts)); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4397 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
Alex Lorenz | e6afa39 | 2017-05-11 13:48:57 +0000 | [diff] [blame] | 4398 | SemaRef.CodeCompleter->includeGlobals(), |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4399 | /*IncludeDependentBases=*/true, |
| 4400 | SemaRef.CodeCompleter->loadExternal()); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4401 | |
| 4402 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 4403 | if (!Results.empty()) { |
| 4404 | // The "template" keyword can follow "->" or "." in the grammar. |
| 4405 | // However, we only want to suggest the template keyword if something |
| 4406 | // is dependent. |
| 4407 | bool IsDependent = BaseType->isDependentType(); |
| 4408 | if (!IsDependent) { |
| 4409 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 4410 | if (DeclContext *Ctx = DepScope->getEntity()) { |
| 4411 | IsDependent = Ctx->isDependentContext(); |
| 4412 | break; |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (IsDependent) |
| 4417 | Results.AddResult(CodeCompletionResult("template")); |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4422 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4423 | Expr *OtherOpBase, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4424 | SourceLocation OpLoc, bool IsArrow, |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4425 | bool IsBaseExprStatement, |
| 4426 | QualType PreferredType) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4427 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4428 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4429 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4430 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4431 | if (ConvertedBase.isInvalid()) |
| 4432 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4433 | QualType ConvertedBaseType = ConvertedBase.get()->getType(); |
| 4434 | |
| 4435 | enum CodeCompletionContext::Kind contextKind; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (IsArrow) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4438 | if (const auto *Ptr = ConvertedBaseType->getAs<PointerType>()) |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4439 | ConvertedBaseType = Ptr->getPointeeType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4440 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4442 | if (IsArrow) { |
| 4443 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4444 | } else { |
| 4445 | if (ConvertedBaseType->isObjCObjectPointerType() || |
| 4446 | ConvertedBaseType->isObjCObjectOrInterfaceType()) { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4447 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4448 | } else { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4449 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 4450 | } |
| 4451 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4452 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4453 | CodeCompletionContext CCContext(contextKind, ConvertedBaseType); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 4454 | CCContext.setPreferredType(PreferredType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4455 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4456 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4457 | &ResultBuilder::IsMember); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4458 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4459 | auto DoCompletion = [&](Expr *Base, bool IsArrow, |
| 4460 | Optional<FixItHint> AccessOpFixIt) -> bool { |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4461 | if (!Base) |
| 4462 | return false; |
| 4463 | |
| 4464 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4465 | if (ConvertedBase.isInvalid()) |
| 4466 | return false; |
| 4467 | Base = ConvertedBase.get(); |
| 4468 | |
| 4469 | QualType BaseType = Base->getType(); |
| 4470 | |
| 4471 | if (IsArrow) { |
| 4472 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4473 | BaseType = Ptr->getPointeeType(); |
| 4474 | else if (BaseType->isObjCObjectPointerType()) |
| 4475 | /*Do nothing*/; |
| 4476 | else |
| 4477 | return false; |
| 4478 | } |
| 4479 | |
| 4480 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 4481 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, |
| 4482 | Record->getDecl(), |
| 4483 | std::move(AccessOpFixIt)); |
| 4484 | } else if (const auto *TST = |
| 4485 | BaseType->getAs<TemplateSpecializationType>()) { |
| 4486 | TemplateName TN = TST->getTemplateName(); |
| 4487 | if (const auto *TD = |
| 4488 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) { |
| 4489 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 4490 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4491 | std::move(AccessOpFixIt)); |
| 4492 | } |
| 4493 | } else if (const auto *ICNT = BaseType->getAs<InjectedClassNameType>()) { |
| 4494 | if (auto *RD = ICNT->getDecl()) |
| 4495 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4496 | std::move(AccessOpFixIt)); |
| 4497 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { |
| 4498 | // Objective-C property reference. |
| 4499 | AddedPropertiesSet AddedProperties; |
| 4500 | |
| 4501 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4502 | BaseType->getAsObjCInterfacePointerType()) { |
| 4503 | // Add property results based on our interface. |
| 4504 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
| 4505 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
| 4506 | /*AllowNullaryMethods=*/true, CurContext, |
| 4507 | AddedProperties, Results, IsBaseExprStatement); |
| 4508 | } |
| 4509 | |
| 4510 | // Add properties from the protocols in a qualified interface. |
| 4511 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) |
| 4512 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 4513 | CurContext, AddedProperties, Results, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4514 | IsBaseExprStatement, /*IsClassProperty*/ false, |
| 4515 | /*InOriginalClass*/ false); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4516 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 4517 | (!IsArrow && BaseType->isObjCObjectType())) { |
| 4518 | // Objective-C instance variable access. |
| 4519 | ObjCInterfaceDecl *Class = nullptr; |
| 4520 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4521 | BaseType->getAs<ObjCObjectPointerType>()) |
| 4522 | Class = ObjCPtr->getInterfaceDecl(); |
| 4523 | else |
| 4524 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
| 4525 | |
| 4526 | // Add all ivars from this class and its superclasses. |
| 4527 | if (Class) { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 4528 | CodeCompletionDeclConsumer Consumer(Results, Class, BaseType); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4529 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
| 4530 | LookupVisibleDecls( |
| 4531 | Class, LookupMemberName, Consumer, CodeCompleter->includeGlobals(), |
| 4532 | /*IncludeDependentBases=*/false, CodeCompleter->loadExternal()); |
| 4533 | } |
| 4534 | } |
| 4535 | |
| 4536 | // FIXME: How do we cope with isa? |
| 4537 | return true; |
| 4538 | }; |
| 4539 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4540 | Results.EnterNewScope(); |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 4541 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4542 | bool CompletionSucceded = DoCompletion(Base, IsArrow, None); |
| 4543 | if (CodeCompleter->includeFixIts()) { |
| 4544 | const CharSourceRange OpRange = |
| 4545 | CharSourceRange::getTokenRange(OpLoc, OpLoc); |
| 4546 | CompletionSucceded |= DoCompletion( |
| 4547 | OtherOpBase, !IsArrow, |
| 4548 | FixItHint::CreateReplacement(OpRange, IsArrow ? "." : "->")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4549 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4551 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4552 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4553 | if (!CompletionSucceded) |
| 4554 | return; |
| 4555 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4556 | // Hand off the results found for code completion. |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4557 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4558 | Results.data(), Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4561 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, |
| 4562 | IdentifierInfo &ClassName, |
| 4563 | SourceLocation ClassNameLoc, |
| 4564 | bool IsBaseExprStatement) { |
| 4565 | IdentifierInfo *ClassNamePtr = &ClassName; |
| 4566 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); |
| 4567 | if (!IFace) |
| 4568 | return; |
| 4569 | CodeCompletionContext CCContext( |
| 4570 | CodeCompletionContext::CCC_ObjCPropertyAccess); |
| 4571 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4572 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| 4573 | &ResultBuilder::IsMember); |
| 4574 | Results.EnterNewScope(); |
| 4575 | AddedPropertiesSet AddedProperties; |
| 4576 | AddObjCProperties(CCContext, IFace, true, |
| 4577 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, |
| 4578 | Results, IsBaseExprStatement, |
| 4579 | /*IsClassProperty=*/true); |
| 4580 | Results.ExitScope(); |
| 4581 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4582 | Results.data(), Results.size()); |
| 4583 | } |
| 4584 | |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 4585 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4586 | if (!CodeCompleter) |
| 4587 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4588 | |
| 4589 | ResultBuilder::LookupFilter Filter = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4590 | enum CodeCompletionContext::Kind ContextKind = |
| 4591 | CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4592 | switch ((DeclSpec::TST)TagSpec) { |
| 4593 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4594 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4595 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4596 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4598 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4599 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4600 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4601 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4602 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4603 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4604 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4605 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4606 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4607 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4608 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4609 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4610 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4611 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4612 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4613 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4614 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4615 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4616 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4617 | |
| 4618 | // First pass: look for tags. |
| 4619 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4620 | LookupVisibleDecls(S, LookupTagName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4621 | CodeCompleter->includeGlobals(), |
| 4622 | CodeCompleter->loadExternal()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4623 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4624 | if (CodeCompleter->includeGlobals()) { |
| 4625 | // Second pass: look for nested name specifiers. |
| 4626 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4627 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 4628 | CodeCompleter->includeGlobals(), |
| 4629 | CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4630 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4632 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4633 | Results.data(), Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4636 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, |
| 4637 | const LangOptions &LangOpts) { |
| 4638 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 4639 | Results.AddResult("const"); |
| 4640 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 4641 | Results.AddResult("volatile"); |
| 4642 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 4643 | Results.AddResult("restrict"); |
| 4644 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 4645 | Results.AddResult("_Atomic"); |
| 4646 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 4647 | Results.AddResult("__unaligned"); |
| 4648 | } |
| 4649 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4650 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4651 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4652 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4653 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4654 | Results.EnterNewScope(); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4655 | AddTypeQualifierResults(DS, Results, LangOpts); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4656 | Results.ExitScope(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4657 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4658 | Results.data(), Results.size()); |
| 4659 | } |
| 4660 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4661 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, |
| 4662 | const VirtSpecifiers *VS) { |
| 4663 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4664 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4665 | CodeCompletionContext::CCC_TypeQualifiers); |
| 4666 | Results.EnterNewScope(); |
| 4667 | AddTypeQualifierResults(DS, Results, LangOpts); |
| 4668 | if (LangOpts.CPlusPlus11) { |
| 4669 | Results.AddResult("noexcept"); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 4670 | if (D.getContext() == DeclaratorContext::MemberContext && |
| 4671 | !D.isCtorOrDtor() && !D.isStaticMember()) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4672 | if (!VS || !VS->isFinalSpecified()) |
| 4673 | Results.AddResult("final"); |
| 4674 | if (!VS || !VS->isOverrideSpecified()) |
| 4675 | Results.AddResult("override"); |
| 4676 | } |
| 4677 | } |
| 4678 | Results.ExitScope(); |
| 4679 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4680 | Results.data(), Results.size()); |
| 4681 | } |
| 4682 | |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 4683 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 4684 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 4685 | } |
| 4686 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4687 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4688 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4689 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4690 | |
Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 4691 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back().getPointer(); |
Kadir Cetinkaya | 6d57266 | 2018-10-23 13:49:37 +0000 | [diff] [blame] | 4692 | // Condition expression might be invalid, do not continue in this case. |
| 4693 | if (!Switch->getCond()) |
| 4694 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4695 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 4696 | if (!type->isEnumeralType()) { |
| 4697 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4698 | Data.IntegralConstantExpression = true; |
| 4699 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4700 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4701 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4702 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4703 | // Code-complete the cases of a switch statement over an enumeration type |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4704 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4705 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4706 | if (EnumDecl *Def = Enum->getDefinition()) |
| 4707 | Enum = Def; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4709 | // Determine which enumerators we have already seen in the switch statement. |
| 4710 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 4711 | // token, in case we are code-completing in the middle of the switch and not |
| 4712 | // at the end. However, we aren't able to do so at the moment. |
| 4713 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4714 | NestedNameSpecifier *Qualifier = nullptr; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4715 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4716 | SC = SC->getNextSwitchCase()) { |
| 4717 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 4718 | if (!Case) |
| 4719 | continue; |
| 4720 | |
| 4721 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4722 | if (auto *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 4723 | if (auto *Enumerator = |
| 4724 | dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4725 | // We look into the AST of the case statement to determine which |
| 4726 | // enumerator was named. Alternatively, we could compute the value of |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4727 | // the integral constant expression, then compare it against the |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4728 | // values of each enumerator. However, value-based approach would not |
| 4729 | // work as well with C++ templates where enumerators declared within a |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4730 | // template are type- and value-dependent. |
| 4731 | EnumeratorsSeen.insert(Enumerator); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4733 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 4734 | // 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] | 4735 | // |
| 4736 | // switch (TagD.getKind()) { |
| 4737 | // case TagDecl::TK_enum: |
| 4738 | // break; |
| 4739 | // case XXX |
| 4740 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4741 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4742 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 4743 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4744 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4745 | } |
| 4746 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4747 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4748 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4749 | // If there are no prior enumerators in C++, check whether we have to |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4750 | // qualify the names of the enumerators that we suggest, because they |
| 4751 | // may not be visible in this scope. |
Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 4752 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4753 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4754 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4755 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4756 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4757 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4758 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4759 | Results.EnterNewScope(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4760 | for (auto *E : Enum->enumerators()) { |
| 4761 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4762 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4763 | |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4764 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4765 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4766 | } |
| 4767 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4769 | if (CodeCompleter->includeMacros()) { |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4770 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4771 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4772 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4773 | Results.data(), Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 4776 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4777 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4778 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4779 | |
| 4780 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4781 | if (!Args[I]) |
| 4782 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4784 | return false; |
| 4785 | } |
| 4786 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4787 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 4788 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4789 | static void mergeCandidatesWithResults( |
| 4790 | Sema &SemaRef, SmallVectorImpl<ResultCandidate> &Results, |
| 4791 | OverloadCandidateSet &CandidateSet, SourceLocation Loc) { |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 4792 | // Sort the overload candidate set by placing the best overloads first. |
| 4793 | llvm::stable_sort(CandidateSet, [&](const OverloadCandidate &X, |
| 4794 | const OverloadCandidate &Y) { |
| 4795 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc, |
| 4796 | CandidateSet.getKind()); |
| 4797 | }); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4798 | |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 4799 | // Add the remaining viable overload candidates as code-completion results. |
| 4800 | for (OverloadCandidate &Candidate : CandidateSet) { |
| 4801 | if (Candidate.Function && Candidate.Function->isDeleted()) |
| 4802 | continue; |
| 4803 | if (Candidate.Viable) |
| 4804 | Results.push_back(ResultCandidate(Candidate.Function)); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4805 | } |
| 4806 | } |
| 4807 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4808 | /// 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] | 4809 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4810 | static QualType getParamType(Sema &SemaRef, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4811 | ArrayRef<ResultCandidate> Candidates, unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4812 | |
| 4813 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 4814 | // up to N, return the type of the Nth parameter if it is the same for all |
| 4815 | // overload candidates. |
| 4816 | QualType ParamType; |
| 4817 | for (auto &Candidate : Candidates) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4818 | if (const auto *FType = Candidate.getFunctionType()) |
| 4819 | if (const auto *Proto = dyn_cast<FunctionProtoType>(FType)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4820 | if (N < Proto->getNumParams()) { |
| 4821 | if (ParamType.isNull()) |
| 4822 | ParamType = Proto->getParamType(N); |
| 4823 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4824 | ParamType.getNonReferenceType(), |
| 4825 | Proto->getParamType(N).getNonReferenceType())) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4826 | // Otherwise return a default-constructed QualType. |
| 4827 | return QualType(); |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | return ParamType; |
| 4832 | } |
| 4833 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4834 | static QualType |
| 4835 | ProduceSignatureHelp(Sema &SemaRef, Scope *S, |
| 4836 | MutableArrayRef<ResultCandidate> Candidates, |
| 4837 | unsigned CurrentArg, SourceLocation OpenParLoc) { |
| 4838 | if (Candidates.empty()) |
| 4839 | return QualType(); |
| 4840 | SemaRef.CodeCompleter->ProcessOverloadCandidates( |
| 4841 | SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc); |
| 4842 | return getParamType(SemaRef, Candidates, CurrentArg); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4845 | QualType Sema::ProduceCallSignatureHelp(Scope *S, Expr *Fn, |
| 4846 | ArrayRef<Expr *> Args, |
| 4847 | SourceLocation OpenParLoc) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4848 | if (!CodeCompleter) |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4849 | return QualType(); |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4850 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4851 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4852 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4853 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4854 | Expr::hasAnyTypeDependentArguments(Args)) { |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4855 | return QualType(); |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4856 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4857 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4858 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4859 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4860 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4861 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4862 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4863 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4864 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4865 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4866 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4867 | /*PartialOverloading=*/true); |
| 4868 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4869 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4870 | if (UME->hasExplicitTemplateArgs()) { |
| 4871 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4872 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4873 | } |
Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Add the base as first argument (use a nullptr if the base is implicit). |
| 4876 | SmallVector<Expr *, 12> ArgExprs( |
| 4877 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4878 | ArgExprs.append(Args.begin(), Args.end()); |
| 4879 | UnresolvedSet<8> Decls; |
| 4880 | Decls.append(UME->decls_begin(), UME->decls_end()); |
Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 4881 | const bool FirstArgumentIsBase = !UME->isImplicitAccess() && UME->getBase(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4882 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4883 | /*SuppressUsedConversions=*/false, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4884 | /*PartialOverloading=*/true, FirstArgumentIsBase); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4885 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4886 | FunctionDecl *FD = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4887 | if (auto *MCE = dyn_cast<MemberExpr>(NakedFn)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4888 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4889 | else if (auto *DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4890 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4891 | 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] | 4892 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4893 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4894 | Results.push_back(ResultCandidate(FD)); |
| 4895 | else |
| 4896 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4897 | Args, CandidateSet, |
| 4898 | /*SuppressUsedConversions=*/false, |
| 4899 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4900 | |
| 4901 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4902 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4903 | // 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] | 4904 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4905 | if (isCompleteType(Loc, NakedFn->getType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4906 | DeclarationName OpName = |
| 4907 | Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4908 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4909 | LookupQualifiedName(R, DC); |
| 4910 | R.suppressDiagnostics(); |
| 4911 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4912 | ArgExprs.append(Args.begin(), Args.end()); |
| 4913 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4914 | /*ExplicitArgs=*/nullptr, |
| 4915 | /*SuppressUsedConversions=*/false, |
| 4916 | /*PartialOverloading=*/true); |
| 4917 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4918 | } else { |
| 4919 | // Lastly we check whether expression's type is function pointer or |
| 4920 | // function. |
| 4921 | QualType T = NakedFn->getType(); |
| 4922 | if (!T->getPointeeType().isNull()) |
| 4923 | T = T->getPointeeType(); |
| 4924 | |
| 4925 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4926 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4927 | /*PartialOverloading=*/true) || |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4928 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4929 | Results.push_back(ResultCandidate(FP)); |
| 4930 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4931 | // 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] | 4932 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4933 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4934 | } |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4935 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4936 | QualType ParamType = |
| 4937 | ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
| 4938 | return !CandidateSet.empty() ? ParamType : QualType(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4941 | QualType Sema::ProduceConstructorSignatureHelp(Scope *S, QualType Type, |
| 4942 | SourceLocation Loc, |
| 4943 | ArrayRef<Expr *> Args, |
| 4944 | SourceLocation OpenParLoc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4945 | if (!CodeCompleter) |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4946 | return QualType(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4947 | |
| 4948 | // A complete type is needed to lookup for constructors. |
Ilya Biryukov | fb9dde7 | 2018-05-14 13:50:36 +0000 | [diff] [blame] | 4949 | CXXRecordDecl *RD = |
| 4950 | isCompleteType(Loc, Type) ? Type->getAsCXXRecordDecl() : nullptr; |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4951 | if (!RD) |
| 4952 | return Type; |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4953 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4954 | // FIXME: Provide support for member initializers. |
| 4955 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4956 | |
| 4957 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4958 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4959 | for (NamedDecl *C : LookupConstructors(RD)) { |
| 4960 | if (auto *FD = dyn_cast<FunctionDecl>(C)) { |
| 4961 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), Args, |
| 4962 | CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4963 | /*SuppressUsedConversions=*/false, |
Hans Wennborg | d2b9fc8 | 2019-05-06 09:51:10 +0000 | [diff] [blame^] | 4964 | /*PartialOverloading=*/true); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 4965 | } else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4966 | AddTemplateOverloadCandidate( |
| 4967 | FTD, DeclAccessPair::make(FTD, C->getAccess()), |
| 4968 | /*ExplicitTemplateArgs=*/nullptr, Args, CandidateSet, |
| 4969 | /*SuppressUsedConversions=*/false, |
Hans Wennborg | d2b9fc8 | 2019-05-06 09:51:10 +0000 | [diff] [blame^] | 4970 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | SmallVector<ResultCandidate, 8> Results; |
| 4975 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4976 | return ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 4979 | QualType Sema::ProduceCtorInitMemberSignatureHelp( |
| 4980 | Scope *S, Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy, |
| 4981 | ArrayRef<Expr *> ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc) { |
| 4982 | if (!CodeCompleter) |
| 4983 | return QualType(); |
| 4984 | |
| 4985 | CXXConstructorDecl *Constructor = |
| 4986 | dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
| 4987 | if (!Constructor) |
| 4988 | return QualType(); |
| 4989 | // FIXME: Add support for Base class constructors as well. |
| 4990 | if (ValueDecl *MemberDecl = tryLookupCtorInitMemberDecl( |
| 4991 | Constructor->getParent(), SS, TemplateTypeTy, II)) |
| 4992 | return ProduceConstructorSignatureHelp(getCurScope(), MemberDecl->getType(), |
| 4993 | MemberDecl->getLocation(), ArgExprs, |
| 4994 | OpenParLoc); |
| 4995 | return QualType(); |
| 4996 | } |
| 4997 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4998 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4999 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5000 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5001 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5002 | return; |
| 5003 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5004 | |
Ilya Biryukov | ebf0a6d | 2018-11-07 10:02:31 +0000 | [diff] [blame] | 5005 | CodeCompleteExpressionData Data; |
| 5006 | Data.PreferredType = VD->getType(); |
| 5007 | // Ignore VD to avoid completing the variable itself, e.g. in 'int foo = ^'. |
| 5008 | Data.IgnoreDecls.push_back(VD); |
| 5009 | |
| 5010 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5013 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5014 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5015 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5016 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 5017 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 5018 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5020 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5021 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5022 | CodeCompleter->includeGlobals(), |
| 5023 | CodeCompleter->loadExternal()); |
| 5024 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5025 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5026 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5027 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5028 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5029 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5030 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 5031 | if (Results.includeCodePatterns()) { |
| 5032 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5033 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5034 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5035 | Builder.AddPlaceholderChunk("statements"); |
| 5036 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5037 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5038 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5039 | Results.AddResult(Builder.TakeString()); |
| 5040 | |
| 5041 | // "else if" block |
| 5042 | Builder.AddTypedTextChunk("else"); |
| 5043 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5044 | Builder.AddTextChunk("if"); |
| 5045 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5046 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5047 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5048 | Builder.AddPlaceholderChunk("condition"); |
| 5049 | else |
| 5050 | Builder.AddPlaceholderChunk("expression"); |
| 5051 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 5052 | if (Results.includeCodePatterns()) { |
| 5053 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5054 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5055 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5056 | Builder.AddPlaceholderChunk("statements"); |
| 5057 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5058 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5059 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5060 | Results.AddResult(Builder.TakeString()); |
| 5061 | |
| 5062 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5064 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 5065 | AddPrettyFunctionResults(getLangOpts(), Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5066 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5067 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 5068 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5069 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5070 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5071 | Results.data(), Results.size()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5074 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 5075 | bool EnteringContext, QualType BaseType) { |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5076 | if (SS.isEmpty() || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 5077 | return; |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 5078 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5079 | // We want to keep the scope specifier even if it's invalid (e.g. the scope |
| 5080 | // "a::b::" is not corresponding to any context/namespace in the AST), since |
| 5081 | // it can be useful for global code completion which have information about |
| 5082 | // contexts/symbols that are not in the AST. |
| 5083 | if (SS.isInvalid()) { |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5084 | CodeCompletionContext CC(CodeCompletionContext::CCC_Symbol); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5085 | CC.setCXXScopeSpecifier(SS); |
Eric Liu | 206740e | 2019-02-21 11:22:58 +0000 | [diff] [blame] | 5086 | // As SS is invalid, we try to collect accessible contexts from the current |
| 5087 | // scope with a dummy lookup so that the completion consumer can try to |
| 5088 | // guess what the specified scope is. |
| 5089 | ResultBuilder DummyResults(*this, CodeCompleter->getAllocator(), |
| 5090 | CodeCompleter->getCodeCompletionTUInfo(), CC); |
| 5091 | if (S->getEntity()) { |
| 5092 | CodeCompletionDeclConsumer Consumer(DummyResults, S->getEntity(), |
| 5093 | BaseType); |
| 5094 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5095 | /*IncludeGlobalScope=*/false, |
| 5096 | /*LoadExternal=*/false); |
| 5097 | } |
| 5098 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5099 | DummyResults.getCompletionContext(), nullptr, 0); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5100 | return; |
| 5101 | } |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 5102 | // Always pretend to enter a context to ensure that a dependent type |
| 5103 | // resolves to a dependent record. |
| 5104 | DeclContext *Ctx = computeDeclContext(SS, /*EnteringContext=*/true); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5105 | if (!Ctx) |
| 5106 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 5107 | |
| 5108 | // Try to instantiate any non-dependent declaration contexts before |
| 5109 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5110 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 5111 | return; |
| 5112 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5113 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5114 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5115 | CodeCompletionContext::CCC_Symbol); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5116 | Results.EnterNewScope(); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5117 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5118 | // The "template" keyword can follow "::" in the grammar, but only |
| 5119 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 5120 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5121 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 5122 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5123 | |
| 5124 | // Add calls to overridden virtual functions, if there are any. |
| 5125 | // |
| 5126 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 5127 | // in a context that permits expressions. This is a general issue with |
| 5128 | // qualified-id completions. |
| 5129 | if (!EnteringContext) |
| 5130 | MaybeAddOverrideCalls(*this, Ctx, Results); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5131 | Results.ExitScope(); |
| 5132 | |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5133 | if (CodeCompleter->includeNamespaceLevelDecls() || |
| 5134 | (!Ctx->isNamespace() && !Ctx->isTranslationUnit())) { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 5135 | CodeCompletionDeclConsumer Consumer(Results, Ctx, BaseType); |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5136 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer, |
| 5137 | /*IncludeGlobalScope=*/true, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5138 | /*IncludeDependentBases=*/true, |
| 5139 | CodeCompleter->loadExternal()); |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 5140 | } |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 5141 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 5142 | auto CC = Results.getCompletionContext(); |
| 5143 | CC.setCXXScopeSpecifier(SS); |
| 5144 | |
| 5145 | HandleCodeCompleteResults(this, CodeCompleter, CC, Results.data(), |
| 5146 | Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 5147 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5148 | |
| 5149 | void Sema::CodeCompleteUsing(Scope *S) { |
| 5150 | if (!CodeCompleter) |
| 5151 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5153 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5154 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5155 | // This can be both a using alias or using |
| 5156 | // declaration, in the former we expect a new name and a |
| 5157 | // symbol in the latter case. |
| 5158 | CodeCompletionContext::CCC_SymbolOrNewName, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5159 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5160 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5162 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 5163 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5164 | Results.AddResult(CodeCompletionResult("namespace")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5165 | |
| 5166 | // After "using", we can see anything that would start a |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5167 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5168 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5169 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5170 | CodeCompleter->includeGlobals(), |
| 5171 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5172 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5173 | |
| 5174 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5175 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 5179 | if (!CodeCompleter) |
| 5180 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5181 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5182 | // After "using namespace", we expect to see a namespace name or namespace |
| 5183 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5184 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5185 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5186 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5187 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5188 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5189 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5190 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5191 | CodeCompleter->includeGlobals(), |
| 5192 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5193 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5194 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5195 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5198 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5199 | if (!CodeCompleter) |
| 5200 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5201 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5202 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5203 | if (!S->getParent()) |
| 5204 | Ctx = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5205 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5206 | bool SuppressedGlobalResults = |
| 5207 | Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5208 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5209 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5210 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5211 | SuppressedGlobalResults |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5212 | ? CodeCompletionContext::CCC_Namespace |
| 5213 | : CodeCompletionContext::CCC_Other, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5214 | &ResultBuilder::IsNamespace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5215 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5216 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5217 | // We only want to see those namespaces that have already been defined |
| 5218 | // within this scope, because its likely that the user is creating an |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5219 | // extended namespace declaration. Keep track of the most recent |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5220 | // definition of each namespace. |
| 5221 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5222 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5223 | NS(Ctx->decls_begin()), |
| 5224 | NSEnd(Ctx->decls_end()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5225 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5226 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5227 | |
| 5228 | // Add the most recent definition (or extended definition) of each |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5229 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5230 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5231 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5232 | NS = OrigToLatest.begin(), |
| 5233 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5234 | NS != NSEnd; ++NS) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5235 | Results.AddResult( |
| 5236 | CodeCompletionResult(NS->second, Results.getBasePriority(NS->second), |
| 5237 | nullptr), |
| 5238 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5239 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5240 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5241 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5242 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5243 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5246 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5247 | if (!CodeCompleter) |
| 5248 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5250 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5251 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5252 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5253 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5254 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5255 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5256 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5257 | CodeCompleter->includeGlobals(), |
| 5258 | CodeCompleter->loadExternal()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5259 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5260 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 5263 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 5264 | if (!CodeCompleter) |
| 5265 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5266 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5267 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5268 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5269 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5270 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5271 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5272 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5273 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5274 | // Add the names of overloadable operators. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5275 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 5276 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 5277 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5278 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5280 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 5281 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 5282 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5283 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5284 | CodeCompleter->includeGlobals(), |
| 5285 | CodeCompleter->loadExternal()); |
| 5286 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 5287 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5288 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 5289 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5290 | |
| 5291 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5292 | Results.data(), Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 5293 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 5294 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5295 | void Sema::CodeCompleteConstructorInitializer( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5296 | Decl *ConstructorD, ArrayRef<CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 5297 | if (!ConstructorD) |
| 5298 | return; |
| 5299 | |
| 5300 | AdjustDeclIfTemplate(ConstructorD); |
| 5301 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5302 | auto *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5303 | if (!Constructor) |
| 5304 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5305 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5306 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5307 | CodeCompleter->getCodeCompletionTUInfo(), |
Kadir Cetinkaya | b006e09 | 2018-10-24 15:23:49 +0000 | [diff] [blame] | 5308 | CodeCompletionContext::CCC_Symbol); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5309 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5311 | // Fill in any already-initialized fields or base classes. |
| 5312 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 5313 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5314 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5315 | if (Initializers[I]->isBaseInitializer()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5316 | InitializedBases.insert(Context.getCanonicalType( |
| 5317 | QualType(Initializers[I]->getBaseClass(), 0))); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5318 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5319 | InitializedFields.insert( |
| 5320 | cast<FieldDecl>(Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5321 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5322 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5323 | // Add completions for base classes. |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 5324 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 5325 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5326 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5327 | |
| 5328 | auto GenerateCCS = [&](const NamedDecl *ND, const char *Name) { |
| 5329 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5330 | Results.getCodeCompletionTUInfo()); |
| 5331 | Builder.AddTypedTextChunk(Name); |
| 5332 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5333 | if (const auto *Function = dyn_cast<FunctionDecl>(ND)) |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5334 | AddFunctionParameterChunks(PP, Policy, Function, Builder); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5335 | else if (const auto *FunTemplDecl = dyn_cast<FunctionTemplateDecl>(ND)) |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5336 | AddFunctionParameterChunks(PP, Policy, FunTemplDecl->getTemplatedDecl(), |
| 5337 | Builder); |
| 5338 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5339 | return Builder.TakeString(); |
| 5340 | }; |
| 5341 | auto AddDefaultCtorInit = [&](const char *Name, const char *Type, |
| 5342 | const NamedDecl *ND) { |
| 5343 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5344 | Results.getCodeCompletionTUInfo()); |
| 5345 | Builder.AddTypedTextChunk(Name); |
| 5346 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5347 | Builder.AddPlaceholderChunk(Type); |
| 5348 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5349 | if (ND) { |
| 5350 | auto CCR = CodeCompletionResult( |
| 5351 | Builder.TakeString(), ND, |
| 5352 | SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration); |
| 5353 | if (isa<FieldDecl>(ND)) |
| 5354 | CCR.CursorKind = CXCursor_MemberRef; |
| 5355 | return Results.AddResult(CCR); |
| 5356 | } |
| 5357 | return Results.AddResult(CodeCompletionResult( |
| 5358 | Builder.TakeString(), |
| 5359 | SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration)); |
| 5360 | }; |
| 5361 | auto AddCtorsWithName = [&](const CXXRecordDecl *RD, unsigned int Priority, |
| 5362 | const char *Name, const FieldDecl *FD) { |
| 5363 | if (!RD) |
| 5364 | return AddDefaultCtorInit(Name, |
| 5365 | FD ? Results.getAllocator().CopyString( |
| 5366 | FD->getType().getAsString(Policy)) |
| 5367 | : Name, |
| 5368 | FD); |
| 5369 | auto Ctors = getConstructors(Context, RD); |
| 5370 | if (Ctors.begin() == Ctors.end()) |
| 5371 | return AddDefaultCtorInit(Name, Name, RD); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5372 | for (const NamedDecl *Ctor : Ctors) { |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5373 | auto CCR = CodeCompletionResult(GenerateCCS(Ctor, Name), RD, Priority); |
| 5374 | CCR.CursorKind = getCursorKindForDecl(Ctor); |
| 5375 | Results.AddResult(CCR); |
| 5376 | } |
| 5377 | }; |
| 5378 | auto AddBase = [&](const CXXBaseSpecifier &Base) { |
| 5379 | const char *BaseName = |
| 5380 | Results.getAllocator().CopyString(Base.getType().getAsString(Policy)); |
| 5381 | const auto *RD = Base.getType()->getAsCXXRecordDecl(); |
| 5382 | AddCtorsWithName( |
| 5383 | RD, SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration, |
| 5384 | BaseName, nullptr); |
| 5385 | }; |
| 5386 | auto AddField = [&](const FieldDecl *FD) { |
| 5387 | const char *FieldName = |
| 5388 | Results.getAllocator().CopyString(FD->getIdentifier()->getName()); |
| 5389 | const CXXRecordDecl *RD = FD->getType()->getAsCXXRecordDecl(); |
| 5390 | AddCtorsWithName( |
| 5391 | RD, SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration, |
| 5392 | FieldName, FD); |
| 5393 | }; |
| 5394 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5395 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5396 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 5397 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5398 | SawLastInitializer = |
| 5399 | !Initializers.empty() && Initializers.back()->isBaseInitializer() && |
| 5400 | Context.hasSameUnqualifiedType( |
| 5401 | Base.getType(), QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5402 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5403 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5404 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5405 | AddBase(Base); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5406 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5407 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5408 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5409 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 5410 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5411 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 5412 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5413 | SawLastInitializer = |
| 5414 | !Initializers.empty() && Initializers.back()->isBaseInitializer() && |
| 5415 | Context.hasSameUnqualifiedType( |
| 5416 | Base.getType(), QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5417 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5418 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5419 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5420 | AddBase(Base); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5421 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5422 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5423 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5424 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5425 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5426 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 5427 | .second) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5428 | SawLastInitializer = !Initializers.empty() && |
| 5429 | Initializers.back()->isAnyMemberInitializer() && |
| 5430 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5431 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5432 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5433 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5434 | if (!Field->getDeclName()) |
| 5435 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5436 | |
Kadir Cetinkaya | fabaaaa | 2018-11-01 15:54:18 +0000 | [diff] [blame] | 5437 | AddField(Field); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5438 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5439 | } |
| 5440 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5441 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5442 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5443 | Results.data(), Results.size()); |
| 5444 | } |
| 5445 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5446 | /// Determine whether this scope denotes a namespace. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5447 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5448 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5449 | if (!DC) |
| 5450 | return false; |
| 5451 | |
| 5452 | return DC->isFileContext(); |
| 5453 | } |
| 5454 | |
| 5455 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 5456 | bool AfterAmpersand) { |
| 5457 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5458 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5459 | CodeCompletionContext::CCC_Other); |
| 5460 | Results.EnterNewScope(); |
| 5461 | |
| 5462 | // Note what has already been captured. |
| 5463 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 5464 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5465 | for (const auto &C : Intro.Captures) { |
| 5466 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5467 | IncludedThis = true; |
| 5468 | continue; |
| 5469 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5470 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5471 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5472 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5473 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5474 | // Look for other capturable variables. |
| 5475 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 5476 | for (const auto *D : S->decls()) { |
| 5477 | const auto *Var = dyn_cast<VarDecl>(D); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5478 | if (!Var || !Var->hasLocalStorage() || Var->hasAttr<BlocksAttr>()) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5479 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5480 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5481 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 5482 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5483 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | // Add 'this', if it would be valid. |
| 5488 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 5489 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5490 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5491 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5492 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5493 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5494 | Results.data(), Results.size()); |
| 5495 | } |
| 5496 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5497 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 5498 | /// Keyword, depending on whether NeedAt is true or false. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5499 | #define OBJC_AT_KEYWORD_NAME(NeedAt, Keyword) ((NeedAt) ? "@" Keyword : Keyword) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5500 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5501 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5502 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5503 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5504 | // Since we have an implementation, we can end it. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5505 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5506 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5507 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5508 | Results.getCodeCompletionTUInfo()); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5509 | if (LangOpts.ObjC) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5510 | // @dynamic |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5511 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5512 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5513 | Builder.AddPlaceholderChunk("property"); |
| 5514 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5516 | // @synthesize |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5517 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5518 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5519 | Builder.AddPlaceholderChunk("property"); |
| 5520 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5521 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5524 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5525 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5526 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5527 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5528 | // Since we have an interface or protocol, we can end it. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5529 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5530 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5531 | if (LangOpts.ObjC) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5532 | // @property |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5533 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "property"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5535 | // @required |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5536 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "required"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5537 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5538 | // @optional |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5539 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5540 | } |
| 5541 | } |
| 5542 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5543 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5544 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5545 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5546 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5547 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5548 | // @class name ; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5549 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5550 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5551 | Builder.AddPlaceholderChunk("name"); |
| 5552 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5553 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5554 | if (Results.includeCodePatterns()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5555 | // @interface name |
| 5556 | // FIXME: Could introduce the whole pattern, including superclasses and |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5557 | // such. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5558 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5559 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5560 | Builder.AddPlaceholderChunk("class"); |
| 5561 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5563 | // @protocol name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5564 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5565 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5566 | Builder.AddPlaceholderChunk("protocol"); |
| 5567 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5568 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5569 | // @implementation name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5570 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5571 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5572 | Builder.AddPlaceholderChunk("class"); |
| 5573 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5574 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5575 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5576 | // @compatibility_alias name |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5577 | Builder.AddTypedTextChunk( |
| 5578 | OBJC_AT_KEYWORD_NAME(NeedAt, "compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5579 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5580 | Builder.AddPlaceholderChunk("alias"); |
| 5581 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5582 | Builder.AddPlaceholderChunk("class"); |
| 5583 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 5584 | |
| 5585 | if (Results.getSema().getLangOpts().Modules) { |
| 5586 | // @import name |
| 5587 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 5588 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5589 | Builder.AddPlaceholderChunk("module"); |
| 5590 | Results.AddResult(Result(Builder.TakeString())); |
| 5591 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5594 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5595 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5596 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5597 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5598 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5599 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5600 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5601 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5602 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5603 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5604 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5605 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5606 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5607 | Results.data(), Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5610 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5611 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5612 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5613 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5614 | |
| 5615 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5616 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5617 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 5618 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5619 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5620 | Builder.AddResultTypeChunk(EncodeType); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5621 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5622 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5623 | Builder.AddPlaceholderChunk("type-name"); |
| 5624 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5625 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5627 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5628 | Builder.AddResultTypeChunk("Protocol *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5629 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5630 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5631 | Builder.AddPlaceholderChunk("protocol-name"); |
| 5632 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5633 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5634 | |
| 5635 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5636 | Builder.AddResultTypeChunk("SEL"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5637 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5638 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5639 | Builder.AddPlaceholderChunk("selector"); |
| 5640 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5641 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5642 | |
| 5643 | // @"string" |
| 5644 | Builder.AddResultTypeChunk("NSString *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5645 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "\"")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5646 | Builder.AddPlaceholderChunk("string"); |
| 5647 | Builder.AddTextChunk("\""); |
| 5648 | Results.AddResult(Result(Builder.TakeString())); |
| 5649 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5650 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5651 | Builder.AddResultTypeChunk("NSArray *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5652 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5653 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5654 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 5655 | Results.AddResult(Result(Builder.TakeString())); |
| 5656 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5657 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5658 | Builder.AddResultTypeChunk("NSDictionary *"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5659 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5660 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5661 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5662 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5663 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5664 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5665 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5667 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5668 | Builder.AddResultTypeChunk("id"); |
| 5669 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5670 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5671 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5672 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5675 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5676 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5677 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5678 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5679 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5680 | if (Results.includeCodePatterns()) { |
| 5681 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 5682 | // { statements } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5683 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5684 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5685 | Builder.AddPlaceholderChunk("statements"); |
| 5686 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5687 | Builder.AddTextChunk("@catch"); |
| 5688 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5689 | Builder.AddPlaceholderChunk("parameter"); |
| 5690 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5691 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5692 | Builder.AddPlaceholderChunk("statements"); |
| 5693 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5694 | Builder.AddTextChunk("@finally"); |
| 5695 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5696 | Builder.AddPlaceholderChunk("statements"); |
| 5697 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5698 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5699 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5701 | // @throw |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5702 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5703 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5704 | Builder.AddPlaceholderChunk("expression"); |
| 5705 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5707 | if (Results.includeCodePatterns()) { |
| 5708 | // @synchronized ( expression ) { statements } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5709 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5710 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5711 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5712 | Builder.AddPlaceholderChunk("expression"); |
| 5713 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5714 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5715 | Builder.AddPlaceholderChunk("statements"); |
| 5716 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5717 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5718 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5719 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5720 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5721 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5722 | ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5723 | typedef CodeCompletionResult Result; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5724 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "private"))); |
| 5725 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "protected"))); |
| 5726 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "public"))); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5727 | if (LangOpts.ObjC) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5728 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt, "package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5732 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5733 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5734 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5735 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5736 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5737 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5738 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5739 | Results.data(), Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5743 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5744 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5745 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5746 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5747 | AddObjCStatementResults(Results, false); |
| 5748 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5749 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5750 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5751 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5755 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5756 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5757 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5758 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5759 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5760 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5761 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5762 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5765 | /// Determine whether the addition of the given flag to an Objective-C |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5766 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5767 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5768 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5769 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5770 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5771 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5772 | Attributes |= NewFlag; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5773 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5774 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5775 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 5776 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5777 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5778 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5779 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5780 | unsigned AssignCopyRetMask = |
| 5781 | Attributes & |
| 5782 | (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 5783 | ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain | |
| 5784 | ObjCDeclSpec::DQ_PR_strong | ObjCDeclSpec::DQ_PR_weak); |
| 5785 | if (AssignCopyRetMask && AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5786 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5787 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5788 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5789 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 5790 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5791 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5792 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5793 | return false; |
| 5794 | } |
| 5795 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5796 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5797 | if (!CodeCompleter) |
| 5798 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5799 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5800 | unsigned Attributes = ODS.getPropertyAttributes(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5802 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5803 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5804 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5805 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5806 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5807 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5808 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5809 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5810 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5811 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 5812 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5813 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5814 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5815 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5816 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5817 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5818 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5819 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5820 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5821 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5822 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5823 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 5824 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5825 | |
| 5826 | // 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] | 5827 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5828 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5829 | Results.AddResult(CodeCompletionResult("weak")); |
| 5830 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5831 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5832 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 5833 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5834 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5835 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5836 | Setter.AddPlaceholderChunk("method"); |
| 5837 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5838 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5839 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5840 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 5841 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5842 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5843 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5844 | Getter.AddPlaceholderChunk("method"); |
| 5845 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5846 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5847 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 5848 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 5849 | Results.AddResult(CodeCompletionResult("nullable")); |
| 5850 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 5851 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 5852 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5853 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5854 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5855 | Results.data(), Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5856 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5857 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5858 | /// Describes the kind of Objective-C method that we want to find |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5859 | /// via code completion. |
| 5860 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 5861 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 5862 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5863 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5864 | }; |
| 5865 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5866 | static bool isAcceptableObjCSelector(Selector Sel, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5867 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5868 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5869 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5870 | if (NumSelIdents > Sel.getNumArgs()) |
| 5871 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5873 | switch (WantKind) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5874 | case MK_Any: |
| 5875 | break; |
| 5876 | case MK_ZeroArgSelector: |
| 5877 | return Sel.isUnarySelector(); |
| 5878 | case MK_OneArgSelector: |
| 5879 | return Sel.getNumArgs() == 1; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5880 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5881 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5882 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 5883 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5885 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 5886 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 5887 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5889 | return true; |
| 5890 | } |
| 5891 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5892 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 5893 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5894 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5895 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5896 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5897 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5898 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5899 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5900 | /// A set of selectors, which is used to avoid introducing multiple |
| 5901 | /// completions with the same selector into the result set. |
| 5902 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5903 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5904 | /// Add all of the Objective-C methods in the given Objective-C |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5905 | /// container to the set of results. |
| 5906 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5907 | /// The container will be a class, protocol, category, or implementation of |
| 5908 | /// any of the above. This mether will recurse to include methods from |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5909 | /// the superclasses of classes along with their categories, protocols, and |
| 5910 | /// implementations. |
| 5911 | /// |
| 5912 | /// \param Container the container in which we'll look to find methods. |
| 5913 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5914 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5915 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5916 | /// |
| 5917 | /// \param CurContext the context in which we're performing the lookup that |
| 5918 | /// finds methods. |
| 5919 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5920 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5921 | /// when it has the same number of parameters as we have selector identifiers. |
| 5922 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5923 | /// \param Results the structure into which we'll add results. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5924 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5925 | bool WantInstanceMethods, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5926 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5927 | DeclContext *CurContext, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5928 | VisitedSelectorSet &Selectors, bool AllowSameLength, |
| 5929 | ResultBuilder &Results, bool InOriginalClass = true, |
| 5930 | bool IsRootClass = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5931 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5932 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5933 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5934 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5935 | for (ObjCMethodDecl *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5936 | // The instance methods on the root class can be messaged via the |
| 5937 | // metaclass. |
| 5938 | if (M->isInstanceMethod() == WantInstanceMethods || |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5939 | (IsRootClass && !WantInstanceMethods)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5940 | // Check whether the selector identifiers we've been given are a |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5941 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5942 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5943 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5944 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5945 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5946 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5947 | |
| 5948 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5949 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5950 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5951 | if (!InOriginalClass) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 5952 | setInBaseClass(R); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5953 | Results.MaybeAddResult(R, CurContext); |
| 5954 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5955 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5956 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5957 | // Visit the protocols of protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5958 | if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5959 | if (Protocol->hasDefinition()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5960 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 5961 | Protocol->getReferencedProtocols(); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5962 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5963 | E = Protocols.end(); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5964 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5965 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5966 | Selectors, AllowSameLength, Results, false, IsRootClass); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5967 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5968 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5969 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5970 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5971 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5972 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5973 | // Add methods in protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5974 | for (ObjCProtocolDecl *I : IFace->protocols()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5975 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5976 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5977 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5978 | // Add methods in categories. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5979 | for (ObjCCategoryDecl *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5980 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5981 | CurContext, Selectors, AllowSameLength, Results, |
| 5982 | InOriginalClass, IsRootClass); |
| 5983 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5984 | // Add a categories protocol methods. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 5985 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 5986 | CatDecl->getReferencedProtocols(); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5987 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5988 | E = Protocols.end(); |
| 5989 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5990 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5991 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5992 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5993 | // Add methods in category implementations. |
| 5994 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5995 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5996 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5997 | IsRootClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5998 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6000 | // Add methods in superclass. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6001 | // Avoid passing in IsRootClass since root classes won't have super classes. |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6002 | if (IFace->getSuperClass()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6003 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 6004 | SelIdents, CurContext, Selectors, AllowSameLength, Results, |
| 6005 | /*IsRootClass=*/false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6006 | |
| 6007 | // Add methods in our implementation, if any. |
| 6008 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 6009 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 6010 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 6011 | IsRootClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6014 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6015 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6016 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6017 | if (!Class) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6018 | if (ObjCCategoryDecl *Category = |
| 6019 | dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6020 | Class = Category->getClassInterface(); |
| 6021 | |
| 6022 | if (!Class) |
| 6023 | return; |
| 6024 | } |
| 6025 | |
| 6026 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6027 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6028 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6029 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6030 | Results.EnterNewScope(); |
| 6031 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6032 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6033 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6034 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6035 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6036 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6037 | Results.data(), Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6040 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6041 | // Try to find the interface where setters might live. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6042 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6043 | if (!Class) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6044 | if (ObjCCategoryDecl *Category = |
| 6045 | dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6046 | Class = Category->getClassInterface(); |
| 6047 | |
| 6048 | if (!Class) |
| 6049 | return; |
| 6050 | } |
| 6051 | |
| 6052 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6053 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6054 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6055 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6056 | Results.EnterNewScope(); |
| 6057 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6058 | VisitedSelectorSet Selectors; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6059 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, Selectors, |
| 6060 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6061 | |
| 6062 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6063 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6064 | Results.data(), Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6065 | } |
| 6066 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6067 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 6068 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6069 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6070 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6071 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6072 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6073 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6074 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 6075 | bool AddedInOut = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6076 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6077 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 6078 | Results.AddResult("in"); |
| 6079 | Results.AddResult("inout"); |
| 6080 | AddedInOut = true; |
| 6081 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6082 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6083 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 6084 | Results.AddResult("out"); |
| 6085 | if (!AddedInOut) |
| 6086 | Results.AddResult("inout"); |
| 6087 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6088 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6089 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 6090 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6091 | Results.AddResult("bycopy"); |
| 6092 | Results.AddResult("byref"); |
| 6093 | Results.AddResult("oneway"); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6094 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6095 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 6096 | Results.AddResult("nonnull"); |
| 6097 | Results.AddResult("nullable"); |
| 6098 | Results.AddResult("null_unspecified"); |
| 6099 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6100 | |
| 6101 | // 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] | 6102 | // identifier IBAction refers to a macro, provide a completion item for |
| 6103 | // an action, e.g., |
| 6104 | // IBAction)<#selector#>:(id)sender |
| 6105 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 6106 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6107 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6108 | Results.getCodeCompletionTUInfo(), |
| 6109 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6110 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6111 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6112 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6113 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 6114 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6115 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 6116 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 6117 | Builder.AddTextChunk("sender"); |
| 6118 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 6119 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 6120 | |
| 6121 | // If we're completing the return type, provide 'instancetype'. |
| 6122 | if (!IsParameter) { |
| 6123 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 6124 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6125 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6126 | // Add various builtin type names and specifiers. |
| 6127 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 6128 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6129 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6130 | // Add the various type names |
| 6131 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 6132 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 6133 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 6134 | CodeCompleter->includeGlobals(), |
| 6135 | CodeCompleter->loadExternal()); |
| 6136 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6137 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 6138 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6139 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6140 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 6141 | Results.data(), Results.size()); |
| 6142 | } |
| 6143 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6144 | /// When we have an expression with type "id", we may assume |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6145 | /// that it has some more-specific class type based on knowledge of |
| 6146 | /// common uses of Objective-C. This routine returns that class type, |
| 6147 | /// or NULL if no better result could be determined. |
| 6148 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6149 | auto *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6150 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6151 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6152 | |
| 6153 | Selector Sel = Msg->getSelector(); |
| 6154 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6155 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6156 | |
| 6157 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 6158 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6159 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6160 | |
| 6161 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 6162 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6163 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6164 | |
| 6165 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6166 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 6167 | switch (Msg->getReceiverKind()) { |
| 6168 | case ObjCMessageExpr::Class: |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6169 | if (const ObjCObjectType *ObjType = |
| 6170 | Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6171 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 6172 | break; |
| 6173 | |
| 6174 | case ObjCMessageExpr::Instance: { |
| 6175 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 6176 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 6177 | IFace = Ptr->getInterfaceDecl(); |
| 6178 | break; |
| 6179 | } |
| 6180 | |
| 6181 | case ObjCMessageExpr::SuperInstance: |
| 6182 | case ObjCMessageExpr::SuperClass: |
| 6183 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6187 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6188 | |
| 6189 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 6190 | if (Method->isInstanceMethod()) |
| 6191 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6192 | .Case("retain", IFace) |
| 6193 | .Case("strong", IFace) |
| 6194 | .Case("autorelease", IFace) |
| 6195 | .Case("copy", IFace) |
| 6196 | .Case("copyWithZone", IFace) |
| 6197 | .Case("mutableCopy", IFace) |
| 6198 | .Case("mutableCopyWithZone", IFace) |
| 6199 | .Case("awakeFromCoder", IFace) |
| 6200 | .Case("replacementObjectFromCoder", IFace) |
| 6201 | .Case("class", IFace) |
| 6202 | .Case("classForCoder", IFace) |
| 6203 | .Case("superclass", Super) |
| 6204 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6205 | |
| 6206 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6207 | .Case("new", IFace) |
| 6208 | .Case("alloc", IFace) |
| 6209 | .Case("allocWithZone", IFace) |
| 6210 | .Case("class", IFace) |
| 6211 | .Case("superclass", Super) |
| 6212 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6215 | // 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] | 6216 | // most likely case of forwarding all of our arguments to the superclass |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6217 | // function. |
| 6218 | /// |
| 6219 | /// \param S The semantic analysis object. |
| 6220 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 6221 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6222 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 6223 | /// |
| 6224 | /// \param SelIdents The identifiers in the selector that have already been |
| 6225 | /// provided as arguments for a send to "super". |
| 6226 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6227 | /// \param Results The set of results to augment. |
| 6228 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6229 | /// \returns the Objective-C method declaration that would be invoked by |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6230 | /// this "super" completion. If NULL, no completion was added. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6231 | static ObjCMethodDecl * |
| 6232 | AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 6233 | ArrayRef<IdentifierInfo *> SelIdents, |
| 6234 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6235 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 6236 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6237 | return nullptr; |
| 6238 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6239 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 6240 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6241 | return nullptr; |
| 6242 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6243 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6244 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6245 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 6246 | // Check in the class |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6247 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6248 | CurMethod->isInstanceMethod()); |
| 6249 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6250 | // Check in categories or class extensions. |
| 6251 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 6252 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6253 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6254 | CurMethod->isInstanceMethod()))) |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6255 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6256 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 6257 | } |
| 6258 | } |
| 6259 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6260 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6261 | return nullptr; |
| 6262 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6263 | // Check whether the superclass method has the same signature. |
| 6264 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 6265 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6266 | return nullptr; |
| 6267 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6268 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6269 | CurPEnd = CurMethod->param_end(), |
| 6270 | SuperP = SuperMethod->param_begin(); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6271 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 6272 | // Make sure the parameter types are compatible. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6273 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6274 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6275 | return nullptr; |
| 6276 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6277 | // Make sure we have a parameter name to forward! |
| 6278 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6279 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6280 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6281 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6282 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6283 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6284 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6285 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6286 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6287 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6288 | Results.getCompletionContext().getBaseType(), Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6289 | |
| 6290 | // If we need the "super" keyword, add it (plus some spacing). |
| 6291 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6292 | Builder.AddTypedTextChunk("super"); |
| 6293 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6294 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6296 | Selector Sel = CurMethod->getSelector(); |
| 6297 | if (Sel.isUnarySelector()) { |
| 6298 | if (NeedSuperKeyword) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6299 | Builder.AddTextChunk( |
| 6300 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6301 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6302 | Builder.AddTypedTextChunk( |
| 6303 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6304 | } else { |
| 6305 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 6306 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6307 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6308 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6309 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6310 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6311 | Builder.AddInformativeChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6312 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6313 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6314 | Builder.AddTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6315 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6316 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6317 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6318 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6319 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6320 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6321 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6322 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6323 | } |
| 6324 | } |
| 6325 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6326 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 6327 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 6328 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6329 | return SuperMethod; |
| 6330 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6331 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6332 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6333 | typedef CodeCompletionResult Result; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6334 | ResultBuilder Results( |
| 6335 | *this, CodeCompleter->getAllocator(), |
| 6336 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6337 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
| 6338 | getLangOpts().CPlusPlus11 |
| 6339 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 6340 | : &ResultBuilder::IsObjCMessageReceiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6342 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 6343 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 6344 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 6345 | CodeCompleter->includeGlobals(), |
| 6346 | CodeCompleter->loadExternal()); |
| 6347 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6348 | // If we are in an Objective-C method inside a class that has a superclass, |
| 6349 | // add "super" as an option. |
| 6350 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 6351 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6352 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6353 | Results.AddResult(Result("super")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6354 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6355 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6356 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6357 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6358 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 6359 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6360 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6361 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6362 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6363 | if (CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 6364 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 6365 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6366 | Results.data(), Results.size()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6369 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6370 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6371 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6372 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6373 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6374 | // Figure out which interface we're in. |
| 6375 | CDecl = CurMethod->getClassInterface(); |
| 6376 | if (!CDecl) |
| 6377 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6378 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6379 | // Find the superclass of this class. |
| 6380 | CDecl = CDecl->getSuperClass(); |
| 6381 | if (!CDecl) |
| 6382 | return; |
| 6383 | |
| 6384 | if (CurMethod->isInstanceMethod()) { |
| 6385 | // We are inside an instance method, which means that the message |
| 6386 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6387 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6388 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6389 | AtArgumentExpression, CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | // Fall through to send to the superclass in CDecl. |
| 6393 | } else { |
| 6394 | // "super" may be the name of a type or variable. Figure out which |
| 6395 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 6396 | IdentifierInfo *Super = getSuperIdentifier(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6397 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, LookupOrdinaryName); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6398 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 6399 | // "super" names an interface. Use it. |
| 6400 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6401 | if (const ObjCObjectType *Iface = |
| 6402 | Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6403 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6404 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 6405 | // "super" names an unresolved type; we can't be more specific. |
| 6406 | } else { |
| 6407 | // Assume that "super" names some kind of value and parse that way. |
| 6408 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 6409 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6410 | UnqualifiedId id; |
| 6411 | id.setIdentifier(Super, SuperLoc); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 6412 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 6413 | /*HasTrailingLParen=*/false, |
| 6414 | /*IsAddressOfOperand=*/false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6415 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6416 | SelIdents, AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | // Fall through |
| 6420 | } |
| 6421 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6422 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6423 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6424 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6425 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6426 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6427 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6430 | /// 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] | 6431 | /// send, determine the preferred type (if any) for that argument expression. |
| 6432 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 6433 | unsigned NumSelIdents) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6434 | typedef CodeCompletionResult Result; |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6435 | ASTContext &Context = Results.getSema().Context; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6436 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6437 | QualType PreferredType; |
| 6438 | unsigned BestPriority = CCP_Unlikely * 2; |
| 6439 | Result *ResultsData = Results.data(); |
| 6440 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 6441 | Result &R = ResultsData[I]; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6442 | if (R.Kind == Result::RK_Declaration && |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6443 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 6444 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 6445 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6446 | if (NumSelIdents <= Method->param_size()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6447 | QualType MyPreferredType = |
| 6448 | Method->parameters()[NumSelIdents - 1]->getType(); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6449 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 6450 | BestPriority = R.Priority; |
| 6451 | PreferredType = MyPreferredType; |
| 6452 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 6453 | MyPreferredType)) { |
| 6454 | PreferredType = QualType(); |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | } |
| 6459 | } |
| 6460 | |
| 6461 | return PreferredType; |
| 6462 | } |
| 6463 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6464 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6465 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6466 | ArrayRef<IdentifierInfo *> SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6467 | bool AtArgumentExpression, bool IsSuper, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6468 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6469 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6470 | ObjCInterfaceDecl *CDecl = nullptr; |
| 6471 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6472 | // If the given name refers to an interface type, retrieve the |
| 6473 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6474 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6475 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6476 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6477 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 6478 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6479 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6480 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6481 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 6482 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6483 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6484 | |
| 6485 | // 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] | 6486 | // completion. |
| 6487 | if (IsSuper) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6488 | if (ObjCMethodDecl *SuperMethod = |
| 6489 | AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6490 | Results.Ignore(SuperMethod); |
| 6491 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6493 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6494 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6495 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6496 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6498 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6499 | if (CDecl) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6500 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, SemaRef.CurContext, |
| 6501 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6502 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6503 | // We're messaging "id" as a type; provide all class/factory methods. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6504 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6505 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6506 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6507 | if (SemaRef.getExternalSource()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6508 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6509 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6510 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6511 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6512 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6513 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6514 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6515 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6516 | } |
| 6517 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6518 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6519 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6520 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6521 | M != MEnd; ++M) { |
| 6522 | for (ObjCMethodList *MethList = &M->second.second; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6523 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6524 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6525 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6526 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6527 | Result R(MethList->getMethod(), |
| 6528 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6529 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6530 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6531 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6532 | } |
| 6533 | } |
| 6534 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6535 | |
| 6536 | Results.ExitScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6537 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6539 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6540 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6541 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6542 | bool IsSuper) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6543 | |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6544 | QualType T = this->GetTypeFromParser(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6545 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6546 | ResultBuilder Results( |
| 6547 | *this, CodeCompleter->getAllocator(), |
| 6548 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6549 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, T, |
| 6550 | SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6551 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6552 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6553 | AtArgumentExpression, IsSuper, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6554 | |
| 6555 | // 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] | 6556 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6557 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6558 | // code-complete the expression using the corresponding parameter type as |
| 6559 | // our preferred type, improving completion results. |
| 6560 | if (AtArgumentExpression) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6561 | QualType PreferredType = |
| 6562 | getPreferredArgumentTypeForMessageSend(Results, SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6563 | if (PreferredType.isNull()) |
| 6564 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6565 | else |
| 6566 | CodeCompleteExpression(S, PreferredType); |
| 6567 | return; |
| 6568 | } |
| 6569 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6570 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6571 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 6574 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6575 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6576 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6577 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6578 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6579 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6580 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6582 | // If necessary, apply function/array conversion to the receiver. |
| 6583 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6584 | if (RecExpr) { |
| 6585 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 6586 | if (Conv.isInvalid()) // conversion failed. bail. |
| 6587 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6588 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6589 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6590 | QualType ReceiverType = RecExpr |
| 6591 | ? RecExpr->getType() |
| 6592 | : Super ? Context.getObjCObjectPointerType( |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6593 | Context.getObjCInterfaceType(Super)) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6594 | : Context.getObjCIdType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6595 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6596 | // If we're messaging an expression with type "id" or "Class", check |
| 6597 | // whether we know something special about the receiver that allows |
| 6598 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6599 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6600 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 6601 | if (ReceiverType->isObjCClassType()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6602 | return CodeCompleteObjCClassMessage( |
| 6603 | S, ParsedType::make(Context.getObjCInterfaceType(IFace)), SelIdents, |
| 6604 | AtArgumentExpression, Super); |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6605 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6606 | ReceiverType = |
| 6607 | Context.getObjCObjectPointerType(Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6608 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6609 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 6610 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 6611 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6612 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6613 | ReceiverType = RecExpr->getType(); |
| 6614 | } |
| 6615 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6616 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6617 | // Build the set of methods we can see. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6618 | ResultBuilder Results( |
| 6619 | *this, CodeCompleter->getAllocator(), |
| 6620 | CodeCompleter->getCodeCompletionTUInfo(), |
| 6621 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
| 6622 | ReceiverType, SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6623 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6624 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6625 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6626 | // 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] | 6627 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6628 | if (Super) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6629 | if (ObjCMethodDecl *SuperMethod = |
| 6630 | AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6631 | Results.Ignore(SuperMethod); |
| 6632 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6634 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6635 | // others. |
| 6636 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 6637 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6638 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6639 | // Keep track of the selectors we've already added. |
| 6640 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6641 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6642 | // Handle messages to Class. This really isn't a message to an instance |
| 6643 | // method, so we treat it the same way we would treat a message send to a |
| 6644 | // class method. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6645 | if (ReceiverType->isObjCClassType() || |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6646 | ReceiverType->isObjCQualifiedClassType()) { |
| 6647 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6648 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6649 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, CurContext, |
| 6650 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6651 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6652 | } |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6653 | // Handle messages to a qualified ID ("id<foo>"). |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6654 | else if (const ObjCObjectPointerType *QualID = |
| 6655 | ReceiverType->getAsObjCQualifiedIdType()) { |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6656 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6657 | for (auto *I : QualID->quals()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6658 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, Selectors, |
| 6659 | AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6660 | } |
| 6661 | // Handle messages to a pointer to interface type. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6662 | else if (const ObjCObjectPointerType *IFacePtr = |
| 6663 | ReceiverType->getAsObjCInterfacePointerType()) { |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6664 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6665 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6666 | CurContext, Selectors, AtArgumentExpression, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6667 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6668 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6669 | for (auto *I : IFacePtr->quals()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6670 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, Selectors, |
| 6671 | AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6672 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6673 | // Handle messages to "id". |
| 6674 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6675 | // We're messaging "id", so provide all instance methods we know |
| 6676 | // about as code-completion results. |
| 6677 | |
| 6678 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6679 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6680 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6681 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6682 | I != N; ++I) { |
| 6683 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6684 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6685 | continue; |
| 6686 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6687 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6688 | } |
| 6689 | } |
| 6690 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6691 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6692 | MEnd = MethodPool.end(); |
| 6693 | M != MEnd; ++M) { |
| 6694 | for (ObjCMethodList *MethList = &M->second.first; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6695 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6696 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6697 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6698 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6699 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6700 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6701 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6702 | Result R(MethList->getMethod(), |
| 6703 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6704 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6705 | R.AllParametersAreInformative = false; |
| 6706 | Results.MaybeAddResult(R, CurContext); |
| 6707 | } |
| 6708 | } |
| 6709 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6710 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6711 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6712 | // 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] | 6713 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6714 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6715 | // code-complete the expression using the corresponding parameter type as |
| 6716 | // our preferred type, improving completion results. |
| 6717 | if (AtArgumentExpression) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6718 | QualType PreferredType = |
| 6719 | getPreferredArgumentTypeForMessageSend(Results, SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6720 | if (PreferredType.isNull()) |
| 6721 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6722 | else |
| 6723 | CodeCompleteExpression(S, PreferredType); |
| 6724 | return; |
| 6725 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6726 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6727 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6728 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6729 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6730 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6731 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6732 | DeclGroupPtrTy IterationVar) { |
| 6733 | CodeCompleteExpressionData Data; |
| 6734 | Data.ObjCCollection = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6735 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6736 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 6737 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6738 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 6739 | if (*I) |
| 6740 | Data.IgnoreDecls.push_back(*I); |
| 6741 | } |
| 6742 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6743 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6744 | CodeCompleteExpression(S, Data); |
| 6745 | } |
| 6746 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6747 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 6748 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6749 | // If we have an external source, load the entire class method |
| 6750 | // pool from the AST file. |
| 6751 | if (ExternalSource) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6752 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); I != N; |
| 6753 | ++I) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6754 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 6755 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 6756 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6757 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6758 | ReadMethodPool(Sel); |
| 6759 | } |
| 6760 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6762 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6763 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6764 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6765 | Results.EnterNewScope(); |
| 6766 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6767 | MEnd = MethodPool.end(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6768 | M != MEnd; ++M) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6769 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6770 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6771 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6772 | continue; |
| 6773 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6774 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6775 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6776 | if (Sel.isUnarySelector()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6777 | Builder.AddTypedTextChunk( |
| 6778 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6779 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6780 | continue; |
| 6781 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6782 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6783 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6784 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6785 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6786 | if (!Accumulator.empty()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6787 | Builder.AddInformativeChunk( |
| 6788 | Builder.getAllocator().CopyString(Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6789 | Accumulator.clear(); |
| 6790 | } |
| 6791 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6792 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6793 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6794 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6795 | } |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6796 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6797 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6798 | } |
| 6799 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6800 | |
| 6801 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6802 | Results.data(), Results.size()); |
| 6803 | } |
| 6804 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6805 | /// Add all of the protocol declarations that we find in the given |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6806 | /// (translation unit) context. |
| 6807 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6808 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6809 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6810 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6811 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6812 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6813 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6814 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6815 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6816 | Results.AddResult( |
| 6817 | Result(Proto, Results.getBasePriority(Proto), nullptr), CurContext, |
| 6818 | nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6819 | } |
| 6820 | } |
| 6821 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6822 | void Sema::CodeCompleteObjCProtocolReferences( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6823 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6824 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6825 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6826 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6827 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6828 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6829 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6831 | // Tell the result set to ignore all of the protocols we have |
| 6832 | // already seen. |
| 6833 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6834 | for (const IdentifierLocPair &Pair : Protocols) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6835 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6836 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6837 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6838 | // Add all protocols. |
| 6839 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6840 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6841 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6842 | Results.ExitScope(); |
| 6843 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6844 | |
| 6845 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6846 | Results.data(), Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6850 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6851 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6852 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6853 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6854 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6855 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6856 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6857 | // Add all protocols. |
| 6858 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 6859 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6860 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6861 | Results.ExitScope(); |
| 6862 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6863 | |
| 6864 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6865 | Results.data(), Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6866 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6867 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6868 | /// Add all of the Objective-C interface declarations that we find in |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6869 | /// the given (translation unit) context. |
| 6870 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 6871 | bool OnlyForwardDeclarations, |
| 6872 | bool OnlyUnimplemented, |
| 6873 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6874 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6875 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6876 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6877 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6878 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 6879 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6880 | (!OnlyUnimplemented || !Class->getImplementation())) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6881 | Results.AddResult( |
| 6882 | Result(Class, Results.getBasePriority(Class), nullptr), CurContext, |
| 6883 | nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6884 | } |
| 6885 | } |
| 6886 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6887 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6888 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6889 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6890 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6891 | Results.EnterNewScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6892 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6893 | if (CodeCompleter->includeGlobals()) { |
| 6894 | // Add all classes. |
| 6895 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6896 | false, Results); |
| 6897 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6898 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6899 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6900 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6901 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6902 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6905 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6906 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6907 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6908 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6909 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6910 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6911 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6912 | // Make sure that we ignore the class we're currently defining. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6913 | NamedDecl *CurClass = |
| 6914 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6915 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6916 | Results.Ignore(CurClass); |
| 6917 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6918 | if (CodeCompleter->includeGlobals()) { |
| 6919 | // Add all classes. |
| 6920 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6921 | false, Results); |
| 6922 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6923 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6924 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6925 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6926 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6927 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6928 | } |
| 6929 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6930 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6931 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6932 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6933 | CodeCompletionContext::CCC_ObjCImplementation); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6934 | Results.EnterNewScope(); |
| 6935 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6936 | if (CodeCompleter->includeGlobals()) { |
| 6937 | // Add all unimplemented classes. |
| 6938 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6939 | true, Results); |
| 6940 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6941 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6942 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6943 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6944 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6945 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6946 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6947 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6948 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6949 | IdentifierInfo *ClassName, |
| 6950 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6951 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6952 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6953 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6954 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6955 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6956 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6957 | // Ignore any categories we find that have already been implemented by this |
| 6958 | // interface. |
| 6959 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6960 | NamedDecl *CurClass = |
| 6961 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
| 6962 | if (ObjCInterfaceDecl *Class = |
| 6963 | dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6964 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6965 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6968 | // Add all of the categories we know about. |
| 6969 | Results.EnterNewScope(); |
| 6970 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6971 | for (const auto *D : TU->decls()) |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6972 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6973 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6974 | Results.AddResult( |
| 6975 | Result(Category, Results.getBasePriority(Category), nullptr), |
| 6976 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6977 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6978 | |
| 6979 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6980 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6983 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6984 | IdentifierInfo *ClassName, |
| 6985 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6986 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6987 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6988 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6989 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6990 | // providing the list of all of the categories we know about. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 6991 | NamedDecl *CurClass = |
| 6992 | LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6993 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6994 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6995 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6996 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6997 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6998 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6999 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7000 | |
| 7001 | // Add all of the categories that have have corresponding interface |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7002 | // declarations in this class and any of its superclasses, except for |
| 7003 | // already-implemented categories in the class itself. |
| 7004 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 7005 | Results.EnterNewScope(); |
| 7006 | bool IgnoreImplemented = true; |
| 7007 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7008 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7009 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7010 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7011 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 7012 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7013 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7014 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7015 | Class = Class->getSuperClass(); |
| 7016 | IgnoreImplemented = false; |
| 7017 | } |
| 7018 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7019 | |
| 7020 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7021 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 7022 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7023 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7024 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7025 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7026 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7027 | CodeCompleter->getCodeCompletionTUInfo(), CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7028 | |
| 7029 | // Figure out where this @synthesize lives. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7030 | ObjCContainerDecl *Container = |
| 7031 | dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| 7032 | if (!Container || (!isa<ObjCImplementationDecl>(Container) && |
| 7033 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7034 | return; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7035 | |
| 7036 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7037 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 7038 | for (const auto *D : Container->decls()) |
| 7039 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7040 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7041 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7042 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 7043 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7044 | Results.EnterNewScope(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7045 | if (ObjCImplementationDecl *ClassImpl = |
| 7046 | dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7047 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7048 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 7049 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7050 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 7051 | AddObjCProperties(CCContext, |
| 7052 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7053 | false, /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 7054 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7055 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7056 | |
| 7057 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7058 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7061 | void Sema::CodeCompleteObjCPropertySynthesizeIvar( |
| 7062 | Scope *S, IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7063 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7064 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7065 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7066 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7067 | |
| 7068 | // Figure out where this @synthesize lives. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7069 | ObjCContainerDecl *Container = |
| 7070 | dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| 7071 | if (!Container || (!isa<ObjCImplementationDecl>(Container) && |
| 7072 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7073 | return; |
| 7074 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7075 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7076 | ObjCInterfaceDecl *Class = nullptr; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7077 | if (ObjCImplementationDecl *ClassImpl = |
| 7078 | dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7079 | Class = ClassImpl->getClassInterface(); |
| 7080 | else |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7081 | Class = cast<ObjCCategoryImplDecl>(Container) |
| 7082 | ->getCategoryDecl() |
| 7083 | ->getClassInterface(); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7085 | // Determine the type of the property we're synthesizing. |
| 7086 | QualType PropertyType = Context.getObjCIdType(); |
| 7087 | if (Class) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 7088 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 7089 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7090 | PropertyType = |
| 7091 | Property->getType().getNonReferenceType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7092 | |
| 7093 | // Give preference to ivars |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7094 | Results.setPreferredType(PropertyType); |
| 7095 | } |
| 7096 | } |
| 7097 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7098 | // Add all of the instance variables in this class and its superclasses. |
| 7099 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7100 | bool SawSimilarlyNamedIvar = false; |
| 7101 | std::string NameWithPrefix; |
| 7102 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 7103 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7104 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 7105 | NameWithSuffix += '_'; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7106 | for (; Class; Class = Class->getSuperClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7107 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7108 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7109 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 7110 | CurContext, nullptr, false); |
| 7111 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7112 | // 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] | 7113 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7114 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7115 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7116 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7117 | SawSimilarlyNamedIvar = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7118 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7119 | // Reduce the priority of this result by one, to give it a slight |
| 7120 | // advantage over other results whose names don't match so closely. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7121 | if (Results.size() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7122 | Results.data()[Results.size() - 1].Kind == |
| 7123 | CodeCompletionResult::RK_Declaration && |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7124 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 7125 | Results.data()[Results.size() - 1].Priority--; |
| 7126 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7127 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7128 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7129 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7130 | if (!SawSimilarlyNamedIvar) { |
| 7131 | // Create ivar result _propName, that the user can use to synthesize |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7132 | // an ivar of the appropriate type. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 7133 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7134 | typedef CodeCompletionResult Result; |
| 7135 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7136 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7137 | Priority, CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7138 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7139 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7140 | Builder.AddResultTypeChunk( |
| 7141 | GetCompletionTypeString(PropertyType, Context, Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7142 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7143 | Results.AddResult( |
| 7144 | Result(Builder.TakeString(), Priority, CXCursor_ObjCIvarDecl)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 7145 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7146 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7147 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7148 | |
| 7149 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7150 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 7151 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7152 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7153 | // Mapping from selectors to the methods that implement that selector, along |
| 7154 | // with the "in original class" flag. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7155 | typedef llvm::DenseMap<Selector, |
| 7156 | llvm::PointerIntPair<ObjCMethodDecl *, 1, bool>> |
| 7157 | KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7158 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7159 | /// Find all of the methods that reside in the given container |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7160 | /// (and its superclasses, protocols, etc.) that meet the given |
| 7161 | /// criteria. Insert those methods into the map of known methods, |
| 7162 | /// indexed by selector so they can be easily found. |
| 7163 | static void FindImplementableMethods(ASTContext &Context, |
| 7164 | ObjCContainerDecl *Container, |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7165 | Optional<bool> WantInstanceMethods, |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7166 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7167 | KnownMethodsMap &KnownMethods, |
| 7168 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7169 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7170 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 7171 | if (!IFace->hasDefinition()) |
| 7172 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7173 | |
| 7174 | IFace = IFace->getDefinition(); |
| 7175 | Container = IFace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7176 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7177 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7178 | IFace->getReferencedProtocols(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7179 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7180 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7181 | I != E; ++I) |
| 7182 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7183 | KnownMethods, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7184 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7185 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7186 | for (auto *Cat : IFace->visible_categories()) { |
| 7187 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7188 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 7189 | } |
| 7190 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7191 | // Visit the superclass. |
| 7192 | if (IFace->getSuperClass()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7193 | FindImplementableMethods(Context, IFace->getSuperClass(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7194 | WantInstanceMethods, ReturnType, KnownMethods, |
| 7195 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 7199 | // Recurse into protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7200 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7201 | Category->getReferencedProtocols(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7202 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7203 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7204 | I != E; ++I) |
| 7205 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7206 | KnownMethods, InOriginalClass); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7207 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7208 | // If this category is the original class, jump to the interface. |
| 7209 | if (InOriginalClass && Category->getClassInterface()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7210 | FindImplementableMethods(Context, Category->getClassInterface(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7211 | WantInstanceMethods, ReturnType, KnownMethods, |
| 7212 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7216 | // Make sure we have a definition; that's what we'll walk. |
| 7217 | if (!Protocol->hasDefinition()) |
| 7218 | return; |
| 7219 | Protocol = Protocol->getDefinition(); |
| 7220 | Container = Protocol; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7221 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7222 | // Recurse into protocols. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7223 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 7224 | Protocol->getReferencedProtocols(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7225 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7226 | E = Protocols.end(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 7227 | I != E; ++I) |
| 7228 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 7229 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
| 7232 | // Add methods in this container. This operation occurs last because |
| 7233 | // we want the methods from this container to override any methods |
| 7234 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 7235 | for (auto *M : Container->methods()) { |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7236 | if (!WantInstanceMethods || M->isInstanceMethod() == *WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7237 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7238 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7239 | continue; |
| 7240 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7241 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 7242 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7243 | } |
| 7244 | } |
| 7245 | } |
| 7246 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7247 | /// Add the parenthesized return or parameter type chunk to a code |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7248 | /// completion string. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7249 | static void AddObjCPassingTypeChunk(QualType Type, unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7250 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7251 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7252 | CodeCompletionBuilder &Builder) { |
| 7253 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7254 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7255 | if (!Quals.empty()) |
| 7256 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7257 | Builder.AddTextChunk( |
| 7258 | GetCompletionTypeString(Type, Context, Policy, Builder.getAllocator())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7259 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7260 | } |
| 7261 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7262 | /// Determine whether the given class is or inherits from a class by |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7263 | /// the given name. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7264 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7265 | if (!Class) |
| 7266 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7267 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7268 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 7269 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7270 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7271 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 7272 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7273 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7274 | /// Add code completions for Objective-C Key-Value Coding (KVC) and |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7275 | /// Key-Value Observing (KVO). |
| 7276 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 7277 | bool IsInstanceMethod, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7278 | QualType ReturnType, ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7279 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7280 | ResultBuilder &Results) { |
| 7281 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 7282 | if (!PropName || PropName->getLength() == 0) |
| 7283 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7284 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7285 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 7286 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7287 | // Builder that will create each code completion. |
| 7288 | typedef CodeCompletionResult Result; |
| 7289 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7290 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7291 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7292 | // The selector table. |
| 7293 | SelectorTable &Selectors = Context.Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7294 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7295 | // The property name, copied into the code completion allocation region |
| 7296 | // on demand. |
| 7297 | struct KeyHolder { |
| 7298 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7299 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7300 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7301 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7302 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7303 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7304 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7305 | operator const char *() { |
| 7306 | if (CopiedKey) |
| 7307 | return CopiedKey; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7308 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7309 | return CopiedKey = Allocator.CopyString(Key); |
| 7310 | } |
| 7311 | } Key(Allocator, PropName->getName()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7312 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7313 | // The uppercased name of the property name. |
| 7314 | std::string UpperKey = PropName->getName(); |
| 7315 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 7316 | UpperKey[0] = toUppercase(UpperKey[0]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7317 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7318 | bool ReturnTypeMatchesProperty = |
| 7319 | ReturnType.isNull() || |
| 7320 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 7321 | Property->getType()); |
| 7322 | bool ReturnTypeMatchesVoid = ReturnType.isNull() || ReturnType->isVoidType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7323 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7324 | // Add the normal accessor -(type)key. |
| 7325 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7326 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7327 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 7328 | if (ReturnType.isNull()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7329 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, Context, Policy, |
| 7330 | Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7331 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7332 | Builder.AddTypedTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7333 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7334 | CXCursor_ObjCInstanceMethodDecl)); |
| 7335 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7336 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7337 | // If we have an integral or boolean property (or the user has provided |
| 7338 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 7339 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7340 | ((!ReturnType.isNull() && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7341 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7342 | (ReturnType.isNull() && (Property->getType()->isIntegerType() || |
| 7343 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7344 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7345 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7346 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7347 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7348 | if (ReturnType.isNull()) { |
| 7349 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7350 | Builder.AddTextChunk("BOOL"); |
| 7351 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7352 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7353 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7354 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7355 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7356 | CXCursor_ObjCInstanceMethodDecl)); |
| 7357 | } |
| 7358 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7359 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7360 | // Add the normal mutator. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7361 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7362 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7363 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7364 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7365 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7366 | if (ReturnType.isNull()) { |
| 7367 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7368 | Builder.AddTextChunk("void"); |
| 7369 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7370 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7371 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7372 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7373 | Builder.AddTypedTextChunk(":"); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7374 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, Context, Policy, |
| 7375 | Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7376 | Builder.AddTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7377 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7378 | CXCursor_ObjCInstanceMethodDecl)); |
| 7379 | } |
| 7380 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7381 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7382 | // Indexed and unordered accessors |
| 7383 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 7384 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 7385 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 7386 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7387 | if (const auto *ObjCPointer = |
| 7388 | Property->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7389 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 7390 | // If this interface type is not provably derived from a known |
| 7391 | // collection, penalize the corresponding completions. |
| 7392 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7393 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7394 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 7395 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7396 | } |
| 7397 | |
| 7398 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7399 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7400 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 7401 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7402 | } |
| 7403 | } |
| 7404 | } else { |
| 7405 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7406 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7407 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7408 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7409 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7410 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7411 | // Add -(NSUInteger)countOf<key> |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7412 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7413 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7414 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7415 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7416 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7417 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7418 | if (ReturnType.isNull()) { |
| 7419 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7420 | Builder.AddTextChunk("NSUInteger"); |
| 7421 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7422 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7423 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7424 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorId->getName())); |
| 7425 | Results.AddResult( |
| 7426 | Result(Builder.TakeString(), |
| 7427 | std::min(IndexedGetterPriority, UnorderedGetterPriority), |
| 7428 | CXCursor_ObjCInstanceMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7429 | } |
| 7430 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7431 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7432 | // Indexed getters |
| 7433 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 7434 | if (IsInstanceMethod && |
| 7435 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7436 | std::string SelectorName = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7437 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7438 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7439 | if (ReturnType.isNull()) { |
| 7440 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7441 | Builder.AddTextChunk("id"); |
| 7442 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7443 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7444 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7445 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7446 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7447 | Builder.AddTextChunk("NSUInteger"); |
| 7448 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7449 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7450 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7451 | CXCursor_ObjCInstanceMethodDecl)); |
| 7452 | } |
| 7453 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7454 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7455 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 7456 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7457 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7458 | (ReturnType->isObjCObjectPointerType() && |
| 7459 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7460 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7461 | ->getInterfaceDecl() |
| 7462 | ->getName() == "NSArray"))) { |
| 7463 | std::string SelectorName = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7464 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7465 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7466 | if (ReturnType.isNull()) { |
| 7467 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7468 | Builder.AddTextChunk("NSArray *"); |
| 7469 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 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 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7473 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7474 | Builder.AddTextChunk("NSIndexSet *"); |
| 7475 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7476 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7477 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7478 | CXCursor_ObjCInstanceMethodDecl)); |
| 7479 | } |
| 7480 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7481 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7482 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 7483 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7484 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7485 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7486 | &Context.Idents.get("range")}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7487 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7488 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7489 | if (ReturnType.isNull()) { |
| 7490 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7491 | Builder.AddTextChunk("void"); |
| 7492 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 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 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7496 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7497 | Builder.AddPlaceholderChunk("object-type"); |
| 7498 | Builder.AddTextChunk(" **"); |
| 7499 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7500 | Builder.AddTextChunk("buffer"); |
| 7501 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7502 | Builder.AddTypedTextChunk("range:"); |
| 7503 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7504 | Builder.AddTextChunk("NSRange"); |
| 7505 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7506 | Builder.AddTextChunk("inRange"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7507 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7508 | CXCursor_ObjCInstanceMethodDecl)); |
| 7509 | } |
| 7510 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7511 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7512 | // Mutable indexed accessors |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7513 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7514 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 7515 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7516 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7517 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get("insertObject"), |
| 7518 | &Context.Idents.get(SelectorName)}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7519 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7520 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7521 | if (ReturnType.isNull()) { |
| 7522 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7523 | Builder.AddTextChunk("void"); |
| 7524 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7525 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7526 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7527 | Builder.AddTypedTextChunk("insertObject:"); |
| 7528 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7529 | Builder.AddPlaceholderChunk("object-type"); |
| 7530 | Builder.AddTextChunk(" *"); |
| 7531 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7532 | Builder.AddTextChunk("object"); |
| 7533 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7534 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7535 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7536 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7537 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7538 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7539 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7540 | CXCursor_ObjCInstanceMethodDecl)); |
| 7541 | } |
| 7542 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7543 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7544 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 7545 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7546 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7547 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7548 | &Context.Idents.get("atIndexes")}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7549 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7550 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7551 | if (ReturnType.isNull()) { |
| 7552 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7553 | Builder.AddTextChunk("void"); |
| 7554 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7555 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7556 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7557 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7558 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7559 | Builder.AddTextChunk("NSArray *"); |
| 7560 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7561 | Builder.AddTextChunk("array"); |
| 7562 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7563 | Builder.AddTypedTextChunk("atIndexes:"); |
| 7564 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7565 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7566 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7567 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7568 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7569 | CXCursor_ObjCInstanceMethodDecl)); |
| 7570 | } |
| 7571 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7572 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7573 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 7574 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7575 | std::string SelectorName = |
| 7576 | (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7577 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7578 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7579 | if (ReturnType.isNull()) { |
| 7580 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7581 | Builder.AddTextChunk("void"); |
| 7582 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7583 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7584 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7585 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7586 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7587 | Builder.AddTextChunk("NSUInteger"); |
| 7588 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7589 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7590 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7591 | CXCursor_ObjCInstanceMethodDecl)); |
| 7592 | } |
| 7593 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7594 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7595 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 7596 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7597 | std::string SelectorName = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7598 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7599 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7600 | if (ReturnType.isNull()) { |
| 7601 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7602 | Builder.AddTextChunk("void"); |
| 7603 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7604 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7605 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7606 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7607 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7608 | Builder.AddTextChunk("NSIndexSet *"); |
| 7609 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7610 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7611 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7612 | CXCursor_ObjCInstanceMethodDecl)); |
| 7613 | } |
| 7614 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7615 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7616 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 7617 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7618 | std::string SelectorName = |
| 7619 | (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
| 7620 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName), |
| 7621 | &Context.Idents.get("withObject")}; |
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("NSUInteger"); |
| 7633 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7634 | Builder.AddTextChunk("index"); |
| 7635 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7636 | Builder.AddTypedTextChunk("withObject:"); |
| 7637 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7638 | Builder.AddTextChunk("id"); |
| 7639 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7640 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7641 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7642 | CXCursor_ObjCInstanceMethodDecl)); |
| 7643 | } |
| 7644 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7645 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7646 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 7647 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7648 | std::string SelectorName1 = |
| 7649 | (Twine("replace") + UpperKey + "AtIndexes").str(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7650 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7651 | IdentifierInfo *SelectorIds[2] = {&Context.Idents.get(SelectorName1), |
| 7652 | &Context.Idents.get(SelectorName2)}; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7653 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7654 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7655 | if (ReturnType.isNull()) { |
| 7656 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7657 | Builder.AddTextChunk("void"); |
| 7658 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7659 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7660 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7661 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 7662 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7663 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7664 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7665 | Builder.AddTextChunk("indexes"); |
| 7666 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7667 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 7668 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7669 | Builder.AddTextChunk("NSArray *"); |
| 7670 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7671 | Builder.AddTextChunk("array"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7672 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7673 | CXCursor_ObjCInstanceMethodDecl)); |
| 7674 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7675 | } |
| 7676 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7677 | // Unordered getters |
| 7678 | // - (NSEnumerator *)enumeratorOfKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7679 | if (IsInstanceMethod && |
| 7680 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7681 | (ReturnType->isObjCObjectPointerType() && |
| 7682 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7683 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7684 | ->getInterfaceDecl() |
| 7685 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7686 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7687 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7688 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7689 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7690 | if (ReturnType.isNull()) { |
| 7691 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7692 | Builder.AddTextChunk("NSEnumerator *"); |
| 7693 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7694 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7695 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7696 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7697 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7698 | CXCursor_ObjCInstanceMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7699 | } |
| 7700 | } |
| 7701 | |
| 7702 | // - (type *)memberOfKey:(type *)object |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7703 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7704 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7705 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7706 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7707 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7708 | if (ReturnType.isNull()) { |
| 7709 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7710 | Builder.AddPlaceholderChunk("object-type"); |
| 7711 | Builder.AddTextChunk(" *"); |
| 7712 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7713 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7714 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7715 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7716 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7717 | if (ReturnType.isNull()) { |
| 7718 | Builder.AddPlaceholderChunk("object-type"); |
| 7719 | Builder.AddTextChunk(" *"); |
| 7720 | } else { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7721 | Builder.AddTextChunk(GetCompletionTypeString( |
| 7722 | ReturnType, Context, Policy, Builder.getAllocator())); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7723 | } |
| 7724 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7725 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7726 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7727 | CXCursor_ObjCInstanceMethodDecl)); |
| 7728 | } |
| 7729 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7730 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7731 | // Mutable unordered accessors |
| 7732 | // - (void)addKeyObject:(type *)object |
| 7733 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7734 | std::string SelectorName = |
| 7735 | (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7736 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7737 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7738 | if (ReturnType.isNull()) { |
| 7739 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7740 | Builder.AddTextChunk("void"); |
| 7741 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7742 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7743 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7744 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7745 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7746 | Builder.AddPlaceholderChunk("object-type"); |
| 7747 | Builder.AddTextChunk(" *"); |
| 7748 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7749 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7750 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7751 | CXCursor_ObjCInstanceMethodDecl)); |
| 7752 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7753 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7754 | |
| 7755 | // - (void)addKey:(NSSet *)objects |
| 7756 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7757 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7758 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7759 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7760 | if (ReturnType.isNull()) { |
| 7761 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7762 | Builder.AddTextChunk("void"); |
| 7763 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7764 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7765 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7766 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7767 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7768 | Builder.AddTextChunk("NSSet *"); |
| 7769 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7770 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7771 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7772 | CXCursor_ObjCInstanceMethodDecl)); |
| 7773 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7776 | // - (void)removeKeyObject:(type *)object |
| 7777 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7778 | std::string SelectorName = |
| 7779 | (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7780 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7781 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7782 | if (ReturnType.isNull()) { |
| 7783 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7784 | Builder.AddTextChunk("void"); |
| 7785 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7786 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7787 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7788 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7789 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7790 | Builder.AddPlaceholderChunk("object-type"); |
| 7791 | Builder.AddTextChunk(" *"); |
| 7792 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7793 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7794 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7795 | CXCursor_ObjCInstanceMethodDecl)); |
| 7796 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7799 | // - (void)removeKey:(NSSet *)objects |
| 7800 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7801 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7802 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7803 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7804 | if (ReturnType.isNull()) { |
| 7805 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7806 | Builder.AddTextChunk("void"); |
| 7807 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7808 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7809 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7810 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7811 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7812 | Builder.AddTextChunk("NSSet *"); |
| 7813 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7814 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7815 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7816 | CXCursor_ObjCInstanceMethodDecl)); |
| 7817 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7818 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7819 | |
| 7820 | // - (void)intersectKey:(NSSet *)objects |
| 7821 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7822 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7823 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7824 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).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("void"); |
| 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 + ":")); |
| 7832 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7833 | Builder.AddTextChunk("NSSet *"); |
| 7834 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7835 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7836 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7837 | CXCursor_ObjCInstanceMethodDecl)); |
| 7838 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7841 | // Key-Value Observing |
| 7842 | // + (NSSet *)keyPathsForValuesAffectingKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7843 | if (!IsInstanceMethod && |
| 7844 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7845 | (ReturnType->isObjCObjectPointerType() && |
| 7846 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7847 | ReturnType->getAs<ObjCObjectPointerType>() |
| 7848 | ->getInterfaceDecl() |
| 7849 | ->getName() == "NSSet"))) { |
| 7850 | std::string SelectorName = |
| 7851 | (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7852 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7853 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7854 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7855 | if (ReturnType.isNull()) { |
| 7856 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Alex Lorenz | 71ecb07 | 2016-12-08 16:49:05 +0000 | [diff] [blame] | 7857 | Builder.AddTextChunk("NSSet<NSString *> *"); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7858 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7859 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7860 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7861 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7862 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7863 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 7868 | if (!IsInstanceMethod && |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7869 | (ReturnType.isNull() || ReturnType->isIntegerType() || |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7870 | ReturnType->isBooleanType())) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7871 | std::string SelectorName = |
| 7872 | (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7873 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7874 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7875 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7876 | if (ReturnType.isNull()) { |
| 7877 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7878 | Builder.AddTextChunk("BOOL"); |
| 7879 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7880 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7881 | |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7882 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7883 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7884 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7885 | } |
| 7886 | } |
| 7887 | } |
| 7888 | |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7889 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, Optional<bool> IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7890 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7891 | // Determine the return type of the method we're declaring, if |
| 7892 | // provided. |
| 7893 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7894 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7895 | if (CurContext->isObjCContainer()) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7896 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 7897 | IDecl = OCD; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7898 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7899 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7900 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7901 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7902 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7903 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7904 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7905 | IsInImplementation = true; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7906 | } else if (ObjCCategoryImplDecl *CatImpl = |
| 7907 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7908 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7909 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7910 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7911 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7915 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7916 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7917 | } |
| 7918 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7919 | if (!SearchDecl) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7920 | HandleCodeCompleteResults(this, CodeCompleter, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7921 | CodeCompletionContext::CCC_Other, nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7922 | return; |
| 7923 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7924 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7925 | // Find all of the methods that we could declare/implement here. |
| 7926 | KnownMethodsMap KnownMethods; |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7927 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, ReturnType, |
| 7928 | KnownMethods); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7929 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7930 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7931 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7932 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7933 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7934 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7935 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7936 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7937 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7938 | MEnd = KnownMethods.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7939 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7940 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7941 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7942 | Results.getCodeCompletionTUInfo()); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7943 | |
| 7944 | // Add the '-'/'+' prefix if it wasn't provided yet. |
| 7945 | if (!IsInstanceMethod) { |
| 7946 | Builder.AddTextChunk(Method->isInstanceMethod() ? "-" : "+"); |
| 7947 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7948 | } |
| 7949 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7950 | // If the result type was not already provided, add it to the |
| 7951 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7952 | if (ReturnType.isNull()) { |
| 7953 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7954 | AttributedType::stripOuterNullability(ResTy); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7955 | AddObjCPassingTypeChunk(ResTy, Method->getObjCDeclQualifier(), Context, |
| 7956 | Policy, Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7957 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7958 | |
| 7959 | Selector Sel = Method->getSelector(); |
| 7960 | |
| 7961 | // Add the first part of the selector to the pattern. |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7962 | Builder.AddTypedTextChunk( |
| 7963 | Builder.getAllocator().CopyString(Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7964 | |
| 7965 | // Add parameters to the pattern. |
| 7966 | unsigned I = 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7967 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7968 | PEnd = Method->param_end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7969 | P != PEnd; (void)++P, ++I) { |
| 7970 | // Add the part of the selector name. |
| 7971 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7972 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7973 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7974 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7975 | Builder.AddTypedTextChunk( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7976 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7977 | } else |
| 7978 | break; |
| 7979 | |
| 7980 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7981 | QualType ParamType; |
| 7982 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7983 | ParamType = (*P)->getType(); |
| 7984 | else |
| 7985 | ParamType = (*P)->getOriginalType(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7986 | ParamType = ParamType.substObjCTypeArgs( |
| 7987 | Context, {}, ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7988 | AttributedType::stripOuterNullability(ParamType); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7989 | AddObjCPassingTypeChunk(ParamType, (*P)->getObjCDeclQualifier(), Context, |
| 7990 | Policy, Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7991 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7992 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 7993 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | if (Method->isVariadic()) { |
| 7997 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7998 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7999 | Builder.AddTextChunk("..."); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8000 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8001 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 8002 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8003 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8004 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8005 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 8006 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8007 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8008 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8009 | Builder.AddTextChunk("return"); |
| 8010 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8011 | Builder.AddPlaceholderChunk("expression"); |
| 8012 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8013 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8014 | Builder.AddPlaceholderChunk("statements"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8015 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8016 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 8017 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 8020 | unsigned Priority = CCP_CodePattern; |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 8021 | auto R = Result(Builder.TakeString(), Method, Priority); |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 8022 | if (!M->second.getInt()) |
Eric Liu | 4a7cd63 | 2018-10-24 12:57:27 +0000 | [diff] [blame] | 8023 | setInBaseClass(R); |
| 8024 | Results.AddResult(std::move(R)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8027 | // 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] | 8028 | // the properties in this class and its categories. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8029 | if (Context.getLangOpts().ObjC) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8030 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8031 | Containers.push_back(SearchDecl); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8032 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 8033 | VisitedSelectorSet KnownSelectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8034 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8035 | MEnd = KnownMethods.end(); |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 8036 | M != MEnd; ++M) |
| 8037 | KnownSelectors.insert(M->first); |
| 8038 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8039 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 8040 | if (!IFace) |
| 8041 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 8042 | IFace = Category->getClassInterface(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8043 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 8044 | if (IFace) |
| 8045 | for (auto *Cat : IFace->visible_categories()) |
| 8046 | Containers.push_back(Cat); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 8047 | |
| 8048 | if (IsInstanceMethod) { |
| 8049 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
| 8050 | for (auto *P : Containers[I]->instance_properties()) |
| 8051 | AddObjCKeyValueCompletions(P, *IsInstanceMethod, ReturnType, Context, |
| 8052 | KnownSelectors, Results); |
| 8053 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 8054 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8055 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8056 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8057 | |
| 8058 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8059 | Results.data(), Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 8060 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8061 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8062 | void Sema::CodeCompleteObjCMethodDeclSelector( |
| 8063 | Scope *S, bool IsInstanceMethod, bool AtParameterName, ParsedType ReturnTy, |
| 8064 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8065 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 8066 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8067 | if (ExternalSource) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8068 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); I != N; |
| 8069 | ++I) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8070 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8071 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8072 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8073 | |
| 8074 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8075 | } |
| 8076 | } |
| 8077 | |
| 8078 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 8079 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8080 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8081 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8082 | CodeCompletionContext::CCC_Other); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8083 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8084 | if (ReturnTy) |
| 8085 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8086 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8087 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 8088 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 8089 | MEnd = MethodPool.end(); |
| 8090 | M != MEnd; ++M) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8091 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first |
| 8092 | : &M->second.second; |
| 8093 | MethList && MethList->getMethod(); MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8094 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8095 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8096 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8097 | if (AtParameterName) { |
| 8098 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 8099 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8100 | if (NumSelIdents && |
| 8101 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 8102 | ParmVarDecl *Param = |
| 8103 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8104 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8105 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8106 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 8107 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8108 | Param->getIdentifier()->getName())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8109 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8110 | } |
| 8111 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8112 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 8113 | continue; |
| 8114 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8115 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 8116 | Result R(MethList->getMethod(), |
| 8117 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 8118 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8119 | R.AllParametersAreInformative = false; |
| 8120 | R.DeclaringEntity = true; |
| 8121 | Results.MaybeAddResult(R, CurContext); |
| 8122 | } |
| 8123 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8124 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8125 | Results.ExitScope(); |
Alex Lorenz | 847fda1 | 2017-01-03 11:56:40 +0000 | [diff] [blame] | 8126 | |
| 8127 | if (!AtParameterName && !SelIdents.empty() && |
| 8128 | SelIdents.front()->getName().startswith("init")) { |
| 8129 | for (const auto &M : PP.macros()) { |
| 8130 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") |
| 8131 | continue; |
| 8132 | Results.EnterNewScope(); |
| 8133 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8134 | Results.getCodeCompletionTUInfo()); |
| 8135 | Builder.AddTypedTextChunk( |
| 8136 | Builder.getAllocator().CopyString(M.first->getName())); |
| 8137 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, |
| 8138 | CXCursor_MacroDefinition)); |
| 8139 | Results.ExitScope(); |
| 8140 | } |
| 8141 | } |
| 8142 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8143 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8144 | Results.data(), Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 8145 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8146 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8147 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8148 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8149 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8150 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8151 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8152 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8153 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8154 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8155 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8156 | Builder.AddTypedTextChunk("if"); |
| 8157 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8158 | Builder.AddPlaceholderChunk("condition"); |
| 8159 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8160 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8161 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8162 | Builder.AddTypedTextChunk("ifdef"); |
| 8163 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8164 | Builder.AddPlaceholderChunk("macro"); |
| 8165 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8166 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8167 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8168 | Builder.AddTypedTextChunk("ifndef"); |
| 8169 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8170 | Builder.AddPlaceholderChunk("macro"); |
| 8171 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8172 | |
| 8173 | if (InConditional) { |
| 8174 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8175 | Builder.AddTypedTextChunk("elif"); |
| 8176 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8177 | Builder.AddPlaceholderChunk("condition"); |
| 8178 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8179 | |
| 8180 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8181 | Builder.AddTypedTextChunk("else"); |
| 8182 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8183 | |
| 8184 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8185 | Builder.AddTypedTextChunk("endif"); |
| 8186 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8187 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8188 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8189 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8190 | Builder.AddTypedTextChunk("include"); |
| 8191 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8192 | Builder.AddTextChunk("\""); |
| 8193 | Builder.AddPlaceholderChunk("header"); |
| 8194 | Builder.AddTextChunk("\""); |
| 8195 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8196 | |
| 8197 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8198 | Builder.AddTypedTextChunk("include"); |
| 8199 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8200 | Builder.AddTextChunk("<"); |
| 8201 | Builder.AddPlaceholderChunk("header"); |
| 8202 | Builder.AddTextChunk(">"); |
| 8203 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8204 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8205 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8206 | Builder.AddTypedTextChunk("define"); |
| 8207 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8208 | Builder.AddPlaceholderChunk("macro"); |
| 8209 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8210 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8211 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8212 | Builder.AddTypedTextChunk("define"); |
| 8213 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8214 | Builder.AddPlaceholderChunk("macro"); |
| 8215 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 8216 | Builder.AddPlaceholderChunk("args"); |
| 8217 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 8218 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8219 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8220 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8221 | Builder.AddTypedTextChunk("undef"); |
| 8222 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8223 | Builder.AddPlaceholderChunk("macro"); |
| 8224 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8225 | |
| 8226 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8227 | Builder.AddTypedTextChunk("line"); |
| 8228 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8229 | Builder.AddPlaceholderChunk("number"); |
| 8230 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8231 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8232 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8233 | Builder.AddTypedTextChunk("line"); |
| 8234 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8235 | Builder.AddPlaceholderChunk("number"); |
| 8236 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8237 | Builder.AddTextChunk("\""); |
| 8238 | Builder.AddPlaceholderChunk("filename"); |
| 8239 | Builder.AddTextChunk("\""); |
| 8240 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8241 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8242 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8243 | Builder.AddTypedTextChunk("error"); |
| 8244 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8245 | Builder.AddPlaceholderChunk("message"); |
| 8246 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8247 | |
| 8248 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8249 | Builder.AddTypedTextChunk("pragma"); |
| 8250 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8251 | Builder.AddPlaceholderChunk("arguments"); |
| 8252 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8253 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8254 | if (getLangOpts().ObjC) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8255 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8256 | Builder.AddTypedTextChunk("import"); |
| 8257 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8258 | Builder.AddTextChunk("\""); |
| 8259 | Builder.AddPlaceholderChunk("header"); |
| 8260 | Builder.AddTextChunk("\""); |
| 8261 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8262 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8263 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8264 | Builder.AddTypedTextChunk("import"); |
| 8265 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8266 | Builder.AddTextChunk("<"); |
| 8267 | Builder.AddPlaceholderChunk("header"); |
| 8268 | Builder.AddTextChunk(">"); |
| 8269 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8270 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8271 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8272 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8273 | Builder.AddTypedTextChunk("include_next"); |
| 8274 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8275 | Builder.AddTextChunk("\""); |
| 8276 | Builder.AddPlaceholderChunk("header"); |
| 8277 | Builder.AddTextChunk("\""); |
| 8278 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8279 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8280 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8281 | Builder.AddTypedTextChunk("include_next"); |
| 8282 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8283 | Builder.AddTextChunk("<"); |
| 8284 | Builder.AddPlaceholderChunk("header"); |
| 8285 | Builder.AddTextChunk(">"); |
| 8286 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8287 | |
| 8288 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8289 | Builder.AddTypedTextChunk("warning"); |
| 8290 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8291 | Builder.AddPlaceholderChunk("message"); |
| 8292 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8293 | |
| 8294 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 8295 | // completions for them. And __include_macros is a Clang-internal extension |
| 8296 | // that we don't want to encourage anyone to use. |
| 8297 | |
| 8298 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 8299 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8300 | |
| 8301 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8302 | Results.data(), Results.size()); |
| 8303 | } |
| 8304 | |
| 8305 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8306 | CodeCompleteOrdinaryName(S, S->getFnParent() ? Sema::PCC_RecoveryInFunction |
| 8307 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 8308 | } |
| 8309 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8310 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8311 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8312 | CodeCompleter->getCodeCompletionTUInfo(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8313 | IsDefinition ? CodeCompletionContext::CCC_MacroName |
| 8314 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8315 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8316 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8317 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8318 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8319 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8320 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8321 | MEnd = PP.macro_end(); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8322 | M != MEnd; ++M) { |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8323 | Builder.AddTypedTextChunk( |
| 8324 | Builder.getAllocator().CopyString(M->first->getName())); |
| 8325 | Results.AddResult(CodeCompletionResult( |
| 8326 | Builder.TakeString(), CCP_CodePattern, CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8327 | } |
| 8328 | Results.ExitScope(); |
| 8329 | } else if (IsDefinition) { |
| 8330 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 8331 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8332 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8333 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8334 | Results.data(), Results.size()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 8335 | } |
| 8336 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8337 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8338 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8339 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 8340 | CodeCompletionContext::CCC_PreprocessorExpression); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8341 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8342 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 8343 | AddMacroResults(PP, Results, |
| 8344 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 8345 | true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8346 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8347 | // defined (<macro>) |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8348 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8349 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 8350 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 8351 | Builder.AddTypedTextChunk("defined"); |
| 8352 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 8353 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 8354 | Builder.AddPlaceholderChunk("macro"); |
| 8355 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 8356 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8357 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8358 | |
| 8359 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8360 | Results.data(), Results.size()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
| 8363 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 8364 | IdentifierInfo *Macro, |
| 8365 | MacroInfo *MacroInfo, |
| 8366 | unsigned Argument) { |
| 8367 | // FIXME: In the future, we could provide "overload" results, much like we |
| 8368 | // do for function calls. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8369 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 8370 | // Now just ignore this. There will be another code-completion callback |
| 8371 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 8372 | } |
| 8373 | |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8374 | // This handles completion inside an #include filename, e.g. #include <foo/ba |
| 8375 | // We look for the directory "foo" under each directory on the include path, |
| 8376 | // list its files, and reassemble the appropriate #include. |
| 8377 | void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) { |
| 8378 | // RelDir should use /, but unescaped \ is possible on windows! |
| 8379 | // Our completions will normalize to / for simplicity, this case is rare. |
| 8380 | std::string RelDir = llvm::sys::path::convert_to_slash(Dir); |
| 8381 | // We need the native slashes for the actual file system interactions. |
| 8382 | SmallString<128> NativeRelDir = StringRef(RelDir); |
| 8383 | llvm::sys::path::native(NativeRelDir); |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 8384 | llvm::vfs::FileSystem &FS = |
| 8385 | getSourceManager().getFileManager().getVirtualFileSystem(); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8386 | |
| 8387 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8388 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8389 | CodeCompletionContext::CCC_IncludedFile); |
| 8390 | llvm::DenseSet<StringRef> SeenResults; // To deduplicate results. |
| 8391 | |
| 8392 | // Helper: adds one file or directory completion result. |
| 8393 | auto AddCompletion = [&](StringRef Filename, bool IsDirectory) { |
| 8394 | SmallString<64> TypedChunk = Filename; |
| 8395 | // Directory completion is up to the slash, e.g. <sys/ |
| 8396 | TypedChunk.push_back(IsDirectory ? '/' : Angled ? '>' : '"'); |
| 8397 | auto R = SeenResults.insert(TypedChunk); |
| 8398 | if (R.second) { // New completion |
| 8399 | const char *InternedTyped = Results.getAllocator().CopyString(TypedChunk); |
| 8400 | *R.first = InternedTyped; // Avoid dangling StringRef. |
| 8401 | CodeCompletionBuilder Builder(CodeCompleter->getAllocator(), |
| 8402 | CodeCompleter->getCodeCompletionTUInfo()); |
| 8403 | Builder.AddTypedTextChunk(InternedTyped); |
| 8404 | // The result is a "Pattern", which is pretty opaque. |
| 8405 | // We may want to include the real filename to allow smart ranking. |
| 8406 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 8407 | } |
| 8408 | }; |
| 8409 | |
| 8410 | // Helper: scans IncludeDir for nice files, and adds results for each. |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8411 | auto AddFilesFromIncludeDir = [&](StringRef IncludeDir, |
| 8412 | bool IsSystem, |
| 8413 | DirectoryLookup::LookupType_t LookupType) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8414 | llvm::SmallString<128> Dir = IncludeDir; |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8415 | if (!NativeRelDir.empty()) { |
| 8416 | if (LookupType == DirectoryLookup::LT_Framework) { |
| 8417 | // For a framework dir, #include <Foo/Bar/> actually maps to |
| 8418 | // a path of Foo.framework/Headers/Bar/. |
| 8419 | auto Begin = llvm::sys::path::begin(NativeRelDir); |
| 8420 | auto End = llvm::sys::path::end(NativeRelDir); |
| 8421 | |
| 8422 | llvm::sys::path::append(Dir, *Begin + ".framework", "Headers"); |
| 8423 | llvm::sys::path::append(Dir, ++Begin, End); |
| 8424 | } else { |
| 8425 | llvm::sys::path::append(Dir, NativeRelDir); |
| 8426 | } |
| 8427 | } |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8428 | |
| 8429 | std::error_code EC; |
| 8430 | unsigned Count = 0; |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 8431 | for (auto It = FS.dir_begin(Dir, EC); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 8432 | !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8433 | if (++Count == 2500) // If we happen to hit a huge directory, |
| 8434 | break; // bail out early so we're not too slow. |
| 8435 | StringRef Filename = llvm::sys::path::filename(It->path()); |
| 8436 | switch (It->type()) { |
| 8437 | case llvm::sys::fs::file_type::directory_file: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8438 | // All entries in a framework directory must have a ".framework" suffix, |
| 8439 | // but the suffix does not appear in the source code's include/import. |
| 8440 | if (LookupType == DirectoryLookup::LT_Framework && |
| 8441 | NativeRelDir.empty() && !Filename.consume_back(".framework")) |
| 8442 | break; |
| 8443 | |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8444 | AddCompletion(Filename, /*IsDirectory=*/true); |
| 8445 | break; |
| 8446 | case llvm::sys::fs::file_type::regular_file: |
| 8447 | // Only files that really look like headers. (Except in system dirs). |
| 8448 | if (!IsSystem) { |
| 8449 | // Header extensions from Types.def, which we can't depend on here. |
| 8450 | if (!(Filename.endswith_lower(".h") || |
| 8451 | Filename.endswith_lower(".hh") || |
| 8452 | Filename.endswith_lower(".hpp") || |
| 8453 | Filename.endswith_lower(".inc"))) |
| 8454 | break; |
| 8455 | } |
| 8456 | AddCompletion(Filename, /*IsDirectory=*/false); |
| 8457 | break; |
| 8458 | default: |
| 8459 | break; |
| 8460 | } |
| 8461 | } |
| 8462 | }; |
| 8463 | |
| 8464 | // Helper: adds results relative to IncludeDir, if possible. |
| 8465 | auto AddFilesFromDirLookup = [&](const DirectoryLookup &IncludeDir, |
| 8466 | bool IsSystem) { |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8467 | switch (IncludeDir.getLookupType()) { |
| 8468 | case DirectoryLookup::LT_HeaderMap: |
| 8469 | // header maps are not (currently) enumerable. |
| 8470 | break; |
| 8471 | case DirectoryLookup::LT_NormalDir: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8472 | AddFilesFromIncludeDir(IncludeDir.getDir()->getName(), IsSystem, |
| 8473 | DirectoryLookup::LT_NormalDir); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8474 | break; |
| 8475 | case DirectoryLookup::LT_Framework: |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8476 | AddFilesFromIncludeDir(IncludeDir.getFrameworkDir()->getName(), IsSystem, |
| 8477 | DirectoryLookup::LT_Framework); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8478 | break; |
| 8479 | } |
| 8480 | }; |
| 8481 | |
| 8482 | // Finally with all our helpers, we can scan the include path. |
| 8483 | // Do this in standard order so deduplication keeps the right file. |
| 8484 | // (In case we decide to add more details to the results later). |
| 8485 | const auto &S = PP.getHeaderSearchInfo(); |
| 8486 | using llvm::make_range; |
| 8487 | if (!Angled) { |
| 8488 | // The current directory is on the include path for "quoted" includes. |
| 8489 | auto *CurFile = PP.getCurrentFileLexer()->getFileEntry(); |
| 8490 | if (CurFile && CurFile->getDir()) |
David Goldman | 3e804d2 | 2019-02-27 17:40:33 +0000 | [diff] [blame] | 8491 | AddFilesFromIncludeDir(CurFile->getDir()->getName(), false, |
| 8492 | DirectoryLookup::LT_NormalDir); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8493 | for (const auto &D : make_range(S.quoted_dir_begin(), S.quoted_dir_end())) |
| 8494 | AddFilesFromDirLookup(D, false); |
| 8495 | } |
| 8496 | 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] | 8497 | AddFilesFromDirLookup(D, false); |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8498 | for (const auto &D : make_range(S.system_dir_begin(), S.system_dir_end())) |
| 8499 | AddFilesFromDirLookup(D, true); |
| 8500 | |
| 8501 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8502 | Results.data(), Results.size()); |
| 8503 | } |
| 8504 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8505 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8506 | HandleCodeCompleteResults(this, CodeCompleter, |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8507 | CodeCompletionContext::CCC_NaturalLanguage, nullptr, |
| 8508 | 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8511 | void Sema::CodeCompleteAvailabilityPlatformName() { |
| 8512 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8513 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8514 | CodeCompletionContext::CCC_Other); |
| 8515 | Results.EnterNewScope(); |
| 8516 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; |
| 8517 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { |
| 8518 | Results.AddResult(CodeCompletionResult(Platform)); |
| 8519 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( |
| 8520 | Twine(Platform) + "ApplicationExtension"))); |
| 8521 | } |
| 8522 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8523 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8524 | Results.data(), Results.size()); |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8525 | } |
| 8526 | |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8527 | void Sema::GatherGlobalCodeCompletions( |
| 8528 | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
| 8529 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8530 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 8531 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8532 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8533 | CodeCompletionDeclConsumer Consumer(Builder, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8534 | Context.getTranslationUnitDecl()); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 8535 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 8536 | Consumer, |
| 8537 | !CodeCompleter || CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8538 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8539 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8540 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 8541 | AddMacroResults(PP, Builder, |
| 8542 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 8543 | true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8544 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8545 | Results.clear(); |
Fangrui Song | 050229d | 2018-11-24 00:14:31 +0000 | [diff] [blame] | 8546 | Results.insert(Results.end(), Builder.data(), |
| 8547 | Builder.data() + Builder.size()); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8548 | } |