Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the code-completion semantic actions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/Overload.h" |
| 24 | #include "clang/Sema/Scope.h" |
| 25 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 33 | #include <list> |
| 34 | #include <map> |
| 35 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | /// \brief A container of code-completion results. |
| 42 | class ResultBuilder { |
| 43 | public: |
| 44 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 45 | /// name-lookup routines to specify which declarations should be included in |
| 46 | /// the result set (when it returns true) and which declarations should be |
| 47 | /// filtered out (returns false). |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 48 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 49 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 50 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | /// \brief The actual results we have found. |
| 54 | std::vector<Result> Results; |
| 55 | |
| 56 | /// \brief A record of all of the declarations we have found and placed |
| 57 | /// into the result set, used to ensure that no declaration ever gets into |
| 58 | /// the result set twice. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 59 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 60 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 61 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 62 | |
| 63 | /// \brief An entry in the shadow map, which is optimized to store |
| 64 | /// a single (declaration, index) mapping (the common case) but |
| 65 | /// can also store a list of (declaration, index) mappings. |
| 66 | class ShadowMapEntry { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 68 | |
| 69 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 70 | /// of (declaration, index) pairs. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 71 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 72 | |
| 73 | /// \brief When the entry contains a single declaration, this is |
| 74 | /// the index associated with that entry. |
| 75 | unsigned SingleDeclIndex; |
| 76 | |
| 77 | public: |
| 78 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 79 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 80 | void Add(const NamedDecl *ND, unsigned Index) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 81 | if (DeclOrVector.isNull()) { |
| 82 | // 0 - > 1 elements: just set the single element information. |
| 83 | DeclOrVector = ND; |
| 84 | SingleDeclIndex = Index; |
| 85 | return; |
| 86 | } |
| 87 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 88 | if (const NamedDecl *PrevND = |
| 89 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 90 | // 1 -> 2 elements: create the vector of results and push in the |
| 91 | // existing declaration. |
| 92 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 93 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 94 | DeclOrVector = Vec; |
| 95 | } |
| 96 | |
| 97 | // Add the new element to the end of the vector. |
| 98 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 99 | DeclIndexPair(ND, Index)); |
| 100 | } |
| 101 | |
| 102 | void Destroy() { |
| 103 | if (DeclIndexPairVector *Vec |
| 104 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 105 | delete Vec; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 106 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | // Iteration. |
| 111 | class iterator; |
| 112 | iterator begin() const; |
| 113 | iterator end() const; |
| 114 | }; |
| 115 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 116 | /// \brief A mapping from declaration names to the declarations that have |
| 117 | /// this name within a particular scope and their index within the list of |
| 118 | /// results. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 119 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 120 | |
| 121 | /// \brief The semantic analysis object for which results are being |
| 122 | /// produced. |
| 123 | Sema &SemaRef; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 124 | |
| 125 | /// \brief The allocator used to allocate new code-completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 126 | CodeCompletionAllocator &Allocator; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 127 | |
| 128 | CodeCompletionTUInfo &CCTUInfo; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 129 | |
| 130 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 131 | /// results that are not desirable. |
| 132 | LookupFilter Filter; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 133 | |
| 134 | /// \brief Whether we should allow declarations as |
| 135 | /// nested-name-specifiers that would otherwise be filtered out. |
| 136 | bool AllowNestedNameSpecifiers; |
| 137 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 138 | /// \brief If set, the type that we would prefer our resulting value |
| 139 | /// declarations to have. |
| 140 | /// |
| 141 | /// Closely matching the preferred type gives a boost to a result's |
| 142 | /// priority. |
| 143 | CanQualType PreferredType; |
| 144 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 145 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 146 | /// different levels of, e.g., the inheritance hierarchy. |
| 147 | std::list<ShadowMap> ShadowMaps; |
| 148 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 149 | /// \brief If we're potentially referring to a C++ member function, the set |
| 150 | /// of qualifiers applied to the object type. |
| 151 | Qualifiers ObjectTypeQualifiers; |
| 152 | |
| 153 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 154 | bool HasObjectTypeQualifiers; |
| 155 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 156 | /// \brief The selector that we prefer. |
| 157 | Selector PreferredSelector; |
| 158 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 159 | /// \brief The completion context in which we are gathering results. |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 160 | CodeCompletionContext CompletionContext; |
| 161 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 162 | /// \brief If we are in an instance method definition, the \@implementation |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 163 | /// object. |
| 164 | ObjCImplementationDecl *ObjCImplementation; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 166 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 168 | void MaybeAddConstructorResults(Result R); |
| 169 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 170 | public: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 171 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 172 | CodeCompletionTUInfo &CCTUInfo, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 173 | const CodeCompletionContext &CompletionContext, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 174 | LookupFilter Filter = nullptr) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 175 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 176 | Filter(Filter), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 177 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 178 | CompletionContext(CompletionContext), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 179 | ObjCImplementation(nullptr) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 180 | { |
| 181 | // If this is an Objective-C instance method definition, dig out the |
| 182 | // corresponding implementation. |
| 183 | switch (CompletionContext.getKind()) { |
| 184 | case CodeCompletionContext::CCC_Expression: |
| 185 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 186 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 187 | case CodeCompletionContext::CCC_Statement: |
| 188 | case CodeCompletionContext::CCC_Recovery: |
| 189 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 190 | if (Method->isInstanceMethod()) |
| 191 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 192 | ObjCImplementation = Interface->getImplementation(); |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 199 | |
| 200 | /// \brief Determine the priority for a reference to the given declaration. |
| 201 | unsigned getBasePriority(const NamedDecl *D); |
| 202 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 203 | /// \brief Whether we should include code patterns in the completion |
| 204 | /// results. |
| 205 | bool includeCodePatterns() const { |
| 206 | return SemaRef.CodeCompleter && |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 207 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 210 | /// \brief Set the filter used for code-completion results. |
| 211 | void setFilter(LookupFilter Filter) { |
| 212 | this->Filter = Filter; |
| 213 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 214 | |
| 215 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 216 | unsigned size() const { return Results.size(); } |
| 217 | bool empty() const { return Results.empty(); } |
| 218 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 219 | /// \brief Specify the preferred type. |
| 220 | void setPreferredType(QualType T) { |
| 221 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 222 | } |
| 223 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 224 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 225 | /// calls to member functions. |
| 226 | /// |
| 227 | /// When there are qualifiers in this set, they will be used to filter |
| 228 | /// out member functions that aren't available (because there will be a |
| 229 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 230 | /// match. |
| 231 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 232 | ObjectTypeQualifiers = Quals; |
| 233 | HasObjectTypeQualifiers = true; |
| 234 | } |
| 235 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 236 | /// \brief Set the preferred selector. |
| 237 | /// |
| 238 | /// When an Objective-C method declaration result is added, and that |
| 239 | /// method's selector matches this preferred selector, we give that method |
| 240 | /// a slight priority boost. |
| 241 | void setPreferredSelector(Selector Sel) { |
| 242 | PreferredSelector = Sel; |
| 243 | } |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 244 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 245 | /// \brief Retrieve the code-completion context for which results are |
| 246 | /// being collected. |
| 247 | const CodeCompletionContext &getCompletionContext() const { |
| 248 | return CompletionContext; |
| 249 | } |
| 250 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 251 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 252 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 253 | AllowNestedNameSpecifiers = Allow; |
| 254 | } |
| 255 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 256 | /// \brief Return the semantic analysis object for which we are collecting |
| 257 | /// code completion results. |
| 258 | Sema &getSema() const { return SemaRef; } |
| 259 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 260 | /// \brief Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 261 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 262 | |
| 263 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 265 | /// \brief Determine whether the given declaration is at all interesting |
| 266 | /// as a code-completion result. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 267 | /// |
| 268 | /// \param ND the declaration that we are inspecting. |
| 269 | /// |
| 270 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 271 | /// only interesting when it is a nested-name-specifier. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 272 | bool isInterestingDecl(const NamedDecl *ND, |
| 273 | bool &AsNestedNameSpecifier) const; |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 274 | |
| 275 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 276 | /// |
| 277 | /// \returns true if the result is hidden and cannot be found, false if |
| 278 | /// the hidden result could still be found. When false, \p R may be |
| 279 | /// modified to describe how the result can be found (e.g., via extra |
| 280 | /// qualification). |
| 281 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 282 | const NamedDecl *Hiding); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 283 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 284 | /// \brief Add a new result to this result set (if it isn't already in one |
| 285 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 286 | /// redeclaration). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 287 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 288 | /// \param R the result to add (if it is unique). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 289 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 290 | /// \param CurContext the context in which this result will be named. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 291 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 292 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 293 | /// \brief Add a new result to this result set, where we already know |
Yaron Keren | 8fbe4398 | 2014-11-14 18:33:42 +0000 | [diff] [blame] | 294 | /// the hiding declaration (if any). |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 295 | /// |
| 296 | /// \param R the result to add (if it is unique). |
| 297 | /// |
| 298 | /// \param CurContext the context in which this result will be named. |
| 299 | /// |
| 300 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 301 | /// |
| 302 | /// \param InBaseClass whether the result was found in a base |
| 303 | /// class of the searched context. |
| 304 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 305 | bool InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 307 | /// \brief Add a new non-declaration result to this result set. |
| 308 | void AddResult(Result R); |
| 309 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 310 | /// \brief Enter into a new scope. |
| 311 | void EnterNewScope(); |
| 312 | |
| 313 | /// \brief Exit from the current scope. |
| 314 | void ExitScope(); |
| 315 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 316 | /// \brief Ignore this declaration, if it is seen again. |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 317 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 319 | /// \name Name lookup predicates |
| 320 | /// |
| 321 | /// These predicates can be passed to the name lookup functions to filter the |
| 322 | /// results of name lookup. All of the predicates have the same type, so that |
| 323 | /// |
| 324 | //@{ |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 325 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 326 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 327 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 328 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 329 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 330 | bool IsEnum(const NamedDecl *ND) const; |
| 331 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 332 | bool IsUnion(const NamedDecl *ND) const; |
| 333 | bool IsNamespace(const NamedDecl *ND) const; |
| 334 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 335 | bool IsType(const NamedDecl *ND) const; |
| 336 | bool IsMember(const NamedDecl *ND) const; |
| 337 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 338 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 339 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 340 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 341 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 342 | //@} |
| 343 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 346 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 347 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 348 | unsigned SingleDeclIndex; |
| 349 | |
| 350 | public: |
| 351 | typedef DeclIndexPair value_type; |
| 352 | typedef value_type reference; |
| 353 | typedef std::ptrdiff_t difference_type; |
| 354 | typedef std::input_iterator_tag iterator_category; |
| 355 | |
| 356 | class pointer { |
| 357 | DeclIndexPair Value; |
| 358 | |
| 359 | public: |
| 360 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 361 | |
| 362 | const DeclIndexPair *operator->() const { |
| 363 | return &Value; |
| 364 | } |
| 365 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 366 | |
| 367 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 368 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 369 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 370 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 371 | |
| 372 | iterator(const DeclIndexPair *Iterator) |
| 373 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 374 | |
| 375 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 376 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 377 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 378 | SingleDeclIndex = 0; |
| 379 | return *this; |
| 380 | } |
| 381 | |
| 382 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 383 | ++I; |
| 384 | DeclOrIterator = I; |
| 385 | return *this; |
| 386 | } |
| 387 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 388 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 389 | iterator tmp(*this); |
| 390 | ++(*this); |
| 391 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 392 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 393 | |
| 394 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 395 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 396 | return reference(ND, SingleDeclIndex); |
| 397 | |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 398 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | pointer operator->() const { |
| 402 | return pointer(**this); |
| 403 | } |
| 404 | |
| 405 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 406 | return X.DeclOrIterator.getOpaqueValue() |
| 407 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 408 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 409 | } |
| 410 | |
| 411 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 412 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 413 | } |
| 414 | }; |
| 415 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 416 | ResultBuilder::ShadowMapEntry::iterator |
| 417 | ResultBuilder::ShadowMapEntry::begin() const { |
| 418 | if (DeclOrVector.isNull()) |
| 419 | return iterator(); |
| 420 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 421 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 422 | return iterator(ND, SingleDeclIndex); |
| 423 | |
| 424 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 425 | } |
| 426 | |
| 427 | ResultBuilder::ShadowMapEntry::iterator |
| 428 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 429 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 430 | return iterator(); |
| 431 | |
| 432 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 433 | } |
| 434 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 435 | /// \brief Compute the qualification required to get from the current context |
| 436 | /// (\p CurContext) to the target context (\p TargetContext). |
| 437 | /// |
| 438 | /// \param Context the AST context in which the qualification will be used. |
| 439 | /// |
| 440 | /// \param CurContext the context where an entity is being named, which is |
| 441 | /// typically based on the current scope. |
| 442 | /// |
| 443 | /// \param TargetContext the context in which the named entity actually |
| 444 | /// resides. |
| 445 | /// |
| 446 | /// \returns a nested name specifier that refers into the target context, or |
| 447 | /// NULL if no qualification is needed. |
| 448 | static NestedNameSpecifier * |
| 449 | getRequiredQualification(ASTContext &Context, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 450 | const DeclContext *CurContext, |
| 451 | const DeclContext *TargetContext) { |
| 452 | SmallVector<const DeclContext *, 4> TargetParents; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 453 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 454 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 455 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 456 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 457 | if (CommonAncestor->isTransparentContext() || |
| 458 | CommonAncestor->isFunctionOrMethod()) |
| 459 | continue; |
| 460 | |
| 461 | TargetParents.push_back(CommonAncestor); |
| 462 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 463 | |
| 464 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 465 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 466 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 467 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 468 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 469 | if (!Namespace->getIdentifier()) |
| 470 | continue; |
| 471 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 472 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 473 | } |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 474 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 475 | Result = NestedNameSpecifier::Create(Context, Result, |
| 476 | false, |
| 477 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 478 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 479 | return Result; |
| 480 | } |
| 481 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 482 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 483 | /// 7.1.3, C++ [lib.global.names]). |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 484 | static bool isReservedName(const IdentifierInfo *Id, |
| 485 | bool doubleUnderscoreOnly = false) { |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 486 | if (Id->getLength() < 2) |
| 487 | return false; |
| 488 | const char *Name = Id->getNameStart(); |
| 489 | return Name[0] == '_' && |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 490 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z' && |
| 491 | !doubleUnderscoreOnly)); |
| 492 | } |
| 493 | |
| 494 | // Some declarations have reserved names that we don't want to ever show. |
| 495 | // Filter out names reserved for the implementation if they come from a |
| 496 | // system header. |
| 497 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 498 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 499 | if (!Id) |
| 500 | return false; |
| 501 | |
| 502 | // Ignore reserved names for compiler provided decls. |
| 503 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 504 | return true; |
| 505 | |
| 506 | // For system headers ignore only double-underscore names. |
| 507 | // This allows for system headers providing private symbols with a single |
| 508 | // underscore. |
| 509 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
| 510 | SemaRef.SourceMgr.isInSystemHeader( |
| 511 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 512 | return true; |
| 513 | |
| 514 | return false; |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 517 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 518 | bool &AsNestedNameSpecifier) const { |
| 519 | AsNestedNameSpecifier = false; |
| 520 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 521 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 522 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 523 | |
| 524 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 525 | if (!ND->getDeclName()) |
| 526 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 527 | |
| 528 | // Friend declarations and declarations introduced due to friends are never |
| 529 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 530 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 531 | return false; |
| 532 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 533 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 534 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 535 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 536 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 538 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 539 | if (isa<UsingDecl>(ND)) |
| 540 | return false; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 541 | |
| 542 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 543 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 544 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 545 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 546 | (isa<NamespaceDecl>(ND) && |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 547 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 548 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 549 | Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 550 | AsNestedNameSpecifier = true; |
| 551 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 552 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 553 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 554 | // Check whether it is interesting as a nested-name-specifier. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 555 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 556 | IsNestedNameSpecifier(ND) && |
| 557 | (Filter != &ResultBuilder::IsMember || |
| 558 | (isa<CXXRecordDecl>(ND) && |
| 559 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 560 | AsNestedNameSpecifier = true; |
| 561 | return true; |
| 562 | } |
| 563 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 564 | return false; |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 565 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 566 | // ... then it must be interesting! |
| 567 | return true; |
| 568 | } |
| 569 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 570 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 571 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 572 | // In C, there is no way to refer to a hidden name. |
| 573 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 574 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 575 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 576 | return true; |
| 577 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 578 | const DeclContext *HiddenCtx = |
| 579 | R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 580 | |
| 581 | // There is no way to qualify a name declared in a function or method. |
| 582 | if (HiddenCtx->isFunctionOrMethod()) |
| 583 | return true; |
| 584 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 585 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 586 | return true; |
| 587 | |
| 588 | // We can refer to the result with the appropriate qualification. Do it. |
| 589 | R.Hidden = true; |
| 590 | R.QualifierIsInformative = false; |
| 591 | |
| 592 | if (!R.Qualifier) |
| 593 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 594 | CurContext, |
| 595 | R.Declaration->getDeclContext()); |
| 596 | return false; |
| 597 | } |
| 598 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 599 | /// \brief A simplified classification of types used to determine whether two |
| 600 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 601 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 602 | switch (T->getTypeClass()) { |
| 603 | case Type::Builtin: |
| 604 | switch (cast<BuiltinType>(T)->getKind()) { |
| 605 | case BuiltinType::Void: |
| 606 | return STC_Void; |
| 607 | |
| 608 | case BuiltinType::NullPtr: |
| 609 | return STC_Pointer; |
| 610 | |
| 611 | case BuiltinType::Overload: |
| 612 | case BuiltinType::Dependent: |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 613 | return STC_Other; |
| 614 | |
| 615 | case BuiltinType::ObjCId: |
| 616 | case BuiltinType::ObjCClass: |
| 617 | case BuiltinType::ObjCSel: |
| 618 | return STC_ObjectiveC; |
| 619 | |
| 620 | default: |
| 621 | return STC_Arithmetic; |
| 622 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 624 | case Type::Complex: |
| 625 | return STC_Arithmetic; |
| 626 | |
| 627 | case Type::Pointer: |
| 628 | return STC_Pointer; |
| 629 | |
| 630 | case Type::BlockPointer: |
| 631 | return STC_Block; |
| 632 | |
| 633 | case Type::LValueReference: |
| 634 | case Type::RValueReference: |
| 635 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 636 | |
| 637 | case Type::ConstantArray: |
| 638 | case Type::IncompleteArray: |
| 639 | case Type::VariableArray: |
| 640 | case Type::DependentSizedArray: |
| 641 | return STC_Array; |
| 642 | |
| 643 | case Type::DependentSizedExtVector: |
| 644 | case Type::Vector: |
| 645 | case Type::ExtVector: |
| 646 | return STC_Arithmetic; |
| 647 | |
| 648 | case Type::FunctionProto: |
| 649 | case Type::FunctionNoProto: |
| 650 | return STC_Function; |
| 651 | |
| 652 | case Type::Record: |
| 653 | return STC_Record; |
| 654 | |
| 655 | case Type::Enum: |
| 656 | return STC_Arithmetic; |
| 657 | |
| 658 | case Type::ObjCObject: |
| 659 | case Type::ObjCInterface: |
| 660 | case Type::ObjCObjectPointer: |
| 661 | return STC_ObjectiveC; |
| 662 | |
| 663 | default: |
| 664 | return STC_Other; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /// \brief Get the type that a given expression will have if this declaration |
| 669 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 670 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 671 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 672 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 673 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 674 | return C.getTypeDeclType(Type); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 675 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 676 | return C.getObjCInterfaceType(Iface); |
| 677 | |
| 678 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 679 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 680 | T = Function->getCallResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 681 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 682 | T = Method->getSendResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 683 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 684 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 685 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 686 | T = Property->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 687 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 688 | T = Value->getType(); |
| 689 | else |
| 690 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 691 | |
| 692 | // Dig through references, function pointers, and block pointers to |
| 693 | // get down to the likely type of an expression when the entity is |
| 694 | // used. |
| 695 | do { |
| 696 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 697 | T = Ref->getPointeeType(); |
| 698 | continue; |
| 699 | } |
| 700 | |
| 701 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 702 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 703 | T = Pointer->getPointeeType(); |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 711 | T = Block->getPointeeType(); |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 716 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 717 | continue; |
| 718 | } |
| 719 | |
| 720 | break; |
| 721 | } while (true); |
| 722 | |
| 723 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 726 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 727 | if (!ND) |
| 728 | return CCP_Unlikely; |
| 729 | |
| 730 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 731 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 732 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 733 | // _cmd is relatively rare |
| 734 | if (const ImplicitParamDecl *ImplicitParam = |
| 735 | dyn_cast<ImplicitParamDecl>(ND)) |
| 736 | if (ImplicitParam->getIdentifier() && |
| 737 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 738 | return CCP_ObjC_cmd; |
| 739 | |
| 740 | return CCP_LocalDeclaration; |
| 741 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 742 | |
| 743 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 744 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 745 | return CCP_MemberDeclaration; |
| 746 | |
| 747 | // Content-based decisions. |
| 748 | if (isa<EnumConstantDecl>(ND)) |
| 749 | return CCP_Constant; |
| 750 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 751 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 752 | // message receiver, or parenthesized expression context. There, it's as |
| 753 | // likely that the user will want to write a type as other declarations. |
| 754 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 755 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 756 | CompletionContext.getKind() |
| 757 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 758 | CompletionContext.getKind() |
| 759 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 760 | return CCP_Type; |
| 761 | |
| 762 | return CCP_Declaration; |
| 763 | } |
| 764 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 765 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 766 | // If this is an Objective-C method declaration whose selector matches our |
| 767 | // preferred selector, give it a priority boost. |
| 768 | if (!PreferredSelector.isNull()) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 769 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 770 | if (PreferredSelector == Method->getSelector()) |
| 771 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 5fb901d | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 773 | // If we have a preferred type, adjust the priority for results with exactly- |
| 774 | // matching or nearly-matching types. |
| 775 | if (!PreferredType.isNull()) { |
| 776 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 777 | if (!T.isNull()) { |
| 778 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 779 | // Check for exactly-matching types (modulo qualifiers). |
| 780 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 781 | R.Priority /= CCF_ExactTypeMatch; |
| 782 | // Check for nearly-matching types, based on classification of each. |
| 783 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 784 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 785 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 786 | R.Priority /= CCF_SimilarTypeMatch; |
| 787 | } |
| 788 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 791 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 792 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 793 | !CompletionContext.wantConstructorResults()) |
| 794 | return; |
| 795 | |
| 796 | ASTContext &Context = SemaRef.Context; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 797 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 798 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 799 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 800 | Record = ClassTemplate->getTemplatedDecl(); |
| 801 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 802 | // Skip specializations and partial specializations. |
| 803 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 804 | return; |
| 805 | } else { |
| 806 | // There are no constructors here. |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | Record = Record->getDefinition(); |
| 811 | if (!Record) |
| 812 | return; |
| 813 | |
| 814 | |
| 815 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 816 | DeclarationName ConstructorName |
| 817 | = Context.DeclarationNames.getCXXConstructorName( |
| 818 | Context.getCanonicalType(RecordTy)); |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 819 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 820 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 821 | E = Ctors.end(); |
| 822 | I != E; ++I) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 823 | R.Declaration = *I; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 824 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 825 | Results.push_back(R); |
| 826 | } |
| 827 | } |
| 828 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 829 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 830 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 831 | |
| 832 | if (R.Kind != Result::RK_Declaration) { |
| 833 | // For non-declaration results, just add the result. |
| 834 | Results.push_back(R); |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 839 | if (const UsingShadowDecl *Using = |
| 840 | dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 841 | MaybeAddResult(Result(Using->getTargetDecl(), |
| 842 | getBasePriority(Using->getTargetDecl()), |
| 843 | R.Qualifier), |
| 844 | CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 848 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 849 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 850 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 851 | bool AsNestedNameSpecifier = false; |
| 852 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 853 | return; |
| 854 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 855 | // C++ constructors are never found by name lookup. |
| 856 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 857 | return; |
| 858 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 859 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 860 | ShadowMapEntry::iterator I, IEnd; |
| 861 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 862 | if (NamePos != SMap.end()) { |
| 863 | I = NamePos->second.begin(); |
| 864 | IEnd = NamePos->second.end(); |
| 865 | } |
| 866 | |
| 867 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 868 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 869 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 870 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 871 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 872 | Results[Index].Declaration = R.Declaration; |
| 873 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 874 | // We're done. |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // This is a new declaration in this scope. However, check whether this |
| 880 | // declaration name is hidden by a similarly-named declaration in an outer |
| 881 | // scope. |
| 882 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 883 | --SMEnd; |
| 884 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 885 | ShadowMapEntry::iterator I, IEnd; |
| 886 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 887 | if (NamePos != SM->end()) { |
| 888 | I = NamePos->second.begin(); |
| 889 | IEnd = NamePos->second.end(); |
| 890 | } |
| 891 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 892 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 893 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 894 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 895 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 896 | continue; |
| 897 | |
| 898 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 899 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 900 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 901 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 902 | continue; |
| 903 | |
| 904 | // 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] | 905 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 906 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 907 | |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | // 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] | 913 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 914 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 916 | // If the filter is for nested-name-specifiers, then this result starts a |
| 917 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 918 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 919 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 920 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 921 | } else |
| 922 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 924 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 925 | if (R.QualifierIsInformative && !R.Qualifier && |
| 926 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 927 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 928 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 929 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 930 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 931 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 932 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 933 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 934 | else |
| 935 | R.QualifierIsInformative = false; |
| 936 | } |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 938 | // Insert this result into the set of results and into the current shadow |
| 939 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 940 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 941 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 942 | |
| 943 | if (!AsNestedNameSpecifier) |
| 944 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 947 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 948 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 949 | if (R.Kind != Result::RK_Declaration) { |
| 950 | // For non-declaration results, just add the result. |
| 951 | Results.push_back(R); |
| 952 | return; |
| 953 | } |
| 954 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 955 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 956 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 957 | AddResult(Result(Using->getTargetDecl(), |
| 958 | getBasePriority(Using->getTargetDecl()), |
| 959 | R.Qualifier), |
| 960 | CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 964 | bool AsNestedNameSpecifier = false; |
| 965 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 966 | return; |
| 967 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 968 | // C++ constructors are never found by name lookup. |
| 969 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 970 | return; |
| 971 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 972 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 973 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 975 | // 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] | 976 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 977 | return; |
| 978 | |
| 979 | // If the filter is for nested-name-specifiers, then this result starts a |
| 980 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 981 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 982 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 983 | R.Priority = CCP_NestedNameSpecifier; |
| 984 | } |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 985 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 986 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 987 | ->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 988 | R.QualifierIsInformative = true; |
| 989 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 990 | // If this result is supposed to have an informative qualifier, add one. |
| 991 | if (R.QualifierIsInformative && !R.Qualifier && |
| 992 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 993 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 994 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 995 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 996 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 997 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 998 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 999 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1000 | else |
| 1001 | R.QualifierIsInformative = false; |
| 1002 | } |
| 1003 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1004 | // Adjust the priority if this result comes from a base class. |
| 1005 | if (InBaseClass) |
| 1006 | R.Priority += CCD_InBaseClass; |
| 1007 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1008 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1010 | if (HasObjectTypeQualifiers) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1011 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1012 | if (Method->isInstance()) { |
| 1013 | Qualifiers MethodQuals |
| 1014 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 1015 | if (ObjectTypeQualifiers == MethodQuals) |
| 1016 | R.Priority += CCD_ObjectQualifierMatch; |
| 1017 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 1018 | // The method cannot be invoked, because doing so would drop |
| 1019 | // qualifiers. |
| 1020 | return; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1024 | // Insert this result into the set of results. |
| 1025 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (!AsNestedNameSpecifier) |
| 1028 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1031 | void ResultBuilder::AddResult(Result R) { |
| 1032 | assert(R.Kind != Result::RK_Declaration && |
| 1033 | "Declaration results need more context"); |
| 1034 | Results.push_back(R); |
| 1035 | } |
| 1036 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1037 | /// \brief Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1038 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1039 | |
| 1040 | /// \brief Exit from the current scope. |
| 1041 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1042 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1043 | EEnd = ShadowMaps.back().end(); |
| 1044 | E != EEnd; |
| 1045 | ++E) |
| 1046 | E->second.Destroy(); |
| 1047 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1048 | ShadowMaps.pop_back(); |
| 1049 | } |
| 1050 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1051 | /// \brief Determines whether this given declaration will be found by |
| 1052 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1053 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1054 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1055 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1056 | // If name lookup finds a local extern declaration, then we are in a |
| 1057 | // context where it behaves like an ordinary name. |
| 1058 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1059 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1060 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1061 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1062 | if (isa<ObjCIvarDecl>(ND)) |
| 1063 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1066 | return ND->getIdentifierNamespace() & IDNS; |
| 1067 | } |
| 1068 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1069 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1070 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1071 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1072 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1073 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 1074 | return false; |
| 1075 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1076 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1077 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1078 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1079 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1080 | if (isa<ObjCIvarDecl>(ND)) |
| 1081 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1084 | return ND->getIdentifierNamespace() & IDNS; |
| 1085 | } |
| 1086 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1087 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1088 | if (!IsOrdinaryNonTypeName(ND)) |
| 1089 | return 0; |
| 1090 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1091 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1092 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1093 | return true; |
| 1094 | |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1098 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1099 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1100 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1101 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1102 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1103 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1104 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1105 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1106 | |
| 1107 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1108 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1109 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1112 | /// \brief Determines whether the given declaration is suitable as the |
| 1113 | /// 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] | 1114 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1115 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1116 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1117 | ND = ClassTemplate->getTemplatedDecl(); |
| 1118 | |
| 1119 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1120 | } |
| 1121 | |
| 1122 | /// \brief Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1123 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1124 | return isa<EnumDecl>(ND); |
| 1125 | } |
| 1126 | |
| 1127 | /// \brief Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1128 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1129 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1130 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1131 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1132 | |
| 1133 | // For purposes of this check, interfaces match too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1134 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1135 | return RD->getTagKind() == TTK_Class || |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1136 | RD->getTagKind() == TTK_Struct || |
| 1137 | RD->getTagKind() == TTK_Interface; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1138 | |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | /// \brief Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1143 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1144 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1145 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1146 | ND = ClassTemplate->getTemplatedDecl(); |
| 1147 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1148 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1149 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1150 | |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | /// \brief Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1155 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1156 | return isa<NamespaceDecl>(ND); |
| 1157 | } |
| 1158 | |
| 1159 | /// \brief Determines whether the given declaration is a namespace or |
| 1160 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1161 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1162 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1165 | /// \brief Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1166 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1167 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1168 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1171 | /// \brief Determines which members of a class should be visible via |
| 1172 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1173 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1174 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1175 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1176 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1177 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1180 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1181 | T = C.getCanonicalType(T); |
| 1182 | switch (T->getTypeClass()) { |
| 1183 | case Type::ObjCObject: |
| 1184 | case Type::ObjCInterface: |
| 1185 | case Type::ObjCObjectPointer: |
| 1186 | return true; |
| 1187 | |
| 1188 | case Type::Builtin: |
| 1189 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1190 | case BuiltinType::ObjCId: |
| 1191 | case BuiltinType::ObjCClass: |
| 1192 | case BuiltinType::ObjCSel: |
| 1193 | return true; |
| 1194 | |
| 1195 | default: |
| 1196 | break; |
| 1197 | } |
| 1198 | return false; |
| 1199 | |
| 1200 | default: |
| 1201 | break; |
| 1202 | } |
| 1203 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1204 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1205 | return false; |
| 1206 | |
| 1207 | // FIXME: We could perform more analysis here to determine whether a |
| 1208 | // particular class type has any conversions to Objective-C types. For now, |
| 1209 | // just accept all class types. |
| 1210 | return T->isDependentType() || T->isRecordType(); |
| 1211 | } |
| 1212 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1213 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1214 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1215 | if (T.isNull()) |
| 1216 | return false; |
| 1217 | |
| 1218 | T = SemaRef.Context.getBaseElementType(T); |
| 1219 | return isObjCReceiverType(SemaRef.Context, T); |
| 1220 | } |
| 1221 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1222 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1223 | if (IsObjCMessageReceiver(ND)) |
| 1224 | return true; |
| 1225 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1226 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1227 | if (!Var) |
| 1228 | return false; |
| 1229 | |
| 1230 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1231 | } |
| 1232 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1233 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1234 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1235 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1236 | return false; |
| 1237 | |
| 1238 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1239 | if (T.isNull()) |
| 1240 | return false; |
| 1241 | |
| 1242 | T = SemaRef.Context.getBaseElementType(T); |
| 1243 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1244 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1245 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1246 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1247 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1248 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 1252 | /// \brief Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1253 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1254 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1255 | return isa<ObjCIvarDecl>(ND); |
| 1256 | } |
| 1257 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1258 | namespace { |
| 1259 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1260 | /// for each visible declaration. |
| 1261 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1262 | ResultBuilder &Results; |
| 1263 | DeclContext *CurContext; |
| 1264 | |
| 1265 | public: |
| 1266 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1267 | : Results(Results), CurContext(CurContext) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1268 | |
| 1269 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1270 | bool InBaseClass) override { |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1271 | bool Accessible = true; |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1272 | if (Ctx) |
| 1273 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1274 | |
| 1275 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
| 1276 | false, Accessible); |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1277 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1278 | } |
| 1279 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1280 | } |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1282 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1283 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1284 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1285 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1286 | Results.AddResult(Result("short", CCP_Type)); |
| 1287 | Results.AddResult(Result("long", CCP_Type)); |
| 1288 | Results.AddResult(Result("signed", CCP_Type)); |
| 1289 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1290 | Results.AddResult(Result("void", CCP_Type)); |
| 1291 | Results.AddResult(Result("char", CCP_Type)); |
| 1292 | Results.AddResult(Result("int", CCP_Type)); |
| 1293 | Results.AddResult(Result("float", CCP_Type)); |
| 1294 | Results.AddResult(Result("double", CCP_Type)); |
| 1295 | Results.AddResult(Result("enum", CCP_Type)); |
| 1296 | Results.AddResult(Result("struct", CCP_Type)); |
| 1297 | Results.AddResult(Result("union", CCP_Type)); |
| 1298 | Results.AddResult(Result("const", CCP_Type)); |
| 1299 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1301 | if (LangOpts.C99) { |
| 1302 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1303 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1304 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1305 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1306 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1309 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1310 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1311 | if (LangOpts.CPlusPlus) { |
| 1312 | // C++-specific |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1313 | Results.AddResult(Result("bool", CCP_Type + |
| 1314 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1315 | Results.AddResult(Result("class", CCP_Type)); |
| 1316 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1318 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1319 | Builder.AddTypedTextChunk("typename"); |
| 1320 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1321 | Builder.AddPlaceholderChunk("qualifier"); |
| 1322 | Builder.AddTextChunk("::"); |
| 1323 | Builder.AddPlaceholderChunk("name"); |
| 1324 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1325 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1326 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1327 | Results.AddResult(Result("auto", CCP_Type)); |
| 1328 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1329 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1331 | Builder.AddTypedTextChunk("decltype"); |
| 1332 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1333 | Builder.AddPlaceholderChunk("expression"); |
| 1334 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1335 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1336 | } |
Alex Lorenz | 46eed9d | 2017-02-13 23:35:59 +0000 | [diff] [blame] | 1337 | } else |
| 1338 | Results.AddResult(Result("__auto_type", CCP_Type)); |
| 1339 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1340 | // GNU extensions |
| 1341 | if (LangOpts.GNUMode) { |
| 1342 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1343 | // Results.AddResult(Result("_Decimal32")); |
| 1344 | // Results.AddResult(Result("_Decimal64")); |
| 1345 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1347 | Builder.AddTypedTextChunk("typeof"); |
| 1348 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1349 | Builder.AddPlaceholderChunk("expression"); |
| 1350 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1351 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1352 | Builder.AddTypedTextChunk("typeof"); |
| 1353 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1354 | Builder.AddPlaceholderChunk("type"); |
| 1355 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1356 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1357 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1360 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1361 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1362 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1365 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1366 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1367 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1368 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1369 | // Note: we don't suggest either "auto" or "register", because both |
| 1370 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1371 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1372 | Results.AddResult(Result("extern")); |
| 1373 | Results.AddResult(Result("static")); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1374 | |
| 1375 | if (LangOpts.CPlusPlus11) { |
| 1376 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 1377 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| 1378 | |
| 1379 | // alignas |
| 1380 | Builder.AddTypedTextChunk("alignas"); |
| 1381 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1382 | Builder.AddPlaceholderChunk("expression"); |
| 1383 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1384 | Results.AddResult(Result(Builder.TakeString())); |
| 1385 | |
| 1386 | Results.AddResult(Result("constexpr")); |
| 1387 | Results.AddResult(Result("thread_local")); |
| 1388 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1391 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1392 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1393 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1394 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1395 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1396 | case Sema::PCC_Class: |
| 1397 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1398 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1399 | Results.AddResult(Result("explicit")); |
| 1400 | Results.AddResult(Result("friend")); |
| 1401 | Results.AddResult(Result("mutable")); |
| 1402 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1403 | } |
| 1404 | // Fall through |
| 1405 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1406 | case Sema::PCC_ObjCInterface: |
| 1407 | case Sema::PCC_ObjCImplementation: |
| 1408 | case Sema::PCC_Namespace: |
| 1409 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1410 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1411 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1414 | case Sema::PCC_ObjCInstanceVariableList: |
| 1415 | case Sema::PCC_Expression: |
| 1416 | case Sema::PCC_Statement: |
| 1417 | case Sema::PCC_ForInit: |
| 1418 | case Sema::PCC_Condition: |
| 1419 | case Sema::PCC_RecoveryInFunction: |
| 1420 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1421 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1422 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | } |
| 1426 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1427 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1428 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1429 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1430 | ResultBuilder &Results, |
| 1431 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1432 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1433 | ResultBuilder &Results, |
| 1434 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1435 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1436 | ResultBuilder &Results, |
| 1437 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1438 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1440 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1441 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1442 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1443 | Builder.AddTypedTextChunk("typedef"); |
| 1444 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1445 | Builder.AddPlaceholderChunk("type"); |
| 1446 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1447 | Builder.AddPlaceholderChunk("name"); |
| 1448 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1451 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1452 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1453 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1454 | case Sema::PCC_Namespace: |
| 1455 | case Sema::PCC_Class: |
| 1456 | case Sema::PCC_ObjCInstanceVariableList: |
| 1457 | case Sema::PCC_Template: |
| 1458 | case Sema::PCC_MemberTemplate: |
| 1459 | case Sema::PCC_Statement: |
| 1460 | case Sema::PCC_RecoveryInFunction: |
| 1461 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1462 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1463 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1464 | return true; |
| 1465 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1466 | case Sema::PCC_Expression: |
| 1467 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1468 | return LangOpts.CPlusPlus; |
| 1469 | |
| 1470 | case Sema::PCC_ObjCInterface: |
| 1471 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1474 | case Sema::PCC_ForInit: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1475 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1476 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1477 | |
| 1478 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1481 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1482 | const Preprocessor &PP) { |
| 1483 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1484 | Policy.AnonymousTagLocations = false; |
| 1485 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1486 | Policy.SuppressUnwrittenScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1487 | return Policy; |
| 1488 | } |
| 1489 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1490 | /// \brief Retrieve a printing policy suitable for code completion. |
| 1491 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1492 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1493 | } |
| 1494 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1495 | /// \brief Retrieve the string representation of the given type as a string |
| 1496 | /// that has the appropriate lifetime for code completion. |
| 1497 | /// |
| 1498 | /// This routine provides a fast path where we provide constant strings for |
| 1499 | /// common type names. |
| 1500 | static const char *GetCompletionTypeString(QualType T, |
| 1501 | ASTContext &Context, |
| 1502 | const PrintingPolicy &Policy, |
| 1503 | CodeCompletionAllocator &Allocator) { |
| 1504 | if (!T.getLocalQualifiers()) { |
| 1505 | // Built-in type names are constant strings. |
| 1506 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1507 | return BT->getNameAsCString(Policy); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Anonymous tag types are constant strings. |
| 1510 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1511 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1512 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1513 | switch (Tag->getTagKind()) { |
| 1514 | case TTK_Struct: return "struct <anonymous>"; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1515 | case TTK_Interface: return "__interface <anonymous>"; |
| 1516 | case TTK_Class: return "class <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1517 | case TTK_Union: return "union <anonymous>"; |
| 1518 | case TTK_Enum: return "enum <anonymous>"; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | // Slow path: format the type as a string. |
| 1524 | std::string Result; |
| 1525 | T.getAsStringInternal(Result, Policy); |
| 1526 | return Allocator.CopyString(Result); |
| 1527 | } |
| 1528 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1529 | /// \brief Add a completion for "this", if we're in a member function. |
| 1530 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1531 | QualType ThisTy = S.getCurrentThisType(); |
| 1532 | if (ThisTy.isNull()) |
| 1533 | return; |
| 1534 | |
| 1535 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1536 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1537 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| 1538 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1539 | S.Context, |
| 1540 | Policy, |
| 1541 | Allocator)); |
| 1542 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1543 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1546 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
| 1547 | ResultBuilder &Results, |
| 1548 | const LangOptions &LangOpts) { |
| 1549 | if (!LangOpts.CPlusPlus11) |
| 1550 | return; |
| 1551 | |
| 1552 | Builder.AddTypedTextChunk("static_assert"); |
| 1553 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1554 | Builder.AddPlaceholderChunk("expression"); |
| 1555 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 1556 | Builder.AddPlaceholderChunk("message"); |
| 1557 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1558 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 1559 | } |
| 1560 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1562 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1563 | Scope *S, |
| 1564 | Sema &SemaRef, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1565 | ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1566 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1567 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1568 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1569 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1570 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1571 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1572 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1573 | if (Results.includeCodePatterns()) { |
| 1574 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1575 | Builder.AddTypedTextChunk("namespace"); |
| 1576 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1577 | Builder.AddPlaceholderChunk("identifier"); |
| 1578 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1579 | Builder.AddPlaceholderChunk("declarations"); |
| 1580 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1581 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1582 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1585 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1586 | Builder.AddTypedTextChunk("namespace"); |
| 1587 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1588 | Builder.AddPlaceholderChunk("name"); |
| 1589 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1590 | Builder.AddPlaceholderChunk("namespace"); |
| 1591 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1592 | |
| 1593 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1594 | Builder.AddTypedTextChunk("using"); |
| 1595 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1596 | Builder.AddTextChunk("namespace"); |
| 1597 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1598 | Builder.AddPlaceholderChunk("identifier"); |
| 1599 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1600 | |
| 1601 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1602 | Builder.AddTypedTextChunk("asm"); |
| 1603 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1604 | Builder.AddPlaceholderChunk("string-literal"); |
| 1605 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1606 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1608 | if (Results.includeCodePatterns()) { |
| 1609 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1610 | Builder.AddTypedTextChunk("template"); |
| 1611 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1612 | Builder.AddPlaceholderChunk("declaration"); |
| 1613 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1614 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1615 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1616 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1617 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1618 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1620 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1621 | // Fall through |
| 1622 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1623 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1624 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1625 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1626 | Builder.AddTypedTextChunk("using"); |
| 1627 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1628 | Builder.AddPlaceholderChunk("qualifier"); |
| 1629 | Builder.AddTextChunk("::"); |
| 1630 | Builder.AddPlaceholderChunk("name"); |
| 1631 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1632 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1633 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1634 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1635 | Builder.AddTypedTextChunk("using"); |
| 1636 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1637 | Builder.AddTextChunk("typename"); |
| 1638 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1639 | Builder.AddPlaceholderChunk("qualifier"); |
| 1640 | Builder.AddTextChunk("::"); |
| 1641 | Builder.AddPlaceholderChunk("name"); |
| 1642 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1645 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| 1646 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1647 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1648 | AddTypedefResult(Results); |
| 1649 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1650 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1651 | Builder.AddTypedTextChunk("public"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1652 | if (Results.includeCodePatterns()) |
| 1653 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1654 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1655 | |
| 1656 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1657 | Builder.AddTypedTextChunk("protected"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1658 | if (Results.includeCodePatterns()) |
| 1659 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1660 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1661 | |
| 1662 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1663 | Builder.AddTypedTextChunk("private"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1664 | if (Results.includeCodePatterns()) |
| 1665 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1666 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | // Fall through |
| 1670 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1671 | case Sema::PCC_Template: |
| 1672 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1673 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1674 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1675 | Builder.AddTypedTextChunk("template"); |
| 1676 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1677 | Builder.AddPlaceholderChunk("parameters"); |
| 1678 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1679 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1682 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1683 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1684 | break; |
| 1685 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1686 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1687 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1688 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1689 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1692 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1693 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1694 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1695 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1698 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1699 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1702 | case Sema::PCC_RecoveryInFunction: |
| 1703 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1704 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1705 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1706 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1707 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1708 | Builder.AddTypedTextChunk("try"); |
| 1709 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1710 | Builder.AddPlaceholderChunk("statements"); |
| 1711 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1712 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1713 | Builder.AddTextChunk("catch"); |
| 1714 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1715 | Builder.AddPlaceholderChunk("declaration"); |
| 1716 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1717 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1718 | Builder.AddPlaceholderChunk("statements"); |
| 1719 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1720 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1721 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1722 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1723 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1724 | AddObjCStatementResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1725 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1726 | if (Results.includeCodePatterns()) { |
| 1727 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1728 | Builder.AddTypedTextChunk("if"); |
| 1729 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1730 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1731 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1732 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1733 | Builder.AddPlaceholderChunk("expression"); |
| 1734 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1735 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1736 | Builder.AddPlaceholderChunk("statements"); |
| 1737 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1738 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1739 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1741 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1742 | Builder.AddTypedTextChunk("switch"); |
| 1743 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1744 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1745 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1746 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1747 | Builder.AddPlaceholderChunk("expression"); |
| 1748 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1749 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1750 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1751 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1752 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1755 | // Switch-specific statements. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1756 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1757 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1758 | Builder.AddTypedTextChunk("case"); |
| 1759 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1760 | Builder.AddPlaceholderChunk("expression"); |
| 1761 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1762 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1763 | |
| 1764 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1765 | Builder.AddTypedTextChunk("default"); |
| 1766 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1767 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1770 | if (Results.includeCodePatterns()) { |
| 1771 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1772 | Builder.AddTypedTextChunk("while"); |
| 1773 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1774 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1775 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1776 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1777 | Builder.AddPlaceholderChunk("expression"); |
| 1778 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1779 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1780 | Builder.AddPlaceholderChunk("statements"); |
| 1781 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1782 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1783 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1784 | |
| 1785 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1786 | Builder.AddTypedTextChunk("do"); |
| 1787 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1788 | Builder.AddPlaceholderChunk("statements"); |
| 1789 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1790 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1791 | Builder.AddTextChunk("while"); |
| 1792 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1793 | Builder.AddPlaceholderChunk("expression"); |
| 1794 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1795 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1797 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1798 | Builder.AddTypedTextChunk("for"); |
| 1799 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1800 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1801 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1802 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1803 | Builder.AddPlaceholderChunk("init-expression"); |
| 1804 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1805 | Builder.AddPlaceholderChunk("condition"); |
| 1806 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1807 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1808 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1809 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1810 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1811 | Builder.AddPlaceholderChunk("statements"); |
| 1812 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1813 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1814 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1815 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (S->getContinueParent()) { |
| 1818 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1819 | Builder.AddTypedTextChunk("continue"); |
| 1820 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | if (S->getBreakParent()) { |
| 1824 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1825 | Builder.AddTypedTextChunk("break"); |
| 1826 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | // "return expression ;" or "return ;", depending on whether we |
| 1830 | // know the function is void or not. |
| 1831 | bool isVoid = false; |
| 1832 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1833 | isVoid = Function->getReturnType()->isVoidType(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1834 | else if (ObjCMethodDecl *Method |
| 1835 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1836 | isVoid = Method->getReturnType()->isVoidType(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1837 | else if (SemaRef.getCurBlock() && |
| 1838 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1839 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1840 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1841 | if (!isVoid) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1842 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1843 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1844 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1845 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1847 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1848 | Builder.AddTypedTextChunk("goto"); |
| 1849 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1850 | Builder.AddPlaceholderChunk("label"); |
| 1851 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1853 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1854 | Builder.AddTypedTextChunk("using"); |
| 1855 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1856 | Builder.AddTextChunk("namespace"); |
| 1857 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1858 | Builder.AddPlaceholderChunk("identifier"); |
| 1859 | Results.AddResult(Result(Builder.TakeString())); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1860 | |
| 1861 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1865 | case Sema::PCC_ForInit: |
| 1866 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1867 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1868 | // Fall through: conditions and statements can have expressions. |
| 1869 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1870 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1871 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1872 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1873 | // (__bridge <type>)<expression> |
| 1874 | Builder.AddTypedTextChunk("__bridge"); |
| 1875 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1876 | Builder.AddPlaceholderChunk("type"); |
| 1877 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1878 | Builder.AddPlaceholderChunk("expression"); |
| 1879 | Results.AddResult(Result(Builder.TakeString())); |
| 1880 | |
| 1881 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1882 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1883 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1884 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1885 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1886 | Builder.AddPlaceholderChunk("expression"); |
| 1887 | Results.AddResult(Result(Builder.TakeString())); |
| 1888 | |
| 1889 | // (__bridge_retained <CF type>)<expression> |
| 1890 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1891 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1892 | Builder.AddPlaceholderChunk("CF type"); |
| 1893 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1894 | Builder.AddPlaceholderChunk("expression"); |
| 1895 | Results.AddResult(Result(Builder.TakeString())); |
| 1896 | } |
| 1897 | // Fall through |
| 1898 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1899 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1900 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1901 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1902 | addThisCompletion(SemaRef, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1904 | // true |
| 1905 | Builder.AddResultTypeChunk("bool"); |
| 1906 | Builder.AddTypedTextChunk("true"); |
| 1907 | Results.AddResult(Result(Builder.TakeString())); |
| 1908 | |
| 1909 | // false |
| 1910 | Builder.AddResultTypeChunk("bool"); |
| 1911 | Builder.AddTypedTextChunk("false"); |
| 1912 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1913 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1914 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1915 | // dynamic_cast < type-id > ( expression ) |
| 1916 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1917 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1918 | Builder.AddPlaceholderChunk("type"); |
| 1919 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1920 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1921 | Builder.AddPlaceholderChunk("expression"); |
| 1922 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1923 | Results.AddResult(Result(Builder.TakeString())); |
| 1924 | } |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1925 | |
| 1926 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1927 | Builder.AddTypedTextChunk("static_cast"); |
| 1928 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1929 | Builder.AddPlaceholderChunk("type"); |
| 1930 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1931 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1932 | Builder.AddPlaceholderChunk("expression"); |
| 1933 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1934 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1935 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1936 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1937 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1938 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1939 | Builder.AddPlaceholderChunk("type"); |
| 1940 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1941 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1942 | Builder.AddPlaceholderChunk("expression"); |
| 1943 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1944 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1946 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1947 | Builder.AddTypedTextChunk("const_cast"); |
| 1948 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1949 | Builder.AddPlaceholderChunk("type"); |
| 1950 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1951 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1952 | Builder.AddPlaceholderChunk("expression"); |
| 1953 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1954 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1955 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1956 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1957 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1958 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1959 | Builder.AddTypedTextChunk("typeid"); |
| 1960 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1961 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1962 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1963 | Results.AddResult(Result(Builder.TakeString())); |
| 1964 | } |
| 1965 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1966 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1967 | Builder.AddTypedTextChunk("new"); |
| 1968 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1969 | Builder.AddPlaceholderChunk("type"); |
| 1970 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1971 | Builder.AddPlaceholderChunk("expressions"); |
| 1972 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1973 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1975 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1976 | Builder.AddTypedTextChunk("new"); |
| 1977 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1978 | Builder.AddPlaceholderChunk("type"); |
| 1979 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1980 | Builder.AddPlaceholderChunk("size"); |
| 1981 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1983 | Builder.AddPlaceholderChunk("expressions"); |
| 1984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1985 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1987 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1988 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1989 | Builder.AddTypedTextChunk("delete"); |
| 1990 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1991 | Builder.AddPlaceholderChunk("expression"); |
| 1992 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1994 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1995 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1996 | Builder.AddTypedTextChunk("delete"); |
| 1997 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1998 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1999 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2000 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2001 | Builder.AddPlaceholderChunk("expression"); |
| 2002 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2003 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2004 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2005 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2006 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2007 | Builder.AddTypedTextChunk("throw"); |
| 2008 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2009 | Builder.AddPlaceholderChunk("expression"); |
| 2010 | Results.AddResult(Result(Builder.TakeString())); |
| 2011 | } |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2012 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 2013 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2014 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2015 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2016 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2017 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2018 | Builder.AddTypedTextChunk("nullptr"); |
| 2019 | Results.AddResult(Result(Builder.TakeString())); |
| 2020 | |
| 2021 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2022 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2023 | Builder.AddTypedTextChunk("alignof"); |
| 2024 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2025 | Builder.AddPlaceholderChunk("type"); |
| 2026 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2027 | Results.AddResult(Result(Builder.TakeString())); |
| 2028 | |
| 2029 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2030 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2031 | Builder.AddTypedTextChunk("noexcept"); |
| 2032 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2033 | Builder.AddPlaceholderChunk("expression"); |
| 2034 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2035 | Results.AddResult(Result(Builder.TakeString())); |
| 2036 | |
| 2037 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2038 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2039 | Builder.AddTypedTextChunk("sizeof..."); |
| 2040 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2041 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2042 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2043 | Results.AddResult(Result(Builder.TakeString())); |
| 2044 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2047 | if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2048 | // 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] | 2049 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2050 | // The interface can be NULL. |
| 2051 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2052 | if (ID->getSuperClass()) { |
| 2053 | std::string SuperType; |
| 2054 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2055 | if (Method->isInstanceMethod()) |
| 2056 | SuperType += " *"; |
| 2057 | |
| 2058 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2059 | Builder.AddTypedTextChunk("super"); |
| 2060 | Results.AddResult(Result(Builder.TakeString())); |
| 2061 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2064 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2067 | if (SemaRef.getLangOpts().C11) { |
| 2068 | // _Alignof |
| 2069 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2070 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2071 | Builder.AddTypedTextChunk("alignof"); |
| 2072 | else |
| 2073 | Builder.AddTypedTextChunk("_Alignof"); |
| 2074 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2075 | Builder.AddPlaceholderChunk("type"); |
| 2076 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2077 | Results.AddResult(Result(Builder.TakeString())); |
| 2078 | } |
| 2079 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2080 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2081 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2082 | Builder.AddTypedTextChunk("sizeof"); |
| 2083 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2084 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2085 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2086 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2089 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2090 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2091 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2092 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2095 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2096 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2097 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2098 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2099 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2102 | /// \brief If the given declaration has an associated type, add it as a result |
| 2103 | /// type chunk. |
| 2104 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2105 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2106 | const NamedDecl *ND, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2107 | QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2108 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2109 | if (!ND) |
| 2110 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2111 | |
| 2112 | // Skip constructors and conversion functions, which have their return types |
| 2113 | // built into their names. |
| 2114 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 2115 | return; |
| 2116 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2117 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2118 | QualType T; |
| 2119 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2120 | T = Function->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2121 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 2122 | if (!BaseType.isNull()) |
| 2123 | T = Method->getSendResultType(BaseType); |
| 2124 | else |
| 2125 | T = Method->getReturnType(); |
| 2126 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2127 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 2128 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 2129 | /* Do nothing: ignore unresolved using declarations*/ |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2130 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 2131 | if (!BaseType.isNull()) |
| 2132 | T = Ivar->getUsageType(BaseType); |
| 2133 | else |
| 2134 | T = Ivar->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2135 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2136 | T = Value->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2137 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
| 2138 | if (!BaseType.isNull()) |
| 2139 | T = Property->getUsageType(BaseType); |
| 2140 | else |
| 2141 | T = Property->getType(); |
| 2142 | } |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2143 | |
| 2144 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2145 | return; |
| 2146 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2147 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2148 | Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2151 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2152 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2153 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2154 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2155 | if (Sentinel->getSentinel() == 0) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2156 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2157 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2158 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2159 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2160 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2161 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2165 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
| 2166 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2167 | std::string Result; |
| 2168 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2169 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2170 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2171 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2172 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2173 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2174 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2175 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2176 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2177 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2178 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2179 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2180 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2181 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2182 | switch (*nullability) { |
| 2183 | case NullabilityKind::NonNull: |
| 2184 | Result += "nonnull "; |
| 2185 | break; |
| 2186 | |
| 2187 | case NullabilityKind::Nullable: |
| 2188 | Result += "nullable "; |
| 2189 | break; |
| 2190 | |
| 2191 | case NullabilityKind::Unspecified: |
| 2192 | Result += "null_unspecified "; |
| 2193 | break; |
| 2194 | } |
| 2195 | } |
| 2196 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2197 | return Result; |
| 2198 | } |
| 2199 | |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2200 | /// \brief Tries to find the most appropriate type location for an Objective-C |
| 2201 | /// block placeholder. |
| 2202 | /// |
| 2203 | /// This function ignores things like typedefs and qualifiers in order to |
| 2204 | /// present the most relevant and accurate block placeholders in code completion |
| 2205 | /// results. |
| 2206 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
| 2207 | FunctionTypeLoc &Block, |
| 2208 | FunctionProtoTypeLoc &BlockProto, |
| 2209 | bool SuppressBlock = false) { |
| 2210 | if (!TSInfo) |
| 2211 | return; |
| 2212 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2213 | while (true) { |
| 2214 | // Look through typedefs. |
| 2215 | if (!SuppressBlock) { |
| 2216 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2217 | if (TypeSourceInfo *InnerTSInfo = |
| 2218 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
| 2219 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2220 | continue; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | // Look through qualified types |
| 2225 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2226 | TL = QualifiedTL.getUnqualifiedLoc(); |
| 2227 | continue; |
| 2228 | } |
| 2229 | |
| 2230 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2231 | TL = AttrTL.getModifiedLoc(); |
| 2232 | continue; |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | // Try to get the function prototype behind the block pointer type, |
| 2237 | // then we're done. |
| 2238 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2239 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2240 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2241 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
| 2242 | } |
| 2243 | break; |
| 2244 | } |
| 2245 | } |
| 2246 | |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2247 | static std::string |
| 2248 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2249 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2250 | bool SuppressBlockName = false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2251 | bool SuppressBlock = false, |
| 2252 | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
| 2253 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2254 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2255 | const ParmVarDecl *Param, |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2256 | bool SuppressName = false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2257 | bool SuppressBlock = false, |
| 2258 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2259 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2260 | if (Param->getType()->isDependentType() || |
| 2261 | !Param->getType()->isBlockPointerType()) { |
| 2262 | // The argument for a dependent or non-block parameter is a placeholder |
| 2263 | // containing that parameter's type. |
| 2264 | std::string Result; |
| 2265 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2266 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2267 | Result = Param->getIdentifier()->getName(); |
| 2268 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2269 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2270 | if (ObjCSubsts) |
| 2271 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2272 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2273 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2274 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2275 | Type); |
| 2276 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2277 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2278 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2279 | } else { |
| 2280 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2281 | } |
| 2282 | return Result; |
| 2283 | } |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2284 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2285 | // The argument for a block pointer parameter is a block literal with |
| 2286 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2287 | FunctionTypeLoc Block; |
| 2288 | FunctionProtoTypeLoc BlockProto; |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2289 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
| 2290 | SuppressBlock); |
Alex Lorenz | 6bf4a58 | 2017-03-13 15:43:42 +0000 | [diff] [blame] | 2291 | // Try to retrieve the block type information from the property if this is a |
| 2292 | // parameter in a setter. |
| 2293 | if (!Block && ObjCMethodParam && |
| 2294 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { |
| 2295 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
| 2296 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
| 2297 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
| 2298 | SuppressBlock); |
| 2299 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2300 | |
| 2301 | if (!Block) { |
| 2302 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2303 | // for the block; just use the parameter type as a placeholder. |
| 2304 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2305 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2306 | Result = Param->getIdentifier()->getName(); |
| 2307 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2308 | QualType Type = Param->getType().getUnqualifiedType(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2309 | |
| 2310 | if (ObjCMethodParam) { |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2311 | Result = Type.getAsString(Policy); |
| 2312 | std::string Quals = |
| 2313 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
| 2314 | if (!Quals.empty()) |
| 2315 | Result = "(" + Quals + " " + Result + ")"; |
| 2316 | if (Result.back() != ')') |
| 2317 | Result += " "; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2318 | if (Param->getIdentifier()) |
| 2319 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2320 | } else { |
| 2321 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | return Result; |
| 2325 | } |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2326 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2327 | // We have the function prototype behind the block pointer type, as it was |
| 2328 | // written in the source. |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2329 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
| 2330 | /*SuppressBlockName=*/false, SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2331 | ObjCSubsts); |
| 2332 | } |
| 2333 | |
| 2334 | /// \brief Returns a placeholder string that corresponds to an Objective-C block |
| 2335 | /// declaration. |
| 2336 | /// |
| 2337 | /// \param BlockDecl A declaration with an Objective-C block type. |
| 2338 | /// |
| 2339 | /// \param Block The most relevant type location for that block type. |
| 2340 | /// |
| 2341 | /// \param SuppressBlockName Determines wether or not the name of the block |
| 2342 | /// declaration is included in the resulting string. |
| 2343 | static std::string |
| 2344 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2345 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2346 | bool SuppressBlockName, bool SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2347 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2348 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2349 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2350 | if (ObjCSubsts) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2351 | ResultType = |
| 2352 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
| 2353 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2354 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2355 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2356 | |
| 2357 | // Format the parameter list. |
| 2358 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2359 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2360 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2361 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2362 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2363 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2364 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2365 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2366 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2367 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2368 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2369 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2370 | /*SuppressName=*/false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2371 | /*SuppressBlock=*/true, ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2372 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2373 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2374 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2375 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2376 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2377 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2378 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2379 | if (SuppressBlock) { |
| 2380 | // Format as a parameter. |
| 2381 | Result = Result + " (^"; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2382 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2383 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2384 | Result += ")"; |
| 2385 | Result += Params; |
| 2386 | } else { |
| 2387 | // Format as a block literal argument. |
| 2388 | Result = '^' + Result; |
| 2389 | Result += Params; |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2390 | |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2391 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2392 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2393 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2394 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2395 | return Result; |
| 2396 | } |
| 2397 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2398 | /// \brief Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2399 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2400 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2401 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2402 | CodeCompletionBuilder &Result, |
| 2403 | unsigned Start = 0, |
| 2404 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2405 | bool FirstParameter = true; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2406 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2407 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2408 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2409 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2410 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2411 | // When we see an optional default argument, put that argument and |
| 2412 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2413 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2414 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2415 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2416 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2417 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2418 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2419 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2422 | if (FirstParameter) |
| 2423 | FirstParameter = false; |
| 2424 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2425 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2426 | |
| 2427 | InOptional = false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2428 | |
| 2429 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2430 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
| 2431 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2432 | if (Function->isVariadic() && P == N - 1) |
| 2433 | PlaceholderStr += ", ..."; |
| 2434 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2435 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2436 | Result.AddPlaceholderChunk( |
| 2437 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2438 | } |
Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2439 | |
| 2440 | if (const FunctionProtoType *Proto |
| 2441 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2442 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2443 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2444 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2445 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2446 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2447 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | /// \brief Add template parameter chunks to the given code completion string. |
| 2451 | static void AddTemplateParameterChunks(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2452 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2453 | const TemplateDecl *Template, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2454 | CodeCompletionBuilder &Result, |
| 2455 | unsigned MaxParameters = 0, |
| 2456 | unsigned Start = 0, |
| 2457 | bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2458 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Prefer to take the template parameter names from the first declaration of |
| 2461 | // the template. |
| 2462 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2463 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2464 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2465 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2466 | if (MaxParameters) |
| 2467 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2468 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2469 | P != PEnd; ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2470 | bool HasDefaultArg = false; |
| 2471 | std::string PlaceholderStr; |
| 2472 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2473 | if (TTP->wasDeclaredWithTypename()) |
| 2474 | PlaceholderStr = "typename"; |
| 2475 | else |
| 2476 | PlaceholderStr = "class"; |
| 2477 | |
| 2478 | if (TTP->getIdentifier()) { |
| 2479 | PlaceholderStr += ' '; |
| 2480 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2481 | } |
| 2482 | |
| 2483 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2484 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2485 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2486 | if (NTTP->getIdentifier()) |
| 2487 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2488 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2489 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2490 | } else { |
| 2491 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2492 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2493 | |
| 2494 | // Since putting the template argument list into the placeholder would |
| 2495 | // be very, very long, we just use an abbreviation. |
| 2496 | PlaceholderStr = "template<...> class"; |
| 2497 | if (TTP->getIdentifier()) { |
| 2498 | PlaceholderStr += ' '; |
| 2499 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2500 | } |
| 2501 | |
| 2502 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2503 | } |
| 2504 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2505 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2506 | // When we see an optional default argument, put that argument and |
| 2507 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2508 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2509 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2510 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2511 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2512 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2513 | P - Params->begin(), true); |
| 2514 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2515 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2518 | InDefaultArg = false; |
| 2519 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2520 | if (FirstParameter) |
| 2521 | FirstParameter = false; |
| 2522 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2523 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2526 | Result.AddPlaceholderChunk( |
| 2527 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2531 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2532 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2533 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2534 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2535 | NestedNameSpecifier *Qualifier, |
| 2536 | bool QualifierIsInformative, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2537 | ASTContext &Context, |
| 2538 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2539 | if (!Qualifier) |
| 2540 | return; |
| 2541 | |
| 2542 | std::string PrintedNNS; |
| 2543 | { |
| 2544 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2545 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2546 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2547 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2548 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2549 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2550 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2553 | static void |
| 2554 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2555 | const FunctionDecl *Function) { |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2556 | const FunctionProtoType *Proto |
| 2557 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2558 | if (!Proto || !Proto->getTypeQuals()) |
| 2559 | return; |
| 2560 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2561 | // FIXME: Add ref-qualifier! |
| 2562 | |
| 2563 | // Handle single qualifiers without copying |
| 2564 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2565 | Result.AddInformativeChunk(" const"); |
| 2566 | return; |
| 2567 | } |
| 2568 | |
| 2569 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2570 | Result.AddInformativeChunk(" volatile"); |
| 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2575 | Result.AddInformativeChunk(" restrict"); |
| 2576 | return; |
| 2577 | } |
| 2578 | |
| 2579 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2580 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2581 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2582 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2583 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2584 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2585 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2586 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2587 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2590 | /// \brief Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2591 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2592 | const NamedDecl *ND, |
| 2593 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2594 | DeclarationName Name = ND->getDeclName(); |
| 2595 | if (!Name) |
| 2596 | return; |
| 2597 | |
| 2598 | switch (Name.getNameKind()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2599 | case DeclarationName::CXXOperatorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2600 | const char *OperatorName = nullptr; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2601 | switch (Name.getCXXOverloadedOperator()) { |
| 2602 | case OO_None: |
| 2603 | case OO_Conditional: |
| 2604 | case NUM_OVERLOADED_OPERATORS: |
| 2605 | OperatorName = "operator"; |
| 2606 | break; |
| 2607 | |
| 2608 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2609 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2610 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2611 | #include "clang/Basic/OperatorKinds.def" |
| 2612 | |
| 2613 | case OO_New: OperatorName = "operator new"; break; |
| 2614 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2615 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2616 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2617 | case OO_Call: OperatorName = "operator()"; break; |
| 2618 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2619 | } |
| 2620 | Result.AddTypedTextChunk(OperatorName); |
| 2621 | break; |
| 2622 | } |
| 2623 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2624 | case DeclarationName::Identifier: |
| 2625 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2626 | case DeclarationName::CXXDestructorName: |
| 2627 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2628 | Result.AddTypedTextChunk( |
| 2629 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2630 | break; |
| 2631 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2632 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2633 | case DeclarationName::CXXUsingDirective: |
| 2634 | case DeclarationName::ObjCZeroArgSelector: |
| 2635 | case DeclarationName::ObjCOneArgSelector: |
| 2636 | case DeclarationName::ObjCMultiArgSelector: |
| 2637 | break; |
| 2638 | |
| 2639 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2640 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2641 | QualType Ty = Name.getCXXNameType(); |
| 2642 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2643 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2644 | else if (const InjectedClassNameType *InjectedTy |
| 2645 | = Ty->getAs<InjectedClassNameType>()) |
| 2646 | Record = InjectedTy->getDecl(); |
| 2647 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2648 | Result.AddTypedTextChunk( |
| 2649 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2650 | break; |
| 2651 | } |
| 2652 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2653 | Result.AddTypedTextChunk( |
| 2654 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2655 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2656 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2657 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2658 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2659 | } |
| 2660 | break; |
| 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2665 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2666 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2667 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2668 | CodeCompletionTUInfo &CCTUInfo, |
| 2669 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2670 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 2671 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2674 | /// \brief If possible, create a new code completion string for the given |
| 2675 | /// result. |
| 2676 | /// |
| 2677 | /// \returns Either a new, heap-allocated code completion string describing |
| 2678 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2679 | /// result is all that is needed. |
| 2680 | CodeCompletionString * |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2681 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2682 | Preprocessor &PP, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2683 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2684 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2685 | CodeCompletionTUInfo &CCTUInfo, |
| 2686 | bool IncludeBriefComments) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2687 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2688 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2689 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2690 | if (Kind == RK_Pattern) { |
| 2691 | Pattern->Priority = Priority; |
| 2692 | Pattern->Availability = Availability; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2693 | |
| 2694 | if (Declaration) { |
| 2695 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2696 | Pattern->ParentName = Result.getParentName(); |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2697 | // Provide code completion comment for self.GetterName where |
| 2698 | // GetterName is the getter method for a property with name |
| 2699 | // different from the property name (declared via a property |
| 2700 | // getter attribute. |
| 2701 | const NamedDecl *ND = Declaration; |
| 2702 | if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND)) |
| 2703 | if (M->isPropertyAccessor()) |
| 2704 | if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl()) |
| 2705 | if (PDecl->getGetterName() == M->getSelector() && |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2706 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 2707 | if (const RawComment *RC = |
| 2708 | Ctx.getRawCommentForAnyRedecl(M)) { |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2709 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2710 | Pattern->BriefComment = Result.getBriefComment(); |
| 2711 | } |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2712 | else if (const RawComment *RC = |
| 2713 | Ctx.getRawCommentForAnyRedecl(PDecl)) { |
| 2714 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2715 | Pattern->BriefComment = Result.getBriefComment(); |
| 2716 | } |
| 2717 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2720 | return Pattern; |
| 2721 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2723 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2724 | Result.AddTypedTextChunk(Keyword); |
| 2725 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2726 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2728 | if (Kind == RK_Macro) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2729 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2730 | Result.AddTypedTextChunk( |
| 2731 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2732 | |
Ben Langmuir | c28ce3a | 2014-09-30 20:00:18 +0000 | [diff] [blame] | 2733 | if (!MI || !MI->isFunctionLike()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2734 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2735 | |
| 2736 | // Format a function-like macro with placeholders for the arguments. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2737 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2738 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2739 | |
| 2740 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2741 | if (MI->isC99Varargs()) { |
| 2742 | --AEnd; |
| 2743 | |
| 2744 | if (A == AEnd) { |
| 2745 | Result.AddPlaceholderChunk("..."); |
| 2746 | } |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2747 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2749 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2750 | if (A != MI->arg_begin()) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2751 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2752 | |
| 2753 | if (MI->isVariadic() && (A+1) == AEnd) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2754 | SmallString<32> Arg = (*A)->getName(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2755 | if (MI->isC99Varargs()) |
| 2756 | Arg += ", ..."; |
| 2757 | else |
| 2758 | Arg += "..."; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2759 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2760 | break; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2761 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2762 | |
| 2763 | // Non-variadic macros are simple. |
| 2764 | Result.AddPlaceholderChunk( |
| 2765 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2766 | } |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2767 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2768 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2771 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2772 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2773 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2774 | |
| 2775 | if (IncludeBriefComments) { |
| 2776 | // Add documentation comment, if it exists. |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 2777 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2778 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Fariborz Jahanian | 15a0b55 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 2779 | } |
| 2780 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 2781 | if (OMD->isPropertyAccessor()) |
| 2782 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 2783 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 2784 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2787 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2788 | Result.AddTypedTextChunk( |
| 2789 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2790 | Result.AddTextChunk("::"); |
| 2791 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2792 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2793 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2794 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2795 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2796 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2797 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2798 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2799 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2800 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2801 | Ctx, Policy); |
| 2802 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2803 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2804 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2805 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2806 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2807 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2810 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2811 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2812 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2813 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2814 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2816 | // Figure out which template parameters are deduced (or have default |
| 2817 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2818 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2819 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2820 | unsigned LastDeducibleArgument; |
| 2821 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2822 | --LastDeducibleArgument) { |
| 2823 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2824 | // C++0x: Figure out if the template argument has a default. If so, |
| 2825 | // the user doesn't need to type this argument. |
| 2826 | // FIXME: We need to abstract template parameters better! |
| 2827 | bool HasDefaultArg = false; |
| 2828 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2829 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2830 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2831 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2832 | else if (NonTypeTemplateParmDecl *NTTP |
| 2833 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2834 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2835 | else { |
| 2836 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2837 | HasDefaultArg |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2838 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | if (!HasDefaultArg) |
| 2842 | break; |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | if (LastDeducibleArgument) { |
| 2847 | // Some of the function template arguments cannot be deduced from a |
| 2848 | // function call, so we introduce an explicit template argument list |
| 2849 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2850 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2851 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2852 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2853 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2857 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2858 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2859 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2860 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2861 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2864 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2865 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2866 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2867 | Result.AddTypedTextChunk( |
| 2868 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2869 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2870 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2871 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2872 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2875 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2876 | Selector Sel = Method->getSelector(); |
| 2877 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2878 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2879 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2880 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2883 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2884 | SelName += ':'; |
| 2885 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2886 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2887 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2888 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2889 | |
| 2890 | // If there is only one parameter, and we're past it, add an empty |
| 2891 | // typed-text chunk since there is nothing to type. |
| 2892 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2893 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2894 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2895 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2896 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2897 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2898 | P != PEnd; (void)++P, ++Idx) { |
| 2899 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2900 | std::string Keyword; |
| 2901 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2902 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2903 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2904 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2905 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2906 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2907 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2908 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2909 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2910 | } |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2911 | |
| 2912 | // If we're before the starting parameter, skip the placeholder. |
| 2913 | if (Idx < StartParameter) |
| 2914 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2915 | |
| 2916 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2917 | QualType ParamType = (*P)->getType(); |
| 2918 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 2919 | if (!CCContext.getBaseType().isNull()) |
| 2920 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 2921 | |
| 2922 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 2923 | Arg = FormatFunctionParameter(Policy, *P, true, |
| 2924 | /*SuppressBlock=*/false, |
| 2925 | ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2926 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2927 | if (ObjCSubsts) |
| 2928 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, |
| 2929 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2930 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2931 | ParamType); |
| 2932 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2933 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2934 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2935 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2938 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2939 | Arg += ", ..."; |
| 2940 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2941 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2942 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2943 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2944 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2945 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2946 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2949 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2950 | if (Method->param_size() == 0) { |
| 2951 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2952 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2953 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2954 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2955 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2956 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2957 | } |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2958 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2959 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2962 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2965 | if (Qualifier) |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2966 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2967 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2969 | Result.AddTypedTextChunk( |
| 2970 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2971 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2974 | /// \brief Add function overload parameter chunks to the given code completion |
| 2975 | /// string. |
| 2976 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 2977 | const PrintingPolicy &Policy, |
| 2978 | const FunctionDecl *Function, |
| 2979 | const FunctionProtoType *Prototype, |
| 2980 | CodeCompletionBuilder &Result, |
| 2981 | unsigned CurrentArg, |
| 2982 | unsigned Start = 0, |
| 2983 | bool InOptional = false) { |
| 2984 | bool FirstParameter = true; |
| 2985 | unsigned NumParams = Function ? Function->getNumParams() |
| 2986 | : Prototype->getNumParams(); |
| 2987 | |
| 2988 | for (unsigned P = Start; P != NumParams; ++P) { |
| 2989 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 2990 | // When we see an optional default argument, put that argument and |
| 2991 | // the remaining default arguments into a new, optional string. |
| 2992 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2993 | Result.getCodeCompletionTUInfo()); |
| 2994 | if (!FirstParameter) |
| 2995 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2996 | // Optional sections are nested. |
| 2997 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 2998 | CurrentArg, P, /*InOptional=*/true); |
| 2999 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3000 | return; |
| 3001 | } |
| 3002 | |
| 3003 | if (FirstParameter) |
| 3004 | FirstParameter = false; |
| 3005 | else |
| 3006 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3007 | |
| 3008 | InOptional = false; |
| 3009 | |
| 3010 | // Format the placeholder string. |
| 3011 | std::string Placeholder; |
| 3012 | if (Function) |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3013 | Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P)); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3014 | else |
| 3015 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
| 3016 | |
| 3017 | if (P == CurrentArg) |
| 3018 | Result.AddCurrentParameterChunk( |
| 3019 | Result.getAllocator().CopyString(Placeholder)); |
| 3020 | else |
| 3021 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 3022 | } |
| 3023 | |
| 3024 | if (Prototype && Prototype->isVariadic()) { |
| 3025 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3026 | Result.getCodeCompletionTUInfo()); |
| 3027 | if (!FirstParameter) |
| 3028 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3029 | |
| 3030 | if (CurrentArg < NumParams) |
| 3031 | Opt.AddPlaceholderChunk("..."); |
| 3032 | else |
| 3033 | Opt.AddCurrentParameterChunk("..."); |
| 3034 | |
| 3035 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3036 | } |
| 3037 | } |
| 3038 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3039 | CodeCompletionString * |
| 3040 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3041 | unsigned CurrentArg, Sema &S, |
| 3042 | CodeCompletionAllocator &Allocator, |
| 3043 | CodeCompletionTUInfo &CCTUInfo, |
| 3044 | bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3045 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3046 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3047 | // FIXME: Set priority, availability appropriately. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3048 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3049 | FunctionDecl *FDecl = getFunction(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3050 | const FunctionProtoType *Proto |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3051 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 3052 | if (!FDecl && !Proto) { |
| 3053 | // Function without a prototype. Just give the return type and a |
| 3054 | // highlighted ellipsis. |
| 3055 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3056 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 3057 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3058 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3059 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 3060 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3061 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3062 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3063 | |
| 3064 | if (FDecl) { |
| 3065 | if (IncludeBriefComments && CurrentArg < FDecl->getNumParams()) |
| 3066 | if (auto RC = S.getASTContext().getRawCommentForAnyRedecl( |
| 3067 | FDecl->getParamDecl(CurrentArg))) |
| 3068 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3069 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3070 | Result.AddTextChunk( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3071 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 3072 | } else { |
| 3073 | Result.AddResultTypeChunk( |
| 3074 | Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3075 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3076 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3077 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3078 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3079 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 3080 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3081 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3083 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3086 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3087 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3088 | bool PreferredTypeIsPointer) { |
| 3089 | unsigned Priority = CCP_Macro; |
| 3090 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3091 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 3092 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 3093 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3094 | Priority = CCP_Constant; |
| 3095 | if (PreferredTypeIsPointer) |
| 3096 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3097 | } |
| 3098 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3099 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3100 | MacroName.equals("true") || MacroName.equals("false")) |
| 3101 | Priority = CCP_Constant; |
| 3102 | // Treat "bool" as a type. |
| 3103 | else if (MacroName.equals("bool")) |
| 3104 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 3105 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3106 | |
| 3107 | return Priority; |
| 3108 | } |
| 3109 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3110 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3111 | if (!D) |
| 3112 | return CXCursor_UnexposedDecl; |
| 3113 | |
| 3114 | switch (D->getKind()) { |
| 3115 | case Decl::Enum: return CXCursor_EnumDecl; |
| 3116 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 3117 | case Decl::Field: return CXCursor_FieldDecl; |
| 3118 | case Decl::Function: |
| 3119 | return CXCursor_FunctionDecl; |
| 3120 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 3121 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3122 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3123 | |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3124 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3125 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 3126 | case Decl::ObjCMethod: |
| 3127 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3128 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 3129 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 3130 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 3131 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 3132 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 3133 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3134 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3135 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 3136 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3137 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 3138 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3139 | case Decl::Var: return CXCursor_VarDecl; |
| 3140 | case Decl::Namespace: return CXCursor_Namespace; |
| 3141 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 3142 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 3143 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 3144 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 3145 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 3146 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3147 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3148 | case Decl::ClassTemplatePartialSpecialization: |
| 3149 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3150 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 3151 | case Decl::StaticAssert: return CXCursor_StaticAssert; |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 3152 | case Decl::Friend: return CXCursor_FriendDecl; |
Douglas Gregor | 3e653b3 | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 3153 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3154 | |
| 3155 | case Decl::Using: |
| 3156 | case Decl::UnresolvedUsingValue: |
| 3157 | case Decl::UnresolvedUsingTypename: |
| 3158 | return CXCursor_UsingDeclaration; |
| 3159 | |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3160 | case Decl::ObjCPropertyImpl: |
| 3161 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3162 | case ObjCPropertyImplDecl::Dynamic: |
| 3163 | return CXCursor_ObjCDynamicDecl; |
| 3164 | |
| 3165 | case ObjCPropertyImplDecl::Synthesize: |
| 3166 | return CXCursor_ObjCSynthesizeDecl; |
| 3167 | } |
Argyrios Kyrtzidis | 50e5b1d | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3168 | |
| 3169 | case Decl::Import: |
| 3170 | return CXCursor_ModuleImportDecl; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3171 | |
| 3172 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; |
| 3173 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3174 | default: |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3175 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3176 | switch (TD->getTagKind()) { |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3177 | case TTK_Interface: // fall through |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3178 | case TTK_Struct: return CXCursor_StructDecl; |
| 3179 | case TTK_Class: return CXCursor_ClassDecl; |
| 3180 | case TTK_Union: return CXCursor_UnionDecl; |
| 3181 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | return CXCursor_UnexposedDecl; |
| 3187 | } |
| 3188 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3189 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3190 | bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3191 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3192 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3193 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3194 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3196 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 3197 | MEnd = PP.macro_end(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3198 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3199 | auto MD = PP.getMacroDefinition(M->first); |
| 3200 | if (IncludeUndefined || MD) { |
| 3201 | if (MacroInfo *MI = MD.getMacroInfo()) |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3202 | if (MI->isUsedForHeaderGuard()) |
| 3203 | continue; |
| 3204 | |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3205 | Results.AddResult(Result(M->first, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3206 | getMacroUsagePriority(M->first->getName(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3207 | PP.getLangOpts(), |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3208 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3209 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3210 | } |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3212 | Results.ExitScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3216 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 3217 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3218 | typedef CodeCompletionResult Result; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3219 | |
| 3220 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3222 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3223 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3224 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3225 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3226 | Results.ExitScope(); |
| 3227 | } |
| 3228 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3229 | static void HandleCodeCompleteResults(Sema *S, |
| 3230 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3231 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3232 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3233 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3234 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3235 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3238 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 3239 | Sema::ParserCompletionContext PCC) { |
| 3240 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3241 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3242 | return CodeCompletionContext::CCC_TopLevel; |
| 3243 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3244 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3245 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3246 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3247 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3248 | return CodeCompletionContext::CCC_ObjCInterface; |
| 3249 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3250 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3251 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3252 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3253 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3254 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 3255 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3256 | case Sema::PCC_Template: |
| 3257 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3258 | if (S.CurContext->isFileContext()) |
| 3259 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3260 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3261 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3262 | return CodeCompletionContext::CCC_Other; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3263 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3264 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3265 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3266 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3267 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3268 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3269 | S.getLangOpts().ObjC1) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3270 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3271 | else |
| 3272 | return CodeCompletionContext::CCC_Expression; |
| 3273 | |
| 3274 | case Sema::PCC_Expression: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3275 | case Sema::PCC_Condition: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3276 | return CodeCompletionContext::CCC_Expression; |
| 3277 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3278 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3279 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3280 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3281 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3282 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3283 | |
| 3284 | case Sema::PCC_ParenthesizedExpression: |
| 3285 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3286 | |
| 3287 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3288 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3289 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3290 | |
| 3291 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3294 | /// \brief If we're in a C++ virtual member function, add completion results |
| 3295 | /// that invoke the functions we override, since it's common to invoke the |
| 3296 | /// overridden function as well as adding new functionality. |
| 3297 | /// |
| 3298 | /// \param S The semantic analysis object for which we are generating results. |
| 3299 | /// |
| 3300 | /// \param InContext This context in which the nested-name-specifier preceding |
| 3301 | /// the code-completion point |
| 3302 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3303 | ResultBuilder &Results) { |
| 3304 | // Look through blocks. |
| 3305 | DeclContext *CurContext = S.CurContext; |
| 3306 | while (isa<BlockDecl>(CurContext)) |
| 3307 | CurContext = CurContext->getParent(); |
| 3308 | |
| 3309 | |
| 3310 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3311 | if (!Method || !Method->isVirtual()) |
| 3312 | return; |
| 3313 | |
| 3314 | // We need to have names for all of the parameters, if we're going to |
| 3315 | // generate a forwarding call. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3316 | for (auto P : Method->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3317 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3318 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3320 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3321 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 3322 | MEnd = Method->end_overridden_methods(); |
| 3323 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3324 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3325 | Results.getCodeCompletionTUInfo()); |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 3326 | const CXXMethodDecl *Overridden = *M; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3327 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3328 | continue; |
| 3329 | |
| 3330 | // If we need a nested-name-specifier, add one now. |
| 3331 | if (!InContext) { |
| 3332 | NestedNameSpecifier *NNS |
| 3333 | = getRequiredQualification(S.Context, CurContext, |
| 3334 | Overridden->getDeclContext()); |
| 3335 | if (NNS) { |
| 3336 | std::string Str; |
| 3337 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3338 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3339 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3340 | } |
| 3341 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3342 | continue; |
| 3343 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3344 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3345 | Overridden->getNameAsString())); |
| 3346 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3347 | bool FirstParam = true; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3348 | for (auto P : Method->parameters()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3349 | if (FirstParam) |
| 3350 | FirstParam = false; |
| 3351 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3352 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3353 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3354 | Builder.AddPlaceholderChunk( |
| 3355 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3356 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3357 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3358 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3359 | CCP_SuperCompletion, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3360 | CXCursor_CXXMethod, |
| 3361 | CXAvailability_Available, |
| 3362 | Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3363 | Results.Ignore(Overridden); |
| 3364 | } |
| 3365 | } |
| 3366 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3367 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
| 3368 | ModuleIdPath Path) { |
| 3369 | typedef CodeCompletionResult Result; |
| 3370 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3371 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3372 | CodeCompletionContext::CCC_Other); |
| 3373 | Results.EnterNewScope(); |
| 3374 | |
| 3375 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3376 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3377 | typedef CodeCompletionResult Result; |
| 3378 | if (Path.empty()) { |
| 3379 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3380 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3381 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3382 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3383 | Builder.AddTypedTextChunk( |
| 3384 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3385 | Results.AddResult(Result(Builder.TakeString(), |
| 3386 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3387 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3388 | Modules[I]->isAvailable() |
| 3389 | ? CXAvailability_Available |
| 3390 | : CXAvailability_NotAvailable)); |
| 3391 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3392 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3393 | // Load the named module. |
| 3394 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3395 | Module::AllVisible, |
| 3396 | /*IsInclusionDirective=*/false); |
| 3397 | // Enumerate submodules. |
| 3398 | if (Mod) { |
| 3399 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 3400 | SubEnd = Mod->submodule_end(); |
| 3401 | Sub != SubEnd; ++Sub) { |
| 3402 | |
| 3403 | Builder.AddTypedTextChunk( |
| 3404 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3405 | Results.AddResult(Result(Builder.TakeString(), |
| 3406 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3407 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3408 | (*Sub)->isAvailable() |
| 3409 | ? CXAvailability_Available |
| 3410 | : CXAvailability_NotAvailable)); |
| 3411 | } |
| 3412 | } |
| 3413 | } |
| 3414 | Results.ExitScope(); |
| 3415 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3416 | Results.data(),Results.size()); |
| 3417 | } |
| 3418 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3419 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3420 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3421 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3422 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3423 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3424 | Results.EnterNewScope(); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3426 | // Determine how to filter results, e.g., so that the names of |
| 3427 | // values (functions, enumerators, function templates, etc.) are |
| 3428 | // only allowed where we can have an expression. |
| 3429 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3430 | case PCC_Namespace: |
| 3431 | case PCC_Class: |
| 3432 | case PCC_ObjCInterface: |
| 3433 | case PCC_ObjCImplementation: |
| 3434 | case PCC_ObjCInstanceVariableList: |
| 3435 | case PCC_Template: |
| 3436 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3437 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3438 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3439 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3440 | break; |
| 3441 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3442 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3443 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3444 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3445 | case PCC_ForInit: |
| 3446 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3447 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3448 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3449 | else |
| 3450 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3451 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3452 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3453 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3454 | break; |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3456 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3457 | // Unfiltered |
| 3458 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3461 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3462 | // the member function to filter/prioritize the results list. |
| 3463 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3464 | if (CurMethod->isInstance()) |
| 3465 | Results.setObjectTypeQualifiers( |
| 3466 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 3467 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3468 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3469 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3470 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3472 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3473 | Results.ExitScope(); |
| 3474 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3475 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3476 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3477 | case PCC_Expression: |
| 3478 | case PCC_Statement: |
| 3479 | case PCC_RecoveryInFunction: |
| 3480 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3481 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3482 | break; |
| 3483 | |
| 3484 | case PCC_Namespace: |
| 3485 | case PCC_Class: |
| 3486 | case PCC_ObjCInterface: |
| 3487 | case PCC_ObjCImplementation: |
| 3488 | case PCC_ObjCInstanceVariableList: |
| 3489 | case PCC_Template: |
| 3490 | case PCC_MemberTemplate: |
| 3491 | case PCC_ForInit: |
| 3492 | case PCC_Condition: |
| 3493 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3494 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3495 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3498 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3499 | AddMacroResults(PP, Results, false); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3501 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3502 | Results.data(),Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3505 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 3506 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3507 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3508 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3509 | bool IsSuper, |
| 3510 | ResultBuilder &Results); |
| 3511 | |
| 3512 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3513 | bool AllowNonIdentifiers, |
| 3514 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3515 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3516 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3517 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3518 | AllowNestedNameSpecifiers |
| 3519 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3520 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3521 | Results.EnterNewScope(); |
| 3522 | |
| 3523 | // Type qualifiers can come after names. |
| 3524 | Results.AddResult(Result("const")); |
| 3525 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3526 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3527 | Results.AddResult(Result("restrict")); |
| 3528 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3529 | if (getLangOpts().CPlusPlus) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 3530 | if (getLangOpts().CPlusPlus11 && |
| 3531 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3532 | DS.getTypeSpecType() == DeclSpec::TST_struct)) |
| 3533 | Results.AddResult("final"); |
| 3534 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3535 | if (AllowNonIdentifiers) { |
| 3536 | Results.AddResult(Result("operator")); |
| 3537 | } |
| 3538 | |
| 3539 | // Add nested-name-specifiers. |
| 3540 | if (AllowNestedNameSpecifiers) { |
| 3541 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3542 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3543 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3544 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 3545 | CodeCompleter->includeGlobals()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3546 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | Results.ExitScope(); |
| 3550 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3551 | // If we're in a context where we might have an expression (rather than a |
| 3552 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3553 | // be a receiver of a class message, this may be a class message send with |
| 3554 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3555 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3556 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3557 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3558 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3559 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3560 | !DS.isTypeAltiVecVector() && |
| 3561 | S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3562 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3563 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 3564 | Scope::FunctionPrototypeScope | |
| 3565 | Scope::AtCatchScope)) == 0) { |
| 3566 | ParsedType T = DS.getRepAsType(); |
| 3567 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3568 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3571 | // Note that we intentionally suppress macro results here, since we do not |
| 3572 | // encourage using macros to produce the names of entities. |
| 3573 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3574 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3575 | Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3576 | Results.data(), Results.size()); |
| 3577 | } |
| 3578 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3579 | struct Sema::CodeCompleteExpressionData { |
| 3580 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 3581 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3582 | ObjCCollection(false) { } |
| 3583 | |
| 3584 | QualType PreferredType; |
| 3585 | bool IntegralConstantExpression; |
| 3586 | bool ObjCCollection; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3587 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3588 | }; |
| 3589 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3590 | /// \brief Perform code-completion in an expression context when we know what |
| 3591 | /// type we're looking for. |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3592 | void Sema::CodeCompleteExpression(Scope *S, |
| 3593 | const CodeCompleteExpressionData &Data) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3594 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3595 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3596 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3597 | if (Data.ObjCCollection) |
| 3598 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3599 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3600 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3601 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3602 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3603 | else |
| 3604 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3605 | |
| 3606 | if (!Data.PreferredType.isNull()) |
| 3607 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 3608 | |
| 3609 | // Ignore any declarations that we were told that we don't care about. |
| 3610 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3611 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3612 | |
| 3613 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3614 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3615 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3616 | |
| 3617 | Results.EnterNewScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3618 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3619 | Results.ExitScope(); |
| 3620 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3621 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3622 | if (!Data.PreferredType.isNull()) |
| 3623 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3624 | || Data.PreferredType->isMemberPointerType() |
| 3625 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3626 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3627 | if (S->getFnParent() && |
| 3628 | !Data.ObjCCollection && |
| 3629 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3630 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3632 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3633 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3634 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3635 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3636 | Data.PreferredType), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3637 | Results.data(),Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3640 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3641 | if (E.isInvalid()) |
| 3642 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3643 | else if (getLangOpts().ObjC1) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3644 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3645 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3647 | /// \brief The set of properties that have already been added, referenced by |
| 3648 | /// property name. |
| 3649 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3650 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3651 | /// \brief Retrieve the container definition, if any? |
| 3652 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3653 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3654 | if (Interface->hasDefinition()) |
| 3655 | return Interface->getDefinition(); |
| 3656 | |
| 3657 | return Interface; |
| 3658 | } |
| 3659 | |
| 3660 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3661 | if (Protocol->hasDefinition()) |
| 3662 | return Protocol->getDefinition(); |
| 3663 | |
| 3664 | return Protocol; |
| 3665 | } |
| 3666 | return Container; |
| 3667 | } |
| 3668 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3669 | /// \brief Adds a block invocation code completion result for the given block |
| 3670 | /// declaration \p BD. |
| 3671 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
| 3672 | CodeCompletionBuilder &Builder, |
| 3673 | const NamedDecl *BD, |
| 3674 | const FunctionTypeLoc &BlockLoc, |
| 3675 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
| 3676 | Builder.AddResultTypeChunk( |
| 3677 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
| 3678 | Policy, Builder.getAllocator())); |
| 3679 | |
| 3680 | AddTypedNameChunk(Context, Policy, BD, Builder); |
| 3681 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3682 | |
| 3683 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { |
| 3684 | Builder.AddPlaceholderChunk("..."); |
| 3685 | } else { |
| 3686 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { |
| 3687 | if (I) |
| 3688 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 3689 | |
| 3690 | // Format the placeholder string. |
| 3691 | std::string PlaceholderStr = |
| 3692 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
| 3693 | |
| 3694 | if (I == N - 1 && BlockProtoLoc && |
| 3695 | BlockProtoLoc.getTypePtr()->isVariadic()) |
| 3696 | PlaceholderStr += ", ..."; |
| 3697 | |
| 3698 | // Add the placeholder string. |
| 3699 | Builder.AddPlaceholderChunk( |
| 3700 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3705 | } |
| 3706 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3707 | static void AddObjCProperties( |
| 3708 | const CodeCompletionContext &CCContext, ObjCContainerDecl *Container, |
| 3709 | bool AllowCategories, bool AllowNullaryMethods, DeclContext *CurContext, |
| 3710 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
| 3711 | bool IsBaseExprStatement = false, bool IsClassProperty = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3712 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3714 | // Retrieve the definition. |
| 3715 | Container = getContainerDef(Container); |
| 3716 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3717 | // Add properties in this container. |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3718 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3719 | if (!AddedProperties.insert(P->getIdentifier()).second) |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3720 | return; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3721 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3722 | // FIXME: Provide block invocation completion for non-statement |
| 3723 | // expressions. |
| 3724 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
| 3725 | !IsBaseExprStatement) { |
| 3726 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3727 | CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3728 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | // Block setter and invocation completion is provided only when we are able |
| 3732 | // to find the FunctionProtoTypeLoc with parameter names for the block. |
| 3733 | FunctionTypeLoc BlockLoc; |
| 3734 | FunctionProtoTypeLoc BlockProtoLoc; |
| 3735 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
| 3736 | BlockProtoLoc); |
| 3737 | if (!BlockLoc) { |
| 3738 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3739 | CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3740 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | // The default completion result for block properties should be the block |
| 3744 | // invocation completion when the base expression is a statement. |
| 3745 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3746 | Results.getCodeCompletionTUInfo()); |
| 3747 | AddObjCBlockCall(Container->getASTContext(), |
| 3748 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
| 3749 | BlockLoc, BlockProtoLoc); |
| 3750 | Results.MaybeAddResult( |
| 3751 | Result(Builder.TakeString(), P, Results.getBasePriority(P)), |
| 3752 | CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3753 | |
| 3754 | // Provide additional block setter completion iff the base expression is a |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3755 | // statement and the block property is mutable. |
| 3756 | if (!P->isReadOnly()) { |
| 3757 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3758 | Results.getCodeCompletionTUInfo()); |
| 3759 | AddResultTypeChunk(Container->getASTContext(), |
| 3760 | getCompletionPrintingPolicy(Results.getSema()), P, |
| 3761 | CCContext.getBaseType(), Builder); |
| 3762 | Builder.AddTypedTextChunk( |
| 3763 | Results.getAllocator().CopyString(P->getName())); |
| 3764 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3765 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3766 | std::string PlaceholderStr = formatBlockPlaceholder( |
| 3767 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
| 3768 | BlockProtoLoc, /*SuppressBlockName=*/true); |
| 3769 | // Add the placeholder string. |
| 3770 | Builder.AddPlaceholderChunk( |
| 3771 | Builder.getAllocator().CopyString(PlaceholderStr)); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3772 | |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3773 | // When completing blocks properties that return void the default |
| 3774 | // property completion result should show up before the setter, |
| 3775 | // otherwise the setter completion should show up before the default |
| 3776 | // property completion, as we normally want to use the result of the |
| 3777 | // call. |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3778 | Results.MaybeAddResult( |
| 3779 | Result(Builder.TakeString(), P, |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3780 | Results.getBasePriority(P) + |
| 3781 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
| 3782 | ? CCD_BlockPropertySetter |
| 3783 | : -CCD_BlockPropertySetter)), |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3784 | CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3785 | } |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3786 | }; |
| 3787 | |
| 3788 | if (IsClassProperty) { |
| 3789 | for (const auto *P : Container->class_properties()) |
| 3790 | AddProperty(P); |
| 3791 | } else { |
| 3792 | for (const auto *P : Container->instance_properties()) |
| 3793 | AddProperty(P); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3794 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3795 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3796 | // Add nullary methods or implicit class properties |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3797 | if (AllowNullaryMethods) { |
| 3798 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3799 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3800 | // Adds a method result |
| 3801 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
| 3802 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
| 3803 | if (!Name) |
| 3804 | return; |
| 3805 | if (!AddedProperties.insert(Name).second) |
| 3806 | return; |
| 3807 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3808 | Results.getCodeCompletionTUInfo()); |
| 3809 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
| 3810 | Builder.AddTypedTextChunk( |
| 3811 | Results.getAllocator().CopyString(Name->getName())); |
| 3812 | Results.MaybeAddResult( |
| 3813 | Result(Builder.TakeString(), M, |
| 3814 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
| 3815 | CurContext); |
| 3816 | }; |
| 3817 | |
| 3818 | if (IsClassProperty) { |
| 3819 | for (const auto *M : Container->methods()) { |
| 3820 | // Gather the class method that can be used as implicit property |
| 3821 | // getters. Methods with arguments or methods that return void aren't |
| 3822 | // added to the results as they can't be used as a getter. |
| 3823 | if (!M->getSelector().isUnarySelector() || |
| 3824 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) |
| 3825 | continue; |
| 3826 | AddMethod(M); |
| 3827 | } |
| 3828 | } else { |
| 3829 | for (auto *M : Container->methods()) { |
| 3830 | if (M->getSelector().isUnarySelector()) |
| 3831 | AddMethod(M); |
| 3832 | } |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3833 | } |
| 3834 | } |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3835 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3836 | // Add properties in referenced protocols. |
| 3837 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3838 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3839 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3840 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3841 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3842 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3843 | if (AllowCategories) { |
| 3844 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3845 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3846 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3847 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3848 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3849 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3851 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3852 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3853 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3854 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3855 | IsBaseExprStatement, IsClassProperty); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3857 | // Look in the superclass. |
| 3858 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3859 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3860 | AllowNullaryMethods, CurContext, AddedProperties, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3861 | Results, IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3862 | } else if (const ObjCCategoryDecl *Category |
| 3863 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3864 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3865 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3866 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3867 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3868 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame^] | 3872 | static void AddRecordMembersCompletionResults(Sema &SemaRef, |
| 3873 | ResultBuilder &Results, Scope *S, |
| 3874 | QualType BaseType, |
| 3875 | RecordDecl *RD) { |
| 3876 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3877 | // for the base object type. |
| 3878 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3879 | |
| 3880 | // Access to a C/C++ class, struct, or union. |
| 3881 | Results.allowNestedNameSpecifiers(); |
| 3882 | CodeCompletionDeclConsumer Consumer(Results, SemaRef.CurContext); |
| 3883 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
| 3884 | SemaRef.CodeCompleter->includeGlobals()); |
| 3885 | |
| 3886 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 3887 | if (!Results.empty()) { |
| 3888 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3889 | // However, we only want to suggest the template keyword if something |
| 3890 | // is dependent. |
| 3891 | bool IsDependent = BaseType->isDependentType(); |
| 3892 | if (!IsDependent) { |
| 3893 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 3894 | if (DeclContext *Ctx = DepScope->getEntity()) { |
| 3895 | IsDependent = Ctx->isDependentContext(); |
| 3896 | break; |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | if (IsDependent) |
| 3901 | Results.AddResult(CodeCompletionResult("template")); |
| 3902 | } |
| 3903 | } |
| 3904 | } |
| 3905 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3906 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3907 | SourceLocation OpLoc, bool IsArrow, |
| 3908 | bool IsBaseExprStatement) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3909 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3910 | return; |
| 3911 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3912 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 3913 | if (ConvertedBase.isInvalid()) |
| 3914 | return; |
| 3915 | Base = ConvertedBase.get(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3917 | QualType BaseType = Base->getType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3918 | |
| 3919 | if (IsArrow) { |
| 3920 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3921 | BaseType = Ptr->getPointeeType(); |
| 3922 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3923 | /*Do nothing*/ ; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3924 | else |
| 3925 | return; |
| 3926 | } |
| 3927 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3928 | enum CodeCompletionContext::Kind contextKind; |
| 3929 | |
| 3930 | if (IsArrow) { |
| 3931 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| 3932 | } |
| 3933 | else { |
| 3934 | if (BaseType->isObjCObjectPointerType() || |
| 3935 | BaseType->isObjCObjectOrInterfaceType()) { |
| 3936 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| 3937 | } |
| 3938 | else { |
| 3939 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 3940 | } |
| 3941 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3942 | |
| 3943 | CodeCompletionContext CCContext(contextKind, BaseType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3944 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3945 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3946 | CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3947 | &ResultBuilder::IsMember); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3948 | Results.EnterNewScope(); |
| 3949 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame^] | 3950 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, |
| 3951 | Record->getDecl()); |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 3952 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3953 | // Objective-C property reference. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3954 | AddedPropertiesSet AddedProperties; |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 3955 | |
| 3956 | if (const ObjCObjectPointerType *ObjCPtr = |
| 3957 | BaseType->getAsObjCInterfacePointerType()) { |
| 3958 | // Add property results based on our interface. |
| 3959 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
| 3960 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
| 3961 | /*AllowNullaryMethods=*/true, CurContext, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3962 | AddedProperties, Results, IsBaseExprStatement); |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3965 | // Add properties from the protocols in a qualified interface. |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 3966 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3967 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3968 | CurContext, AddedProperties, Results, |
| 3969 | IsBaseExprStatement); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3970 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3971 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3972 | // Objective-C instance variable access. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3973 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3974 | if (const ObjCObjectPointerType *ObjCPtr |
| 3975 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3976 | Class = ObjCPtr->getInterfaceDecl(); |
| 3977 | else |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3978 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3979 | |
| 3980 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3981 | if (Class) { |
| 3982 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3983 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3984 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3985 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3986 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3987 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3988 | |
| 3989 | // FIXME: How do we cope with isa? |
| 3990 | |
| 3991 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3992 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3993 | // Hand off the results found for code completion. |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3994 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3995 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3996 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3999 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, |
| 4000 | IdentifierInfo &ClassName, |
| 4001 | SourceLocation ClassNameLoc, |
| 4002 | bool IsBaseExprStatement) { |
| 4003 | IdentifierInfo *ClassNamePtr = &ClassName; |
| 4004 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); |
| 4005 | if (!IFace) |
| 4006 | return; |
| 4007 | CodeCompletionContext CCContext( |
| 4008 | CodeCompletionContext::CCC_ObjCPropertyAccess); |
| 4009 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4010 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| 4011 | &ResultBuilder::IsMember); |
| 4012 | Results.EnterNewScope(); |
| 4013 | AddedPropertiesSet AddedProperties; |
| 4014 | AddObjCProperties(CCContext, IFace, true, |
| 4015 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, |
| 4016 | Results, IsBaseExprStatement, |
| 4017 | /*IsClassProperty=*/true); |
| 4018 | Results.ExitScope(); |
| 4019 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4020 | Results.data(), Results.size()); |
| 4021 | } |
| 4022 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4023 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 4024 | if (!CodeCompleter) |
| 4025 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4026 | |
| 4027 | ResultBuilder::LookupFilter Filter = nullptr; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4028 | enum CodeCompletionContext::Kind ContextKind |
| 4029 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4030 | switch ((DeclSpec::TST)TagSpec) { |
| 4031 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4032 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4033 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4034 | break; |
| 4035 | |
| 4036 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4037 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4038 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4039 | break; |
| 4040 | |
| 4041 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4042 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4043 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4044 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4045 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4046 | break; |
| 4047 | |
| 4048 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4049 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4050 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4051 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4052 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4053 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4054 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4055 | |
| 4056 | // First pass: look for tags. |
| 4057 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4058 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 4059 | CodeCompleter->includeGlobals()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4060 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4061 | if (CodeCompleter->includeGlobals()) { |
| 4062 | // Second pass: look for nested name specifiers. |
| 4063 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 4064 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 4065 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4067 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4068 | Results.data(),Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4071 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, |
| 4072 | const LangOptions &LangOpts) { |
| 4073 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 4074 | Results.AddResult("const"); |
| 4075 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 4076 | Results.AddResult("volatile"); |
| 4077 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 4078 | Results.AddResult("restrict"); |
| 4079 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 4080 | Results.AddResult("_Atomic"); |
| 4081 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 4082 | Results.AddResult("__unaligned"); |
| 4083 | } |
| 4084 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4085 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4086 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4087 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4088 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4089 | Results.EnterNewScope(); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4090 | AddTypeQualifierResults(DS, Results, LangOpts); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4091 | Results.ExitScope(); |
| 4092 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4093 | Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4094 | Results.data(), Results.size()); |
| 4095 | } |
| 4096 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4097 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, |
| 4098 | const VirtSpecifiers *VS) { |
| 4099 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4100 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4101 | CodeCompletionContext::CCC_TypeQualifiers); |
| 4102 | Results.EnterNewScope(); |
| 4103 | AddTypeQualifierResults(DS, Results, LangOpts); |
| 4104 | if (LangOpts.CPlusPlus11) { |
| 4105 | Results.AddResult("noexcept"); |
| 4106 | if (D.getContext() == Declarator::MemberContext && !D.isCtorOrDtor() && |
| 4107 | !D.isStaticMember()) { |
| 4108 | if (!VS || !VS->isFinalSpecified()) |
| 4109 | Results.AddResult("final"); |
| 4110 | if (!VS || !VS->isOverrideSpecified()) |
| 4111 | Results.AddResult("override"); |
| 4112 | } |
| 4113 | } |
| 4114 | Results.ExitScope(); |
| 4115 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4116 | Results.data(), Results.size()); |
| 4117 | } |
| 4118 | |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 4119 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 4120 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 4121 | } |
| 4122 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4123 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4124 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4125 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4126 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4127 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4128 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 4129 | if (!type->isEnumeralType()) { |
| 4130 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4131 | Data.IntegralConstantExpression = true; |
| 4132 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4133 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4134 | } |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4135 | |
| 4136 | // Code-complete the cases of a switch statement over an enumeration type |
| 4137 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4138 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4139 | if (EnumDecl *Def = Enum->getDefinition()) |
| 4140 | Enum = Def; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4141 | |
| 4142 | // Determine which enumerators we have already seen in the switch statement. |
| 4143 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 4144 | // token, in case we are code-completing in the middle of the switch and not |
| 4145 | // at the end. However, we aren't able to do so at the moment. |
| 4146 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4147 | NestedNameSpecifier *Qualifier = nullptr; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4148 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 4149 | SC = SC->getNextSwitchCase()) { |
| 4150 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 4151 | if (!Case) |
| 4152 | continue; |
| 4153 | |
| 4154 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 4155 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 4156 | if (EnumConstantDecl *Enumerator |
| 4157 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 4158 | // We look into the AST of the case statement to determine which |
| 4159 | // enumerator was named. Alternatively, we could compute the value of |
| 4160 | // the integral constant expression, then compare it against the |
| 4161 | // values of each enumerator. However, value-based approach would not |
| 4162 | // work as well with C++ templates where enumerators declared within a |
| 4163 | // template are type- and value-dependent. |
| 4164 | EnumeratorsSeen.insert(Enumerator); |
| 4165 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4166 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 4167 | // 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] | 4168 | // |
| 4169 | // switch (TagD.getKind()) { |
| 4170 | // case TagDecl::TK_enum: |
| 4171 | // break; |
| 4172 | // case XXX |
| 4173 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4174 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4175 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 4176 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4177 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4181 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4182 | // If there are no prior enumerators in C++, check whether we have to |
| 4183 | // qualify the names of the enumerators that we suggest, because they |
| 4184 | // may not be visible in this scope. |
Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 4185 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4188 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4189 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4190 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4191 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4192 | Results.EnterNewScope(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4193 | for (auto *E : Enum->enumerators()) { |
| 4194 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4195 | continue; |
| 4196 | |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4197 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4198 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4199 | } |
| 4200 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 4201 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4202 | //We need to make sure we're setting the right context, |
| 4203 | //so only say we include macros if the code completer says we do |
| 4204 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; |
| 4205 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4206 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4207 | kind = CodeCompletionContext::CCC_OtherWithMacros; |
| 4208 | } |
| 4209 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4210 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4211 | kind, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4212 | Results.data(),Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 4215 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4216 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4217 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4218 | |
| 4219 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4220 | if (!Args[I]) |
| 4221 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4222 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4223 | return false; |
| 4224 | } |
| 4225 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4226 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 4227 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4228 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 4229 | SmallVectorImpl<ResultCandidate> &Results, |
| 4230 | OverloadCandidateSet &CandidateSet, |
| 4231 | SourceLocation Loc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4232 | if (!CandidateSet.empty()) { |
| 4233 | // Sort the overload candidate set by placing the best overloads first. |
| 4234 | std::stable_sort( |
| 4235 | CandidateSet.begin(), CandidateSet.end(), |
| 4236 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
| 4237 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc); |
| 4238 | }); |
| 4239 | |
| 4240 | // Add the remaining viable overload candidates as code-completion results. |
| 4241 | for (auto &Candidate : CandidateSet) |
| 4242 | if (Candidate.Viable) |
| 4243 | Results.push_back(ResultCandidate(Candidate.Function)); |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | /// \brief Get the type of the Nth parameter from a given set of overload |
| 4248 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4249 | static QualType getParamType(Sema &SemaRef, |
| 4250 | ArrayRef<ResultCandidate> Candidates, |
| 4251 | unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4252 | |
| 4253 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 4254 | // up to N, return the type of the Nth parameter if it is the same for all |
| 4255 | // overload candidates. |
| 4256 | QualType ParamType; |
| 4257 | for (auto &Candidate : Candidates) { |
| 4258 | if (auto FType = Candidate.getFunctionType()) |
| 4259 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 4260 | if (N < Proto->getNumParams()) { |
| 4261 | if (ParamType.isNull()) |
| 4262 | ParamType = Proto->getParamType(N); |
| 4263 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 4264 | ParamType.getNonReferenceType(), |
| 4265 | Proto->getParamType(N).getNonReferenceType())) |
| 4266 | // Otherwise return a default-constructed QualType. |
| 4267 | return QualType(); |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | return ParamType; |
| 4272 | } |
| 4273 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4274 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, |
| 4275 | MutableArrayRef<ResultCandidate> Candidates, |
| 4276 | unsigned CurrentArg, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4277 | bool CompleteExpressionWithCurrentArg = true) { |
| 4278 | QualType ParamType; |
| 4279 | if (CompleteExpressionWithCurrentArg) |
| 4280 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); |
| 4281 | |
| 4282 | if (ParamType.isNull()) |
| 4283 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); |
| 4284 | else |
| 4285 | SemaRef.CodeCompleteExpression(S, ParamType); |
| 4286 | |
| 4287 | if (!Candidates.empty()) |
| 4288 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, |
| 4289 | Candidates.data(), |
| 4290 | Candidates.size()); |
| 4291 | } |
| 4292 | |
| 4293 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4294 | if (!CodeCompleter) |
| 4295 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4296 | |
| 4297 | // When we're code-completing for a call, we fall back to ordinary |
| 4298 | // name code-completion whenever we can't produce specific |
| 4299 | // results. We may want to revisit this strategy in the future, |
| 4300 | // e.g., by merging the two kinds of results. |
| 4301 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4302 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4303 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4304 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4305 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4306 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4307 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4308 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4309 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4310 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4311 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4312 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4313 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4314 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4315 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4316 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4317 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4318 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4319 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4320 | /*PartialOverloading=*/true); |
| 4321 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4322 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4323 | if (UME->hasExplicitTemplateArgs()) { |
| 4324 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4325 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4326 | } |
Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 4327 | |
| 4328 | // Add the base as first argument (use a nullptr if the base is implicit). |
| 4329 | SmallVector<Expr *, 12> ArgExprs( |
| 4330 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4331 | ArgExprs.append(Args.begin(), Args.end()); |
| 4332 | UnresolvedSet<8> Decls; |
| 4333 | Decls.append(UME->decls_begin(), UME->decls_end()); |
| 4334 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4335 | /*SuppressUsedConversions=*/false, |
| 4336 | /*PartialOverloading=*/true); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4337 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4338 | FunctionDecl *FD = nullptr; |
| 4339 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 4340 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 4341 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 4342 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4343 | 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] | 4344 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4345 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4346 | Results.push_back(ResultCandidate(FD)); |
| 4347 | else |
| 4348 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4349 | Args, CandidateSet, |
| 4350 | /*SuppressUsedConversions=*/false, |
| 4351 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4352 | |
| 4353 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4354 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4355 | // 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] | 4356 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4357 | if (isCompleteType(Loc, NakedFn->getType())) { |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4358 | DeclarationName OpName = Context.DeclarationNames |
| 4359 | .getCXXOperatorName(OO_Call); |
| 4360 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4361 | LookupQualifiedName(R, DC); |
| 4362 | R.suppressDiagnostics(); |
| 4363 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4364 | ArgExprs.append(Args.begin(), Args.end()); |
| 4365 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4366 | /*ExplicitArgs=*/nullptr, |
| 4367 | /*SuppressUsedConversions=*/false, |
| 4368 | /*PartialOverloading=*/true); |
| 4369 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4370 | } else { |
| 4371 | // Lastly we check whether expression's type is function pointer or |
| 4372 | // function. |
| 4373 | QualType T = NakedFn->getType(); |
| 4374 | if (!T->getPointeeType().isNull()) |
| 4375 | T = T->getPointeeType(); |
| 4376 | |
| 4377 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4378 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4379 | /*PartialOverloading=*/true) || |
| 4380 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4381 | Results.push_back(ResultCandidate(FP)); |
| 4382 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4383 | // 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] | 4384 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4385 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4386 | } |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4387 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4388 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4389 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), |
| 4390 | !CandidateSet.empty()); |
| 4391 | } |
| 4392 | |
| 4393 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, |
| 4394 | ArrayRef<Expr *> Args) { |
| 4395 | if (!CodeCompleter) |
| 4396 | return; |
| 4397 | |
| 4398 | // A complete type is needed to lookup for constructors. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4399 | if (!isCompleteType(Loc, Type)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4400 | return; |
| 4401 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4402 | CXXRecordDecl *RD = Type->getAsCXXRecordDecl(); |
| 4403 | if (!RD) { |
| 4404 | CodeCompleteExpression(S, Type); |
| 4405 | return; |
| 4406 | } |
| 4407 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4408 | // FIXME: Provide support for member initializers. |
| 4409 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4410 | |
| 4411 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4412 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4413 | for (auto C : LookupConstructors(RD)) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4414 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4415 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4416 | Args, CandidateSet, |
| 4417 | /*SuppressUsedConversions=*/false, |
| 4418 | /*PartialOverloading=*/true); |
| 4419 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4420 | AddTemplateOverloadCandidate(FTD, |
| 4421 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4422 | /*ExplicitTemplateArgs=*/nullptr, |
| 4423 | Args, CandidateSet, |
| 4424 | /*SuppressUsedConversions=*/false, |
| 4425 | /*PartialOverloading=*/true); |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | SmallVector<ResultCandidate, 8> Results; |
| 4430 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4431 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4434 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4435 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4436 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4437 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | } |
| 4440 | |
| 4441 | CodeCompleteExpression(S, VD->getType()); |
| 4442 | } |
| 4443 | |
| 4444 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4445 | QualType ResultType; |
| 4446 | if (isa<BlockDecl>(CurContext)) { |
| 4447 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4448 | ResultType = BSI->ReturnType; |
| 4449 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4450 | ResultType = Function->getReturnType(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4451 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4452 | ResultType = Method->getReturnType(); |
| 4453 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4454 | if (ResultType.isNull()) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4455 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4456 | else |
| 4457 | CodeCompleteExpression(S, ResultType); |
| 4458 | } |
| 4459 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4460 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4461 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4462 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4463 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4464 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4465 | Results.EnterNewScope(); |
| 4466 | |
| 4467 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4468 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4469 | CodeCompleter->includeGlobals()); |
| 4470 | |
| 4471 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| 4472 | |
| 4473 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4474 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4475 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4476 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4477 | if (Results.includeCodePatterns()) { |
| 4478 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4479 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4480 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4481 | Builder.AddPlaceholderChunk("statements"); |
| 4482 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4483 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4484 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4485 | Results.AddResult(Builder.TakeString()); |
| 4486 | |
| 4487 | // "else if" block |
| 4488 | Builder.AddTypedTextChunk("else"); |
| 4489 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4490 | Builder.AddTextChunk("if"); |
| 4491 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4492 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4493 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4494 | Builder.AddPlaceholderChunk("condition"); |
| 4495 | else |
| 4496 | Builder.AddPlaceholderChunk("expression"); |
| 4497 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4498 | if (Results.includeCodePatterns()) { |
| 4499 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4500 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4501 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4502 | Builder.AddPlaceholderChunk("statements"); |
| 4503 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4504 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4505 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4506 | Results.AddResult(Builder.TakeString()); |
| 4507 | |
| 4508 | Results.ExitScope(); |
| 4509 | |
| 4510 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4511 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4512 | |
| 4513 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4514 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4515 | |
| 4516 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4517 | Results.data(),Results.size()); |
| 4518 | } |
| 4519 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4520 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4521 | if (LHS) |
| 4522 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4523 | else |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4524 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4527 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4528 | bool EnteringContext) { |
| 4529 | if (!SS.getScopeRep() || !CodeCompleter) |
| 4530 | return; |
| 4531 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4532 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 4533 | if (!Ctx) |
| 4534 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4535 | |
| 4536 | // Try to instantiate any non-dependent declaration contexts before |
| 4537 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4538 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4539 | return; |
| 4540 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4541 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4542 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4543 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4544 | Results.EnterNewScope(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4546 | // The "template" keyword can follow "::" in the grammar, but only |
| 4547 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4548 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4549 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4550 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4551 | |
| 4552 | // Add calls to overridden virtual functions, if there are any. |
| 4553 | // |
| 4554 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4555 | // in a context that permits expressions. This is a general issue with |
| 4556 | // qualified-id completions. |
| 4557 | if (!EnteringContext) |
| 4558 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 4559 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4560 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4561 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4562 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 4563 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4564 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4565 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4566 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4567 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4568 | |
| 4569 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4570 | if (!CodeCompleter) |
| 4571 | return; |
| 4572 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4573 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4574 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4575 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4576 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4577 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4578 | |
| 4579 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4580 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4581 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4582 | |
| 4583 | // After "using", we can see anything that would start a |
| 4584 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4585 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4586 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4587 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4588 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4589 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4590 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4591 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4592 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4596 | if (!CodeCompleter) |
| 4597 | return; |
| 4598 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4599 | // After "using namespace", we expect to see a namespace name or namespace |
| 4600 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4601 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4602 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4603 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4604 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4605 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4606 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4607 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4608 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4609 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4610 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4611 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4612 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4616 | if (!CodeCompleter) |
| 4617 | return; |
| 4618 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4619 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4620 | if (!S->getParent()) |
| 4621 | Ctx = Context.getTranslationUnitDecl(); |
| 4622 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4623 | bool SuppressedGlobalResults |
| 4624 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 4625 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4626 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4627 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4628 | SuppressedGlobalResults |
| 4629 | ? CodeCompletionContext::CCC_Namespace |
| 4630 | : CodeCompletionContext::CCC_Other, |
| 4631 | &ResultBuilder::IsNamespace); |
| 4632 | |
| 4633 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4634 | // We only want to see those namespaces that have already been defined |
| 4635 | // within this scope, because its likely that the user is creating an |
| 4636 | // extended namespace declaration. Keep track of the most recent |
| 4637 | // definition of each namespace. |
| 4638 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 4639 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 4640 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4641 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4642 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4643 | |
| 4644 | // Add the most recent definition (or extended definition) of each |
| 4645 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4646 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4647 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4648 | NS = OrigToLatest.begin(), |
| 4649 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4650 | NS != NSEnd; ++NS) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4651 | Results.AddResult(CodeCompletionResult( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4652 | NS->second, Results.getBasePriority(NS->second), |
| 4653 | nullptr), |
| 4654 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4655 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4658 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4659 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4660 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4664 | if (!CodeCompleter) |
| 4665 | return; |
| 4666 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4667 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4668 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4669 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4670 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4671 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4672 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4673 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4674 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4675 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4676 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4677 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4680 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4681 | if (!CodeCompleter) |
| 4682 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4683 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4684 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4685 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4686 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4687 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4688 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4689 | Results.EnterNewScope(); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4691 | // Add the names of overloadable operators. |
| 4692 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4693 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4694 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4695 | #include "clang/Basic/OperatorKinds.def" |
| 4696 | |
| 4697 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4698 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4699 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4700 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4701 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4702 | |
| 4703 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4704 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4705 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4707 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4708 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4709 | Results.data(),Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4710 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4711 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4712 | void Sema::CodeCompleteConstructorInitializer( |
| 4713 | Decl *ConstructorD, |
| 4714 | ArrayRef <CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4715 | if (!ConstructorD) |
| 4716 | return; |
| 4717 | |
| 4718 | AdjustDeclIfTemplate(ConstructorD); |
| 4719 | |
| 4720 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4721 | if (!Constructor) |
| 4722 | return; |
| 4723 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4724 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4725 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4726 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4727 | Results.EnterNewScope(); |
| 4728 | |
| 4729 | // Fill in any already-initialized fields or base classes. |
| 4730 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4731 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4732 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4733 | if (Initializers[I]->isBaseInitializer()) |
| 4734 | InitializedBases.insert( |
| 4735 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4736 | else |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4737 | InitializedFields.insert(cast<FieldDecl>( |
| 4738 | Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | // Add completions for base classes. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4742 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4743 | Results.getCodeCompletionTUInfo()); |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4744 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4745 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4746 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4747 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4748 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4749 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4750 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4751 | = !Initializers.empty() && |
| 4752 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4753 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4754 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4755 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4756 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4757 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4758 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4759 | Results.getAllocator().CopyString( |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4760 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4761 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4762 | Builder.AddPlaceholderChunk("args"); |
| 4763 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4764 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4765 | SawLastInitializer? CCP_NextInitializer |
| 4766 | : CCP_MemberDeclaration)); |
| 4767 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4771 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4772 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4773 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4774 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4775 | = !Initializers.empty() && |
| 4776 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4777 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4778 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4779 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4780 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4782 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4783 | Builder.getAllocator().CopyString( |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4784 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4785 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4786 | Builder.AddPlaceholderChunk("args"); |
| 4787 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4788 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4789 | SawLastInitializer? CCP_NextInitializer |
| 4790 | : CCP_MemberDeclaration)); |
| 4791 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4795 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4796 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4797 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4798 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4799 | = !Initializers.empty() && |
| 4800 | Initializers.back()->isAnyMemberInitializer() && |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4801 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4802 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4803 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4804 | |
| 4805 | if (!Field->getDeclName()) |
| 4806 | continue; |
| 4807 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4808 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4809 | Field->getIdentifier()->getName())); |
| 4810 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4811 | Builder.AddPlaceholderChunk("args"); |
| 4812 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4813 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4814 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4815 | : CCP_MemberDeclaration, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4816 | CXCursor_MemberRef, |
| 4817 | CXAvailability_Available, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4818 | Field)); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4819 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4820 | } |
| 4821 | Results.ExitScope(); |
| 4822 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4823 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4824 | Results.data(), Results.size()); |
| 4825 | } |
| 4826 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4827 | /// \brief Determine whether this scope denotes a namespace. |
| 4828 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4829 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4830 | if (!DC) |
| 4831 | return false; |
| 4832 | |
| 4833 | return DC->isFileContext(); |
| 4834 | } |
| 4835 | |
| 4836 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 4837 | bool AfterAmpersand) { |
| 4838 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4839 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4840 | CodeCompletionContext::CCC_Other); |
| 4841 | Results.EnterNewScope(); |
| 4842 | |
| 4843 | // Note what has already been captured. |
| 4844 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 4845 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4846 | for (const auto &C : Intro.Captures) { |
| 4847 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4848 | IncludedThis = true; |
| 4849 | continue; |
| 4850 | } |
| 4851 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4852 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | // Look for other capturable variables. |
| 4856 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 4857 | for (const auto *D : S->decls()) { |
| 4858 | const auto *Var = dyn_cast<VarDecl>(D); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4859 | if (!Var || |
| 4860 | !Var->hasLocalStorage() || |
| 4861 | Var->hasAttr<BlocksAttr>()) |
| 4862 | continue; |
| 4863 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4864 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4865 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4866 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4867 | } |
| 4868 | } |
| 4869 | |
| 4870 | // Add 'this', if it would be valid. |
| 4871 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 4872 | addThisCompletion(*this, Results); |
| 4873 | |
| 4874 | Results.ExitScope(); |
| 4875 | |
| 4876 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4877 | Results.data(), Results.size()); |
| 4878 | } |
| 4879 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4880 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 4881 | /// Keyword, depending on whether NeedAt is true or false. |
| 4882 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 4883 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4884 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4885 | ResultBuilder &Results, |
| 4886 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4887 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4888 | // Since we have an implementation, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4889 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4890 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4891 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4892 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4893 | if (LangOpts.ObjC2) { |
| 4894 | // @dynamic |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4895 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4896 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4897 | Builder.AddPlaceholderChunk("property"); |
| 4898 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4899 | |
| 4900 | // @synthesize |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4901 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4902 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4903 | Builder.AddPlaceholderChunk("property"); |
| 4904 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4908 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4909 | ResultBuilder &Results, |
| 4910 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4911 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4912 | |
| 4913 | // Since we have an interface or protocol, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4914 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4915 | |
| 4916 | if (LangOpts.ObjC2) { |
| 4917 | // @property |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4918 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4919 | |
| 4920 | // @required |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4921 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4922 | |
| 4923 | // @optional |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4924 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4925 | } |
| 4926 | } |
| 4927 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4928 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4929 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4930 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4931 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4932 | |
| 4933 | // @class name ; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4934 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4935 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4936 | Builder.AddPlaceholderChunk("name"); |
| 4937 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4939 | if (Results.includeCodePatterns()) { |
| 4940 | // @interface name |
| 4941 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 4942 | // such. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4943 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4944 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4945 | Builder.AddPlaceholderChunk("class"); |
| 4946 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4948 | // @protocol name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4949 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4950 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4951 | Builder.AddPlaceholderChunk("protocol"); |
| 4952 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4953 | |
| 4954 | // @implementation name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4955 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4956 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4957 | Builder.AddPlaceholderChunk("class"); |
| 4958 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4959 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4960 | |
| 4961 | // @compatibility_alias name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4962 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4963 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4964 | Builder.AddPlaceholderChunk("alias"); |
| 4965 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4966 | Builder.AddPlaceholderChunk("class"); |
| 4967 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 4968 | |
| 4969 | if (Results.getSema().getLangOpts().Modules) { |
| 4970 | // @import name |
| 4971 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 4972 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4973 | Builder.AddPlaceholderChunk("module"); |
| 4974 | Results.AddResult(Result(Builder.TakeString())); |
| 4975 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4978 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4979 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4980 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4981 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4982 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4983 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4984 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4985 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4986 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4987 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4988 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4989 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4990 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4991 | CodeCompletionContext::CCC_Other, |
| 4992 | Results.data(),Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4995 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4996 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4997 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4998 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4999 | |
| 5000 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5001 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5002 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 5003 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5004 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5005 | Builder.AddResultTypeChunk(EncodeType); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5006 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5007 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5008 | Builder.AddPlaceholderChunk("type-name"); |
| 5009 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5010 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5011 | |
| 5012 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5013 | Builder.AddResultTypeChunk("Protocol *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5014 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5015 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5016 | Builder.AddPlaceholderChunk("protocol-name"); |
| 5017 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5018 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5019 | |
| 5020 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5021 | Builder.AddResultTypeChunk("SEL"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5022 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5023 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5024 | Builder.AddPlaceholderChunk("selector"); |
| 5025 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5026 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5027 | |
| 5028 | // @"string" |
| 5029 | Builder.AddResultTypeChunk("NSString *"); |
| 5030 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 5031 | Builder.AddPlaceholderChunk("string"); |
| 5032 | Builder.AddTextChunk("\""); |
| 5033 | Results.AddResult(Result(Builder.TakeString())); |
| 5034 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5035 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5036 | Builder.AddResultTypeChunk("NSArray *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5037 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5038 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5039 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 5040 | Results.AddResult(Result(Builder.TakeString())); |
| 5041 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5042 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5043 | Builder.AddResultTypeChunk("NSDictionary *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5044 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5045 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5046 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5047 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5048 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5049 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5050 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5051 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5052 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5053 | Builder.AddResultTypeChunk("id"); |
| 5054 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5055 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5056 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5057 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5060 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5061 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5062 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5063 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5065 | if (Results.includeCodePatterns()) { |
| 5066 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 5067 | // { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5068 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5069 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5070 | Builder.AddPlaceholderChunk("statements"); |
| 5071 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5072 | Builder.AddTextChunk("@catch"); |
| 5073 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5074 | Builder.AddPlaceholderChunk("parameter"); |
| 5075 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5076 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5077 | Builder.AddPlaceholderChunk("statements"); |
| 5078 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5079 | Builder.AddTextChunk("@finally"); |
| 5080 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5081 | Builder.AddPlaceholderChunk("statements"); |
| 5082 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5083 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5084 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5085 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5086 | // @throw |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5087 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5088 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5089 | Builder.AddPlaceholderChunk("expression"); |
| 5090 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5092 | if (Results.includeCodePatterns()) { |
| 5093 | // @synchronized ( expression ) { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5094 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5095 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5096 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5097 | Builder.AddPlaceholderChunk("expression"); |
| 5098 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5099 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5100 | Builder.AddPlaceholderChunk("statements"); |
| 5101 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5102 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5103 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5104 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5106 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5107 | ResultBuilder &Results, |
| 5108 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5109 | typedef CodeCompletionResult Result; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5110 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 5111 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 5112 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5113 | if (LangOpts.ObjC2) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5114 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5118 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5119 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5120 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5121 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5122 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5123 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5124 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5125 | CodeCompletionContext::CCC_Other, |
| 5126 | Results.data(),Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5130 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5131 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5132 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5133 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5134 | AddObjCStatementResults(Results, false); |
| 5135 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5136 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5137 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5138 | CodeCompletionContext::CCC_Other, |
| 5139 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5143 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5144 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5145 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5146 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5147 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5148 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5149 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5150 | CodeCompletionContext::CCC_Other, |
| 5151 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5154 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 5155 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5156 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5157 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5158 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5159 | return true; |
| 5160 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5161 | Attributes |= NewFlag; |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5162 | |
| 5163 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5164 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 5165 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5166 | return true; |
| 5167 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5168 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5169 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5170 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5171 | ObjCDeclSpec::DQ_PR_copy | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5172 | ObjCDeclSpec::DQ_PR_retain | |
| 5173 | ObjCDeclSpec::DQ_PR_strong | |
| 5174 | ObjCDeclSpec::DQ_PR_weak); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5175 | if (AssignCopyRetMask && |
| 5176 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5177 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5178 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5179 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5180 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 5181 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5182 | return true; |
| 5183 | |
| 5184 | return false; |
| 5185 | } |
| 5186 | |
Douglas Gregor | 36029f4 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 5187 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5188 | if (!CodeCompleter) |
| 5189 | return; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5190 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5191 | unsigned Attributes = ODS.getPropertyAttributes(); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5192 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5193 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5194 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5195 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5196 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5197 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5198 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5199 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5200 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5201 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5202 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 5203 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5204 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5205 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5206 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5207 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5208 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5209 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5210 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5211 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5212 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5213 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5214 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 5215 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5216 | |
| 5217 | // 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] | 5218 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5219 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5220 | Results.AddResult(CodeCompletionResult("weak")); |
| 5221 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5222 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5223 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 5224 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5225 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5226 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5227 | Setter.AddPlaceholderChunk("method"); |
| 5228 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5229 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5230 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5231 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 5232 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5233 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5234 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5235 | Getter.AddPlaceholderChunk("method"); |
| 5236 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5237 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5238 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 5239 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 5240 | Results.AddResult(CodeCompletionResult("nullable")); |
| 5241 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 5242 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 5243 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5244 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5245 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5246 | CodeCompletionContext::CCC_Other, |
| 5247 | Results.data(),Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5248 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5249 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 5250 | /// \brief Describes the kind of Objective-C method that we want to find |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5251 | /// via code completion. |
| 5252 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 5253 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 5254 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 5255 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5256 | }; |
| 5257 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5258 | static bool isAcceptableObjCSelector(Selector Sel, |
| 5259 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5260 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5261 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5262 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5263 | if (NumSelIdents > Sel.getNumArgs()) |
| 5264 | return false; |
| 5265 | |
| 5266 | switch (WantKind) { |
| 5267 | case MK_Any: break; |
| 5268 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 5269 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 5270 | } |
| 5271 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5272 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 5273 | return false; |
| 5274 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5275 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 5276 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 5277 | return false; |
| 5278 | |
| 5279 | return true; |
| 5280 | } |
| 5281 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5282 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 5283 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5284 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5285 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5286 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5287 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5288 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5289 | |
| 5290 | namespace { |
| 5291 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 5292 | /// completions with the same selector into the result set. |
| 5293 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 5294 | } |
| 5295 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5296 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 5297 | /// container to the set of results. |
| 5298 | /// |
| 5299 | /// The container will be a class, protocol, category, or implementation of |
| 5300 | /// any of the above. This mether will recurse to include methods from |
| 5301 | /// the superclasses of classes along with their categories, protocols, and |
| 5302 | /// implementations. |
| 5303 | /// |
| 5304 | /// \param Container the container in which we'll look to find methods. |
| 5305 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5306 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5307 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5308 | /// |
| 5309 | /// \param CurContext the context in which we're performing the lookup that |
| 5310 | /// finds methods. |
| 5311 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5312 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5313 | /// when it has the same number of parameters as we have selector identifiers. |
| 5314 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5315 | /// \param Results the structure into which we'll add results. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5316 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5317 | bool WantInstanceMethods, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5318 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5319 | DeclContext *CurContext, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5320 | VisitedSelectorSet &Selectors, bool AllowSameLength, |
| 5321 | ResultBuilder &Results, bool InOriginalClass = true, |
| 5322 | bool IsRootClass = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5323 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5324 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5325 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5326 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5327 | for (auto *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5328 | // The instance methods on the root class can be messaged via the |
| 5329 | // metaclass. |
| 5330 | if (M->isInstanceMethod() == WantInstanceMethods || |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5331 | (IsRootClass && !WantInstanceMethods)) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5332 | // Check whether the selector identifiers we've been given are a |
| 5333 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5334 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5335 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5336 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5337 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5338 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5339 | |
| 5340 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5341 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5342 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5343 | if (!InOriginalClass) |
| 5344 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5345 | Results.MaybeAddResult(R, CurContext); |
| 5346 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5349 | // Visit the protocols of protocols. |
| 5350 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5351 | if (Protocol->hasDefinition()) { |
| 5352 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5353 | = Protocol->getReferencedProtocols(); |
| 5354 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5355 | E = Protocols.end(); |
| 5356 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5357 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5358 | Selectors, AllowSameLength, Results, false, IsRootClass); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5359 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5362 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5363 | return; |
| 5364 | |
| 5365 | // Add methods in protocols. |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 5366 | for (auto *I : IFace->protocols()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5367 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5368 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5369 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5370 | // Add methods in categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5371 | for (auto *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5372 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5373 | CurContext, Selectors, AllowSameLength, Results, |
| 5374 | InOriginalClass, IsRootClass); |
| 5375 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5376 | // Add a categories protocol methods. |
| 5377 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5378 | = CatDecl->getReferencedProtocols(); |
| 5379 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5380 | E = Protocols.end(); |
| 5381 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5382 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5383 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5384 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5385 | // Add methods in category implementations. |
| 5386 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5387 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5388 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5389 | IsRootClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | // Add methods in superclass. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5393 | // Avoid passing in IsRootClass since root classes won't have super classes. |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5394 | if (IFace->getSuperClass()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5395 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 5396 | SelIdents, CurContext, Selectors, AllowSameLength, Results, |
| 5397 | /*IsRootClass=*/false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5398 | |
| 5399 | // Add methods in our implementation, if any. |
| 5400 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5401 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5402 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5403 | IsRootClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5407 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5408 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5409 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5410 | if (!Class) { |
| 5411 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5412 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5413 | Class = Category->getClassInterface(); |
| 5414 | |
| 5415 | if (!Class) |
| 5416 | return; |
| 5417 | } |
| 5418 | |
| 5419 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5420 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5421 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5422 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5423 | Results.EnterNewScope(); |
| 5424 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5425 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5426 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5427 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5428 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5429 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5430 | CodeCompletionContext::CCC_Other, |
| 5431 | Results.data(),Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5434 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5435 | // Try to find the interface where setters might live. |
| 5436 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5437 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5438 | if (!Class) { |
| 5439 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5440 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5441 | Class = Category->getClassInterface(); |
| 5442 | |
| 5443 | if (!Class) |
| 5444 | return; |
| 5445 | } |
| 5446 | |
| 5447 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5448 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5449 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5450 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5451 | Results.EnterNewScope(); |
| 5452 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5453 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5454 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5455 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5456 | |
| 5457 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5458 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5459 | CodeCompletionContext::CCC_Other, |
| 5460 | Results.data(),Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5463 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5464 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5465 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5466 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5467 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5468 | Results.EnterNewScope(); |
| 5469 | |
| 5470 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5471 | bool AddedInOut = false; |
| 5472 | if ((DS.getObjCDeclQualifier() & |
| 5473 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5474 | Results.AddResult("in"); |
| 5475 | Results.AddResult("inout"); |
| 5476 | AddedInOut = true; |
| 5477 | } |
| 5478 | if ((DS.getObjCDeclQualifier() & |
| 5479 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5480 | Results.AddResult("out"); |
| 5481 | if (!AddedInOut) |
| 5482 | Results.AddResult("inout"); |
| 5483 | } |
| 5484 | if ((DS.getObjCDeclQualifier() & |
| 5485 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5486 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5487 | Results.AddResult("bycopy"); |
| 5488 | Results.AddResult("byref"); |
| 5489 | Results.AddResult("oneway"); |
| 5490 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5491 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 5492 | Results.AddResult("nonnull"); |
| 5493 | Results.AddResult("nullable"); |
| 5494 | Results.AddResult("null_unspecified"); |
| 5495 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5496 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5497 | // If we're completing the return type of an Objective-C method and the |
| 5498 | // identifier IBAction refers to a macro, provide a completion item for |
| 5499 | // an action, e.g., |
| 5500 | // IBAction)<#selector#>:(id)sender |
| 5501 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 5502 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5503 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5504 | Results.getCodeCompletionTUInfo(), |
| 5505 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5506 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5507 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5508 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5509 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5510 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5511 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5512 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5513 | Builder.AddTextChunk("sender"); |
| 5514 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5515 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5516 | |
| 5517 | // If we're completing the return type, provide 'instancetype'. |
| 5518 | if (!IsParameter) { |
| 5519 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5520 | } |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5521 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5522 | // Add various builtin type names and specifiers. |
| 5523 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5524 | Results.ExitScope(); |
| 5525 | |
| 5526 | // Add the various type names |
| 5527 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5528 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5529 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5530 | CodeCompleter->includeGlobals()); |
| 5531 | |
| 5532 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5533 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5534 | |
| 5535 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5536 | CodeCompletionContext::CCC_Type, |
| 5537 | Results.data(), Results.size()); |
| 5538 | } |
| 5539 | |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5540 | /// \brief When we have an expression with type "id", we may assume |
| 5541 | /// that it has some more-specific class type based on knowledge of |
| 5542 | /// common uses of Objective-C. This routine returns that class type, |
| 5543 | /// or NULL if no better result could be determined. |
| 5544 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5545 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5546 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5547 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5548 | |
| 5549 | Selector Sel = Msg->getSelector(); |
| 5550 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5551 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5552 | |
| 5553 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5554 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5555 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5556 | |
| 5557 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5558 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5559 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5560 | |
| 5561 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5562 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5563 | switch (Msg->getReceiverKind()) { |
| 5564 | case ObjCMessageExpr::Class: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5565 | if (const ObjCObjectType *ObjType |
| 5566 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5567 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5568 | break; |
| 5569 | |
| 5570 | case ObjCMessageExpr::Instance: { |
| 5571 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5572 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5573 | IFace = Ptr->getInterfaceDecl(); |
| 5574 | break; |
| 5575 | } |
| 5576 | |
| 5577 | case ObjCMessageExpr::SuperInstance: |
| 5578 | case ObjCMessageExpr::SuperClass: |
| 5579 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5583 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5584 | |
| 5585 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5586 | if (Method->isInstanceMethod()) |
| 5587 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5588 | .Case("retain", IFace) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5589 | .Case("strong", IFace) |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5590 | .Case("autorelease", IFace) |
| 5591 | .Case("copy", IFace) |
| 5592 | .Case("copyWithZone", IFace) |
| 5593 | .Case("mutableCopy", IFace) |
| 5594 | .Case("mutableCopyWithZone", IFace) |
| 5595 | .Case("awakeFromCoder", IFace) |
| 5596 | .Case("replacementObjectFromCoder", IFace) |
| 5597 | .Case("class", IFace) |
| 5598 | .Case("classForCoder", IFace) |
| 5599 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5600 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5601 | |
| 5602 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5603 | .Case("new", IFace) |
| 5604 | .Case("alloc", IFace) |
| 5605 | .Case("allocWithZone", IFace) |
| 5606 | .Case("class", IFace) |
| 5607 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5608 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5611 | // Add a special completion for a message send to "super", which fills in the |
| 5612 | // most likely case of forwarding all of our arguments to the superclass |
| 5613 | // function. |
| 5614 | /// |
| 5615 | /// \param S The semantic analysis object. |
| 5616 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5617 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5618 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5619 | /// |
| 5620 | /// \param SelIdents The identifiers in the selector that have already been |
| 5621 | /// provided as arguments for a send to "super". |
| 5622 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5623 | /// \param Results The set of results to augment. |
| 5624 | /// |
| 5625 | /// \returns the Objective-C method declaration that would be invoked by |
| 5626 | /// this "super" completion. If NULL, no completion was added. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5627 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5628 | Sema &S, bool NeedSuperKeyword, |
| 5629 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5630 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5631 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5632 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5633 | return nullptr; |
| 5634 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5635 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5636 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5637 | return nullptr; |
| 5638 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5639 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5640 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5641 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5642 | // Check in the class |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5643 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 5644 | CurMethod->isInstanceMethod()); |
| 5645 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5646 | // Check in categories or class extensions. |
| 5647 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5648 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5649 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5650 | CurMethod->isInstanceMethod()))) |
| 5651 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5652 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5653 | } |
| 5654 | } |
| 5655 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5656 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5657 | return nullptr; |
| 5658 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5659 | // Check whether the superclass method has the same signature. |
| 5660 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5661 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5662 | return nullptr; |
| 5663 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5664 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5665 | CurPEnd = CurMethod->param_end(), |
| 5666 | SuperP = SuperMethod->param_begin(); |
| 5667 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5668 | // Make sure the parameter types are compatible. |
| 5669 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 5670 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5671 | return nullptr; |
| 5672 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5673 | // Make sure we have a parameter name to forward! |
| 5674 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5675 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
| 5678 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5679 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5680 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5681 | |
| 5682 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 5683 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5684 | Results.getCompletionContext().getBaseType(), |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5685 | Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5686 | |
| 5687 | // If we need the "super" keyword, add it (plus some spacing). |
| 5688 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5689 | Builder.AddTypedTextChunk("super"); |
| 5690 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | Selector Sel = CurMethod->getSelector(); |
| 5694 | if (Sel.isUnarySelector()) { |
| 5695 | if (NeedSuperKeyword) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5696 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5697 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5698 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5699 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5700 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5701 | } else { |
| 5702 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5703 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5704 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5705 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5706 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5707 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5708 | Builder.AddInformativeChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5709 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5710 | Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5711 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5712 | Builder.AddTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5713 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5714 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5715 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5716 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5717 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5718 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5719 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5720 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5721 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5722 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | } |
| 5726 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5727 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5728 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5729 | return SuperMethod; |
| 5730 | } |
| 5731 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5732 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5733 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5734 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5735 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5736 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5737 | getLangOpts().CPlusPlus11 |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5738 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5739 | : &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5740 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5741 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5742 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5743 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5744 | CodeCompleter->includeGlobals()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5745 | |
| 5746 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5747 | // add "super" as an option. |
| 5748 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5749 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5750 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5751 | Results.AddResult(Result("super")); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5752 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5753 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5754 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5755 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5756 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5757 | addThisCompletion(*this, Results); |
| 5758 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5759 | Results.ExitScope(); |
| 5760 | |
| 5761 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5762 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5763 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5764 | Results.data(), Results.size()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5765 | |
| 5766 | } |
| 5767 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5768 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5769 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5770 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5771 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5772 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5773 | // Figure out which interface we're in. |
| 5774 | CDecl = CurMethod->getClassInterface(); |
| 5775 | if (!CDecl) |
| 5776 | return; |
| 5777 | |
| 5778 | // Find the superclass of this class. |
| 5779 | CDecl = CDecl->getSuperClass(); |
| 5780 | if (!CDecl) |
| 5781 | return; |
| 5782 | |
| 5783 | if (CurMethod->isInstanceMethod()) { |
| 5784 | // We are inside an instance method, which means that the message |
| 5785 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5786 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5787 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5788 | AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5789 | CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | // Fall through to send to the superclass in CDecl. |
| 5793 | } else { |
| 5794 | // "super" may be the name of a type or variable. Figure out which |
| 5795 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5796 | IdentifierInfo *Super = getSuperIdentifier(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5797 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 5798 | LookupOrdinaryName); |
| 5799 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5800 | // "super" names an interface. Use it. |
| 5801 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5802 | if (const ObjCObjectType *Iface |
| 5803 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5804 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5805 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5806 | // "super" names an unresolved type; we can't be more specific. |
| 5807 | } else { |
| 5808 | // Assume that "super" names some kind of value and parse that way. |
| 5809 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5810 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5811 | UnqualifiedId id; |
| 5812 | id.setIdentifier(Super, SuperLoc); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5813 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 5814 | false, false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5815 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5816 | SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5817 | AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
| 5820 | // Fall through |
| 5821 | } |
| 5822 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5823 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5824 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5825 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5826 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5827 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5828 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5831 | /// \brief Given a set of code-completion results for the argument of a message |
| 5832 | /// send, determine the preferred type (if any) for that argument expression. |
| 5833 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 5834 | unsigned NumSelIdents) { |
| 5835 | typedef CodeCompletionResult Result; |
| 5836 | ASTContext &Context = Results.getSema().Context; |
| 5837 | |
| 5838 | QualType PreferredType; |
| 5839 | unsigned BestPriority = CCP_Unlikely * 2; |
| 5840 | Result *ResultsData = Results.data(); |
| 5841 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 5842 | Result &R = ResultsData[I]; |
| 5843 | if (R.Kind == Result::RK_Declaration && |
| 5844 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 5845 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 5846 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5847 | if (NumSelIdents <= Method->param_size()) { |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 5848 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5849 | ->getType(); |
| 5850 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 5851 | BestPriority = R.Priority; |
| 5852 | PreferredType = MyPreferredType; |
| 5853 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 5854 | MyPreferredType)) { |
| 5855 | PreferredType = QualType(); |
| 5856 | } |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | return PreferredType; |
| 5863 | } |
| 5864 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5865 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 5866 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5867 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5868 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5869 | bool IsSuper, |
| 5870 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5871 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5872 | ObjCInterfaceDecl *CDecl = nullptr; |
| 5873 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5874 | // If the given name refers to an interface type, retrieve the |
| 5875 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5876 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5877 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5878 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5879 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 5880 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5881 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5882 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5883 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 5884 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5885 | Results.EnterNewScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5887 | // If this is a send-to-super, try to add the special "super" send |
| 5888 | // completion. |
| 5889 | if (IsSuper) { |
| 5890 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5891 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5892 | Results.Ignore(SuperMethod); |
| 5893 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5895 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5896 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5897 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5898 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5899 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5900 | VisitedSelectorSet Selectors; |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5901 | if (CDecl) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5902 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5903 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 5904 | Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5905 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5906 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5907 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5908 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5909 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5910 | if (SemaRef.getExternalSource()) { |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5911 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5912 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5913 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5914 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5915 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5916 | continue; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5917 | |
| 5918 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5919 | } |
| 5920 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5921 | |
| 5922 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 5923 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5924 | M != MEnd; ++M) { |
| 5925 | for (ObjCMethodList *MethList = &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5926 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5927 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5928 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5929 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5930 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5931 | Result R(MethList->getMethod(), |
| 5932 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5933 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5934 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5935 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5936 | } |
| 5937 | } |
| 5938 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5939 | |
| 5940 | Results.ExitScope(); |
| 5941 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5942 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5943 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5944 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5945 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5946 | bool IsSuper) { |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5947 | |
| 5948 | QualType T = this->GetTypeFromParser(Receiver); |
| 5949 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5950 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5951 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5952 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5953 | T, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5954 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5955 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5956 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5957 | |
| 5958 | // If we're actually at the argument expression (rather than prior to the |
| 5959 | // selector), we're actually performing code completion for an expression. |
| 5960 | // Determine whether we have a single, best method. If so, we can |
| 5961 | // code-complete the expression using the corresponding parameter type as |
| 5962 | // our preferred type, improving completion results. |
| 5963 | if (AtArgumentExpression) { |
| 5964 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5965 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5966 | if (PreferredType.isNull()) |
| 5967 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5968 | else |
| 5969 | CodeCompleteExpression(S, PreferredType); |
| 5970 | return; |
| 5971 | } |
| 5972 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5973 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5974 | Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5975 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5976 | } |
| 5977 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 5978 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5979 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5980 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5981 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5982 | typedef CodeCompletionResult Result; |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5983 | |
| 5984 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5985 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5986 | // If necessary, apply function/array conversion to the receiver. |
| 5987 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5988 | if (RecExpr) { |
| 5989 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 5990 | if (Conv.isInvalid()) // conversion failed. bail. |
| 5991 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5992 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5993 | } |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5994 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 5995 | : Super? Context.getObjCObjectPointerType( |
| 5996 | Context.getObjCInterfaceType(Super)) |
| 5997 | : Context.getObjCIdType(); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5998 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5999 | // If we're messaging an expression with type "id" or "Class", check |
| 6000 | // whether we know something special about the receiver that allows |
| 6001 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6002 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6003 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 6004 | if (ReceiverType->isObjCClassType()) |
| 6005 | return CodeCompleteObjCClassMessage(S, |
| 6006 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6007 | SelIdents, |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6008 | AtArgumentExpression, Super); |
| 6009 | |
| 6010 | ReceiverType = Context.getObjCObjectPointerType( |
| 6011 | Context.getObjCInterfaceType(IFace)); |
| 6012 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6013 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 6014 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 6015 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6016 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6017 | ReceiverType = RecExpr->getType(); |
| 6018 | } |
| 6019 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6020 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6021 | // Build the set of methods we can see. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6022 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6023 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6024 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6025 | ReceiverType, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6026 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6027 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6029 | // If this is a send-to-super, try to add the special "super" send |
| 6030 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6031 | if (Super) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6032 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6033 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6034 | Results.Ignore(SuperMethod); |
| 6035 | } |
| 6036 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6037 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6038 | // others. |
| 6039 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 6040 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6041 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6042 | // Keep track of the selectors we've already added. |
| 6043 | VisitedSelectorSet Selectors; |
| 6044 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6045 | // Handle messages to Class. This really isn't a message to an instance |
| 6046 | // method, so we treat it the same way we would treat a message send to a |
| 6047 | // class method. |
| 6048 | if (ReceiverType->isObjCClassType() || |
| 6049 | ReceiverType->isObjCQualifiedClassType()) { |
| 6050 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6051 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6052 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6053 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6054 | } |
| 6055 | } |
| 6056 | // Handle messages to a qualified ID ("id<foo>"). |
| 6057 | else if (const ObjCObjectPointerType *QualID |
| 6058 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 6059 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6060 | for (auto *I : QualID->quals()) |
| 6061 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6062 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6063 | } |
| 6064 | // Handle messages to a pointer to interface type. |
| 6065 | else if (const ObjCObjectPointerType *IFacePtr |
| 6066 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 6067 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6068 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6069 | CurContext, Selectors, AtArgumentExpression, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6070 | Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6071 | |
| 6072 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6073 | for (auto *I : IFacePtr->quals()) |
| 6074 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6075 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6076 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6077 | // Handle messages to "id". |
| 6078 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6079 | // We're messaging "id", so provide all instance methods we know |
| 6080 | // about as code-completion results. |
| 6081 | |
| 6082 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6083 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6084 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6085 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6086 | I != N; ++I) { |
| 6087 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6088 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6089 | continue; |
| 6090 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6091 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6092 | } |
| 6093 | } |
| 6094 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6095 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6096 | MEnd = MethodPool.end(); |
| 6097 | M != MEnd; ++M) { |
| 6098 | for (ObjCMethodList *MethList = &M->second.first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6099 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 6100 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6101 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6102 | continue; |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6103 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6104 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6105 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6106 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6107 | Result R(MethList->getMethod(), |
| 6108 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6109 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6110 | R.AllParametersAreInformative = false; |
| 6111 | Results.MaybeAddResult(R, CurContext); |
| 6112 | } |
| 6113 | } |
| 6114 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6115 | Results.ExitScope(); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6116 | |
| 6117 | |
| 6118 | // If we're actually at the argument expression (rather than prior to the |
| 6119 | // selector), we're actually performing code completion for an expression. |
| 6120 | // Determine whether we have a single, best method. If so, we can |
| 6121 | // code-complete the expression using the corresponding parameter type as |
| 6122 | // our preferred type, improving completion results. |
| 6123 | if (AtArgumentExpression) { |
| 6124 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6125 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6126 | if (PreferredType.isNull()) |
| 6127 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6128 | else |
| 6129 | CodeCompleteExpression(S, PreferredType); |
| 6130 | return; |
| 6131 | } |
| 6132 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6133 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6134 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6135 | Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6136 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6137 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6138 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 6139 | DeclGroupPtrTy IterationVar) { |
| 6140 | CodeCompleteExpressionData Data; |
| 6141 | Data.ObjCCollection = true; |
| 6142 | |
| 6143 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 6144 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6145 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 6146 | if (*I) |
| 6147 | Data.IgnoreDecls.push_back(*I); |
| 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | CodeCompleteExpression(S, Data); |
| 6152 | } |
| 6153 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6154 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 6155 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6156 | // If we have an external source, load the entire class method |
| 6157 | // pool from the AST file. |
| 6158 | if (ExternalSource) { |
| 6159 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6160 | I != N; ++I) { |
| 6161 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 6162 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 6163 | continue; |
| 6164 | |
| 6165 | ReadMethodPool(Sel); |
| 6166 | } |
| 6167 | } |
| 6168 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6169 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6170 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6171 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6172 | Results.EnterNewScope(); |
| 6173 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6174 | MEnd = MethodPool.end(); |
| 6175 | M != MEnd; ++M) { |
| 6176 | |
| 6177 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6178 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6179 | continue; |
| 6180 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6181 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6182 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6183 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6184 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6185 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6186 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6187 | continue; |
| 6188 | } |
| 6189 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6190 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6191 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6192 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6193 | if (!Accumulator.empty()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6194 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6195 | Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6196 | Accumulator.clear(); |
| 6197 | } |
| 6198 | } |
| 6199 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6200 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6201 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6202 | } |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6203 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6204 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6205 | } |
| 6206 | Results.ExitScope(); |
| 6207 | |
| 6208 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6209 | CodeCompletionContext::CCC_SelectorName, |
| 6210 | Results.data(), Results.size()); |
| 6211 | } |
| 6212 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6213 | /// \brief Add all of the protocol declarations that we find in the given |
| 6214 | /// (translation unit) context. |
| 6215 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6216 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6217 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6218 | typedef CodeCompletionResult Result; |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6219 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6220 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6221 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6222 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6223 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6224 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 6225 | CurContext, nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6226 | } |
| 6227 | } |
| 6228 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6229 | void Sema::CodeCompleteObjCProtocolReferences( |
| 6230 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6231 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6232 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6233 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6234 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6235 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6236 | Results.EnterNewScope(); |
| 6237 | |
| 6238 | // Tell the result set to ignore all of the protocols we have |
| 6239 | // already seen. |
| 6240 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6241 | for (const IdentifierLocPair &Pair : Protocols) |
| 6242 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, |
| 6243 | Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6244 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6245 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6246 | // Add all protocols. |
| 6247 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6248 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6249 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6250 | Results.ExitScope(); |
| 6251 | } |
| 6252 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6253 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6254 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 6255 | Results.data(),Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
| 6258 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6259 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6260 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6261 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6262 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6263 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6264 | Results.EnterNewScope(); |
| 6265 | |
| 6266 | // Add all protocols. |
| 6267 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 6268 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6269 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6270 | Results.ExitScope(); |
| 6271 | } |
| 6272 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6273 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6274 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 6275 | Results.data(),Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6276 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6277 | |
| 6278 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 6279 | /// the given (translation unit) context. |
| 6280 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 6281 | bool OnlyForwardDeclarations, |
| 6282 | bool OnlyUnimplemented, |
| 6283 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6284 | typedef CodeCompletionResult Result; |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6285 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6286 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6287 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6288 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 6289 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6290 | (!OnlyUnimplemented || !Class->getImplementation())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6291 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 6292 | CurContext, nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6297 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6298 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6299 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6300 | Results.EnterNewScope(); |
| 6301 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6302 | if (CodeCompleter->includeGlobals()) { |
| 6303 | // Add all classes. |
| 6304 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6305 | false, Results); |
| 6306 | } |
| 6307 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6308 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6309 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6310 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6311 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6312 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6315 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 6316 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6317 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6318 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6319 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6320 | Results.EnterNewScope(); |
| 6321 | |
| 6322 | // Make sure that we ignore the class we're currently defining. |
| 6323 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6324 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6325 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6326 | Results.Ignore(CurClass); |
| 6327 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6328 | if (CodeCompleter->includeGlobals()) { |
| 6329 | // Add all classes. |
| 6330 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6331 | false, Results); |
| 6332 | } |
| 6333 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6334 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6335 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6336 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6337 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6338 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6342 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6343 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6344 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6345 | Results.EnterNewScope(); |
| 6346 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6347 | if (CodeCompleter->includeGlobals()) { |
| 6348 | // Add all unimplemented classes. |
| 6349 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6350 | true, Results); |
| 6351 | } |
| 6352 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6353 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6355 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6356 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6357 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6358 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6359 | |
| 6360 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6361 | IdentifierInfo *ClassName, |
| 6362 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6363 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6364 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6365 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6366 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6367 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6368 | |
| 6369 | // Ignore any categories we find that have already been implemented by this |
| 6370 | // interface. |
| 6371 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6372 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6373 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6374 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6375 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6376 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6379 | // Add all of the categories we know about. |
| 6380 | Results.EnterNewScope(); |
| 6381 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6382 | for (const auto *D : TU->decls()) |
| 6383 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6384 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6385 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6386 | nullptr), |
| 6387 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6388 | Results.ExitScope(); |
| 6389 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6390 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6391 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6392 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6396 | IdentifierInfo *ClassName, |
| 6397 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6398 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6399 | |
| 6400 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6401 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6402 | // providing the list of all of the categories we know about. |
| 6403 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6404 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6405 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6406 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6407 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6408 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6409 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6410 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6411 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6412 | |
| 6413 | // Add all of the categories that have have corresponding interface |
| 6414 | // declarations in this class and any of its superclasses, except for |
| 6415 | // already-implemented categories in the class itself. |
| 6416 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6417 | Results.EnterNewScope(); |
| 6418 | bool IgnoreImplemented = true; |
| 6419 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6420 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6421 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6422 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6423 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6424 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6425 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6426 | |
| 6427 | Class = Class->getSuperClass(); |
| 6428 | IgnoreImplemented = false; |
| 6429 | } |
| 6430 | Results.ExitScope(); |
| 6431 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6432 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6433 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6434 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6435 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6436 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6437 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6438 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6439 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6440 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6441 | CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6442 | |
| 6443 | // Figure out where this @synthesize lives. |
| 6444 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6445 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6446 | if (!Container || |
| 6447 | (!isa<ObjCImplementationDecl>(Container) && |
| 6448 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6449 | return; |
| 6450 | |
| 6451 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6452 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6453 | for (const auto *D : Container->decls()) |
| 6454 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6455 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 6456 | |
| 6457 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6458 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6459 | Results.EnterNewScope(); |
| 6460 | if (ObjCImplementationDecl *ClassImpl |
| 6461 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6462 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6463 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6464 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6465 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6466 | AddObjCProperties(CCContext, |
| 6467 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6468 | false, /*AllowNullaryMethods=*/false, CurContext, |
| 6469 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6470 | Results.ExitScope(); |
| 6471 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6472 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6473 | CodeCompletionContext::CCC_Other, |
| 6474 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6478 | IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6479 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6480 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6481 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6482 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6483 | |
| 6484 | // Figure out where this @synthesize lives. |
| 6485 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6486 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6487 | if (!Container || |
| 6488 | (!isa<ObjCImplementationDecl>(Container) && |
| 6489 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6490 | return; |
| 6491 | |
| 6492 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6493 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6494 | if (ObjCImplementationDecl *ClassImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6495 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6496 | Class = ClassImpl->getClassInterface(); |
| 6497 | else |
| 6498 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6499 | ->getClassInterface(); |
| 6500 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6501 | // Determine the type of the property we're synthesizing. |
| 6502 | QualType PropertyType = Context.getObjCIdType(); |
| 6503 | if (Class) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6504 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 6505 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6506 | PropertyType |
| 6507 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| 6508 | |
| 6509 | // Give preference to ivars |
| 6510 | Results.setPreferredType(PropertyType); |
| 6511 | } |
| 6512 | } |
| 6513 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6514 | // Add all of the instance variables in this class and its superclasses. |
| 6515 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6516 | bool SawSimilarlyNamedIvar = false; |
| 6517 | std::string NameWithPrefix; |
| 6518 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6519 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6520 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6521 | NameWithSuffix += '_'; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6522 | for(; Class; Class = Class->getSuperClass()) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6523 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| 6524 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6525 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6526 | CurContext, nullptr, false); |
| 6527 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6528 | // Determine whether we've seen an ivar with a name similar to the |
| 6529 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6530 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6531 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6532 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6533 | SawSimilarlyNamedIvar = true; |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6534 | |
| 6535 | // Reduce the priority of this result by one, to give it a slight |
| 6536 | // advantage over other results whose names don't match so closely. |
| 6537 | if (Results.size() && |
| 6538 | Results.data()[Results.size() - 1].Kind |
| 6539 | == CodeCompletionResult::RK_Declaration && |
| 6540 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6541 | Results.data()[Results.size() - 1].Priority--; |
| 6542 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6543 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6544 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6545 | |
| 6546 | if (!SawSimilarlyNamedIvar) { |
| 6547 | // Create ivar result _propName, that the user can use to synthesize |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6548 | // an ivar of the appropriate type. |
| 6549 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6550 | typedef CodeCompletionResult Result; |
| 6551 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6552 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6553 | Priority,CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6555 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6556 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6557 | Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6558 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| 6559 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| 6560 | CXCursor_ObjCIvarDecl)); |
| 6561 | } |
| 6562 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6563 | Results.ExitScope(); |
| 6564 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6565 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6566 | CodeCompletionContext::CCC_Other, |
| 6567 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6568 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6569 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6570 | // Mapping from selectors to the methods that implement that selector, along |
| 6571 | // with the "in original class" flag. |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6572 | typedef llvm::DenseMap< |
| 6573 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6574 | |
| 6575 | /// \brief Find all of the methods that reside in the given container |
| 6576 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6577 | /// criteria. Insert those methods into the map of known methods, |
| 6578 | /// indexed by selector so they can be easily found. |
| 6579 | static void FindImplementableMethods(ASTContext &Context, |
| 6580 | ObjCContainerDecl *Container, |
| 6581 | bool WantInstanceMethods, |
| 6582 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6583 | KnownMethodsMap &KnownMethods, |
| 6584 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6585 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6586 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6587 | if (!IFace->hasDefinition()) |
| 6588 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6589 | |
| 6590 | IFace = IFace->getDefinition(); |
| 6591 | Container = IFace; |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6593 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6594 | = IFace->getReferencedProtocols(); |
| 6595 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6596 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6597 | I != E; ++I) |
| 6598 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6599 | KnownMethods, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6600 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6601 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6602 | for (auto *Cat : IFace->visible_categories()) { |
| 6603 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6604 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6607 | // Visit the superclass. |
| 6608 | if (IFace->getSuperClass()) |
| 6609 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 6610 | WantInstanceMethods, ReturnType, |
| 6611 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6615 | // Recurse into protocols. |
| 6616 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6617 | = Category->getReferencedProtocols(); |
| 6618 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6619 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6620 | I != E; ++I) |
| 6621 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6622 | KnownMethods, InOriginalClass); |
| 6623 | |
| 6624 | // If this category is the original class, jump to the interface. |
| 6625 | if (InOriginalClass && Category->getClassInterface()) |
| 6626 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 6627 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6628 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6632 | // Make sure we have a definition; that's what we'll walk. |
| 6633 | if (!Protocol->hasDefinition()) |
| 6634 | return; |
| 6635 | Protocol = Protocol->getDefinition(); |
| 6636 | Container = Protocol; |
| 6637 | |
| 6638 | // Recurse into protocols. |
| 6639 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6640 | = Protocol->getReferencedProtocols(); |
| 6641 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6642 | E = Protocols.end(); |
| 6643 | I != E; ++I) |
| 6644 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6645 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | // Add methods in this container. This operation occurs last because |
| 6649 | // we want the methods from this container to override any methods |
| 6650 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6651 | for (auto *M : Container->methods()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6652 | if (M->isInstanceMethod() == WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6653 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6654 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6655 | continue; |
| 6656 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6657 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6658 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | } |
| 6662 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6663 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 6664 | /// completion string. |
| 6665 | static void AddObjCPassingTypeChunk(QualType Type, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6666 | unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6667 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6668 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6669 | CodeCompletionBuilder &Builder) { |
| 6670 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6671 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6672 | if (!Quals.empty()) |
| 6673 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6674 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6675 | Builder.getAllocator())); |
| 6676 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6677 | } |
| 6678 | |
| 6679 | /// \brief Determine whether the given class is or inherits from a class by |
| 6680 | /// the given name. |
| 6681 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6682 | StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6683 | if (!Class) |
| 6684 | return false; |
| 6685 | |
| 6686 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6687 | return true; |
| 6688 | |
| 6689 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6690 | } |
| 6691 | |
| 6692 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 6693 | /// Key-Value Observing (KVO). |
| 6694 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6695 | bool IsInstanceMethod, |
| 6696 | QualType ReturnType, |
| 6697 | ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6698 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6699 | ResultBuilder &Results) { |
| 6700 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6701 | if (!PropName || PropName->getLength() == 0) |
| 6702 | return; |
| 6703 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6704 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6705 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6706 | // Builder that will create each code completion. |
| 6707 | typedef CodeCompletionResult Result; |
| 6708 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6709 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6710 | |
| 6711 | // The selector table. |
| 6712 | SelectorTable &Selectors = Context.Selectors; |
| 6713 | |
| 6714 | // The property name, copied into the code completion allocation region |
| 6715 | // on demand. |
| 6716 | struct KeyHolder { |
| 6717 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6718 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6719 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6720 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6721 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6722 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6723 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6724 | operator const char *() { |
| 6725 | if (CopiedKey) |
| 6726 | return CopiedKey; |
| 6727 | |
| 6728 | return CopiedKey = Allocator.CopyString(Key); |
| 6729 | } |
| 6730 | } Key(Allocator, PropName->getName()); |
| 6731 | |
| 6732 | // The uppercased name of the property name. |
| 6733 | std::string UpperKey = PropName->getName(); |
| 6734 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6735 | UpperKey[0] = toUppercase(UpperKey[0]); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6736 | |
| 6737 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 6738 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 6739 | Property->getType()); |
| 6740 | bool ReturnTypeMatchesVoid |
| 6741 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 6742 | |
| 6743 | // Add the normal accessor -(type)key. |
| 6744 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6745 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6746 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6747 | if (ReturnType.isNull()) |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6748 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6749 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6750 | |
| 6751 | Builder.AddTypedTextChunk(Key); |
| 6752 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6753 | CXCursor_ObjCInstanceMethodDecl)); |
| 6754 | } |
| 6755 | |
| 6756 | // If we have an integral or boolean property (or the user has provided |
| 6757 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6758 | if (IsInstanceMethod && |
| 6759 | ((!ReturnType.isNull() && |
| 6760 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 6761 | (ReturnType.isNull() && |
| 6762 | (Property->getType()->isIntegerType() || |
| 6763 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6764 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6765 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6766 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6767 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6768 | if (ReturnType.isNull()) { |
| 6769 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6770 | Builder.AddTextChunk("BOOL"); |
| 6771 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6772 | } |
| 6773 | |
| 6774 | Builder.AddTypedTextChunk( |
| 6775 | Allocator.CopyString(SelectorId->getName())); |
| 6776 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6777 | CXCursor_ObjCInstanceMethodDecl)); |
| 6778 | } |
| 6779 | } |
| 6780 | |
| 6781 | // Add the normal mutator. |
| 6782 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 6783 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6784 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6785 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6786 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6787 | if (ReturnType.isNull()) { |
| 6788 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6789 | Builder.AddTextChunk("void"); |
| 6790 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6791 | } |
| 6792 | |
| 6793 | Builder.AddTypedTextChunk( |
| 6794 | Allocator.CopyString(SelectorId->getName())); |
| 6795 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6796 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6797 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6798 | Builder.AddTextChunk(Key); |
| 6799 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6800 | CXCursor_ObjCInstanceMethodDecl)); |
| 6801 | } |
| 6802 | } |
| 6803 | |
| 6804 | // Indexed and unordered accessors |
| 6805 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6806 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6807 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6808 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 6809 | if (const ObjCObjectPointerType *ObjCPointer |
| 6810 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6811 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6812 | // If this interface type is not provably derived from a known |
| 6813 | // collection, penalize the corresponding completions. |
| 6814 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 6815 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6816 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6817 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6818 | } |
| 6819 | |
| 6820 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 6821 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6822 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 6823 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6824 | } |
| 6825 | } |
| 6826 | } else { |
| 6827 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6828 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6829 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6830 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6831 | } |
| 6832 | |
| 6833 | // Add -(NSUInteger)countOf<key> |
| 6834 | if (IsInstanceMethod && |
| 6835 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6836 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6837 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6838 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6839 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6840 | if (ReturnType.isNull()) { |
| 6841 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6842 | Builder.AddTextChunk("NSUInteger"); |
| 6843 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6844 | } |
| 6845 | |
| 6846 | Builder.AddTypedTextChunk( |
| 6847 | Allocator.CopyString(SelectorId->getName())); |
| 6848 | Results.AddResult(Result(Builder.TakeString(), |
| 6849 | std::min(IndexedGetterPriority, |
| 6850 | UnorderedGetterPriority), |
| 6851 | CXCursor_ObjCInstanceMethodDecl)); |
| 6852 | } |
| 6853 | } |
| 6854 | |
| 6855 | // Indexed getters |
| 6856 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 6857 | if (IsInstanceMethod && |
| 6858 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6859 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6860 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6861 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6862 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6863 | if (ReturnType.isNull()) { |
| 6864 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6865 | Builder.AddTextChunk("id"); |
| 6866 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6867 | } |
| 6868 | |
| 6869 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6870 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6871 | Builder.AddTextChunk("NSUInteger"); |
| 6872 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6873 | Builder.AddTextChunk("index"); |
| 6874 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6875 | CXCursor_ObjCInstanceMethodDecl)); |
| 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 6880 | if (IsInstanceMethod && |
| 6881 | (ReturnType.isNull() || |
| 6882 | (ReturnType->isObjCObjectPointerType() && |
| 6883 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6884 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6885 | ->getName() == "NSArray"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6886 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6887 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6888 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6889 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6890 | if (ReturnType.isNull()) { |
| 6891 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6892 | Builder.AddTextChunk("NSArray *"); |
| 6893 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6894 | } |
| 6895 | |
| 6896 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6897 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6898 | Builder.AddTextChunk("NSIndexSet *"); |
| 6899 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6900 | Builder.AddTextChunk("indexes"); |
| 6901 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6902 | CXCursor_ObjCInstanceMethodDecl)); |
| 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 6907 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6908 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6909 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6910 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6911 | &Context.Idents.get("range") |
| 6912 | }; |
| 6913 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6914 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6915 | if (ReturnType.isNull()) { |
| 6916 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6917 | Builder.AddTextChunk("void"); |
| 6918 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6919 | } |
| 6920 | |
| 6921 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6922 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6923 | Builder.AddPlaceholderChunk("object-type"); |
| 6924 | Builder.AddTextChunk(" **"); |
| 6925 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6926 | Builder.AddTextChunk("buffer"); |
| 6927 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6928 | Builder.AddTypedTextChunk("range:"); |
| 6929 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6930 | Builder.AddTextChunk("NSRange"); |
| 6931 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6932 | Builder.AddTextChunk("inRange"); |
| 6933 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6934 | CXCursor_ObjCInstanceMethodDecl)); |
| 6935 | } |
| 6936 | } |
| 6937 | |
| 6938 | // Mutable indexed accessors |
| 6939 | |
| 6940 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 6941 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6942 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6943 | IdentifierInfo *SelectorIds[2] = { |
| 6944 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6945 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6946 | }; |
| 6947 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6948 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6949 | if (ReturnType.isNull()) { |
| 6950 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6951 | Builder.AddTextChunk("void"); |
| 6952 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6953 | } |
| 6954 | |
| 6955 | Builder.AddTypedTextChunk("insertObject:"); |
| 6956 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6957 | Builder.AddPlaceholderChunk("object-type"); |
| 6958 | Builder.AddTextChunk(" *"); |
| 6959 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6960 | Builder.AddTextChunk("object"); |
| 6961 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6962 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6963 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6964 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6965 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6966 | Builder.AddTextChunk("index"); |
| 6967 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6968 | CXCursor_ObjCInstanceMethodDecl)); |
| 6969 | } |
| 6970 | } |
| 6971 | |
| 6972 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 6973 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6974 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6975 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6976 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6977 | &Context.Idents.get("atIndexes") |
| 6978 | }; |
| 6979 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6980 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6981 | if (ReturnType.isNull()) { |
| 6982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6983 | Builder.AddTextChunk("void"); |
| 6984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6985 | } |
| 6986 | |
| 6987 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6988 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6989 | Builder.AddTextChunk("NSArray *"); |
| 6990 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6991 | Builder.AddTextChunk("array"); |
| 6992 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6993 | Builder.AddTypedTextChunk("atIndexes:"); |
| 6994 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6995 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6996 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6997 | Builder.AddTextChunk("indexes"); |
| 6998 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6999 | CXCursor_ObjCInstanceMethodDecl)); |
| 7000 | } |
| 7001 | } |
| 7002 | |
| 7003 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 7004 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7005 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7006 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7007 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7008 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7009 | if (ReturnType.isNull()) { |
| 7010 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7011 | Builder.AddTextChunk("void"); |
| 7012 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7013 | } |
| 7014 | |
| 7015 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7016 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7017 | Builder.AddTextChunk("NSUInteger"); |
| 7018 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7019 | Builder.AddTextChunk("index"); |
| 7020 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 7021 | CXCursor_ObjCInstanceMethodDecl)); |
| 7022 | } |
| 7023 | } |
| 7024 | |
| 7025 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 7026 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7027 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7028 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7029 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7030 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7031 | if (ReturnType.isNull()) { |
| 7032 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7033 | Builder.AddTextChunk("void"); |
| 7034 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7035 | } |
| 7036 | |
| 7037 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7038 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7039 | Builder.AddTextChunk("NSIndexSet *"); |
| 7040 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7041 | Builder.AddTextChunk("indexes"); |
| 7042 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 7043 | CXCursor_ObjCInstanceMethodDecl)); |
| 7044 | } |
| 7045 | } |
| 7046 | |
| 7047 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 7048 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7049 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7050 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7051 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7052 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7053 | &Context.Idents.get("withObject") |
| 7054 | }; |
| 7055 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7056 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7057 | if (ReturnType.isNull()) { |
| 7058 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7059 | Builder.AddTextChunk("void"); |
| 7060 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7061 | } |
| 7062 | |
| 7063 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7064 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7065 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7066 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7067 | Builder.AddTextChunk("index"); |
| 7068 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7069 | Builder.AddTypedTextChunk("withObject:"); |
| 7070 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7071 | Builder.AddTextChunk("id"); |
| 7072 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7073 | Builder.AddTextChunk("object"); |
| 7074 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 7075 | CXCursor_ObjCInstanceMethodDecl)); |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 7080 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7081 | std::string SelectorName1 |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7082 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 7083 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7084 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7085 | &Context.Idents.get(SelectorName1), |
| 7086 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7087 | }; |
| 7088 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7089 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7090 | if (ReturnType.isNull()) { |
| 7091 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7092 | Builder.AddTextChunk("void"); |
| 7093 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7094 | } |
| 7095 | |
| 7096 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 7097 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7098 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7099 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7100 | Builder.AddTextChunk("indexes"); |
| 7101 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7102 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 7103 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7104 | Builder.AddTextChunk("NSArray *"); |
| 7105 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7106 | Builder.AddTextChunk("array"); |
| 7107 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 7108 | CXCursor_ObjCInstanceMethodDecl)); |
| 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | // Unordered getters |
| 7113 | // - (NSEnumerator *)enumeratorOfKey |
| 7114 | if (IsInstanceMethod && |
| 7115 | (ReturnType.isNull() || |
| 7116 | (ReturnType->isObjCObjectPointerType() && |
| 7117 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7118 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7119 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7120 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7121 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7122 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7123 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7124 | if (ReturnType.isNull()) { |
| 7125 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7126 | Builder.AddTextChunk("NSEnumerator *"); |
| 7127 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7128 | } |
| 7129 | |
| 7130 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7131 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 7132 | CXCursor_ObjCInstanceMethodDecl)); |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | // - (type *)memberOfKey:(type *)object |
| 7137 | if (IsInstanceMethod && |
| 7138 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7139 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7140 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7141 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7142 | if (ReturnType.isNull()) { |
| 7143 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7144 | Builder.AddPlaceholderChunk("object-type"); |
| 7145 | Builder.AddTextChunk(" *"); |
| 7146 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7147 | } |
| 7148 | |
| 7149 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7150 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7151 | if (ReturnType.isNull()) { |
| 7152 | Builder.AddPlaceholderChunk("object-type"); |
| 7153 | Builder.AddTextChunk(" *"); |
| 7154 | } else { |
| 7155 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7156 | Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7157 | Builder.getAllocator())); |
| 7158 | } |
| 7159 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7160 | Builder.AddTextChunk("object"); |
| 7161 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 7162 | CXCursor_ObjCInstanceMethodDecl)); |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | // Mutable unordered accessors |
| 7167 | // - (void)addKeyObject:(type *)object |
| 7168 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7169 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7170 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7171 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7172 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7173 | if (ReturnType.isNull()) { |
| 7174 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7175 | Builder.AddTextChunk("void"); |
| 7176 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7177 | } |
| 7178 | |
| 7179 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7180 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7181 | Builder.AddPlaceholderChunk("object-type"); |
| 7182 | Builder.AddTextChunk(" *"); |
| 7183 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7184 | Builder.AddTextChunk("object"); |
| 7185 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 7186 | CXCursor_ObjCInstanceMethodDecl)); |
| 7187 | } |
| 7188 | } |
| 7189 | |
| 7190 | // - (void)addKey:(NSSet *)objects |
| 7191 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7192 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7193 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7194 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7195 | if (ReturnType.isNull()) { |
| 7196 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7197 | Builder.AddTextChunk("void"); |
| 7198 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7199 | } |
| 7200 | |
| 7201 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7202 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7203 | Builder.AddTextChunk("NSSet *"); |
| 7204 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7205 | Builder.AddTextChunk("objects"); |
| 7206 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 7207 | CXCursor_ObjCInstanceMethodDecl)); |
| 7208 | } |
| 7209 | } |
| 7210 | |
| 7211 | // - (void)removeKeyObject:(type *)object |
| 7212 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7213 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7214 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7215 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7216 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7217 | if (ReturnType.isNull()) { |
| 7218 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7219 | Builder.AddTextChunk("void"); |
| 7220 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7221 | } |
| 7222 | |
| 7223 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7224 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7225 | Builder.AddPlaceholderChunk("object-type"); |
| 7226 | Builder.AddTextChunk(" *"); |
| 7227 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7228 | Builder.AddTextChunk("object"); |
| 7229 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 7230 | CXCursor_ObjCInstanceMethodDecl)); |
| 7231 | } |
| 7232 | } |
| 7233 | |
| 7234 | // - (void)removeKey:(NSSet *)objects |
| 7235 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7236 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7237 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7238 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7239 | if (ReturnType.isNull()) { |
| 7240 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7241 | Builder.AddTextChunk("void"); |
| 7242 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7243 | } |
| 7244 | |
| 7245 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7246 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7247 | Builder.AddTextChunk("NSSet *"); |
| 7248 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7249 | Builder.AddTextChunk("objects"); |
| 7250 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 7251 | CXCursor_ObjCInstanceMethodDecl)); |
| 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | // - (void)intersectKey:(NSSet *)objects |
| 7256 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7257 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7258 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7259 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7260 | if (ReturnType.isNull()) { |
| 7261 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7262 | Builder.AddTextChunk("void"); |
| 7263 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7264 | } |
| 7265 | |
| 7266 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7267 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7268 | Builder.AddTextChunk("NSSet *"); |
| 7269 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7270 | Builder.AddTextChunk("objects"); |
| 7271 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 7272 | CXCursor_ObjCInstanceMethodDecl)); |
| 7273 | } |
| 7274 | } |
| 7275 | |
| 7276 | // Key-Value Observing |
| 7277 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 7278 | if (!IsInstanceMethod && |
| 7279 | (ReturnType.isNull() || |
| 7280 | (ReturnType->isObjCObjectPointerType() && |
| 7281 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7282 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7283 | ->getName() == "NSSet"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7284 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7285 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7286 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7287 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7288 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7289 | if (ReturnType.isNull()) { |
| 7290 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Alex Lorenz | 71ecb07 | 2016-12-08 16:49:05 +0000 | [diff] [blame] | 7291 | Builder.AddTextChunk("NSSet<NSString *> *"); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7292 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7293 | } |
| 7294 | |
| 7295 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7296 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7297 | CXCursor_ObjCClassMethodDecl)); |
| 7298 | } |
| 7299 | } |
| 7300 | |
| 7301 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 7302 | if (!IsInstanceMethod && |
| 7303 | (ReturnType.isNull() || |
| 7304 | ReturnType->isIntegerType() || |
| 7305 | ReturnType->isBooleanType())) { |
| 7306 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7307 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7308 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7309 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7310 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7311 | if (ReturnType.isNull()) { |
| 7312 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7313 | Builder.AddTextChunk("BOOL"); |
| 7314 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7315 | } |
| 7316 | |
| 7317 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7318 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 7319 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7320 | } |
| 7321 | } |
| 7322 | } |
| 7323 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7324 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 7325 | bool IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7326 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7327 | // Determine the return type of the method we're declaring, if |
| 7328 | // provided. |
| 7329 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7330 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7331 | if (CurContext->isObjCContainer()) { |
| 7332 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 7333 | IDecl = cast<Decl>(OCD); |
| 7334 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7335 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7336 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7337 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7338 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7339 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7340 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7341 | IsInImplementation = true; |
| 7342 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7343 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7344 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7345 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7346 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7347 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7351 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7352 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7355 | if (!SearchDecl) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7356 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7357 | CodeCompletionContext::CCC_Other, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7358 | nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7359 | return; |
| 7360 | } |
| 7361 | |
| 7362 | // Find all of the methods that we could declare/implement here. |
| 7363 | KnownMethodsMap KnownMethods; |
| 7364 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7365 | ReturnType, KnownMethods); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7366 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7367 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7368 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7369 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7370 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7371 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7372 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7373 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7374 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7375 | MEnd = KnownMethods.end(); |
| 7376 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7377 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7378 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7379 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7380 | |
| 7381 | // If the result type was not already provided, add it to the |
| 7382 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7383 | if (ReturnType.isNull()) { |
| 7384 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7385 | AttributedType::stripOuterNullability(ResTy); |
| 7386 | AddObjCPassingTypeChunk(ResTy, |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7387 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7388 | Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7389 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7390 | |
| 7391 | Selector Sel = Method->getSelector(); |
| 7392 | |
| 7393 | // Add the first part of the selector to the pattern. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7394 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7395 | Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7396 | |
| 7397 | // Add parameters to the pattern. |
| 7398 | unsigned I = 0; |
| 7399 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 7400 | PEnd = Method->param_end(); |
| 7401 | P != PEnd; (void)++P, ++I) { |
| 7402 | // Add the part of the selector name. |
| 7403 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7404 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7405 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7407 | Builder.AddTypedTextChunk( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7408 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7409 | } else |
| 7410 | break; |
| 7411 | |
| 7412 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7413 | QualType ParamType; |
| 7414 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7415 | ParamType = (*P)->getType(); |
| 7416 | else |
| 7417 | ParamType = (*P)->getOriginalType(); |
Douglas Gregor | 9b7b3e9 | 2015-07-07 06:20:27 +0000 | [diff] [blame] | 7418 | ParamType = ParamType.substObjCTypeArgs(Context, {}, |
| 7419 | ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7420 | AttributedType::stripOuterNullability(ParamType); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7421 | AddObjCPassingTypeChunk(ParamType, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7422 | (*P)->getObjCDeclQualifier(), |
| 7423 | Context, Policy, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7424 | Builder); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7425 | |
| 7426 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7427 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7428 | } |
| 7429 | |
| 7430 | if (Method->isVariadic()) { |
| 7431 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7432 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7433 | Builder.AddTextChunk("..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 7434 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7435 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7436 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7437 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7438 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7439 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7440 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7441 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7442 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7443 | Builder.AddTextChunk("return"); |
| 7444 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7445 | Builder.AddPlaceholderChunk("expression"); |
| 7446 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7447 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7448 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7449 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7450 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7451 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7454 | unsigned Priority = CCP_CodePattern; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7455 | if (!M->second.getInt()) |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7456 | Priority += CCD_InBaseClass; |
| 7457 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7458 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7461 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 7462 | // the properties in this class and its categories. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7463 | if (Context.getLangOpts().ObjC2) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7464 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7465 | Containers.push_back(SearchDecl); |
| 7466 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7467 | VisitedSelectorSet KnownSelectors; |
| 7468 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7469 | MEnd = KnownMethods.end(); |
| 7470 | M != MEnd; ++M) |
| 7471 | KnownSelectors.insert(M->first); |
| 7472 | |
| 7473 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7474 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7475 | if (!IFace) |
| 7476 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7477 | IFace = Category->getClassInterface(); |
| 7478 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7479 | if (IFace) |
| 7480 | for (auto *Cat : IFace->visible_categories()) |
| 7481 | Containers.push_back(Cat); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7482 | |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7483 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 7484 | for (auto *P : Containers[I]->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7485 | AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7486 | KnownSelectors, Results); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7487 | } |
| 7488 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7489 | Results.ExitScope(); |
| 7490 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7491 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7492 | CodeCompletionContext::CCC_Other, |
| 7493 | Results.data(),Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7494 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7495 | |
| 7496 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 7497 | bool IsInstanceMethod, |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7498 | bool AtParameterName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7499 | ParsedType ReturnTy, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7500 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7501 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7502 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7503 | if (ExternalSource) { |
| 7504 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7505 | I != N; ++I) { |
| 7506 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7507 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7508 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7509 | |
| 7510 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7511 | } |
| 7512 | } |
| 7513 | |
| 7514 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7515 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7516 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7517 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7518 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7519 | |
| 7520 | if (ReturnTy) |
| 7521 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7522 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7523 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7524 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7525 | MEnd = MethodPool.end(); |
| 7526 | M != MEnd; ++M) { |
| 7527 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7528 | &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7529 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7530 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7531 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7532 | continue; |
| 7533 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7534 | if (AtParameterName) { |
| 7535 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7536 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7537 | if (NumSelIdents && |
| 7538 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7539 | ParmVarDecl *Param = |
| 7540 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7541 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7542 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7543 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7544 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7545 | Param->getIdentifier()->getName())); |
| 7546 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7547 | } |
| 7548 | } |
| 7549 | |
| 7550 | continue; |
| 7551 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7552 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7553 | Result R(MethList->getMethod(), |
| 7554 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7555 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7556 | R.AllParametersAreInformative = false; |
| 7557 | R.DeclaringEntity = true; |
| 7558 | Results.MaybeAddResult(R, CurContext); |
| 7559 | } |
| 7560 | } |
| 7561 | |
| 7562 | Results.ExitScope(); |
Alex Lorenz | 847fda1 | 2017-01-03 11:56:40 +0000 | [diff] [blame] | 7563 | |
| 7564 | if (!AtParameterName && !SelIdents.empty() && |
| 7565 | SelIdents.front()->getName().startswith("init")) { |
| 7566 | for (const auto &M : PP.macros()) { |
| 7567 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") |
| 7568 | continue; |
| 7569 | Results.EnterNewScope(); |
| 7570 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7571 | Results.getCodeCompletionTUInfo()); |
| 7572 | Builder.AddTypedTextChunk( |
| 7573 | Builder.getAllocator().CopyString(M.first->getName())); |
| 7574 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, |
| 7575 | CXCursor_MacroDefinition)); |
| 7576 | Results.ExitScope(); |
| 7577 | } |
| 7578 | } |
| 7579 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7580 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7581 | CodeCompletionContext::CCC_Other, |
| 7582 | Results.data(),Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7583 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7584 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7585 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7586 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7587 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7588 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7589 | Results.EnterNewScope(); |
| 7590 | |
| 7591 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7592 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7593 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7594 | Builder.AddTypedTextChunk("if"); |
| 7595 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7596 | Builder.AddPlaceholderChunk("condition"); |
| 7597 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7598 | |
| 7599 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7600 | Builder.AddTypedTextChunk("ifdef"); |
| 7601 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7602 | Builder.AddPlaceholderChunk("macro"); |
| 7603 | Results.AddResult(Builder.TakeString()); |
| 7604 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7605 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7606 | Builder.AddTypedTextChunk("ifndef"); |
| 7607 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7608 | Builder.AddPlaceholderChunk("macro"); |
| 7609 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7610 | |
| 7611 | if (InConditional) { |
| 7612 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7613 | Builder.AddTypedTextChunk("elif"); |
| 7614 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7615 | Builder.AddPlaceholderChunk("condition"); |
| 7616 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7617 | |
| 7618 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7619 | Builder.AddTypedTextChunk("else"); |
| 7620 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7621 | |
| 7622 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7623 | Builder.AddTypedTextChunk("endif"); |
| 7624 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7628 | Builder.AddTypedTextChunk("include"); |
| 7629 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7630 | Builder.AddTextChunk("\""); |
| 7631 | Builder.AddPlaceholderChunk("header"); |
| 7632 | Builder.AddTextChunk("\""); |
| 7633 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7634 | |
| 7635 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7636 | Builder.AddTypedTextChunk("include"); |
| 7637 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7638 | Builder.AddTextChunk("<"); |
| 7639 | Builder.AddPlaceholderChunk("header"); |
| 7640 | Builder.AddTextChunk(">"); |
| 7641 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7642 | |
| 7643 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7644 | Builder.AddTypedTextChunk("define"); |
| 7645 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7646 | Builder.AddPlaceholderChunk("macro"); |
| 7647 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7648 | |
| 7649 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7650 | Builder.AddTypedTextChunk("define"); |
| 7651 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7652 | Builder.AddPlaceholderChunk("macro"); |
| 7653 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7654 | Builder.AddPlaceholderChunk("args"); |
| 7655 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7656 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7657 | |
| 7658 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7659 | Builder.AddTypedTextChunk("undef"); |
| 7660 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7661 | Builder.AddPlaceholderChunk("macro"); |
| 7662 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7663 | |
| 7664 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7665 | Builder.AddTypedTextChunk("line"); |
| 7666 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7667 | Builder.AddPlaceholderChunk("number"); |
| 7668 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7669 | |
| 7670 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7671 | Builder.AddTypedTextChunk("line"); |
| 7672 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7673 | Builder.AddPlaceholderChunk("number"); |
| 7674 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7675 | Builder.AddTextChunk("\""); |
| 7676 | Builder.AddPlaceholderChunk("filename"); |
| 7677 | Builder.AddTextChunk("\""); |
| 7678 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7679 | |
| 7680 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7681 | Builder.AddTypedTextChunk("error"); |
| 7682 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7683 | Builder.AddPlaceholderChunk("message"); |
| 7684 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7685 | |
| 7686 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7687 | Builder.AddTypedTextChunk("pragma"); |
| 7688 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7689 | Builder.AddPlaceholderChunk("arguments"); |
| 7690 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7691 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7692 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7693 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7694 | Builder.AddTypedTextChunk("import"); |
| 7695 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7696 | Builder.AddTextChunk("\""); |
| 7697 | Builder.AddPlaceholderChunk("header"); |
| 7698 | Builder.AddTextChunk("\""); |
| 7699 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7700 | |
| 7701 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7702 | Builder.AddTypedTextChunk("import"); |
| 7703 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7704 | Builder.AddTextChunk("<"); |
| 7705 | Builder.AddPlaceholderChunk("header"); |
| 7706 | Builder.AddTextChunk(">"); |
| 7707 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7708 | } |
| 7709 | |
| 7710 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7711 | Builder.AddTypedTextChunk("include_next"); |
| 7712 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7713 | Builder.AddTextChunk("\""); |
| 7714 | Builder.AddPlaceholderChunk("header"); |
| 7715 | Builder.AddTextChunk("\""); |
| 7716 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7717 | |
| 7718 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7719 | Builder.AddTypedTextChunk("include_next"); |
| 7720 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7721 | Builder.AddTextChunk("<"); |
| 7722 | Builder.AddPlaceholderChunk("header"); |
| 7723 | Builder.AddTextChunk(">"); |
| 7724 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7725 | |
| 7726 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7727 | Builder.AddTypedTextChunk("warning"); |
| 7728 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7729 | Builder.AddPlaceholderChunk("message"); |
| 7730 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7731 | |
| 7732 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7733 | // completions for them. And __include_macros is a Clang-internal extension |
| 7734 | // that we don't want to encourage anyone to use. |
| 7735 | |
| 7736 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7737 | Results.ExitScope(); |
| 7738 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7739 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 7740 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7741 | Results.data(), Results.size()); |
| 7742 | } |
| 7743 | |
| 7744 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7745 | CodeCompleteOrdinaryName(S, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7746 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 7747 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7750 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7751 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7752 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7753 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7754 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7755 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 7756 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7757 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7758 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7759 | Results.EnterNewScope(); |
| 7760 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 7761 | MEnd = PP.macro_end(); |
| 7762 | M != MEnd; ++M) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7763 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7764 | M->first->getName())); |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7765 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7766 | CCP_CodePattern, |
| 7767 | CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7768 | } |
| 7769 | Results.ExitScope(); |
| 7770 | } else if (IsDefinition) { |
| 7771 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7772 | } |
| 7773 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7774 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7775 | Results.data(), Results.size()); |
| 7776 | } |
| 7777 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7778 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7779 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7780 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7781 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7782 | |
| 7783 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7784 | AddMacroResults(PP, Results, true); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7785 | |
| 7786 | // defined (<macro>) |
| 7787 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7788 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7789 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7790 | Builder.AddTypedTextChunk("defined"); |
| 7791 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7792 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7793 | Builder.AddPlaceholderChunk("macro"); |
| 7794 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7795 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7796 | Results.ExitScope(); |
| 7797 | |
| 7798 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7799 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 7800 | Results.data(), Results.size()); |
| 7801 | } |
| 7802 | |
| 7803 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7804 | IdentifierInfo *Macro, |
| 7805 | MacroInfo *MacroInfo, |
| 7806 | unsigned Argument) { |
| 7807 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7808 | // do for function calls. |
| 7809 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7810 | // Now just ignore this. There will be another code-completion callback |
| 7811 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7812 | } |
| 7813 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7814 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7815 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 7816 | CodeCompletionContext::CCC_NaturalLanguage, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7817 | nullptr, 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 7820 | void Sema::CodeCompleteAvailabilityPlatformName() { |
| 7821 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 7822 | CodeCompleter->getCodeCompletionTUInfo(), |
| 7823 | CodeCompletionContext::CCC_Other); |
| 7824 | Results.EnterNewScope(); |
| 7825 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; |
| 7826 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { |
| 7827 | Results.AddResult(CodeCompletionResult(Platform)); |
| 7828 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( |
| 7829 | Twine(Platform) + "ApplicationExtension"))); |
| 7830 | } |
| 7831 | Results.ExitScope(); |
| 7832 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7833 | CodeCompletionContext::CCC_Other, Results.data(), |
| 7834 | Results.size()); |
| 7835 | } |
| 7836 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7837 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7838 | CodeCompletionTUInfo &CCTUInfo, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7839 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7840 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 7841 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 7842 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 7843 | CodeCompletionDeclConsumer Consumer(Builder, |
| 7844 | Context.getTranslationUnitDecl()); |
| 7845 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 7846 | Consumer); |
| 7847 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7848 | |
| 7849 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7850 | AddMacroResults(PP, Builder, true); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7851 | |
| 7852 | Results.clear(); |
| 7853 | Results.insert(Results.end(), |
| 7854 | Builder.data(), Builder.data() + Builder.size()); |
| 7855 | } |